PLearn 0.1
AutoSDBVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037 
00038 /* *******************************************************      
00039  * $Id: AutoSDBVMatrix.cc 3994 2005-08-25 13:35:03Z chapados $
00040  * AUTHOR: Pascal Vincent
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 #include "AutoSDBVMatrix.h"
00045 #include <plearn/io/load_and_save.h>
00046 
00047 
00048 namespace PLearn {
00049 using namespace std;
00050 
00051 AutoSDBVMatrix::AutoSDBVMatrix(const string& dbname)
00052     :sdb_(extract_filename(dbname), extract_directory(dbname), SDB::readonly, true), string_field_map()
00053 {
00054     metadatadir = extract_directory(dbname) + extract_filename(dbname) + ".metadata";
00055     if(!force_mkdir(metadatadir))
00056         PLWARNING("In AutoSDBVMatrix constructor, could not create directory %s",metadatadir.c_str());
00057 
00058     const Schema& sc = sdb_.getSchema();
00059 
00060     //get string mappings from sdb metadatadir
00061     getMappings();
00062 
00063     row_ = Row(&sc);
00064     Schema::const_iterator it = sc.begin();
00065     Schema::const_iterator itend = sc.end();
00066 
00067     width_= sdb_.width();
00068     length_ = sdb_.length();
00069 
00070     // resize the string mappings (TODO/WARNING : two string maping systems coexist (not so peacfully), next 2 line are from the newer system but
00071     // transition is uncompleted
00072     map_sr = TVec<map<string,real> >(width_);
00073     map_rs = TVec<map<real,string> >(width_);
00074 
00075     int i=0;
00076     for(it=sc.begin(); it!=itend; ++it)
00077     {
00078         if(it->field_type==DateType)
00079             declareField(i++, it->name, VMField::Date);
00080         else
00081             declareField(i++, it->name, VMField::UnknownType);
00082     }
00083 }
00084 
00085 void AutoSDBVMatrix::getNewRow(int i, const Vec& v) const
00086 {
00087     sdb_.getInRow(i, row_);
00088     Row::const_iterator it = row_.begin();
00089     int w = width();
00090     if(w!=v.length())
00091         PLERROR("In AutoSDBVMatrix::getNewRow length of v must be width of VMatrix");
00092 
00093     int j=0;
00094     while(j<w)
00095     {
00096         if(it.isString())
00097             v[j]= string_field_map.find(it.name())->second[it.asString()];
00098         else if(it.isMissing())
00099             v[j] = MISSING_VALUE;
00100         else if(it.isCharacter())
00101             v[j] = (real)*(it.asCharacter());
00102         else
00103             v[j] = (real)it.toDouble();
00104         ++j;
00105         ++it;
00106     }
00107 }
00108 
00109 
00110 void AutoSDBVMatrix::getMappings()
00111 {
00112     const Schema& sc = sdb_.getSchema();
00113   
00114     for(Schema::const_iterator it= sc.begin(); it < sc.end(); ++it)
00115         if(it->field_type == StringType)
00116         {
00117             PPath field_filename = metadatadir / (it->name + ".strings");
00118             real  dft_val        = MISSING_VALUE;
00119       
00120             // Get value for others, if file exists
00121             if( isfile(field_filename + ".others") )
00122                 PLearn::load(field_filename + ".others", dft_val);
00123       
00124             // Get mapping, if it exists
00125             string_field_map[it->name] = StringFieldMapping(field_filename, dft_val);
00126             num2string_map[it->name]   = NumToStringMapping(field_filename);
00127         }
00128 }
00129 
00130 
00131 } // end of namespace PLearn
00132 
00133 
00134 /*
00135   Local Variables:
00136   mode:c++
00137   c-basic-offset:4
00138   c-file-style:"stroustrup"
00139   c-file-offsets:((innamespace . 0)(inline-open . 0))
00140   indent-tabs-mode:nil
00141   fill-column:79
00142   End:
00143 */
00144 // 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