PLearn 0.1
DisregardRowsVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // DisregardRowsVMatrix.cc
00004 //
00005 // Copyright (C) 2005 Christian Dorion
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: DisregardRowsVMatrix.cc 8617 2008-03-03 17:45:54Z nouiz $
00037  ******************************************************* */
00038 
00039 // Authors: Christian Dorion
00040 
00043 #define PL_LOG_MODULE_NAME "DisregardRowsVMatrix"
00044 
00045 #include "DisregardRowsVMatrix.h"
00046 #include <plearn/io/pl_log.h>
00047 
00048 namespace PLearn {
00049 using namespace std;
00050 
00051 PLEARN_IMPLEMENT_OBJECT(
00052     DisregardRowsVMatrix,
00053     "A vmat that disregard rows containing any specified values.\n",
00054     "Typically, this vmat is used to select the rows of the source vmat which\n"
00055     "do not contain missing values. However, this behaviour can be changed by\n"
00056     "setting the 'disregard_missings' flag to false and by providing any\n"
00057     "real value list through 'disregard_values'.\n"
00058     "\n"
00059     "The default behavior of the class is to inspect all columns of the\n"
00060     "underlying vmat, but one may specify a subset of the source's fieldnames\n"
00061     "to restrict the inspection." );
00062 
00063 
00064 //#####  Constructors  ########################################################
00065 
00066 DisregardRowsVMatrix::DisregardRowsVMatrix()
00067     : inherited(),
00068       _disregard_missings(true),
00069       _maximum_length(-1)
00070 { }
00071 
00072 DisregardRowsVMatrix::DisregardRowsVMatrix(VMat the_source)
00073     : inherited(),
00074       _disregard_missings(true),
00075       _maximum_length(-1)
00076 {
00077     PLASSERT( the_source );
00078     source = the_source;
00079     build();
00080 }
00081 
00082 
00084 // declareOptions //
00086 void
00087 DisregardRowsVMatrix::
00088 declareOptions(OptionList& ol)
00089 {
00090     declareOption(
00091         ol, "inspected_fieldnames", &DisregardRowsVMatrix::_inspected_fieldnames,
00092         OptionBase::buildoption,
00093         "Field names of the source vmat for which a triggering value (see the\n"
00094         "disregard_values option) cause this vmat to neglect a row.\n"
00095         "\n"
00096         "If empty, all source's fieldnames are used.\n"
00097         "\n"
00098         "Default: []." );
00099 
00100     declareOption(
00101         ol, "disregard_missings", &DisregardRowsVMatrix::_disregard_missings,
00102         OptionBase::buildoption,
00103         "Should missing values cause a row to be neglected.\n"
00104         "\n"
00105         "Default: 1 (True)" );
00106 
00107     declareOption(
00108         ol, "disregard_values", &DisregardRowsVMatrix::_disregard_values,
00109         OptionBase::buildoption,
00110         "If any of these values is encountered in any column designated in\n"
00111         "inspected_fieldnames, the whole row is disregarded.\n"
00112         "\n"
00113         "Default: [ ]" );
00114 
00115     declareOption(
00116         ol, "maximum_length", &DisregardRowsVMatrix::_maximum_length,
00117         OptionBase::buildoption,
00118         "If positive, only the last 'maximum_length' rows kept from the source\n"
00119         "vmat will be considered, all other rows being disregarded.\n"
00120         "\n"
00121         "Default: -1. " );
00122 
00123     // Now call the parent class' declareOptions
00124     inherited::declareOptions(ol);
00125 }
00126 
00128 // build //
00130 void
00131 DisregardRowsVMatrix::
00132 build()
00133 {
00134     inherited::build();
00135     build_();
00136 }
00137 
00139 // build_ //
00141 void
00142 DisregardRowsVMatrix::
00143 build_()
00144 {
00145     if ( !source )
00146         return;
00147 
00148     updateMtime(source);
00149     /* Option: inspected_fieldnames */
00150 
00151     // Default: All fields are inspected.
00152     if ( _inspected_fieldnames.isEmpty() )
00153     {
00154         _inspected_columns.resize( width() );
00155         for ( int c=0; c < width(); c++ )
00156             _inspected_columns[c] = c;
00157     }
00158 
00159     // Get the column indices of the fields to inspect.
00160     else
00161     {
00162         _inspected_columns.resize( _inspected_fieldnames.length() );
00163         for ( int f=0; f < _inspected_fieldnames.length(); f++ )
00164             _inspected_columns[f] =
00165                 source->fieldIndex( _inspected_fieldnames[f] );
00166     }
00167 
00168     inferIndices();
00169 
00170     DBG_MODULE_LOG
00171         << "Out of " << source.length() << " rows, "
00172         << "kept " << indices.length() << " rows"
00173         << endl;
00174 
00175     // Calls back the inherited build now that the row indices are known
00176     inherited::build();
00177 }
00178 
00179 void
00180 DisregardRowsVMatrix::
00181 inferIndices( )
00182 {
00183     if ( !source )
00184         return;
00185 
00186     indices.resize( 0, source.length() );
00187     for ( int r=0; r < source.length(); r++ )
00188     {
00189         bool disregard_row = false;
00190         for ( int inspected=0;
00191               inspected < _inspected_columns.length() && !disregard_row;
00192               ++inspected )
00193         {
00194             int c = _inspected_columns[ inspected ];
00195             if( (_disregard_missings && is_missing( source(r,c) ))
00196                 || _disregard_values.contains( source(r,c) ) )
00197                 disregard_row = true;
00198         }
00199         if ( !disregard_row )
00200             indices.append( r );
00201     }
00202 
00203     if ( _maximum_length > 0 && indices.length() > _maximum_length )
00204         indices =
00205             indices.subVec( indices.length()-_maximum_length, _maximum_length );
00206 }
00207 
00209 // makeDeepCopyFromShallowCopy //
00211 void
00212 DisregardRowsVMatrix::
00213 makeDeepCopyFromShallowCopy(CopiesMap& copies)
00214 {
00215     inherited::makeDeepCopyFromShallowCopy(copies);
00216 
00217     deepCopyField( _inspected_columns,    copies );
00218 
00219     deepCopyField( _inspected_fieldnames, copies );
00220     deepCopyField( _disregard_values,    copies );
00221 }
00222 
00223 } // end of namespace PLearn
00224 
00225 
00226 /*
00227   Local Variables:
00228   mode:c++
00229   c-basic-offset:4
00230   c-file-style:"stroustrup"
00231   c-file-offsets:((innamespace . 0)(inline-open . 0))
00232   indent-tabs-mode:nil
00233   fill-column:79
00234   End:
00235 */
00236 // 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