PLearn 0.1
FileVMatrixTest.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // FileVMatrixTest.cc
00004 //
00005 // Copyright (C) 2005 Pascal Vincent
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 /* *******************************************************
00036    * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $
00037    ******************************************************* */
00038 
00039 // Authors: Pascal Vincent
00040 
00044 #include "FileVMatrixTest.h"
00045 #include <plearn/math/TMat_maths.h>
00046 #include <plearn/math/pl_math.h>
00047 #include <plearn/vmat/FileVMatrix.h>
00048 #include <plearn/io/fileutils.h>
00049 
00050 namespace PLearn {
00051 using namespace std;
00052 
00053 PLEARN_IMPLEMENT_OBJECT(
00054     FileVMatrixTest,
00055     "ONE LINE USER DESCRIPTION",
00056     "MULTI LINE\nHELP FOR USERS"
00057 );
00058 
00060 // FileVMatrixTest //
00062 FileVMatrixTest::FileVMatrixTest()
00063     /* ### Initialize all fields to their default value */
00064 {
00065     // ...
00066 
00067     // ### You may (or not) want to call build_() to finish building the object
00068     // ### (doing so assumes the parent classes' build_() have been called too
00069     // ### in the parent classes' constructors, something that you must ensure)
00070 }
00071 
00073 // build //
00075 void FileVMatrixTest::build()
00076 {
00077     inherited::build();
00078     build_();
00079 }
00080 
00082 // makeDeepCopyFromShallowCopy //
00084 void FileVMatrixTest::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00085 {
00086     inherited::makeDeepCopyFromShallowCopy(copies);
00087 
00088     // ### Call deepCopyField on all "pointer-like" fields
00089     // ### that you wish to be deepCopied rather than
00090     // ### shallow-copied.
00091     // ### ex:
00092     // deepCopyField(trainvec, copies);
00093 
00094     // ### Remove this line when you have fully implemented this method.
00095     PLERROR("FileVMatrixTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00096 }
00097 
00099 // declareOptions //
00101 void FileVMatrixTest::declareOptions(OptionList& ol)
00102 {
00103     // ### Declare all of this object's options here
00104     // ### For the "flags" of each option, you should typically specify
00105     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00106     // ### OptionBase::tuningoption. Another possible flag to be combined with
00107     // ### is OptionBase::nosave
00108 
00109     // ### ex:
00110     // declareOption(ol, "myoption", &FileVMatrixTest::myoption, OptionBase::buildoption,
00111     //               "Help text describing this option");
00112     // ...
00113 
00114     // Now call the parent class' declareOptions
00115     inherited::declareOptions(ol);
00116 }
00117 
00119 // build_ //
00121 void FileVMatrixTest::build_()
00122 {
00123     // ### This method should do the real building of the object,
00124     // ### according to set 'options', in *any* situation.
00125     // ### Typical situations include:
00126     // ###  - Initial building of an object from a few user-specified options
00127     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00128     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00129     // ### You should assume that the parent class' build_() has already been called.
00130 }
00131 
00133 // perform //
00135 void FileVMatrixTest::perform()
00136 {
00137     pout << "Testing small FileVMatrix" << endl;
00138 
00139     pout << "TEST m1" << endl;
00140     FileVMatrix* m1 = new FileVMatrix("fvm1.pmat",10,3);
00141     Vec v(3);
00142     v[0] = 1.0;
00143     v[1] = 2.0;
00144     v[2] = 3.0;
00145 
00146     for(int i=0; i<m1->length(); i++)
00147     {
00148         m1->putRow(i,v);
00149         v += 1.0;
00150     }
00151 
00152     m1->appendRow(v);
00153     m1->flush();
00154     m1->putRow(3,v);
00155     m1->appendRow(v);
00156     v[1] = MISSING_VALUE;
00157     m1->putRow(2,v);
00158     delete m1;
00159 
00160     pout << "TEST m2" << endl;
00161     FileVMatrix* m2 = new FileVMatrix("fvm1.pmat",true);
00162     v += 10.;
00163     m2->appendRow(v);
00164     m2->putRow(4,v);
00165     delete m2;
00166 
00167     pout << "TEST m3" << endl;
00168     FileVMatrix* m3 = new FileVMatrix("fvm1.pmat");
00169     TVec<string> fieldnames(3);
00170     fieldnames[0]="aaa";
00171     fieldnames[1]="bbb";
00172     fieldnames[2]="ccc";
00173 
00174     pout << "TEST m4" << endl;
00175     FileVMatrix* m4 = new FileVMatrix("fvm4.pmat",0,fieldnames);
00176     int l = m3->length();
00177     for(int i=0; i<l; i++)
00178     {
00179         m3->getRow(i,v);
00180         m4->appendRow(v);
00181     }
00182     delete m4;
00183 
00184     pout << "TEST m5" << endl;
00185     FileVMatrix* m5 = new FileVMatrix("fvm4.pmat");
00186     pout << "Fieldnames: " << m5->fieldNames() << " SAME? " << (m5->fieldNames()==fieldnames) << endl;
00187     pout << "toMat(): \n" << m5->toMat() << endl;
00188 
00189     pout << "TEST m6" << endl;
00190     pout << "Testing huge FileVMatrix" << endl;
00191     v[0] = 100;
00192     v[1] = 101;
00193     v[2] = 102;
00194     l = 200000000;
00195     FileVMatrix* m6 = new FileVMatrix("fvm6.pmat",l,3);
00196     m6->putRow(l-5,v+1.0);
00197     m6->putRow(5,v-1.0);
00198 
00199     m6->getRow(l-5, v);
00200     pout << l-5 << ": " << v << " (should be 101 102 103)" << endl;
00201     m6->getRow(5, v);
00202     pout << 5 << ": " << v << " (should be 99 100 101)" << endl;
00203 
00204     v.fill(999);
00205     m6->appendRow(v);
00206 
00207     pout << "l=" << l << " length=" << m6->length() << " (should be l+1)" << endl;
00208     m6->getRow(l-5, v);
00209     pout << l-5 << ": " << v << " (should be 101 102 103)" << endl;
00210     m6->getRow(5, v);
00211     pout << 5 << ": " << v << " (should be 99 100 101)" << endl;
00212     m6->getRow(l, v);
00213     pout << l << ": " << v << " (should be 999 999 999)" << endl;
00214 
00215     PRUint64 siz1 = m6->length();
00216     siz1 *= m6->width()*sizeof(real);
00217     siz1 += DATAFILE_HEADERLENGTH;
00218     delete m6;
00219 
00220 
00221     PRUint64 siz2 = filesize64("fvm6.pmat");
00222     pout << "Huge file size: " << siz2 << " sohuld equal " << siz1 << "  ? " << (siz2==siz1) << endl;
00223 
00224     pout << "TEST m7" << endl;
00225     FileVMatrix* m7 = new FileVMatrix("fvm6.pmat");
00226 
00227     pout << "l=" << l << " length=" << m7->length() << " (should be l+1)" << endl;
00228     m7->getRow(l-5, v);
00229     pout << l-5 << ": " << v << " (should be 101 102 103)" << endl;
00230     m7->getRow(5, v);
00231     pout << 5 << ": " << v << " (should be 99 100 101)" << endl;
00232     m7->getRow(l, v);
00233     pout << l << ": " << v << " (should be 999 999 999)" << endl;
00234     delete m7;
00235 
00236     // remove it because we don't want to keep such a huge file under revision control
00237     rm("fvm6.pmat");
00238     force_rmdir("fvm6.pmat.metadata");
00239 }
00240 
00241 } // end of namespace PLearn
00242 
00243 
00244 /*
00245   Local Variables:
00246   mode:c++
00247   c-basic-offset:4
00248   c-file-style:"stroustrup"
00249   c-file-offsets:((innamespace . 0)(inline-open . 0))
00250   indent-tabs-mode:nil
00251   fill-column:79
00252   End:
00253 */
00254 // 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