PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PythonTableVMatrix.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 "PythonTableVMatrix.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 00046 PLEARN_IMPLEMENT_OBJECT( 00047 PythonTableVMatrix, 00048 "VMatrix wrapper for a python table (module apstat.data.table)", 00049 "VMatrix wrapper for a python table (module apstat.data.table).\n" 00050 "The underlying table must be numeric.\n" 00051 ); 00052 00053 PythonTableVMatrix::PythonTableVMatrix(PyObject* table) 00054 :the_table(table) 00055 { 00056 } 00057 00058 void PythonTableVMatrix::getNewRow(int i, const Vec& v) const 00059 { 00060 PLASSERT(the_table); 00061 // Casting away const is okay here, PyObject_CallMethod does not 00062 // modify its arguments. XXX 00063 PyObject* row= PyObject_CallMethod(the_table, const_cast<char*>("getRow"), 00064 const_cast<char*>("i"), i); 00065 if(!row) 00066 { 00067 if (PyErr_Occurred()) PyErr_Print(); 00068 PLERROR("in PythonTableVMatrix::getNewRow : " 00069 "call to underlying table's 'getRow' failed."); 00070 } 00071 v << PythonObjectWrapper(row).as<Vec>(); 00072 Py_DECREF(row); 00073 } 00074 00075 void PythonTableVMatrix::declareOptions(OptionList& ol) 00076 { 00077 00078 declareOption(ol, "table", &PythonTableVMatrix::the_table, 00079 OptionBase::buildoption, 00080 "underlying table"); 00081 00082 inherited::declareOptions(ol); 00083 } 00084 00085 void PythonTableVMatrix::build_() 00086 { 00087 if(!the_table) return; 00088 // Casting away const is okay, PyObject_CallMethod does not modify its 00089 // arguments. 00090 PyObject* pywidth= PyObject_CallMethod(the_table, const_cast<char*>("width"), NULL); 00091 if(!pywidth) 00092 { 00093 if (PyErr_Occurred()) PyErr_Print(); 00094 PLERROR("in PythonTableVMatrix::build_ : " 00095 "call to underlying table's 'width' failed."); 00096 } 00097 width_= PythonObjectWrapper(pywidth); 00098 Py_DECREF(pywidth); 00099 PyObject* pylength= PyObject_CallMethod(the_table, const_cast<char*>("length"), NULL); 00100 if(!pylength) 00101 { 00102 if (PyErr_Occurred()) PyErr_Print(); 00103 PLERROR("in PythonTableVMatrix::build_ : " 00104 "call to underlying table's 'length' failed."); 00105 } 00106 length_= PythonObjectWrapper(pylength); 00107 Py_DECREF(pylength); 00108 PyObject* pyfieldnames= PyObject_GetAttrString(the_table, "fieldnames"); 00109 if(!pyfieldnames) 00110 { 00111 if (PyErr_Occurred()) PyErr_Print(); 00112 PLERROR("in PythonTableVMatrix::build_ : " 00113 "access to underlying table's 'fieldnames' failed."); 00114 } 00115 declareFieldNames(PythonObjectWrapper(pyfieldnames)); 00116 Py_DECREF(pyfieldnames); 00117 00118 if(PyObject_HasAttrString(the_table, "inputsize")) 00119 { 00120 PyObject* inpsz= PyObject_GetAttrString(the_table, "inputsize"); 00121 if(!inpsz) 00122 { 00123 if (PyErr_Occurred()) PyErr_Print(); 00124 PLERROR("in PythonTableVMatrix::build_ : " 00125 "access to underlying table's 'inputsize' failed."); 00126 } 00127 inputsize_= PythonObjectWrapper(inpsz).as<int>(); 00128 } 00129 if(PyObject_HasAttrString(the_table, "targetsize")) 00130 { 00131 PyObject* targsz= PyObject_GetAttrString(the_table, "targetsize"); 00132 if(!targsz) 00133 { 00134 if (PyErr_Occurred()) PyErr_Print(); 00135 PLERROR("in PythonTableVMatrix::build_ : " 00136 "access to underlying table's 'targetsize' failed."); 00137 } 00138 targetsize_= PythonObjectWrapper(targsz).as<int>(); 00139 } 00140 if(PyObject_HasAttrString(the_table, "weightsize")) 00141 { 00142 PyObject* wgtsz= PyObject_GetAttrString(the_table, "weightsize"); 00143 if(!wgtsz) 00144 { 00145 if (PyErr_Occurred()) PyErr_Print(); 00146 PLERROR("in PythonTableVMatrix::build_ : " 00147 "access to underlying table's 'weightsize' failed."); 00148 } 00149 weightsize_= PythonObjectWrapper(wgtsz).as<int>(); 00150 } 00151 00152 00153 } 00154 00155 void PythonTableVMatrix::build() 00156 { 00157 inherited::build(); 00158 build_(); 00159 } 00160 00161 void PythonTableVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00162 { 00163 inherited::makeDeepCopyFromShallowCopy(copies); 00164 00165 // deepCopyField(trainvec, copies); 00166 00167 PLERROR("PythonTableVMatrix::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00168 } 00169 00170 } // end of namespace PLearn 00171 00172 00173 /* 00174 Local Variables: 00175 mode:c++ 00176 c-basic-offset:4 00177 c-file-style:"stroustrup" 00178 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00179 indent-tabs-mode:nil 00180 fill-column:79 00181 End: 00182 */ 00183 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :