PLearn 0.1
BasicIdentityCallsTest.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // BasicIdentityCallsTest.cc
00004 //
00005 // Copyright (C) 2005 Nicolas Chapados 
00006 // Copyright (C) 2005-2006 Olivier Delalleau 
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************      
00037    * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 
00038    ******************************************************* */
00039 
00040 // Authors: Nicolas Chapados, Olivier Delalleau
00041 
00045 #include <plearn/python/PythonCodeSnippet.h>
00046 #include <iostream>
00047 #include <vector>
00048 #include <map>
00049 
00050 #include "BasicIdentityCallsTest.h"
00051 #include <plearn/io/openString.h>
00052 
00053 namespace PLearn {
00054 using namespace std;
00055 
00056 PLEARN_IMPLEMENT_OBJECT(
00057     BasicIdentityCallsTest,
00058     "Tests the core of PythonCodeSnippet through identity function calls in Python",
00059     ""
00060 );
00061 
00063 // BasicIdentityCallsTest //
00065 BasicIdentityCallsTest::BasicIdentityCallsTest() 
00066     /* ### Initialize all fields to their default value */
00067 {
00068     // ...
00069 
00070     // ### You may (or not) want to call build_() to finish building the object
00071     // ### (doing so assumes the parent classes' build_() have been called too
00072     // ### in the parent classes' constructors, something that you must ensure)
00073 }
00074 
00076 // build //
00078 void BasicIdentityCallsTest::build()
00079 {
00080     inherited::build();
00081     build_();
00082 }
00083 
00085 // makeDeepCopyFromShallowCopy //
00087 void BasicIdentityCallsTest::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00088 {
00089     inherited::makeDeepCopyFromShallowCopy(copies);
00090 
00091     // ### Call deepCopyField on all "pointer-like" fields 
00092     // ### that you wish to be deepCopied rather than 
00093     // ### shallow-copied.
00094     // ### ex:
00095     // deepCopyField(trainvec, copies);
00096 
00097     // ### Remove this line when you have fully implemented this method.
00098     PLERROR("BasicIdentityCallsTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00099 }
00100 
00102 // declareOptions //
00104 void BasicIdentityCallsTest::declareOptions(OptionList& ol)
00105 {
00106     // ### Declare all of this object's options here
00107     // ### For the "flags" of each option, you should typically specify  
00108     // ### one of OptionBase::buildoption, OptionBase::learntoption or 
00109     // ### OptionBase::tuningoption. Another possible flag to be combined with
00110     // ### is OptionBase::nosave
00111 
00112     // ### ex:
00113     // declareOption(ol, "myoption", &BasicIdentityCallsTest::myoption, OptionBase::buildoption,
00114     //               "Help text describing this option");
00115     // ...
00116 
00117     // Now call the parent class' declareOptions
00118     inherited::declareOptions(ol);
00119 }
00120 
00122 // build_ //
00124 void BasicIdentityCallsTest::build_()
00125 {
00126     // ### This method should do the real building of the object,
00127     // ### according to set 'options', in *any* situation. 
00128     // ### Typical situations include:
00129     // ###  - Initial building of an object from a few user-specified options
00130     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00131     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00132     // ### You should assume that the parent class' build_() has already been called.
00133 }
00134 
00135 string BasicIdentityCallsTest::python_code =
00136 "import sys\n"
00137 "#from numarray import *\n"
00138 "from numpy import *\n"
00139 "\n"
00140 "def nullary():\n"
00141 "    print >>sys.stderr, 'Called nullary()'\n"
00142 "    sys.stderr.flush()\n"
00143 "\n"
00144 "def unary_int(x):\n"
00145 "    assert isinstance(x,int)\n"
00146 "    return x\n"
00147 "\n"
00148 "def unary_long(x):\n"
00149 "    assert isinstance(x,long)\n"
00150 "    return x\n"
00151 "\n"
00152 "def unary_float(x):\n"
00153 "    assert isinstance(x,float)\n"
00154 "    return x\n"
00155 "\n"
00156 "def unary_str(x):\n"
00157 "    assert isinstance(x,str)\n"
00158 "    return x\n"
00159 "\n"
00160 "def unary_vec(x):\n"
00161 "    #assert isinstance(x,numarraycore.NumArray) and len(x.shape) == 1\n"
00162 "    assert isinstance(x,ndarray) and len(x.shape) == 1\n"
00163 "    return x\n"
00164 "\n"
00165 "def unary_mat(x):\n"
00166 "    print >>sys.stderr, 'Called unary_mat with:\\n',x\n"
00167 "    sys.stderr.flush()\n"
00168 "    #assert isinstance(x,numarraycore.NumArray) and len(x.shape) == 2\n"
00169 "    assert isinstance(x,ndarray) and len(x.shape) == 2\n"
00170 "    return x\n"
00171 "\n"
00172 "def unary_list_str(x):\n"
00173 "    assert isinstance(x,list)\n"
00174 "    return x\n"
00175 "\n"
00176 "def unary_dict(x):\n"
00177 "    assert isinstance(x,dict)\n"
00178 "    return x\n"
00179 "\n"
00180 "def binary(a,b):\n"
00181 "    return a,b\n"
00182 "\n"
00183 "def ternary(a,b,c):\n"
00184 "    return a,b,c\n"
00185 "\n"
00186 "def quaternary(a,b,c,d):\n"
00187 "    return a,b,c,d\n"
00188 ;
00189 
00190 
00191 void BasicIdentityCallsTest::nullary(const PythonCodeSnippet* python)
00192 {
00193     cout << "isInvokable(nullary)           : " << python->isInvokable("nullary") << endl;
00194     cout << "Calling nullary                : " << flush;
00195     python->invoke("nullary");
00196 }
00197 
00198 void BasicIdentityCallsTest::unary(const PythonCodeSnippet* python)
00199 {
00200     cout << "Calling unary_int(42)          : "
00201          << python->invoke("unary_int", 42).as<int>() << endl;
00202 
00203     cout << "Calling unary_long(2**64 - 1)  : "
00204          << python->invoke("unary_long", 18446744073709551615ULL).as<unsigned long long>() << endl;
00205 
00206     cout << "Calling unary_float(42.01)     : "
00207          << python->invoke("unary_float", 42.01).as<double>() << endl;
00208 
00209     cout << "Calling unary_str('Hello')     : "
00210          << python->invoke("unary_str", "Hello").as<string>() << endl;
00211 
00212     Vec v;
00213     string str_vec = "[2,3,5,7,11,13,17,19,23]";
00214     PStream is = openString(str_vec, PStream::plearn_ascii);
00215     is >> v;
00216     Mat m(3,3);
00217     m.toVec() << v;
00218 
00219     cout << "Calling unary_vec(v)           : "
00220          << tostring( python->invoke("unary_vec", v).as<Vec>() )
00221          << endl;
00222 
00223     // Test full matrix (mod == width)
00224     cout << "Calling unary_mat(m)           : " << endl;
00225     cout << tostring( python->invoke("unary_mat", m).as<Mat>() )
00226          << endl;
00227 
00228     // Test sliced matrix (mod > width)
00229     cout << "Calling unary_mat(m)           : " << endl;
00230     cout << tostring( python->invoke("unary_mat", m.subMatColumns(1,2)).as<Mat>() )
00231          << endl;
00232 
00233     TVec<string> tvs;
00234     string str_tvs = "[\"Cela\", \"est\", \"juste\", \"et\", \"bon\"]";
00235     PStream is_tvs = openString(str_tvs, PStream::plearn_ascii);
00236     is_tvs >> tvs;
00237     vector<string> vecs(tvs.begin(), tvs.end());
00238 
00239     cout << "Calling unary_list_str(tvs)    : "
00240          << tostring( python->invoke("unary_list_str", tvs).as< TVec<string> >() )
00241          << endl;
00242     cout << "Calling unary_list_str(vecs)   : "
00243          << tostring( TVec<string>(python->invoke("unary_list_str", vecs)
00244                                    .as< vector<string> >() ))
00245          << endl;
00246 
00247     map<string,int> mapsd;
00248     string str_mapsd = "{ Oui:16 il:32 est:64 juste:128 et:256 bon:512 }";
00249     PStream is_mapsd = openString(str_mapsd, PStream::plearn_ascii);
00250     is_mapsd >> mapsd;
00251 
00252     cout << "Calling unary_dict(mapsd)      : "
00253          << tostring( python->invoke("unary_dict", mapsd).as< map<string,int> >() )
00254          << endl;
00255 }
00256 
00257 void BasicIdentityCallsTest::binary(const PythonCodeSnippet* python)
00258 {
00259     cout << "Calling binary(2,4)            : "
00260          << tostring( python->invoke("binary",2,4).as< TVec<int> >())
00261          << endl;
00262 }
00263 
00264 void BasicIdentityCallsTest::ternary(const PythonCodeSnippet* python)
00265 {
00266     cout << "Calling ternary(2,4,8)         : "
00267          << tostring( python->invoke("ternary",2,4,8).as< TVec<int> >())
00268          << endl;
00269 }
00270 
00271 void BasicIdentityCallsTest::quaternary(const PythonCodeSnippet* python)
00272 {
00273     cout << "Calling quaternary(2,4,8,16)   : "
00274          << tostring( python->invoke("quaternary",2,4,8,16).as< TVec<int> >())
00275          << endl;
00276 }
00277 
00279 // perform //
00281 void BasicIdentityCallsTest::perform()
00282 {
00283     // ### The test code should go here.
00284     cout << "Python code to be executed: " << endl
00285          << ">>>" << python_code << "<<<" << endl;
00286     
00287     PP<PythonCodeSnippet> python = new PythonCodeSnippet(python_code);
00288     python->build();
00289     
00290     nullary(python);
00291     unary(python);
00292     binary(python);
00293     ternary(python);
00294     quaternary(python);
00295 }
00296 
00297 } // end of namespace PLearn
00298 
00299 
00300 /*
00301   Local Variables:
00302   mode:c++
00303   c-basic-offset:4
00304   c-file-style:"stroustrup"
00305   c-file-offsets:((innamespace . 0)(inline-open . 0))
00306   indent-tabs-mode:nil
00307   fill-column:79
00308   End:
00309 */
00310 // 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