PLearn 0.1
MoleculeTemplate.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // MoleculeTemplate.cc
00004 //
00005 // Copyright (C) 2006 Pascal Lamblin
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: Pascal Lamblin
00036 
00040 #include "MoleculeTemplate.h"
00041 #include <plearn/math/TMat.h>
00042 #include <plearn/db/getDataSet.h>
00043 #include <plearn/vmat/MemoryVMatrix.h>
00044 #include <plearn/vmat/ConcatColumnsVMatrix.h>
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00049 PLEARN_IMPLEMENT_OBJECT(
00050     MoleculeTemplate,
00051     "Subclass of Molecule, plus standard devs of points' positions and"
00052     " features.",
00053     "There is only one geometric standard deviation per point (since space\n"
00054     "dimenstions are equivalent), there is one chemical standard deviation\n"
00055     "per chemical feature on every point (stored in the same order as the\n"
00056     "corresponding feature value).\n"
00057     );
00058 
00059 MoleculeTemplate::MoleculeTemplate( int the_class_label )
00060     : class_label( the_class_label )
00061 {
00062 }
00063 
00064 MoleculeTemplate::MoleculeTemplate( const PPath& filename,
00065                                     int the_class_label )
00066     : class_label( the_class_label )
00067 {
00068     readFromFile( filename );
00069     build();
00070 }
00071 
00072 MoleculeTemplate::MoleculeTemplate( const Molecule& molecule,
00073                                     const Vec& the_geom_dev,
00074                                     const Mat& the_feat_dev,
00075                                     int the_class_label )
00076     : inherited( molecule ),
00077       geom_dev( the_geom_dev ),
00078       feat_dev( the_feat_dev ),
00079       class_label( the_class_label )
00080 {
00081 //    build();
00082 }
00083 
00084 void MoleculeTemplate::build()
00085 {
00086     inherited::build();
00087     build_();
00088 }
00089 
00090 void MoleculeTemplate::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00091 {
00092     inherited::makeDeepCopyFromShallowCopy(copies);
00093 
00094     // deepCopyField(trainvec, copies);
00095 
00096     deepCopyField(geom_dev, copies);
00097     deepCopyField(feat_dev, copies);
00098 }
00099 
00100 void MoleculeTemplate::readFromAMATFile( const PPath& filename )
00101 {
00102     VMat all = getDataSet( filename );
00103     Mat all_mat = all->toMat();
00104     int all_width = all->width();
00105     TVec<string> all_names = all->fieldNames();
00106 
00107     TVec<int> geom_dev_positions = all_names.findIndices( "geom_dev" );
00108     if( geom_dev_positions.size() == 0 )
00109     {
00110         features = all_mat;
00111         features.compact();
00112 
00113         feature_names = all_names;
00114     }
00115     else if( geom_dev_positions.size() == 1 )
00116     {
00117         int gd_pos = geom_dev_positions[0];
00118         if( all_width != gd_pos+1 && all_width != 2*gd_pos+1 )
00119             PLERROR( "MoleculeTemplate::readFromAMATFile - 'geom_dev' is field"
00120                      " number %d,\n"
00121                      "there are %d following fields (expecting 0 or %d).\n",
00122                      gd_pos, all_width-gd_pos-1, gd_pos );
00123 
00124         features = all_mat.subMatColumns(0, gd_pos);
00125         features.compact();
00126 
00127         geom_dev = all_mat.column( gd_pos ).toVecCopy();
00128         feat_dev = all_mat.subMatColumns(all_width - gd_pos, gd_pos );
00129         feat_dev.compact();
00130 
00131         feature_names = all_names.subVec(0, gd_pos );
00132     }
00133     else
00134     {
00135         PLERROR( "MoleculeTemplate::readFromAMATFile - 'geom_dev' field should"
00136                  " be present\n"
00137                  "only once in amat file (present %d times).\n",
00138                  geom_dev_positions.size() );
00139     }
00140 }
00141 
00142 
00143 void MoleculeTemplate::writeToAMATFile( const PPath& filename )
00144 {
00145     VMat features_ = new MemoryVMatrix( features );
00146     features_->declareFieldNames( feature_names );
00147 
00148     VMat geom_dev_ = new MemoryVMatrix( geom_dev.toMat(geom_dev.length(), 1) );
00149     TVec<string> geom_dev_names( 1, "geom_dev" );
00150     geom_dev_->declareFieldNames( geom_dev_names );
00151 
00152     VMat feat_dev_ = new MemoryVMatrix( feat_dev );
00153     TVec<string> feat_dev_names = feature_names.copy();
00154     for( int i=0 ; i<n_features() ; i++ )
00155         feat_dev_names[i] += "_dev";
00156     feat_dev_->declareFieldNames( feat_dev_names );
00157 
00158     VMat all = hconcat( features_, hconcat( geom_dev_, feat_dev_ ) );
00159     all->defineSizes( all->width(), 0, 0 );
00160     all->saveAMAT( filename, false );
00161 }
00162 
00163 
00164 void MoleculeTemplate::declareOptions(OptionList& ol)
00165 {
00166     // declareOption(ol, "myoption", &MoleculeTemplate::myoption, OptionBase::buildoption,
00167     //               "Help text describing this option");
00168 
00169     declareOption(ol, "geom_dev", &MoleculeTemplate::geom_dev,
00170                   OptionBase::buildoption,
00171                   "Standard deviations of the geometrical distance");
00172 
00173     declareOption(ol, "feat_dev", &MoleculeTemplate::feat_dev,
00174                   OptionBase::buildoption,
00175                   "Standard deviations of each chemical property");
00176 
00177     declareOption(ol, "class_label", &MoleculeTemplate::class_label,
00178                   OptionBase::buildoption,
00179                   "Class label (0 for inactive, 1 for active, -1 for"
00180                   " uninitialized");
00181 
00182     // Now call the parent class' declareOptions
00183     inherited::declareOptions(ol);
00184 }
00185 
00186 void MoleculeTemplate::build_()
00187 {
00188     // Size check
00189     int geom_dev_length = geom_dev.length();
00190     int feat_dev_length = feat_dev.length();
00191     int feat_dev_width = feat_dev.width();
00192 
00193     // TODO: resize if empty?
00194     if( geom_dev_length == 0 )
00195     {
00196         PLWARNING( "MoleculeTemplate::build_ - geom_dev.length() == 0,\n"
00197                    "resizing to n_points() (%d), and filling with"
00198                    " 1's.\n", n_points() );
00199         geom_dev = Vec( n_points(), 1 );
00200     }
00201     else if( geom_dev_length != n_points() )
00202         PLERROR( "MoleculeTemplate::build_ - geom_dev.length() should be equal"
00203                  " to\n"
00204                  "n_points() (%d != %d).\n",
00205                  geom_dev_length, n_points() );
00206 
00207     if( feat_dev_length == 0 && feat_dev_width == 0 )
00208     {
00209         PLWARNING( "MoleculeTemplate::build_ - feat_dev.length() == 0 and\n"
00210                    "feat_dev.width() == 0. Resizing to n_points() ×"
00211                    " n_features()\n"
00212                    "(%d × %d), and filling with 1's.\n",
00213                    n_points(), n_features() );
00214         feat_dev = Mat( n_points(), n_features(), 1 );
00215     }
00216     else if( feat_dev_length != n_points() )
00217         PLERROR( "MoleculeTemplate::build_ - feat_dev.length() should be equal"
00218                  " to\n"
00219                  "n_points() (%d != %d).\n",
00220                  feat_dev_length, n_points() );
00221     else if( feat_dev_width != n_features() )
00222         PLERROR( "MoleculeTemplate::build_ - feat_dev.width() should be equal"
00223                  " to\n"
00224                  "n_features() (%d != %d).\n", feat_dev_width, n_features() );
00225 
00226 }
00227 
00228 
00229 } // end of namespace PLearn
00230 
00231 
00232 /*
00233   Local Variables:
00234   mode:c++
00235   c-basic-offset:4
00236   c-file-style:"stroustrup"
00237   c-file-offsets:((innamespace . 0)(inline-open . 0))
00238   indent-tabs-mode:nil
00239   fill-column:79
00240   End:
00241 */
00242 // 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