PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // InstanceSnippetTest.cc 00004 // 00005 // Copyright (C) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc. 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: Xavier Saint-Mleux 00036 00040 #include "InstanceSnippetTest.h" 00041 #include <plearn_learners/testers/PTester.h> 00042 #include <plearn/vmat/TrainTestSplitter.h> 00043 #include <plearn/sys/procinfo.h> 00044 #include <boost/regex.hpp> 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 PLEARN_IMPLEMENT_OBJECT( 00050 InstanceSnippetTest, 00051 "ONE LINE USER DESCRIPTION", 00052 "MULTI LINE\nHELP FOR USERS" 00053 ); 00054 00056 // InstanceSnippetTest // 00058 InstanceSnippetTest::InstanceSnippetTest() 00059 /* ### Initialize all fields to their default value */ 00060 { 00061 // ... 00062 00063 // ### You may (or not) want to call build_() to finish building the object 00064 // ### (doing so assumes the parent classes' build_() have been called too 00065 // ### in the parent classes' constructors, something that you must ensure) 00066 } 00067 00069 // build // 00071 void InstanceSnippetTest::build() 00072 { 00073 inherited::build(); 00074 build_(); 00075 } 00076 00078 // makeDeepCopyFromShallowCopy // 00080 void InstanceSnippetTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00081 { 00082 inherited::makeDeepCopyFromShallowCopy(copies); 00083 00084 // ### Call deepCopyField on all "pointer-like" fields 00085 // ### that you wish to be deepCopied rather than 00086 // ### shallow-copied. 00087 // ### ex: 00088 // deepCopyField(trainvec, copies); 00089 00090 // ### Remove this line when you have fully implemented this method. 00091 PLERROR("InstanceSnippetTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00092 } 00093 00095 // declareOptions // 00097 void InstanceSnippetTest::declareOptions(OptionList& ol) 00098 { 00099 // ### Declare all of this object's options here. 00100 // ### For the "flags" of each option, you should typically specify 00101 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00102 // ### OptionBase::tuningoption. If you don't provide one of these three, 00103 // ### this option will be ignored when loading values from a script. 00104 // ### You can also combine flags, for example with OptionBase::nosave: 00105 // ### (OptionBase::buildoption | OptionBase::nosave) 00106 00107 // ### ex: 00108 // declareOption(ol, "myoption", &InstanceSnippetTest::myoption, 00109 // OptionBase::buildoption, 00110 // "Help text describing this option"); 00111 // ... 00112 00113 // Now call the parent class' declareOptions 00114 inherited::declareOptions(ol); 00115 } 00116 00118 // build_ // 00120 void InstanceSnippetTest::build_() 00121 { 00122 // ### This method should do the real building of the object, 00123 // ### according to set 'options', in *any* situation. 00124 // ### Typical situations include: 00125 // ### - Initial building of an object from a few user-specified options 00126 // ### - Building of a "reloaded" object: i.e. from the complete set of 00127 // ### all serialised options. 00128 // ### - Updating or "re-building" of an object after a few "tuning" 00129 // ### options have been modified. 00130 // ### You should assume that the parent class' build_() has already been 00131 // ### called. 00132 } 00133 00135 // perform // 00137 void InstanceSnippetTest::perform() 00138 { 00139 TestSnippet zz1; 00140 zz1.build(); 00141 00142 int n= zz1.invoke("getN"); 00143 00144 zz1.invoke("test0"); 00145 00146 PP<PTester> t= new PTester();//on heap, please 00147 t->final_commands= TVec<string>(2, "allo"); 00148 t->splitter= new TrainTestSplitter(); 00149 t->learner= zz1.ll; 00150 zz1.invoke("chkObj", t); 00151 00152 zz1.invoke("testRec", n); 00153 zz1.invoke("testRec2", (Object*)zz1.ll); 00154 00155 try 00156 { 00157 zz1.invoke("testRecCrash", 3); 00158 } 00159 catch(const PythonException& e) 00160 { 00161 string exception_msg = e.message(); 00162 // Remove memory addresses from the exception message, as they may 00163 // differ from one computer to another, causing the test to fail. 00164 boost::regex memory_adr("0x[abcdef0123456789]{6,}", 00165 boost::regex::perl|boost::regex::icase); 00166 string msg_without_sys_dependent_data = 00167 regex_replace(exception_msg, memory_adr, 00168 "0x[memory_address]"); 00169 boost::regex python_ver("Python 2\\.[0-9]\\.[0-9]", 00170 boost::regex::perl|boost::regex::icase); 00171 msg_without_sys_dependent_data = 00172 regex_replace(msg_without_sys_dependent_data, python_ver, 00173 "Python 2.X.Y"); 00174 boost::regex plearn_path("(/[a-zA-Z0-9_-]+)+/python_modules/", 00175 boost::regex::perl); 00176 msg_without_sys_dependent_data = 00177 regex_replace(msg_without_sys_dependent_data, plearn_path, 00178 "PLEARN_PYTHON_PATH:"); 00179 pout << "[INTENDED ERROR] Caught Python Exception: '" 00180 << msg_without_sys_dependent_data 00181 << "'" << endl; 00182 } 00183 00184 PythonCodeSnippet zz2("\nimport gc\n" 00185 "gc.collect()\n" 00186 "print 'nrefs 1010', len(gc.get_referrers(1010))\n"); 00187 zz2.build(); 00188 00189 TVec<PyObject*> refs= zz1.invoke("getRecTestReferrers"); 00190 00191 pout << "refs0: " << (refs[0] == zz1.m_compiled_code.getPyObject()) << endl; 00192 PyObject* dic= PyObject_GetAttrString(zz1.m_instance.getPyObject(), "__dict__"); 00193 pout << "refs1: " << (refs[1] == dic) << endl; 00194 Py_DECREF(dic); 00195 00196 00197 #ifndef PYTEST_ACTIVE 00198 for(int i= 0; i < 1000; ++i) 00199 { 00200 zz1.invoke("testRec", n); 00201 zz1.ll->nstages= n; 00202 zz1.invoke("testRec2", (Object*)zz1.ll); 00203 pout << "at iter " << i << "\t: " << getProcessDataMemory() << endl; 00204 } 00205 #endif //ndef PYTEST_ACTIVE 00206 00207 00208 00209 zz1.invoke("fini"); 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 :