PLearn 0.1
EncodedVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 2006 Xavier Saint-Mleux
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //
00009 //  1. Redistributions of source code must retain the above copyright
00010 //     notice, this list of conditions and the following disclaimer.
00011 //
00012 //  2. Redistributions in binary form must reproduce the above copyright
00013 //     notice, this list of conditions and the following disclaimer in the
00014 //     documentation and/or other materials provided with the distribution.
00015 //
00016 //  3. The name of the authors may not be used to endorse or promote
00017 //     products derived from this software without specific prior written
00018 //     permission.
00019 //
00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //
00031 // This file is part of the PLearn library. For more information on the PLearn
00032 // library, go to the PLearn Web site at www.plearn.org
00033 
00034 #include "EncodedVMatrix.h"
00035 
00036 namespace PLearn {
00037 using namespace std;
00038 
00039 PLEARN_IMPLEMENT_OBJECT(EncodedVMatrix,
00040                         "Applies an encoding (map) to each column of a sub-matrix.",
00041                         ""
00042                        );
00043 
00044 EncodedVMatrix::EncodedVMatrix(bool call_build_)
00045     : inherited(call_build_)
00046 {
00047     if(call_build_)
00048         build_();
00049 }
00050 
00051 EncodedVMatrix::EncodedVMatrix(VMat the_source, TVec<map<real,real> > encodings_, TVec<real> defaults_, 
00052                                TVec<bool> encode_, bool call_build_)
00053     : inherited(the_source,
00054                 the_source->length(),
00055                 the_source->width(),
00056                 call_build_),
00057       encodings(encodings_),
00058       defaults(defaults_),
00059       encode(encode_)
00060 {
00061     if(call_build_)
00062         build_();
00063 }
00064 
00065 
00066 void EncodedVMatrix::declareOptions(OptionList& ol)
00067 {
00068     declareOption(ol, "encodings", &EncodedVMatrix::encodings,
00069                   OptionBase::buildoption,
00070                   "Maps used to encode fields.");
00071 
00072     declareOption(ol, "defaults", &EncodedVMatrix::defaults,
00073                   OptionBase::buildoption,
00074                   "Default values to use if no encoding is present for some field value.");
00075 
00076     declareOption(ol, "encode", &EncodedVMatrix::encode,
00077                   OptionBase::buildoption,
00078                   "True if this column should be encoded (one boolean per col.)");
00079 
00080     // Now call the parent class' declareOptions
00081     inherited::declareOptions(ol);
00082 }
00083 
00085 // build_ //
00087 void EncodedVMatrix::build_()
00088 {
00089     if(source)
00090     {
00091         updateMtime(source);
00092         copySizesFrom(source);
00093         declareFieldNames(source->fieldNames());
00094         for(int i= 0; i < source.width(); ++i)
00095         {
00096             if(i < encode.length() && encode[i])
00097             {
00098                 deleteStringMapping(i);
00099                 map<string,real> mm= source->getStringToRealMapping(i);
00100                 for(map<string,real>::iterator it= mm.begin(); it != mm.end(); ++it)
00101                     addStringMapping(i, it->first, encodings[i][it->second]);
00102             }
00103             else
00104                 setStringMapping(i, source->getStringToRealMapping(i));
00105         }
00106     }
00107     
00108 }
00109 
00111 // getNewRow //
00113 void EncodedVMatrix::getNewRow(int i, const Vec& v) const
00114 {
00115     source->getRow(i, v);
00116     encodeRow(encodings, defaults, encode, v.subVec(0, inputsize()));
00117 }
00118 
00119 void EncodedVMatrix::encodeRow(const TVec<map<real,real> >& encodings, const TVec<real>& defaults, 
00120                                const TVec<bool>& encode, const Vec& v)
00121 {
00122     int l= v.length();
00123     if(l != encodings.length() ||
00124        l != defaults.length() ||
00125        l != encode.length())
00126         PLERROR("in EncodedVMatrix::encodeRow: encodings, defaults and encode should be the same"
00127                 " size as a row's inputsize. (%d / %d / %d / %d)", encodings.length(), defaults.length(), encode.length(), l);
00128     
00129     for(int j= 0; j < v.length() && j < encodings.length(); ++j)
00130         if(encode[j])
00131         {
00132             map<real, real> enc= encodings[j];
00133             map<real, real>::iterator it= enc.find(v[j]);
00134             if(it == enc.end())
00135                 v[j]= defaults[j];
00136             else
00137                 v[j]= it->second;
00138         }
00139 }
00140 
00141 
00143 // build //
00145 void EncodedVMatrix::build()
00146 {
00147     inherited::build();
00148     build_();
00149 }
00150 
00152 // makeDeepCopyFromShallowCopy //
00154 void EncodedVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00155 {
00156     inherited::makeDeepCopyFromShallowCopy(copies);
00157     deepCopyField(encodings, copies);
00158     deepCopyField(defaults, copies);
00159     deepCopyField(encode, copies);
00160 }
00161 
00162 } // end of namespace PLearn
00163 
00164 
00165 /*
00166   Local Variables:
00167   mode:c++
00168   c-basic-offset:4
00169   c-file-style:"stroustrup"
00170   c-file-offsets:((innamespace . 0)(inline-open . 0))
00171   indent-tabs-mode:nil
00172   fill-column:79
00173   End:
00174 */
00175 // 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