PLearn 0.1
JoinVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn ("A C++ Machine Learning Library")
00004 // Copyright (C) 2002 Julien Keable and Pascal Vincent
00005 //
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: JoinVMatrix.cc 8916 2008-04-30 14:12:24Z nouiz $
00037  * This file is part of the PLearn library.
00038  ******************************************************* */
00039 
00040 #include "JoinVMatrix.h"
00041 #include <plearn/base/stringutils.h>
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 
00047 PLEARN_IMPLEMENT_OBJECT(JoinVMatrix, "ONE LINE DESCR", "NO HELP");
00048 
00049 JoinVMatrix::JoinVMatrix(VMat mas,VMat sla,TVec<int> mi,TVec<int> si)
00050     : inherited(mas.length(),mas.width()),master(mas),slave(sla),master_idx(mi),slave_idx(si)
00051 {
00052     build();
00053 }
00054 
00055 void
00056 JoinVMatrix::build()
00057 {
00058     inherited::build();
00059     build_();
00060 }
00061 
00062 void
00063 JoinVMatrix::build_()
00064 {
00065     if (master && slave) {
00066         if(master_idx.size()!=slave_idx.size())
00067             PLERROR("JoinVMatrix : master and slave field correspondance don't have same dimensions ");
00068 
00069         for(int j=0;j<width();j++)
00070             declareField(j, master->fieldName(j), VMField::UnknownType);
00071 
00072         temp.resize(slave.width());
00073         tempkey.resize(master_idx.size());
00074 
00075         for(int i=0;i<slave.length();i++) {
00076             slave->getRow(i,temp);
00077             int s=slave_idx.size();
00078             for(int j=0;j<s;j++)
00079                 tempkey[j]=temp[slave_idx[j]];
00080             mp.insert(make_pair(tempkey,i));
00081         }
00082         updateMtime(master);
00083         updateMtime(slave);
00084     }
00085 }
00086 
00087 void
00088 JoinVMatrix::declareOptions(OptionList &ol)
00089 {
00090     declareOption(ol, "master", &JoinVMatrix::master, OptionBase::buildoption, "");
00091     declareOption(ol, "slave", &JoinVMatrix::slave, OptionBase::buildoption, "");
00092     declareOption(ol, "master_idx", &JoinVMatrix::master_idx, OptionBase::buildoption, "");
00093     declareOption(ol, "slave_idx", &JoinVMatrix::slave_idx, OptionBase::buildoption, "");
00094     inherited::declareOptions(ol);
00095 }
00096 
00097 void JoinVMatrix::addStatField(const string & statis,const string & namefrom,const string & nameto)
00098 {
00099     width_++;
00100     int from=slave->fieldIndex(namefrom),to=width()-1;
00101     if(from==-1)
00102         PLERROR("Unknown field in JOIN operation : %s",namefrom.c_str());
00103     declareField(to, nameto, VMField::UnknownType);
00104 
00105     if(statis=="COUNT")
00106         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::COUNT));
00107     else if(statis=="NMISSING")
00108         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::NMISSING));
00109     else if(statis=="NNONMISSING")
00110         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::NNONMISSING));
00111     else if(statis=="SUM")
00112         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::SUM));
00113     else if(statis=="SUMSQUARE")
00114         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::SUMSQUARE));
00115     else if(statis=="MEAN")
00116         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::MEAN));
00117     else if(statis=="VARIANCE")
00118         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::VARIANCE));
00119     else if(statis=="MIN")
00120         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::MIN));
00121     else if(statis=="MAX")
00122         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::MAX));
00123     else if(statis=="STDDEV")
00124         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::STDDEV));
00125     else if(statis=="STDERR")
00126         fld.push_back(JoinFieldStat(from,to,JoinFieldStat::STDERR));
00127     else PLERROR("Unknown statistic in JOIN operation : %s",statis.c_str());
00128 }
00129 
00130 void JoinVMatrix::getNewRow(int idx, const Vec& v) const
00131 {
00132     real nonmiss;
00133     master->getRow(idx,v.subVec(0,master.width()));
00134 
00135     // build a key used to search in the slave matrix
00136     int s=master_idx.size();
00137     for(int j=0;j<s;j++)
00138         tempkey[j]=v[master_idx[j]];
00139     Maptype::const_iterator it,low,upp;
00140     pair<Maptype::const_iterator,Maptype::const_iterator> tit=mp.equal_range(tempkey);
00141     low=tit.first;
00142     upp=tit.second;
00143 
00144     Vec popo(v.subVec(master.width(),width()-master.width()));
00145 
00146     int sz=(int)fld.size();
00147     Vec count(sz,0.0),nmissing(sz,0.0),sum(sz,0.0),sumsquare(sz,0.0),min(sz,FLT_MAX),max(sz,-FLT_MAX);
00148     real val;
00149     if(low!=mp.end())
00150     {
00151         for(it=low;it!=upp;++it)
00152         {
00153             slave->getRow(it->second,temp);
00154             for(int i=0;i<sz;i++)
00155             {
00156                 val=temp[fld[i].from];
00157                 count[i]++;
00158                 if(is_missing(val))nmissing[i]++;
00159                 else
00160                 {
00161                     sum[i]+=val;
00162                     sumsquare[i]+=val*val;
00163                     if(min[i]>val)min[i]=val;
00164                     if(max[i]<val)max[i]=val;
00165                 }
00166             }
00167         }
00168     }
00169     for(int i=0;i<sz;i++)
00170     {
00171         nonmiss=count[i]-nmissing[i];
00172         switch(fld[i].stat)
00173         {
00174         case JoinFieldStat::COUNT:
00175             popo[i]=count[i];
00176             break;
00177         case JoinFieldStat::NMISSING:
00178             popo[i]=nmissing[i];
00179             break;
00180         case JoinFieldStat::NNONMISSING:
00181             popo[i]=nonmiss;
00182             break;
00183         case JoinFieldStat::SUM:
00184             popo[i]=sum[i];
00185             break;
00186         case JoinFieldStat::SUMSQUARE:
00187             popo[i]=sumsquare[i];
00188             break;
00189         case JoinFieldStat::MEAN:
00190             popo[i]=sum[i]/count[i];
00191             break;
00192         case JoinFieldStat::VARIANCE:
00193             popo[i]=(sumsquare[i] - sum[i]*sum[i]/nonmiss)/(nonmiss-1);
00194             break;
00195         case JoinFieldStat::STDDEV:
00196             popo[i]=sqrt((sumsquare[i] - sum[i]*sum[i]/nonmiss)/(nonmiss-1));
00197             break;
00198         case JoinFieldStat::STDERR:
00199             popo[i]=sqrt((sumsquare[i] - sum[i]*sum[i]/nonmiss)/(nonmiss-1)/nonmiss);
00200             break;
00201         case JoinFieldStat::MIN:
00202             popo[i]=min[i];
00203             break;
00204         case JoinFieldStat::MAX:
00205             popo[i]=max[i];
00206             break;
00207         default:PLERROR("Unknown statistic in JoinVMatrix!");
00208         }
00209     }
00210 }
00211 
00212 string JoinVMatrix::getValString(int col, real val) const
00213 {
00214     if(col<master.width())
00215         return master->getValString(col,val);
00216     else
00217         return slave->getValString(col,val);
00218 }
00219 
00220 real JoinVMatrix::getStringVal(int col, const string & str) const
00221 {
00222     if(col<master.width())
00223         return master->getStringVal(col,str);
00224     else
00225         return slave->getStringVal(col,str);
00226 }
00227 
00228 const map<string,real>& JoinVMatrix::getStringToRealMapping(int col) const
00229 {
00230     if(col<master.width())
00231         return master->getStringToRealMapping(col);
00232     else
00233         return slave->getStringToRealMapping(col);
00234 
00235 }
00236 
00237 const map<real,string>& JoinVMatrix::getRealToStringMapping(int col) const
00238 {
00239     if(col<master.width())
00240         return master->getRealToStringMapping(col);
00241     else
00242         return slave->getRealToStringMapping(col);
00243 }
00244 
00245 
00246 string JoinVMatrix::getString(int row,int col) const
00247 {
00248     if(col<master.width())
00249         return master->getString(row,col);
00250     else
00251         return slave->getString(row,col);
00252 }
00253 
00254 } // end of namespace PLearn
00255 
00256 
00257 /*
00258   Local Variables:
00259   mode:c++
00260   c-basic-offset:4
00261   c-file-style:"stroustrup"
00262   c-file-offsets:((innamespace . 0)(inline-open . 0))
00263   indent-tabs-mode:nil
00264   fill-column:79
00265   End:
00266 */
00267 // 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