PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RunICPVariable.cc 00004 // 00005 // Copyright (C) 2006 Olivier Delalleau 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: Olivier Delalleau 00036 00040 #include "RunICPVariable.h" 00041 #include "ScoreLayerVariable.h" 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00048 PLEARN_IMPLEMENT_OBJECT( 00049 RunICPVariable, 00050 "Runs multiple ICPs and outputs the transformed template coordinates.", 00051 "" 00052 ); 00053 00055 // RunICPVariable // 00057 RunICPVariable::RunICPVariable(): 00058 score_layer(0) 00059 { 00060 } 00061 00062 RunICPVariable::RunICPVariable(Variable* input): 00063 inherited(input, 0, 3), 00064 score_layer(0) 00065 {} 00066 00068 // addTemplate // 00070 void RunICPVariable::addTemplate(PP<ChemicalICP> icp_aligner, 00071 PP<Molecule> mol_template, 00072 Var mol_coordinates) 00073 { 00074 icp_aligners.append(icp_aligner); 00075 templates.append(mol_template); 00076 molecule_coordinates.append(mol_coordinates); 00077 this->sizeprop(); // Resize this variable. 00078 } 00079 00080 // constructor from input variable and parameters 00081 // RunICPVariable::RunICPVariable(Variable* input, param_type the_parameter,...) 00082 // ### replace with actual parameters 00083 // : inherited(input, this_variable_length, this_variable_width), 00084 // parameter(the_parameter), 00085 // ... 00086 //{ 00087 // // ### You may (or not) want to call build_() to finish building the 00088 // // ### object 00089 //} 00090 00092 // recomputeSize // 00094 void RunICPVariable::recomputeSize(int& l, int& w) const 00095 { 00096 l = 0; 00097 for (int i = 0; i < templates.length(); i++) 00098 l += templates[i]->n_points(); 00099 w = 3; 00100 } 00101 00103 // fprop // 00105 void RunICPVariable::fprop() 00106 { 00107 PLASSERT( score_layer ); 00108 // We obtain the molecule from the input variable. 00109 PP<Molecule> molecule = score_layer->getMolecule(input->value[0]); 00110 // Launch all ICPs. 00111 int index_coord = 0; 00112 for (int i = 0; i < icp_aligners.length(); i++) { 00113 // Run ICP. 00114 PP<ChemicalICP> icp = icp_aligners[i]; 00115 PP<Molecule> template_mol = templates[i]; 00116 icp->setMolecule(molecule); 00117 icp->run(); 00118 // Modify score normalization coefficient if needed. 00119 if (score_layer->normalize_by_n_features) 00120 score_layer->setScalingCoefficient(i, 00121 real(1.0 / (template_mol->n_points() * 00122 (3 + icp->used_template_features.width())))); 00123 // Some variables will need to be resized after ICP has been run. 00124 paths_to_resize[i].sizeprop(); 00125 // Compute the aligned coordinates of the template and store them in 00126 // this variable. 00127 Mat template_coords = 00128 this->matValue.subMat(index_coord, 0, template_mol->n_points(), 3); 00129 index_coord += template_mol->n_points(); 00130 productTranspose(template_coords, template_mol->coordinates, 00131 icp->rotation); 00132 template_coords += icp->translation; 00133 // Obtain the coordinates of the nearest neighbors in the aligned 00134 // molecule. 00135 Mat molecule_coords = molecule_coordinates[i]->matValue; 00136 PLASSERT( molecule_coords.length() == template_mol->n_points() ); 00137 for (int k = 0; k < template_mol->n_points(); k++) { 00138 int neighbor = icp->matching[k]; 00139 molecule_coords(k) << molecule->coordinates(neighbor); 00140 } 00141 } 00142 } 00143 00145 // bprop // 00147 void RunICPVariable::bprop() 00148 { 00149 // We do not backprop anything in this Variable. 00150 } 00151 00152 // ### You can implement these methods: 00153 // void RunICPVariable::bbprop() {} 00154 // void RunICPVariable::symbolicBprop() {} 00155 // void RunICPVariable::rfprop() {} 00156 00157 00159 // build // 00161 void RunICPVariable::build() 00162 { 00163 inherited::build(); 00164 build_(); 00165 } 00166 00167 void RunICPVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00168 { 00169 inherited::makeDeepCopyFromShallowCopy(copies); 00170 00171 // ### Call deepCopyField on all "pointer-like" fields 00172 // ### that you wish to be deepCopied rather than 00173 // ### shallow-copied. 00174 // ### ex: 00175 // deepCopyField(trainvec, copies); 00176 // To be deeped copied 00177 deepCopyField(icp_aligners, copies); 00178 deepCopyField(templates,copies); 00179 deepCopyField(molecule_coordinates,copies); 00180 deepCopyField(paths_to_resize, copies); 00181 deepCopyField(score_layer, copies); 00182 00183 // ### If you want to deepCopy a Var field: 00184 // varDeepCopyField(somevariable, copies); 00185 00186 00187 // ### Remove this line when you have fully implemented this method. 00188 // PLERROR("RunICPVariable::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00189 } 00190 00191 void RunICPVariable::declareOptions(OptionList& ol) 00192 { 00193 // ### Declare all of this object's options here. 00194 // ### For the "flags" of each option, you should typically specify 00195 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00196 // ### OptionBase::tuningoption. If you don't provide one of these three, 00197 // ### this option will be ignored when loading values from a script. 00198 // ### You can also combine flags, for example with OptionBase::nosave: 00199 // ### (OptionBase::buildoption | OptionBase::nosave) 00200 00201 // ### ex: 00202 // declareOption(ol, "myoption", &RunICPVariable::myoption, 00203 // OptionBase::buildoption, 00204 // "Help text describing this option"); 00205 // ... 00206 00207 // Now call the parent class' declareOptions 00208 inherited::declareOptions(ol); 00209 } 00210 00211 void RunICPVariable::build_() 00212 { 00213 // ### This method should do the real building of the object, 00214 // ### according to set 'options', in *any* situation. 00215 // ### Typical situations include: 00216 // ### - Initial building of an object from a few user-specified options 00217 // ### - Building of a "reloaded" object: i.e. from the complete set of 00218 // ### all serialised options. 00219 // ### - Updating or "re-building" of an object after a few "tuning" 00220 // ### options have been modified. 00221 // ### You should assume that the parent class' build_() has already been 00222 // ### called. 00223 } 00224 00226 // getPathsToResize // 00228 VarArray& RunICPVariable::getPathsToResize(int i) 00229 { 00230 if (i >= paths_to_resize.length()) 00231 paths_to_resize.resize(i + 1); 00232 return paths_to_resize[i]; 00233 } 00234 00236 // setScoreLayer // 00238 void RunICPVariable::setScoreLayer(ScoreLayerVariable* the_score_layer) 00239 { 00240 score_layer = the_score_layer; 00241 } 00242 00243 } // end of namespace PLearn 00244 00245 00246 /* 00247 Local Variables: 00248 mode:c++ 00249 c-basic-offset:4 00250 c-file-style:"stroustrup" 00251 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00252 indent-tabs-mode:nil 00253 fill-column:79 00254 End: 00255 */ 00256 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :