PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // AddBagInformationVMatrix.cc 00004 // 00005 // Copyright (C) 2007 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 // Authors: Olivier Delalleau 00036 00040 #include "AddBagInformationVMatrix.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 PLEARN_IMPLEMENT_OBJECT( 00046 AddBagInformationVMatrix, 00047 "Add bag information to an existing VMat.", 00048 "This VMatrix adds an extra target column to its source VMat, that\n" 00049 "contains bag information as follows:\n" 00050 " - 1 = beginning of bag\n" 00051 " - 2 = end of bag\n" 00052 " - 0 = middle of bag\n" 00053 " - 3 = single-row bag (both beginning and end)\n" 00054 "Bags are found by looking at a specific column of the source (given by\n" 00055 "the 'bag_info_column' option): a new bag is started when this column's\n" 00056 "value changes between two consecutive samples.\n" 00057 ); 00058 00060 // AddBagInformationVMatrix // 00062 AddBagInformationVMatrix::AddBagInformationVMatrix(): 00063 remove_bag_info_column(false), 00064 bag_info_idx(-1) 00065 { 00066 } 00067 00069 // declareOptions // 00071 void AddBagInformationVMatrix::declareOptions(OptionList& ol) 00072 { 00073 declareOption(ol, "bag_info_column", 00074 &AddBagInformationVMatrix::bag_info_column, 00075 OptionBase::buildoption, 00076 "The source's column that is used to find bags in the data. It can\n" 00077 "be either a number or a column's name."); 00078 00079 declareOption(ol, "remove_bag_info_column", 00080 &AddBagInformationVMatrix::remove_bag_info_column, 00081 OptionBase::buildoption, 00082 "If true, then the source's column given by bag_info_column is\n" 00083 "removed from the resulting data."); 00084 00085 // Now call the parent class' declareOptions 00086 inherited::declareOptions(ol); 00087 } 00088 00090 // build // 00092 void AddBagInformationVMatrix::build() 00093 { 00094 inherited::build(); 00095 build_(); 00096 } 00097 00099 // build_ // 00101 void AddBagInformationVMatrix::build_() 00102 { 00103 if (source) { 00104 updateMtime(source); 00105 bag_info_idx = source->getFieldIndex(bag_info_column); 00106 sourcerow.resize(source->width()); 00107 int st = source->targetsize(); 00108 if (st >= 0) 00109 targetsize_ = st + 1; 00110 00111 // Set field infos. 00112 Array<VMField> fields = source->getFieldInfos().copy(); 00113 fields.append(VMField("bag_info")); 00114 00115 width_ = source->width() + 1; 00116 inputsize_ = source->inputsize(); 00117 00118 if (remove_bag_info_column) { 00119 // We need to remove a column from the source VMat. 00120 width_--; 00121 if( bag_info_idx < inputsize_ ) 00122 inputsize_--; 00123 Array<VMField> new_fields; 00124 for (int i = 0; i < fields.length(); i++) 00125 if (i != bag_info_idx) 00126 new_fields.append(fields[i]); 00127 fields = new_fields; 00128 } 00129 00130 setFieldInfos(fields); 00131 setMetaInfoFromSource(); 00132 } 00133 } 00134 00136 // getNewRow // 00138 void AddBagInformationVMatrix::getNewRow(int i, const Vec& v) const 00139 { 00140 bool is_beg = false; 00141 bool is_end = false; 00142 // Obtain current bag information. 00143 source->getRow(i, sourcerow); 00144 real cur = sourcerow[bag_info_idx]; 00145 if (i == 0) 00146 is_beg = true; 00147 else { 00148 // Compare with previous sample. 00149 real prev = source->get(i - 1, bag_info_idx); 00150 if (!is_equal(cur, prev)) 00151 is_beg = true; 00152 } 00153 if (i == length_ - 1) 00154 is_end = true; 00155 else { 00156 // Compare with next sample. 00157 real next = source->get(i + 1, bag_info_idx); 00158 if (!is_equal(cur, next)) 00159 is_end = true; 00160 } 00161 real bag_info = is_beg ? is_end ? 3 00162 : 1 00163 : is_end ? 2 00164 : 0; 00165 if (remove_bag_info_column) { 00166 v.subVec(0, bag_info_idx) << sourcerow.subVec(0, bag_info_idx); 00167 int n_left = sourcerow.length() - bag_info_idx - 1; 00168 v.subVec(bag_info_idx, n_left) << 00169 sourcerow.subVec(bag_info_idx + 1, n_left); 00170 } else 00171 v.subVec(0, sourcerow.length()) << sourcerow; 00172 v.lastElement() = bag_info; 00173 } 00174 00176 // makeDeepCopyFromShallowCopy // 00178 void AddBagInformationVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00179 { 00180 inherited::makeDeepCopyFromShallowCopy(copies); 00181 } 00182 00183 } // end of namespace PLearn 00184 00185 00186 /* 00187 Local Variables: 00188 mode:c++ 00189 c-basic-offset:4 00190 c-file-style:"stroustrup" 00191 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00192 indent-tabs-mode:nil 00193 fill-column:79 00194 End: 00195 */ 00196 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :