PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::SelectRowsVMatrix Class Reference

selects samples from a source matrix according to given vector of indices More...

#include <SelectRowsVMatrix.h>

Inheritance diagram for PLearn::SelectRowsVMatrix:
Inheritance graph
[legend]
Collaboration diagram for PLearn::SelectRowsVMatrix:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SelectRowsVMatrix ()
 SelectRowsVMatrix (VMat the_source, TVec< int > the_indices, bool the_rows_to_remove=false, bool warn=true)
 Also copies the original fieldinfos upon construction Here the indices will be shared for efficiency.
 SelectRowsVMatrix (VMat the_source, Vec the_indices, bool the_rows_to_remove=false, bool warn=true)
 Here the indices will be copied locally into an integer vector.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual SelectRowsVMatrixdeepCopy (CopiesMap &copies) const
virtual void build ()
 Simply calls inherited::build() then build_().
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.
virtual real get (int i, int j) const
 These methods are implemented by buffering calls to getNewRow.
virtual void getSubRow (int i, int j, Vec v) const
 fills v with the subrow i laying between columns j (inclusive) and j+v.length() (exclusive)
virtual real getStringVal (int col, const string &str) const
 Returns value associated with a string (or MISSING_VALUE if there's no association for this string).
virtual string getValString (int col, real val) const
 Returns the string associated with value val for field# col.
virtual string getString (int row, int col) const
 Returns element as a string, even if value doesn't map to a string, in which case tostring(value) is returned.
virtual const map< string, real > & getStringToRealMapping (int col) const
 returns the whole string->value mapping
virtual const map< real, string > & getRealToStringMapping (int col) const
 Returns the real->string mapping for column 'col'.
virtual void reset_dimensions ()
 In case the dimensions of an underlying VMat has changed, recompute it.
virtual real dot (int i1, int i2, int inputsize) const
 Returns the dot product between row i1 and row i2 (considering only the inputsize first elements).
virtual real dot (int i, const Vec &v) const
 Returns the result of the dot product between row i and the given vec (only v.length() first elements of row i are considered).
virtual PP< DictionarygetDictionary (int col) const
 Return the Dictionary object for a certain field, or a null pointer if there isn't one.
virtual void getValues (int row, int col, Vec &values) const
 Returns the possible values for a certain field in the VMatrix.
virtual void getValues (const Vec &input, int col, Vec &values) const
 Returns the possible values of a certain field (column) given the input.

Static Public Member Functions

static string _classname_ ()
 SelectRowsVMatrix.
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static Object_new_instance_for_typemap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()
static void declareOptions (OptionList &ol)
 Declares this class' options.

Public Attributes

TVec< intindices
 Public build options.
bool rows_to_remove
 Indication that the rows specified in indices or indices_vmat should be removed, not selected from the source VMatrix.
VMat indices_vmat

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

void getNewRow (int i, const Vec &v) const
 Must be implemented in subclasses: default version returns an error.

Protected Attributes

bool obtained_inputsize_from_source
 Protected learnt options.
bool obtained_targetsize_from_source
bool obtained_weightsize_from_source
bool obtained_extrasize_from_source
bool warn_if_all_rows_selected
TVec< intselected_indices

Private Types

typedef SourceVMatrix inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

selects samples from a source matrix according to given vector of indices

Definition at line 55 of file SelectRowsVMatrix.h.


Member Typedef Documentation


Constructor & Destructor Documentation

PLearn::SelectRowsVMatrix::SelectRowsVMatrix ( )
PLearn::SelectRowsVMatrix::SelectRowsVMatrix ( VMat  the_source,
TVec< int the_indices,
bool  the_rows_to_remove = false,
bool  warn = true 
)

Also copies the original fieldinfos upon construction Here the indices will be shared for efficiency.

But you should not modify them afterwards!

Definition at line 63 of file SelectRowsVMatrix.cc.

References build_(), and PLearn::SourceVMatrix::source.

Here is the call graph for this function:

PLearn::SelectRowsVMatrix::SelectRowsVMatrix ( VMat  the_source,
Vec  the_indices,
bool  the_rows_to_remove = false,
bool  warn = true 
)

Here the indices will be copied locally into an integer vector.

Definition at line 77 of file SelectRowsVMatrix.cc.

References build_(), indices, PLearn::TVec< T >::length(), PLearn::TVec< T >::resize(), and PLearn::SourceVMatrix::source.

    : obtained_inputsize_from_source(false),
      obtained_targetsize_from_source(false),
      obtained_weightsize_from_source(false),
      obtained_extrasize_from_source(false),
      warn_if_all_rows_selected(warn),
      rows_to_remove(the_rows_to_remove)
{
    source = the_source;
    indices.resize(the_indices.length());
    indices << the_indices; // copy to integer indices
    build_();
}

Here is the call graph for this function:


Member Function Documentation

string PLearn::SelectRowsVMatrix::_classname_ ( ) [static]
OptionList & PLearn::SelectRowsVMatrix::_getOptionList_ ( ) [static]
RemoteMethodMap & PLearn::SelectRowsVMatrix::_getRemoteMethodMap_ ( ) [static]
bool PLearn::SelectRowsVMatrix::_isa_ ( const Object o) [static]
Object * PLearn::SelectRowsVMatrix::_new_instance_for_typemap_ ( ) [static]
StaticInitializer SelectRowsVMatrix::_static_initializer_ & PLearn::SelectRowsVMatrix::_static_initialize_ ( ) [static]
void PLearn::SelectRowsVMatrix::build ( ) [virtual]
void PLearn::SelectRowsVMatrix::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::SourceVMatrix.

Reimplemented in PLearn::BootstrapVMatrix, PLearn::ClassSubsetVMatrix, PLearn::DisregardRowsVMatrix, PLearn::MultiToUniInstanceSelectRandomVMatrix, PLearn::RemoveDuplicateVMatrix, PLearn::ReorderByMissingVMatrix, PLearn::ReplicateSamplesVMatrix, PLearn::SortRowsVMatrix, and PLearn::ValueSelectRowsVMatrix.

Definition at line 182 of file SelectRowsVMatrix.cc.

References PLearn::VMatrix::extrasize_, PLearn::VMatrix::fieldinfos, PLearn::TVec< T >::fill(), i, indices, indices_vmat, PLearn::VMatrix::inputsize_, PLearn::TVec< T >::length(), PLearn::VMat::length(), PLearn::VMatrix::length_, n, obtained_extrasize_from_source, obtained_inputsize_from_source, obtained_targetsize_from_source, obtained_weightsize_from_source, PLERROR, PLWARNING, PLearn::TVec< T >::push_back(), PLearn::TVec< T >::resize(), rows_to_remove, selected_indices, PLearn::SourceVMatrix::source, PLearn::VMatrix::targetsize_, warn_if_all_rows_selected, PLearn::VMatrix::weightsize_, PLearn::VMat::width(), and PLearn::VMatrix::width_.

Referenced by build(), and SelectRowsVMatrix().

{
    if (indices_vmat) {
        int n = indices_vmat->length();
        indices.resize(n);
        for (int i = 0; i < n; i++)
            indices[i] = int(round(indices_vmat->get(i,0)));
    }

    if(rows_to_remove)
    {
        TVec<bool> tag(source->length());
        tag.fill(true);
        int count_to_remove = 0;
        for(int i=0; i<indices.length(); i++) {
            tag[indices[i]] = false;
            count_to_remove++;
        }
        // Allocate enough memory.
        selected_indices.resize(source->length() - count_to_remove);
        selected_indices.resize(0);
        for(int i=0; i<source->length(); i++)
            if(tag[i]) selected_indices.push_back(i);
        
    }
    else
    {
        selected_indices.resize(indices.length());
        selected_indices << indices;
    }
    //we don't display the warning for SortRowsVMatrix as it always select all row!
    if(warn_if_all_rows_selected && selected_indices.length()==source.length()
            && source.length()>0)
        PLWARNING("In SelectRowsVMatrix::build_() - "
                  "All rows have been selected!");

    length_ = selected_indices.length();
    if (source) {
        string error_msg =
            "In SelectRowsVMatrix::build_ - For safety reasons, it is forbidden to "
            "re-use sizes obtained from a previous source VMatrix with a new source "
            "VMatrix having different sizes";
        width_ = source->width();
        if(inputsize_<0) {
            inputsize_ = source->inputsize();
            obtained_inputsize_from_source = true;
        } else if (obtained_inputsize_from_source && inputsize_ != source->inputsize())
            PLERROR(error_msg.c_str());
        if(targetsize_<0) {
            targetsize_ = source->targetsize();
            obtained_targetsize_from_source = true;
        } else if (obtained_targetsize_from_source && targetsize_ != source->targetsize())
            PLERROR(error_msg.c_str());
        if(weightsize_<0) {
            weightsize_ = source->weightsize();
            obtained_weightsize_from_source = true;
        } else if (obtained_weightsize_from_source && weightsize_ != source->weightsize())
            PLERROR(error_msg.c_str());
        if(extrasize_ == 0) {
            extrasize_ = source->extrasize();
            obtained_extrasize_from_source = true;
        } else if (obtained_extrasize_from_source && extrasize_ != source->extrasize())
            PLERROR(error_msg.c_str());
        fieldinfos = source->fieldinfos;
    } else {
        // Restore the original undefined sizes if the current one had been obtained
        // from the source VMatrix.
        if (obtained_inputsize_from_source) {
            inputsize_ = -1;
            obtained_inputsize_from_source = false;
        }
        if (obtained_targetsize_from_source) {
            targetsize_ = -1;
            obtained_targetsize_from_source = false;
        }
        if (obtained_weightsize_from_source) {
            weightsize_ = -1;
            obtained_weightsize_from_source = false;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

string PLearn::SelectRowsVMatrix::classname ( ) const [virtual]
void PLearn::SelectRowsVMatrix::declareOptions ( OptionList ol) [static]

Declares this class' options.

Reimplemented from PLearn::SourceVMatrix.

Reimplemented in PLearn::BootstrapVMatrix, PLearn::ClassSubsetVMatrix, PLearn::DisregardRowsVMatrix, PLearn::MultiToUniInstanceSelectRandomVMatrix, PLearn::RemoveDuplicateVMatrix, PLearn::ReorderByMissingVMatrix, PLearn::ReplicateSamplesVMatrix, PLearn::SortRowsVMatrix, and PLearn::ValueSelectRowsVMatrix.

Definition at line 112 of file SelectRowsVMatrix.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::SourceVMatrix::declareOptions(), indices, indices_vmat, PLearn::OptionBase::learntoption, PLearn::VMatrix::length_, PLearn::OptionBase::nosave, obtained_extrasize_from_source, obtained_inputsize_from_source, obtained_targetsize_from_source, obtained_weightsize_from_source, PLearn::redeclareOption(), rows_to_remove, warn_if_all_rows_selected, PLearn::VMatrix::width_, and PLearn::VMatrix::writable.

Referenced by PLearn::SortRowsVMatrix::declareOptions(), PLearn::ReplicateSamplesVMatrix::declareOptions(), PLearn::ReorderByMissingVMatrix::declareOptions(), PLearn::RemoveDuplicateVMatrix::declareOptions(), PLearn::MultiToUniInstanceSelectRandomVMatrix::declareOptions(), PLearn::DisregardRowsVMatrix::declareOptions(), PLearn::ClassSubsetVMatrix::declareOptions(), and PLearn::BootstrapVMatrix::declareOptions().

{
    // Build options.

    declareOption(ol, "indices", &SelectRowsVMatrix::indices, OptionBase::buildoption,
                  "The array of row indices to extract");

    declareOption(ol, "indices_vmat", &SelectRowsVMatrix::indices_vmat, OptionBase::buildoption,
                  "If provided, will override the 'indices' option: the indices will be taken\n"
                  "from the first column of the given VMatrix (taking the closest integer).");

    declareOption(ol, "rows_to_remove", &SelectRowsVMatrix::rows_to_remove, OptionBase::buildoption,
                  "Indication that the rows specified in indices or indices_vmat\n"
                  "should be removed, not selected from the source VMatrix.");

    // Learnt options.

    declareOption(ol, "obtained_inputsize_from_source", &SelectRowsVMatrix::obtained_inputsize_from_source, OptionBase::learntoption,
                  "Set to 1 if the inputsize was obtained from the source VMat.");

    declareOption(ol, "obtained_targetsize_from_source", &SelectRowsVMatrix::obtained_targetsize_from_source, OptionBase::learntoption,
                  "Set to 1 if the targetsize was obtained from the source VMat.");

    declareOption(ol, "obtained_weightsize_from_source", &SelectRowsVMatrix::obtained_weightsize_from_source, OptionBase::learntoption,
                  "Set to 1 if the weightsize was obtained from the source VMat.");

    declareOption(ol, "obtained_extrasize_from_source", &SelectRowsVMatrix::obtained_extrasize_from_source, OptionBase::learntoption,
                  "Set to 1 if the extrasize was obtained from the source VMat.");

    declareOption(ol, "warn_if_all_rows_selected", &SelectRowsVMatrix::warn_if_all_rows_selected, OptionBase::buildoption,
                  "If true, we generate a warning if we select all row.");

    inherited::declareOptions(ol);

    // Hide unused options.
    // Note: it is not obvious that all the options below are useless, one may
    // want to un-hide some in the future. However, the more hidden, the
    // simpler for the beginner.

    redeclareOption(ol, "writable", &SelectRowsVMatrix::writable,
                    OptionBase::nosave, "Not used.");

    redeclareOption(ol, "length", &SelectRowsVMatrix::length_,
                    OptionBase::nosave, "Not used.");

    redeclareOption(ol, "width", &SelectRowsVMatrix::width_,
                    OptionBase::nosave, "Not used.");

}

Here is the call graph for this function:

Here is the caller graph for this function:

static const PPath& PLearn::SelectRowsVMatrix::declaringFile ( ) [inline, static]
SelectRowsVMatrix * PLearn::SelectRowsVMatrix::deepCopy ( CopiesMap copies) const [virtual]
real PLearn::SelectRowsVMatrix::dot ( int  i,
const Vec v 
) const [virtual]

Returns the result of the dot product between row i and the given vec (only v.length() first elements of row i are considered).

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 100 of file SelectRowsVMatrix.cc.

References selected_indices, and PLearn::SourceVMatrix::source.

{ return source->dot(selected_indices[i],v); }
real PLearn::SelectRowsVMatrix::dot ( int  i1,
int  i2,
int  inputsize 
) const [virtual]

Returns the dot product between row i1 and row i2 (considering only the inputsize first elements).

The default version in VMatrix is somewhat inefficient, as it repeatedly calls get(i,j) The default version in RowBufferedVMatrix is a little better as it buffers the 2 Vecs between calls in case one of them is needed again. But the real strength of this method is for specialised and efficient versions in subbclasses. This method is typically used by SmartKernels so that they can compute kernel values between input samples efficiently.

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 97 of file SelectRowsVMatrix.cc.

References selected_indices, and PLearn::SourceVMatrix::source.

{ return source->dot(int(selected_indices[i1]), int(selected_indices[i2]), inputsize); }
real PLearn::SelectRowsVMatrix::get ( int  i,
int  j 
) const [virtual]

These methods are implemented by buffering calls to getNewRow.

returns element (i,j)

Reimplemented from PLearn::RowBufferedVMatrix.

Reimplemented in PLearn::ClassSubsetVMatrix.

Definition at line 91 of file SelectRowsVMatrix.cc.

References selected_indices, and PLearn::SourceVMatrix::source.

{ return source->get(selected_indices[i], j); }
PP< Dictionary > PLearn::SelectRowsVMatrix::getDictionary ( int  col) const [virtual]

Return the Dictionary object for a certain field, or a null pointer if there isn't one.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 270 of file SelectRowsVMatrix.cc.

References PLERROR, PLearn::SourceVMatrix::source, and PLearn::VMatrix::width_.

{
#ifdef BOUNDCHECK
    if(col>=width_)
        PLERROR("access out of bound. Width=%i accessed col=%i",width_,col);
#endif
    return source->getDictionary(col);
}
void PLearn::SelectRowsVMatrix::getNewRow ( int  i,
const Vec v 
) const [inline, protected, virtual]

Must be implemented in subclasses: default version returns an error.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 107 of file SelectRowsVMatrix.h.

{ getSubRow(i, 0, v); }
OptionList & PLearn::SelectRowsVMatrix::getOptionList ( ) const [virtual]
OptionMap & PLearn::SelectRowsVMatrix::getOptionMap ( ) const [virtual]
const map< real, string > & PLearn::SelectRowsVMatrix::getRealToStringMapping ( int  col) const [virtual]

Returns the real->string mapping for column 'col'.

Reimplemented from PLearn::VMatrix.

Definition at line 109 of file SelectRowsVMatrix.cc.

References PLearn::SourceVMatrix::source.

{ return source->getRealToStringMapping(col);}
RemoteMethodMap & PLearn::SelectRowsVMatrix::getRemoteMethodMap ( ) const [virtual]
string PLearn::SelectRowsVMatrix::getString ( int  row,
int  col 
) const [virtual]

Returns element as a string, even if value doesn't map to a string, in which case tostring(value) is returned.

Reimplemented from PLearn::VMatrix.

Definition at line 103 of file SelectRowsVMatrix.cc.

References selected_indices, and PLearn::SourceVMatrix::source.

{ return source->getString(selected_indices[row], col); }
const map< string, real > & PLearn::SelectRowsVMatrix::getStringToRealMapping ( int  col) const [virtual]

returns the whole string->value mapping

Reimplemented from PLearn::VMatrix.

Definition at line 106 of file SelectRowsVMatrix.cc.

References PLearn::SourceVMatrix::source.

{ return source->getStringToRealMapping(col);}
real PLearn::SelectRowsVMatrix::getStringVal ( int  col,
const string &  str 
) const [virtual]

Returns value associated with a string (or MISSING_VALUE if there's no association for this string).

Reimplemented from PLearn::VMatrix.

Definition at line 264 of file SelectRowsVMatrix.cc.

References PLearn::SourceVMatrix::source.

{ return source->getStringVal(col, str); }
void PLearn::SelectRowsVMatrix::getSubRow ( int  i,
int  j,
Vec  v 
) const [virtual]

fills v with the subrow i laying between columns j (inclusive) and j+v.length() (exclusive)

Reimplemented from PLearn::RowBufferedVMatrix.

Reimplemented in PLearn::ClassSubsetVMatrix.

Definition at line 94 of file SelectRowsVMatrix.cc.

References PLearn::VMat::getSubRow(), selected_indices, and PLearn::SourceVMatrix::source.

{ source->getSubRow(selected_indices[i], j, v); }

Here is the call graph for this function:

string PLearn::SelectRowsVMatrix::getValString ( int  col,
real  val 
) const [virtual]

Returns the string associated with value val for field# col.

Or returns "" if no string is associated.

Reimplemented from PLearn::VMatrix.

Definition at line 267 of file SelectRowsVMatrix.cc.

References PLearn::SourceVMatrix::source.

{ return source->getValString(col,val); }
void PLearn::SelectRowsVMatrix::getValues ( int  row,
int  col,
Vec values 
) const [virtual]

Returns the possible values for a certain field in the VMatrix.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 280 of file SelectRowsVMatrix.cc.

References PLERROR, selected_indices, PLearn::SourceVMatrix::source, and PLearn::VMatrix::width_.

{
#ifdef BOUNDCHECK
    if(col>=width_)
        PLERROR("access out of bound. Width=%i accessed col=%i",width_,col);
#endif
    source->getValues(selected_indices[row],col,values);
}
void PLearn::SelectRowsVMatrix::getValues ( const Vec input,
int  col,
Vec values 
) const [virtual]

Returns the possible values of a certain field (column) given the input.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 289 of file SelectRowsVMatrix.cc.

References PLERROR, PLearn::SourceVMatrix::source, and PLearn::VMatrix::width_.

{
#ifdef BOUNDCHECK
    if(col>=width_)
        PLERROR("access out of bound. Width=%i accessed col=%i",width_,col);
#endif
    source->getValues(input, col, values);
}
void PLearn::SelectRowsVMatrix::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]
virtual void PLearn::SelectRowsVMatrix::reset_dimensions ( ) [inline, virtual]

In case the dimensions of an underlying VMat has changed, recompute it.

Reimplemented from PLearn::VMatrix.

Definition at line 120 of file SelectRowsVMatrix.h.

{ source->reset_dimensions(); width_=source->width(); }

Member Data Documentation

Definition at line 68 of file SelectRowsVMatrix.h.

Referenced by build_(), and declareOptions().

Protected learnt options.

Definition at line 65 of file SelectRowsVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 66 of file SelectRowsVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 67 of file SelectRowsVMatrix.h.

Referenced by build_(), and declareOptions().

Indication that the rows specified in indices or indices_vmat should be removed, not selected from the source VMatrix.

Definition at line 81 of file SelectRowsVMatrix.h.

Referenced by build_(), PLearn::BootstrapVMatrix::build_(), and declareOptions().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines