PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ReorderByMissingVMatrix.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: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Olivier Delalleau 00040 00044 #include "ReorderByMissingVMatrix.h" 00045 #include <vector> 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 PLEARN_IMPLEMENT_OBJECT( 00051 ReorderByMissingVMatrix, 00052 "Re-order samples in a source VMat by their missing attributes", 00053 00054 "Each sample is associated with a binary string indicating which elements\n" 00055 "are missing or not.\n" 00056 "For instance, the string '1001' indicates that the second and third\n" 00057 "features are missing, while the first and fourth ones are present.\n" 00058 "Samples are then sorted according to the usual lexicographic order on\n" 00059 "these strings.\n" 00060 ); 00061 00063 // ReorderByMissingVMatrix // 00065 ReorderByMissingVMatrix::ReorderByMissingVMatrix(): 00066 verbosity(1) 00067 {} 00068 00070 // declareOptions // 00072 void ReorderByMissingVMatrix::declareOptions(OptionList& ol) 00073 { 00074 00075 // Build options. 00076 00077 declareOption(ol, "verbosity", &ReorderByMissingVMatrix::verbosity, 00078 OptionBase::buildoption, 00079 "Control the amount of output."); 00080 00081 // Learnt options. 00082 00083 declareOption(ol, "missing_pattern_change", 00084 &ReorderByMissingVMatrix::missing_pattern_change, 00085 OptionBase::learntoption, 00086 "A vector whose i-th element is a boolean indicating whether the\n" 00087 "missing pattern has changed going from the (i-1)-th sample to the\n" 00088 "i-th sample (note: the first element is always true)."); 00089 00090 // Now call the parent class' declareOptions 00091 inherited::declareOptions(ol); 00092 00093 // Hide unused options. 00094 00095 redeclareOption(ol, "indices", &ReorderByMissingVMatrix::indices, 00096 OptionBase::nosave, "Not used."); 00097 00098 redeclareOption(ol, "indices_vmat", &ReorderByMissingVMatrix::indices_vmat, 00099 OptionBase::nosave, "Not used."); 00100 00101 } 00102 00104 // build // 00106 void ReorderByMissingVMatrix::build() 00107 { 00108 inherited::build(); 00109 build_(); 00110 } 00111 00114 struct IndexAndMissingFlags { 00115 int index; 00116 string missing_flags; 00117 }; 00118 00120 struct compareIndexAndMissingFlags { 00121 bool operator() (const IndexAndMissingFlags& x, 00122 const IndexAndMissingFlags& y) 00123 { 00124 return (x.missing_flags < y.missing_flags); 00125 } 00126 } compare_index_and_missing_flags; 00127 00128 00130 // build_ // 00132 void ReorderByMissingVMatrix::build_() 00133 { 00134 updateMtime(indices_vmat); 00135 updateMtime(source); 00136 if (source) { 00137 // Construct a vector containing each sample index associated with its 00138 // missing flags. 00139 vector<IndexAndMissingFlags> vec; 00140 int n = source.length(); 00141 int w = source.width(); 00142 sourcerow.resize(w); 00143 string previous_flags; 00144 int n_flag_changes = 0; 00145 for (int i = 0; i < n; i++) { 00146 source->getRow(i, sourcerow); 00147 string missing_flags; 00148 for (int j = 0; j < w; j++) 00149 if (is_missing(sourcerow[j])) 00150 missing_flags += '0'; 00151 else 00152 missing_flags += '1'; 00153 IndexAndMissingFlags ex; 00154 ex.index = i; 00155 ex.missing_flags = missing_flags; 00156 vec.push_back(ex); 00157 if (!previous_flags.empty() && missing_flags != previous_flags) 00158 n_flag_changes++; 00159 previous_flags = missing_flags; 00160 } 00161 if (verbosity >= 1) 00162 pout << "Number of flag changes before sorting: " 00163 << n_flag_changes << endl; 00164 // Sort this vector. 00165 sort(vec.begin(), vec.end(), compare_index_and_missing_flags); 00166 // Build the 'indices' vector. 00167 indices.resize(n); 00168 indices.resize(0); 00169 vector<IndexAndMissingFlags>::const_iterator it = vec.begin(); 00170 previous_flags = ""; 00171 n_flag_changes = 0; 00172 int index = 0; 00173 missing_pattern_change.resize(int(vec.size())); 00174 for (; it != vec.end(); it++) { 00175 indices.append(it->index); 00176 const string& missing_flags = it->missing_flags; 00177 if (missing_flags != previous_flags) { 00178 if (!previous_flags.empty()) 00179 n_flag_changes++; 00180 missing_pattern_change[index++] = true; 00181 } else 00182 missing_pattern_change[index++] = false; 00183 previous_flags = missing_flags; 00184 } 00185 if (verbosity >= 1) 00186 pout << "Number of flag changes after sorting: " 00187 << n_flag_changes << endl; 00188 // Re-build the parent class according to the new 'indices' vector. 00189 inherited::build(); 00190 } 00191 } 00192 00194 // makeDeepCopyFromShallowCopy // 00196 void ReorderByMissingVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00197 { 00198 inherited::makeDeepCopyFromShallowCopy(copies); 00199 deepCopyField(missing_pattern_change, copies); 00200 } 00201 00202 } // end of namespace PLearn 00203 00204 00205 /* 00206 Local Variables: 00207 mode:c++ 00208 c-basic-offset:4 00209 c-file-style:"stroustrup" 00210 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00211 indent-tabs-mode:nil 00212 fill-column:79 00213 End: 00214 */ 00215 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :