PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ProcessingVMatrix.cc 00004 // 00005 // Copyright (C) 2003 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: ProcessingVMatrix.cc 8664 2008-03-11 20:48:52Z tihocan $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00044 #include "ProcessingVMatrix.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 00050 ProcessingVMatrix::ProcessingVMatrix() 00051 :inherited() 00052 { 00053 } 00054 00055 PLEARN_IMPLEMENT_OBJECT( 00056 ProcessingVMatrix, 00057 "VMatrix whose rows are processed using a VPL script", 00058 "This VMatrix class processes each for of a source VMatrix using VPL scripts.\n" 00059 "The result of this VMatrix can be defined in one of two ways. The first way is to\n" 00060 "use a single program (in option 'prg') to process the whole row\n" 00061 "and set the inputsize, targetsize, weightsize and extrasize variables to defines\n" 00062 "which part of the output of the VPL program is treated as input, target, etc.\n" 00063 "of the VMatrix \n" 00064 "\n" 00065 "The second way is to have separate programs for each of the input, target, etc.\n" 00066 "parts of the VMatrix (using options 'input_prg', 'target_prg', 'weight_prg' and\n" 00067 "extra_prg), and then the respective inputsize, etc. will be inferred automatically\n" 00068 "from the length of the output of each respective VPL program.\n" 00069 "\n" 00070 "Note that in both cases, an empty program will produce a vector of length 0 as output\n" 00071 "If you want for example the output target vector of this VMatrix to be a copy of its\n" 00072 "input target vector, this must be done explicitly using a short VPL program.\n" 00073 "\n" 00074 "See class VMatLanguage for a description of the VPL syntax." 00075 ); 00076 00077 void ProcessingVMatrix::getNewRow(int i, const Vec& v) const 00078 { 00079 if(!prg.empty()) // we'll use prg rather than the *_prg 00080 program.run(i,v); 00081 else // we'll use the *_prg 00082 { 00083 if(!input_prg.empty()) 00084 input_prg_.run(i, v.subVec(0, inputsize_)); 00085 if(!target_prg.empty()) 00086 target_prg_.run(i, v.subVec(inputsize_, targetsize_)); 00087 if(!weight_prg.empty()) 00088 weight_prg_.run(i, v.subVec(inputsize_+targetsize_, weightsize_)); 00089 if(!extra_prg.empty()) 00090 extra_prg_.run(i, v.subVec(inputsize_+targetsize_+weightsize_, extrasize_)); 00091 } 00092 } 00093 00094 void ProcessingVMatrix::declareOptions(OptionList& ol) 00095 { 00096 declareOption(ol, "prg", &ProcessingVMatrix::prg, OptionBase::buildoption, 00097 "The VPL code to be applied to each row of the vmat\n" 00098 "You can either specify prg and inputsie, targetsize, weightsize, extrasize\n" 00099 "OR ALTERNATIVELY leave prg empty, and specify one or several of \n" 00100 "input_prg, target_prg, weight_prg, extra_prg. In this latter case,\n" 00101 "the sizes will be set from the corresponding program's number of output fieldnames \n"); 00102 00103 declareOption(ol, "input_prg", &ProcessingVMatrix::input_prg, OptionBase::buildoption, 00104 "This option is considered only if prg is the empty string\n" 00105 "Program string in VPL language to be applied to each raw input \n" 00106 "to generate the new preprocessed input.\n" 00107 "Note that names must be given to the generated values with :fieldname VPL syntax.\n"); 00108 00109 declareOption(ol, "target_prg", &ProcessingVMatrix::target_prg, OptionBase::buildoption, 00110 "This option is considered only if prg is the empty string\n" 00111 "Program string in VPL language to be applied to a dataset row\n" 00112 "to generate a proper target for the underlying learner.\n" 00113 "Note that names must be given to the generated values with :fieldname VPL syntax.\n"); 00114 00115 declareOption(ol, "weight_prg", &ProcessingVMatrix::weight_prg, OptionBase::buildoption, 00116 "This option is considered only if prg is the empty string\n" 00117 "Program string in VPL language to be applied to a dataset row\n" 00118 "to generate a proper weight for the underlying learner.\n" 00119 "Note that names must be given to the generated values with :fieldname VPL syntax.\n"); 00120 00121 declareOption(ol, "extra_prg", &ProcessingVMatrix::extra_prg, OptionBase::buildoption, 00122 "This option is considered only if prg is the empty string\n" 00123 "Program string in VPL language to be applied to a dataset row\n" 00124 "to generate proper extra fields for the underlying learner.\n" 00125 "Note that names must be given to the generated values with :fieldname VPL syntax.\n"); 00126 00127 // Now call the parent class' declareOptions 00128 inherited::declareOptions(ol); 00129 } 00130 00131 void ProcessingVMatrix::build_() 00132 { 00133 // Do not do anything until we get the source VMat. 00134 if (!source) 00135 return; 00136 00137 updateMtime(source); 00138 length_ = source->length(); 00139 00140 TVec<string> fieldnames; 00141 if(!prg.empty()) // ignore the *_prg 00142 { 00143 program.setSource(source); 00144 program.compileString(prg,fieldnames); 00145 int nfields = fieldnames.size(); 00146 width_ = nfields; 00147 00148 declareFieldNames(fieldnames); 00149 setMetaInfoFromSource(); 00150 00151 if(inputsize_<0) 00152 inputsize_ = nfields; 00153 if(targetsize_<0) 00154 targetsize_ = width_ - inputsize_ - max(0, weightsize_) 00155 - max(0, extrasize_); 00156 if(weightsize_<0) 00157 weightsize_ = width_ - inputsize_ - targetsize_ 00158 - max(0, extrasize_); 00159 if(extrasize_<0) 00160 extrasize_ = width_ - inputsize_ - targetsize_ - weightsize_; 00161 00162 PLCHECK_MSG( 00163 width_ == inputsize_ + targetsize_ + weightsize_ + extrasize_, 00164 "Width does not match sizes!"); 00165 } 00166 else // use the *_prg 00167 { 00168 00169 TVec<string> addfieldnames; 00170 if(!input_prg.empty()) 00171 { 00172 input_prg_.setSource(source); 00173 input_prg_.compileString(input_prg, addfieldnames); 00174 inputsize_ = addfieldnames.length(); 00175 fieldnames.append(addfieldnames); 00176 } 00177 else 00178 inputsize_ = 0; 00179 00180 if(!target_prg.empty()) 00181 { 00182 target_prg_.setSource(source); 00183 target_prg_.compileString(target_prg, addfieldnames); 00184 targetsize_ = addfieldnames.length(); 00185 fieldnames.append(addfieldnames); 00186 } 00187 else 00188 targetsize_ = 0; 00189 00190 if(!weight_prg.empty()) 00191 { 00192 weight_prg_.setSource(source); 00193 weight_prg_.compileString(weight_prg, addfieldnames); 00194 weightsize_ = addfieldnames.length(); 00195 fieldnames.append(addfieldnames); 00196 } 00197 else 00198 weightsize_ = 0; 00199 00200 if(!extra_prg.empty()) 00201 { 00202 extra_prg_.setSource(source); 00203 extra_prg_.compileString(extra_prg, addfieldnames); 00204 extrasize_ = addfieldnames.length(); 00205 fieldnames.append(addfieldnames); 00206 } 00207 else 00208 extrasize_ = 0; 00209 00210 width_ = inputsize_+targetsize_+weightsize_+extrasize_; 00211 declareFieldNames(fieldnames); 00212 setMetaInfoFromSource(); 00213 } 00214 00215 sourcevec.resize(source->width()); 00216 } 00217 00218 void ProcessingVMatrix::build() 00219 { 00220 inherited::build(); 00221 build_(); 00222 } 00223 00224 void ProcessingVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00225 { 00226 inherited::makeDeepCopyFromShallowCopy(copies); 00227 deepCopyField(sourcevec, copies); 00228 } 00229 00230 } // end of namespace PLearn 00231 00232 00233 /* 00234 Local Variables: 00235 mode:c++ 00236 c-basic-offset:4 00237 c-file-style:"stroustrup" 00238 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00239 indent-tabs-mode:nil 00240 fill-column:79 00241 End: 00242 */ 00243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :