PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // AutoVMatrixTest.cc 00004 // 00005 // Copyright (C) 2005 Nicolas Chapados 00006 // Copyright (C) 2005 Olivier Delalleau 00007 // Copyright (C) 2005 Christian Dorion 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 /* ******************************************************* 00038 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00039 ******************************************************* */ 00040 00041 // Authors: Nicolas Chapados, Olivier Delalleau, Christian Dorion 00042 00046 #include "AutoVMatrixTest.h" 00047 00048 namespace PLearn { 00049 using namespace std; 00050 00051 PLEARN_IMPLEMENT_OBJECT( 00052 AutoVMatrixTest, 00053 "Test various VMat conversions.", 00054 "" 00055 ); 00056 00058 // AutoVMatrixTest // 00060 AutoVMatrixTest::AutoVMatrixTest() 00061 /* ### Initialize all fields to their default value */ 00062 { 00063 // ... 00064 00065 // ### You may (or not) want to call build_() to finish building the object 00066 // ### (doing so assumes the parent classes' build_() have been called too 00067 // ### in the parent classes' constructors, something that you must ensure) 00068 } 00069 00071 // build // 00073 void AutoVMatrixTest::build() 00074 { 00075 inherited::build(); 00076 build_(); 00077 } 00078 00080 // makeDeepCopyFromShallowCopy // 00082 void AutoVMatrixTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00083 { 00084 inherited::makeDeepCopyFromShallowCopy(copies); 00085 00086 // ### Call deepCopyField on all "pointer-like" fields 00087 // ### that you wish to be deepCopied rather than 00088 // ### shallow-copied. 00089 // ### ex: 00090 // deepCopyField(trainvec, copies); 00091 00092 // ### Remove this line when you have fully implemented this method. 00093 PLERROR("AutoVMatrixTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00094 } 00095 00097 // declareOptions // 00099 void AutoVMatrixTest::declareOptions(OptionList& ol) 00100 { 00101 // ### Declare all of this object's options here 00102 // ### For the "flags" of each option, you should typically specify 00103 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00104 // ### OptionBase::tuningoption. Another possible flag to be combined with 00105 // ### is OptionBase::nosave 00106 00107 // ### ex: 00108 // declareOption(ol, "myoption", &AutoVMatrixTest::myoption, OptionBase::buildoption, 00109 // "Help text describing this option"); 00110 // ... 00111 00112 // Now call the parent class' declareOptions 00113 inherited::declareOptions(ol); 00114 } 00115 00117 // build_ // 00119 void AutoVMatrixTest::build_() 00120 { 00121 // ### This method should do the real building of the object, 00122 // ### according to set 'options', in *any* situation. 00123 // ### Typical situations include: 00124 // ### - Initial building of an object from a few user-specified options 00125 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00126 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00127 // ### You should assume that the parent class' build_() has already been called. 00128 } 00129 00130 void save_load_compare( const AutoVMatrix& vm, 00131 const PPath& prefix, 00132 const PPath& base, 00133 const string& ext, int dot ) 00134 { 00135 PPath save_to = prefix + PPath(base).replace(dot, base.length(), ext); 00136 if ( ext == ".amat" ) 00137 vm.saveAMAT( save_to ); 00138 else if ( ext == ".pmat" ) 00139 vm.savePMAT( save_to ); 00140 else if ( ext == ".dmat" ) 00141 vm.saveDMAT( save_to ); 00142 else 00143 PLERROR("!!!"); 00144 00145 AutoVMatrix reloaded( save_to ); 00146 bool success = ( vm.toMat().isEqual( reloaded.toMat() ) ); 00147 if ( success ) 00148 {MAND_LOG << "Save and load suceeded on " << save_to << endl << endl;} 00149 else 00150 {MAND_LOG << "!!! Save and load FAILED on " << save_to << endl << endl;} 00151 } 00152 00153 void unitTest(const PPath& path) 00154 { 00155 AutoVMatrix vm(path); 00156 MAND_LOG << vm << endl; 00157 00158 Mat m(vm); 00159 MAND_LOG << m << endl; 00160 00161 PPath base = path.basename(); 00162 unsigned int dot = base.rfind('.'); 00163 base[dot] = '_'; 00164 PPath prefix = base + "__to__"; 00165 00166 save_load_compare( vm, prefix, base, ".amat", dot ); 00167 save_load_compare( vm, prefix, base, ".pmat", dot ); 00168 save_load_compare( vm, prefix, base, ".dmat", dot ); 00169 } 00170 00171 inline void UNIT_TEST(const string& argument) 00172 { 00173 MAND_LOG << plhead(argument) << endl; 00174 try { 00175 unitTest(argument); 00176 MAND_LOG << endl; 00177 } 00178 catch(const PLearnError& e) 00179 { 00180 perr << "FATAL ERROR: " << e.message() << endl << endl; 00181 } 00182 catch (...) 00183 { 00184 perr << "FATAL ERROR: uncaught unknown exception" << endl << endl; 00185 } 00186 } 00187 00188 00190 // perform // 00192 void AutoVMatrixTest::perform() 00193 { 00194 try { 00195 PL_Log::instance().verbosity(VLEVEL_NORMAL); 00196 PL_Log::instance().outmode(PStream::plearn_ascii); 00197 00198 UNIT_TEST("PLEARNDIR:examples/data/test_suite/linear_4x_2y.amat"); 00199 UNIT_TEST("PLEARNDIR:examples/data/test_suite/linear_4x_2y.pmat"); 00200 UNIT_TEST("PLEARNDIR:examples/data/test_suite/eslt_mixture/data_train.amat"); 00201 } 00202 catch(const PLearnError& e) 00203 { 00204 perr << "FATAL ERROR: " << e.message() << endl << endl; 00205 } 00206 catch (...) 00207 { 00208 perr << "FATAL ERROR: uncaught unknown exception" << endl << endl; 00209 } 00210 00211 } 00212 00213 } // end of namespace PLearn 00214 00215 00216 /* 00217 Local Variables: 00218 mode:c++ 00219 c-basic-offset:4 00220 c-file-style:"stroustrup" 00221 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00222 indent-tabs-mode:nil 00223 fill-column:79 00224 End: 00225 */ 00226 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :