PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // AddMissingVMatrix.cc 00004 // 00005 // Copyright (C) 2005 Olivier Delalleau 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: AddMissingVMatrix.cc,v 1.2 2005/08/26 16:50:57 delallea Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Olivier Delalleau 00040 00044 #include "AddMissingVMatrix.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00050 // AddMissingVMatrix // 00052 AddMissingVMatrix::AddMissingVMatrix(): 00053 random_gen(new PRandom()), 00054 add_missing_target(true), 00055 missing_prop(0), 00056 only_on_first(-1), 00057 seed(-1) 00058 {} 00059 00060 PLEARN_IMPLEMENT_OBJECT(AddMissingVMatrix, 00061 "Adds missing values to an underlying VMatrix", 00062 "The columns which will be filled with missing values can be defined\n" 00063 "by the user using the field missing_values_columns or determined\n" 00064 "randomly, for each sample (if missing_values_columns is empty).\n" 00065 "When randomly adding missing values to an underlying VMatrix,\n" 00066 "note that you should precompute such a VMatrix, since the missing\n" 00067 "values will change within the same row if it is accessed more than\n" 00068 "once.\n" 00069 ); 00070 00072 // declareOptions // 00074 void AddMissingVMatrix::declareOptions(OptionList& ol) 00075 { 00076 declareOption(ol, "missing_prop", &AddMissingVMatrix::missing_prop, OptionBase::buildoption, 00077 "Percentage of missing values."); 00078 00079 declareOption(ol, "only_on_first", &AddMissingVMatrix::only_on_first, OptionBase::buildoption, 00080 "Only add missing values in the first 'only_on_first' samples (ignored if < 0)."); 00081 00082 declareOption(ol, "seed", &AddMissingVMatrix::seed, OptionBase::buildoption, 00083 "Random numbers seed."); 00084 00085 declareOption(ol, "missing_values_columns", &AddMissingVMatrix::missing_values_columns, OptionBase::buildoption, 00086 "Columns which will be filled with missing values."); 00087 00088 declareOption(ol, "add_missing_target", 00089 &AddMissingVMatrix::add_missing_target, 00090 OptionBase::buildoption, 00091 "Whether or not to add missing values in the target part."); 00092 00093 // Now call the parent class' declareOptions 00094 inherited::declareOptions(ol); 00095 } 00096 00098 // build // 00100 void AddMissingVMatrix::build() 00101 { 00102 // ### Nothing to add here, simply calls build_ 00103 inherited::build(); 00104 build_(); 00105 } 00106 00108 // build_ // 00110 void AddMissingVMatrix::build_() 00111 { 00112 updateMtime(source); 00113 // Ensure we are not using both a missing values proportion and 00114 // user-specified missing columns. 00115 if (!fast_exact_is_equal(missing_prop, 0) && 00116 !missing_values_columns.isEmpty()) 00117 PLERROR("In AddMissingVMatrix::build_ - You may only use one of these " 00118 "two options: 'missing_prop' or 'missing_values_columns'"); 00119 00120 random_gen->manual_seed(seed); 00121 setMetaInfoFromSource(); 00122 } 00123 00125 // getNewRow // 00127 void AddMissingVMatrix::getNewRow(int i, const Vec& v) const 00128 { 00129 source->getRow(i, v); 00130 if (only_on_first >= 0 && i >= only_on_first) 00131 return; 00132 int n = v.length(); 00133 00134 if (missing_values_columns.length()>0) 00135 { 00136 for (int j = 0; j < missing_values_columns.length(); j++) 00137 v[missing_values_columns[j]] = MISSING_VALUE; 00138 } 00139 else 00140 { 00141 for (int j = 0; j < n; j++) { 00142 bool is_target = (j >= source->inputsize() && 00143 j < source->inputsize() + source->targetsize()); 00144 if ((add_missing_target || !is_target) && 00145 random_gen->uniform_sample() < missing_prop) 00146 { 00147 v[j] = MISSING_VALUE; 00148 } 00149 } 00150 } 00151 } 00152 00154 // makeDeepCopyFromShallowCopy // 00156 void AddMissingVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00157 { 00158 inherited::makeDeepCopyFromShallowCopy(copies); 00159 deepCopyField(random_gen, copies); 00160 deepCopyField(missing_values_columns, copies); 00161 } 00162 00163 real AddMissingVMatrix::getStringVal(int col, const string & str) const 00164 { 00165 #ifdef BOUNDCHECK 00166 if(col>=width_) 00167 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00168 #endif 00169 return source->getStringVal(col,str); 00170 } 00171 00172 string AddMissingVMatrix::getValString(int col, real val) const 00173 { 00174 #ifdef BOUNDCHECK 00175 if(col>=width_) 00176 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00177 #endif 00178 return source->getValString(col,val); 00179 } 00180 00181 PP<Dictionary> AddMissingVMatrix::getDictionary(int col) const 00182 { 00183 #ifdef BOUNDCHECK 00184 if(col>=width_) 00185 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00186 #endif 00187 return source->getDictionary(col); 00188 } 00189 00190 00191 void AddMissingVMatrix::getValues(int row, int col, Vec& values) const 00192 { 00193 #ifdef BOUNDCHECK 00194 if(col>=width_) 00195 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00196 #endif 00197 source->getValues(row,col,values); 00198 } 00199 00200 void AddMissingVMatrix::getValues(const Vec& input, int col, Vec& values) const 00201 { 00202 #ifdef BOUNDCHECK 00203 if(col>=width_) 00204 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00205 #endif 00206 source->getValues(input, col,values); 00207 } 00208 00209 } // end of namespace PLearn 00210 00211 /* 00212 Local Variables: 00213 mode:c++ 00214 c-basic-offset:4 00215 c-file-style:"stroustrup" 00216 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00217 indent-tabs-mode:nil 00218 fill-column:79 00219 End: 00220 */ 00221 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :