PLearn 0.1
|
VMat class that sees a matrix as a collection of triplets (row, column, value) Thus it is a N x 3 matrix, with N = the number of elements in the original matrix. More...
#include <IndexedVMatrix.h>
Public Member Functions | |
IndexedVMatrix (bool call_build_=false) | |
IndexedVMatrix (VMat the_source, bool the_fully_check_mappings=false, bool call_build_=false) | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual IndexedVMatrix * | deepCopy (CopiesMap &copies) const |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
virtual void | build () |
Simply calls inherited::build() then build_(). | |
virtual void | getNewRow (int i, const Vec &v) const |
Must be implemented in subclasses: default version returns an error. | |
virtual void | put (int i, int j, real value) |
This method must be implemented in all subclasses of writable matrices. | |
Static Public Member Functions | |
static string | _classname_ () |
Declares name and deepCopy methods. | |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Public Attributes | |
bool | fully_check_mappings |
Public build options. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Attributes | |
bool | need_fix_mappings |
Will be set to 'true' iff two columns of source have not the same mapping for a given string. | |
TVec< map< real, real > > | fixed_mappings |
This is a vector with as many elements as columns in source. | |
Private Types | |
typedef SourceVMatrix | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. | |
void | ensureMappingsConsistency () |
Build the string <-> real mappings so that they are consistent with the different mappings from the concatenated VMats (the same string must be mapped to the same value). | |
void | fullyCheckMappings () |
Browse through all data in the VMats to make sure there is no numerical value conflicting with a string mapping. |
VMat class that sees a matrix as a collection of triplets (row, column, value) Thus it is a N x 3 matrix, with N = the number of elements in the original matrix.
Definition at line 53 of file IndexedVMatrix.h.
typedef SourceVMatrix PLearn::IndexedVMatrix::inherited [private] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 55 of file IndexedVMatrix.h.
PLearn::IndexedVMatrix::IndexedVMatrix | ( | bool | call_build_ = false | ) |
Definition at line 51 of file IndexedVMatrix.cc.
References build_().
: inherited(call_build_), need_fix_mappings(false) { if( call_build_ ) build_(); }
PLearn::IndexedVMatrix::IndexedVMatrix | ( | VMat | the_source, |
bool | the_fully_check_mappings = false , |
||
bool | call_build_ = false |
||
) |
Definition at line 59 of file IndexedVMatrix.cc.
References build_().
: inherited(the_source, call_build_), need_fix_mappings(false), fully_check_mappings(the_fully_check_mappings) { if( call_build_ ) build_(); }
string PLearn::IndexedVMatrix::_classname_ | ( | ) | [static] |
Declares name and deepCopy methods.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
OptionList & PLearn::IndexedVMatrix::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
RemoteMethodMap & PLearn::IndexedVMatrix::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
Object * PLearn::IndexedVMatrix::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
StaticInitializer IndexedVMatrix::_static_initializer_ & PLearn::IndexedVMatrix::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
void PLearn::IndexedVMatrix::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::SourceVMatrix.
Definition at line 94 of file IndexedVMatrix.cc.
References PLearn::SourceVMatrix::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::IndexedVMatrix::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 103 of file IndexedVMatrix.cc.
References PLearn::VMatrix::declareFieldNames(), ensureMappingsConsistency(), fully_check_mappings, fullyCheckMappings(), PLearn::VMat::length(), PLearn::VMatrix::length_, need_fix_mappings, PLWARNING, PLearn::SourceVMatrix::setMetaInfoFromSource(), PLearn::SourceVMatrix::source, PLearn::VMat::width(), and PLearn::VMatrix::width_.
Referenced by build(), and IndexedVMatrix().
{ width_ = 3; length_ = source->length() * source->width(); ensureMappingsConsistency(); if( fully_check_mappings ) fullyCheckMappings(); if( need_fix_mappings && !fully_check_mappings ) PLWARNING( "In IndexedVMatrix::build_ - Mappings need to be fixed,\n" "but you did not set 'fully_check_mappings' to true,\n" "this might be dangerous.\n" ); TVec<string> fieldnames(3); fieldnames[0] = "row"; fieldnames[1] = "column"; fieldnames[2] = "value"; declareFieldNames( fieldnames ); setMetaInfoFromSource(); }
string PLearn::IndexedVMatrix::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
void PLearn::IndexedVMatrix::declareOptions | ( | OptionList & | ol | ) | [static] |
Declares this class' options.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 70 of file IndexedVMatrix.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::SourceVMatrix::declareOptions(), fully_check_mappings, PLearn::OptionBase::learntoption, PLearn::OptionBase::nosave, and PLearn::SourceVMatrix::source.
{ declareOption(ol, "m", &IndexedVMatrix::source, (OptionBase::learntoption | OptionBase::nosave), "DEPRECATED - use 'source' instead."); declareOption(ol, "fully_check_mappings", &IndexedVMatrix::fully_check_mappings, OptionBase::buildoption, "If set to 1, then columns for which there is a" " string <-> real mapping\n" "will be examined, to ensure that no numerical data in a\n" "column conflicts with a mapping in another column.\n"); inherited::declareOptions(ol); }
static const PPath& PLearn::IndexedVMatrix::declaringFile | ( | ) | [inline, static] |
IndexedVMatrix * PLearn::IndexedVMatrix::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
void PLearn::IndexedVMatrix::ensureMappingsConsistency | ( | ) | [private] |
Build the string <-> real mappings so that they are consistent with the different mappings from the concatenated VMats (the same string must be mapped to the same value).
Definition at line 197 of file IndexedVMatrix.cc.
References PLearn::VMatrix::addStringMapping(), PLearn::fast_exact_is_equal(), fixed_mappings, PLearn::VMatrix::getValString(), i, PLearn::VMatrix::map_sr, PLearn::max(), need_fix_mappings, PLearn::TVec< T >::resize(), PLearn::VMatrix::setStringMapping(), PLearn::SourceVMatrix::source, and PLearn::VMat::width().
Referenced by build_().
{ // Make sure the string mappings are consistent. // For this, we start from the mapping of the first column, and add // missing mappings obtained from the other columns. If another // column has a different mapping for the same string, we remember // it in order to fix the output when a getxxxx() method is called. need_fix_mappings = false; setStringMapping(2, source->getStringToRealMapping(0)); map<string, real>* first_map = &map_sr[2]; map<string, real> other_map; map<string, real>::iterator it, find_map; // Find the maximum mapping value for first column. real max = -REAL_MAX; for( it = first_map->begin() ; it != first_map->end() ; it++ ) if( it->second > max ) max = it->second; for( int i = 1 ; i < source->width() ; i++ ) { other_map = source->getStringToRealMapping(i); for( it = other_map.begin() ; it != other_map.end() ; it++ ) { find_map = first_map->find( it->first ); if( find_map != first_map->end() ) { // The string mapped in column 'i' is also mapped in our first // mapping. if( !fast_exact_is_equal(find_map->second, it->second) ) { // But the same string is not mapped to the same value. // This needs to be fixed. need_fix_mappings = true; fixed_mappings.resize( source->width() ); fixed_mappings[i][it->second] = find_map->second; } } else { // The string mapped in VMat 'i' is not mapped in our // current mapping. We need to add this mapping. // But we must make sure there is no existing mapping to // the same real number. real new_map_val = it->second; if( getValString(2, it->second) != "" ) { // There is already a mapping to this real number. need_fix_mappings = true; fixed_mappings.resize( source->width() ); // We pick for the number the maximum of the current mapped // numbers, +1. max++; fixed_mappings[i][it->second] = max; new_map_val = max; } else { // There is no mapping to this real number, it is thus ok // to add it. if( new_map_val > max ) max = new_map_val; } addStringMapping(2, it->first, new_map_val); } } } }
void PLearn::IndexedVMatrix::fullyCheckMappings | ( | ) | [private] |
Browse through all data in the VMats to make sure there is no numerical value conflicting with a string mapping.
An error occurs if it is the case.
Definition at line 269 of file IndexedVMatrix.cc.
References PLearn::TVec< T >::end(), PLearn::VMat::fieldName(), PLearn::VMatrix::find(), i, PLearn::is_missing(), j, PLearn::VMat::length(), PLearn::VMatrix::map_rs, PLearn::VMatrix::map_sr, PLERROR, PLearn::SourceVMatrix::source, and PLearn::VMat::width().
Referenced by build_().
{ if( map_sr[2].size() == 0 ) return; Vec row( source->width() ); for( int i = 0 ; i < source->length() ; i++ ) { source->getRow(i, row); for( int j = 0 ; j < source->width() ; j++ ) { if( !is_missing(row[j]) ) { // Note that if the value is missing, we should not // look for a mapping from this value, because it would // find it even if it does not exist (see the STL map // documentation to understand why this happens). if( source->getValString(j, row[j]) == "" ) { // It is a numerical value for the source's j-th column if( map_rs[2].find(row[j]) != map_rs[2].end() ) { // And, unfortunately, we have used this // numerical value in the column (2) string // mapping. It could be fixed, but this would be // pretty annoying, thus we just raise an error. PLERROR("In IndexedVMatrix::fullyCheckMappings - In" " column %d (%s) of source, the row %d\n" "contains a numerical value (%f) that is used" " in a string mapping (mapped to %s).\n", j, source->fieldName(j).c_str(), i, row[j], map_rs[2][ row[j] ].c_str() ); } } } } } }
Must be implemented in subclasses: default version returns an error.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 130 of file IndexedVMatrix.cc.
References PLearn::TVec< T >::find(), fixed_mappings, PLearn::is_missing(), PLearn::TVec< T >::length(), PLearn::VMatrix::length(), need_fix_mappings, PLERROR, PLearn::SourceVMatrix::source, w, PLearn::VMat::width(), and PLearn::VMatrix::width().
{ #ifdef BOUNDCHECK if(i<0 || i>=length()) PLERROR("In IndexedVMatrix::getNewRow OUT OF BOUNDS"); if(v.length() != width()) PLERROR("In IndexedVMatrix::getNewRow v.length() must be equal to 3"); #endif int w = source->width(); int i_ = i / w; // the value of the first column at row i int j_ = i % w; // the value of the second column at row i real val = source->get(i_,j_); if( need_fix_mappings && fixed_mappings[j_].size()>0 && !is_missing(val) ) { map<real, real>::iterator it = fixed_mappings[j_].find(val); if( it != fixed_mappings[j_].end() ) { // We need to modify this value. val = it->second; } } v[0] = i_; v[1] = j_; v[2] = val; }
OptionList & PLearn::IndexedVMatrix::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
OptionMap & PLearn::IndexedVMatrix::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
RemoteMethodMap & PLearn::IndexedVMatrix::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 48 of file IndexedVMatrix.cc.
void PLearn::IndexedVMatrix::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 87 of file IndexedVMatrix.cc.
References PLearn::deepCopyField(), fixed_mappings, and PLearn::SourceVMatrix::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(fixed_mappings, copies); }
This method must be implemented in all subclasses of writable matrices.
Sets element (i,j) to value.
Reimplemented from PLearn::VMatrix.
Definition at line 184 of file IndexedVMatrix.cc.
References PLERROR, PLearn::SourceVMatrix::source, w, and PLearn::VMat::width().
{ if (j != 2) { PLERROR("In IndexedVMatrix::put You can only modify the third column\n"); } int w = source->width(); int i_ = i / w; // the value of the first column at row i int j_ = i % w; // the value of the second column at row i source->put(i_, j_, value); }
Reimplemented from PLearn::SourceVMatrix.
Definition at line 81 of file IndexedVMatrix.h.
TVec< map<real, real> > PLearn::IndexedVMatrix::fixed_mappings [protected] |
This is a vector with as many elements as columns in source.
The element (i) is a mapping that says which value needs to be replaced with what in the i-th column of the source matrix. This is to fix the mappings when 'need_fix_mappings' is true.
Definition at line 67 of file IndexedVMatrix.h.
Referenced by ensureMappingsConsistency(), getNewRow(), and makeDeepCopyFromShallowCopy().
Public build options.
Definition at line 73 of file IndexedVMatrix.h.
Referenced by build_(), and declareOptions().
bool PLearn::IndexedVMatrix::need_fix_mappings [protected] |
Will be set to 'true' iff two columns of source have not the same mapping for a given string.
This means the output must systematically be checked to ensure consistency.
Definition at line 61 of file IndexedVMatrix.h.
Referenced by build_(), ensureMappingsConsistency(), and getNewRow().