PLearn 0.1
|
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: VMat.cc 8607 2008-02-29 17:11:46Z nouiz $ * 00040 * This file is part of the PLearn library. * 00041 ******************************************************** */ 00042 #include "VMat.h" 00043 #include <plearn/io/fileutils.h> 00044 00045 #include "MemoryVMatrix.h" 00046 #include "FileVMatrix.h" 00047 #include "SelectRowsVMatrix.h" 00048 #include "SelectColumnsVMatrix.h" 00049 #include "SelectRowsFileIndexVMatrix.h" 00050 #include <plearn/io/MatIO.h> 00051 00052 // TODO-PPath : this class is now PPath compliant 00053 // TODO-PStream : this class is now PStream compliant 00054 00055 namespace PLearn { 00056 using namespace std; 00057 00060 VMat::VMat() {} 00061 VMat::VMat(VMatrix* d): PP<VMatrix>(d) {} 00062 VMat::VMat(const VMat& d) :PP<VMatrix>(d) {} 00063 VMat::VMat(const Mat& datamat): PP<VMatrix>(new MemoryVMatrix(datamat)) {} 00064 VMat::~VMat() {} 00065 00066 VMat VMat::subMatRows(int i, int l) const 00067 { 00068 VMat res = ptr->subMat(i,0,l,width()); 00069 res->defineSizes(ptr->inputsize(), ptr->targetsize(), ptr->weightsize()); 00070 return res; 00071 } 00072 00073 VMat VMat::rows(TVec<int> rows_indices) const 00074 { return new SelectRowsVMatrix(*this, rows_indices); } 00075 00076 VMat VMat::rows(Vec rows_indices) const 00077 { return new SelectRowsVMatrix(*this, rows_indices); } 00078 00079 VMat VMat::rows(const PPath& indexfile) const 00080 { return new SelectRowsFileIndexVMatrix(*this, indexfile); } 00081 00082 VMat VMat::columns(TVec<int> columns_indices) const 00083 { return new SelectColumnsVMatrix(*this, columns_indices); } 00084 00085 VMat VMat::columns(Vec columns_indices) const 00086 { return new SelectColumnsVMatrix(*this, columns_indices); } 00087 00089 // precompute // 00091 void VMat::precompute() { 00092 VMat backup = *this; 00093 *this = new MemoryVMatrix(Mat(*this)); 00094 (*this)->setFieldInfos( backup->getFieldInfos() ); 00095 00096 // We restore the sizes info (lost in the Mat conversion). 00097 // Note that there would probably be more info to restore (like 00098 // field infos, string mappings, ...). 00099 (*this)->copySizesFrom(backup); 00100 00101 //TODO 00102 //(*this)->copyFieldInfosFrom(backup); 00103 } 00104 00105 void VMat::precompute(const PPath& pmatfile, bool use_existing_file) 00106 { 00107 VMat backup = *this; 00108 Array<VMField> infos = (*this)->getFieldInfos(); 00109 if(!use_existing_file || !pathexists(pmatfile)) 00110 save(pmatfile); 00111 *this = new FileVMatrix(pmatfile); 00112 (*this)->setFieldInfos( infos ); 00113 (*this)->copySizesFrom(backup); 00114 // TODO same as above 00115 } 00116 00117 00118 template <> 00119 void deepCopyField(VMat& field, CopiesMap& copies) 00120 { 00121 if (field) 00122 field = static_cast<VMatrix*>(field->deepCopy(copies)); 00123 } 00124 00126 // loadAsciiAsVMat // 00128 VMat loadAsciiAsVMat(const PPath& filename) 00129 { 00130 Mat m; 00131 TVec<string> fn; 00132 TVec< map<string,real> > map_sr; // String -> real mappings. 00133 int inputsize = -1; 00134 int targetsize = -1; 00135 int weightsize = -1; 00136 loadAscii(filename, m, fn, inputsize, targetsize, weightsize, &map_sr); 00137 VMat vm = new MemoryVMatrix(m); 00138 if(inputsize>=0) 00139 vm->defineSizes(inputsize,targetsize,weightsize); 00140 else 00141 vm->defineSizes(m.width(),0,0); 00142 00143 vm->updateMtime(filename); 00144 // Set the discovered string -> real mappings. 00145 for (int i = 0; i < map_sr.length(); i++) { 00146 vm->setStringMapping(i, map_sr[i]); 00147 } 00148 for(int i=0;i<fn.size();i++) 00149 vm->declareField(i, fn[i]); 00150 return vm; 00151 } 00152 00153 } // end of namespace PLearn 00154 00155 00156 /* 00157 Local Variables: 00158 mode:c++ 00159 c-basic-offset:4 00160 c-file-style:"stroustrup" 00161 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00162 indent-tabs-mode:nil 00163 fill-column:79 00164 End: 00165 */ 00166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :