PLearn 0.1
BinaryNumbersVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // BinaryNumbersVMatrix.cc
00004 //
00005 // Copyright (C) 2007 Yoshua Bengio
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Yoshua Bengio
00036 
00040 #include "BinaryNumbersVMatrix.h"
00041 
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 
00047 PLEARN_IMPLEMENT_OBJECT(
00048     BinaryNumbersVMatrix,
00049     "VMatrix reading file with numbers in various possible binary formats",
00050     "VMatrix that can take its values from a possibly large file (greater than 2Gig)\n"
00051     "containing numbers in a user-given binary format, preceded by an arbitrary header whose\n"
00052     "length is user-given. The user must also specify the dimensions of the matrix\n"
00053     "(length and width).\n"
00054     );
00055 
00056 BinaryNumbersVMatrix::BinaryNumbersVMatrix()
00057     : format("u1"), header_size(0), file_is_bigendian(false), f(0), buffer(0)
00058 {
00059 }
00060 
00061 void BinaryNumbersVMatrix::getNewRow(int i, const Vec& v) const
00062 {
00063     PLASSERT_MSG(v.length()==width_,"BinaryNumbersVMatrix::getNewRow(i,v) with v.length!= vmatrix width");
00064     PRInt64 offset = i;
00065     offset *= row_size;
00066     offset += header_size;
00067     PR_Seek64(f,offset,PR_SEEK_SET);
00068     PR_Read(f,buffer,row_size);
00069     bool swap_endian=false;
00070 #ifdef LITTLEENDIAN
00071     if(file_is_bigendian)
00072         swap_endian=true;
00073 #endif
00074 #ifdef BIGENDIAN
00075     if(!file_is_bigendian)
00076         swap_endian=true;
00077 #endif
00078     
00079     int l=v.length();
00080     if (format=="u1") 
00081         for (int i=0;i<l;i++)
00082             v[i] = (real)((unsigned char*)buffer)[i];
00083     else if (format=="u2") {
00084         if (swap_endian)
00085             endianswap2(buffer,width_);
00086         for (int i=0;i<l;i++)
00087             v[i] = (real)((unsigned short*)buffer)[i];
00088     }
00089     else if (format=="i4") {
00090         if (swap_endian)
00091             endianswap4(buffer,width_);
00092         for (int i=0;i<l;i++)
00093             v[i] = (real)((int*)buffer)[i];
00094     }
00095     else if (format=="f4") {
00096         if (swap_endian)
00097             endianswap4(buffer,width_);
00098         for (int i=0;i<l;i++)
00099             v[i] = (real)((float*)buffer)[i];
00100     }
00101     else if (format=="f8") {
00102         if (swap_endian)
00103             endianswap8(buffer,width_);
00104         for (int i=0;i<l;i++)
00105             v[i] = (real)((double*)buffer)[i];
00106     }
00107     else
00108         PLERROR("BinaryNumbersVMatrix: unknown format = %s\n",format.c_str());
00109 }
00110 
00111 void BinaryNumbersVMatrix::declareOptions(OptionList& ol)
00112 {
00113     declareOption(ol, "filename", &BinaryNumbersVMatrix::filename,
00114                    OptionBase::buildoption,
00115                   "Name of file to be read.");
00116 
00117     declareOption(ol, "format", &BinaryNumbersVMatrix::format,
00118                    OptionBase::buildoption,
00119                   "2-character specification of binary format of the numbers in the file:\n"
00120                   "  u1 = 1-byte unsigned integers\n"
00121                   "  u2 = 1-byte unsigned integers\n"
00122                   "  i4 = 4-byte signed integers\n"
00123                   "  f4 = 4-byte floating point\n"
00124                   "  f8 = 8-byte floating point\n");
00125 
00126     declareOption(ol, "header_size", &BinaryNumbersVMatrix::header_size,
00127                    OptionBase::buildoption,
00128                   "Number of bytes of header at beginning of file.");
00129 
00130     declareOption(ol, "file_is_bigendian", &BinaryNumbersVMatrix::file_is_bigendian,
00131                    OptionBase::buildoption,
00132                   "Whether the byte order is 'BIG ENDIAN' or not.");
00133 
00134     declareOption(ol, "row_size", &BinaryNumbersVMatrix::row_size,
00135                    OptionBase::learntoption,
00136                   "Number of bytes in each row");
00137 
00138     // Now call the parent class' declareOptions
00139     inherited::declareOptions(ol);
00140 }
00141 
00142 void BinaryNumbersVMatrix::build_()
00143 {
00144     if (f)
00145         PR_Close(f);
00146     updateMtime(filename);
00147     f = PR_Open(filename.c_str(), PR_RDONLY, 0666);
00148     if (width_>0)
00149     {
00150         if (buffer) 
00151             delete[] (char*)buffer;
00152         row_size = width_*(format[1]-'0');
00153         buffer = (void*) (new char[row_size]);
00154     }
00155 }
00156 
00157 // ### Nothing to add here, simply calls build_
00158 void BinaryNumbersVMatrix::build()
00159 {
00160     inherited::build();
00161     build_();
00162 }
00163 
00164 void BinaryNumbersVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00165 {
00166     inherited::makeDeepCopyFromShallowCopy(copies);
00167 }
00168 
00169 
00170 BinaryNumbersVMatrix::~BinaryNumbersVMatrix()
00171 {
00172     if (buffer) delete[] (char*)buffer;
00173     buffer=0;
00174     if (f) 
00175         PR_Close(f);
00176     f=0;
00177 }
00178 
00179 
00180 } // end of namespace PLearn
00181 
00182 
00183 /*
00184   Local Variables:
00185   mode:c++
00186   c-basic-offset:4
00187   c-file-style:"stroustrup"
00188   c-file-offsets:((innamespace . 0)(inline-open . 0))
00189   indent-tabs-mode:nil
00190   fill-column:79
00191   End:
00192 */
00193 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines