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

Mapping between ranges and values. More...

#include <RealMapping.h>

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

List of all members.

Public Types

typedef pair< RealRange, realsingle_mapping_t
typedef TVec< single_mapping_tordered_mapping_t
typedef std::map< RealRange, realmapping_t
typedef mapping_t::iterator iterator
typedef mapping_t::const_iterator const_iterator

Public Member Functions

virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual RealMappingdeepCopy (CopiesMap &copies) const
 RealMapping ()
int size () const
int length () const
void clear ()
 Removes all entries in mapping. Does not change other params.
void buildOrderedMapping ()
bool checkConsistency ()
void removeMapping (const RealRange &range)
void removeMapping (real x)
void addMapping (const RealRange &range, real val)
void setMappingForMissing (real what_missing_mapsto)
 Set mapping for missing value (by default it maps to MISSING_VALUE)
void setMappingForOther (real what_other_mapsto)
 Set mapping for any other value (by default it is kept as is)
void keepOtherAsIs ()
real map (real val) const
int binnumber (real val) const
void transform (const Vec &v) const
pair< RealRange, reallastMapping ()
iterator begin ()
const_iterator begin () const
iterator end ()
const_iterator end () const
void erase (iterator it)
bool operator== (const RealMapping &rm) const
virtual void newwrite (PStream &out) const
 Overridden to use the specific real mapping format in raw_ascii mode.
virtual void print (ostream &out) const
virtual void write (ostream &out) const
 Write the object to a C++ ostream.
virtual void read (PStream &in)
real maxMappedToValue ()
Vec getCutPoints () const
 If all ranges in the mapping are consecutive, return the cut points between different ranges.

Static Public Member Functions

static string _classname_ ()
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 ()

Public Attributes

mapping_t mapping
 Defines mapping from real ranges to values.
real missing_mapsto
 Value to which to map missing values (can be missing value)
bool keep_other_as_is
 If true, values not in mapping are left as is, otherwise they're mapped to other_mapsto.
real other_mapsto
 Value to which to map values not inmapping, if keep_other_as_is is false.
ordered_mapping_t o_mapping
 o_mapping contains the same mappings as 'mapping', but they are ordered so that the lower limits of ranges are in ascending order NOTE : before any access, it must be created with a call to buildOrderedMapping()

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declare options (data fields) for the class.

Private Types

typedef Object inherited

Detailed Description

Mapping between ranges and values.

RealMapping is used as a component of binning operations. It divides the real line in a set of ranges, and associates each range with a single (usually integer) value. The ranges are specified as follows:

Definition at line 136 of file RealMapping.h.


Member Typedef Documentation

typedef mapping_t::const_iterator PLearn::RealMapping::const_iterator

Definition at line 145 of file RealMapping.h.

Reimplemented from PLearn::Object.

Definition at line 138 of file RealMapping.h.

typedef mapping_t::iterator PLearn::RealMapping::iterator

Definition at line 144 of file RealMapping.h.

Definition at line 143 of file RealMapping.h.

Definition at line 142 of file RealMapping.h.

Definition at line 141 of file RealMapping.h.


Constructor & Destructor Documentation

PLearn::RealMapping::RealMapping ( ) [inline]

Member Function Documentation

string PLearn::RealMapping::_classname_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
OptionList & PLearn::RealMapping::_getOptionList_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
RemoteMethodMap & PLearn::RealMapping::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
bool PLearn::RealMapping::_isa_ ( const Object o) [static]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
Object * PLearn::RealMapping::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
StaticInitializer RealMapping::_static_initializer_ & PLearn::RealMapping::_static_initialize_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
void PLearn::RealMapping::addMapping ( const RealRange range,
real  val 
)

Definition at line 215 of file RealMapping.cc.

Referenced by PLearn::StatsCollector::getAllValuesMapping(), and PLearn::StatsCollector::getBinMapping().

{
    //PStream perr(&cerr);
    //perr << "BEFORE " << mapping << endl;
    //perr << "ADDING " << range << "->" << val << endl;
    mapping[range]= val; 
    //perr << "AFTER " << mapping << endl;
    // cout<<"adding :"<<range<<" = "<<val<<endl;
    // cout<<"MAPPING:"<<*this<<endl;
}

Here is the caller graph for this function:

iterator PLearn::RealMapping::begin ( ) [inline]

Definition at line 235 of file RealMapping.h.

    { return mapping.begin(); }
const_iterator PLearn::RealMapping::begin ( ) const [inline]

Definition at line 237 of file RealMapping.h.

    { return mapping.begin(); }
int PLearn::RealMapping::binnumber ( real  val) const

Definition at line 228 of file RealMapping.cc.

References PLearn::is_missing().

{
    if(is_missing(val))
        return -2;
    mapping_t::const_iterator it = mapping.begin();
    mapping_t::const_iterator itend = mapping.end();
    for(int num=0; it!=itend; ++it,num++)
        if(it->first.contains(val))
            return num;
    return -1;      
}

Here is the call graph for this function:

void PLearn::RealMapping::buildOrderedMapping ( )

Definition at line 193 of file RealMapping.cc.

{ 
    o_mapping.resize(0);
    for(iterator it = begin();it!=end();++it)
        o_mapping.push_back(*it);
    sort(o_mapping.begin(),o_mapping.end());
}
bool PLearn::RealMapping::checkConsistency ( )

Definition at line 392 of file RealMapping.cc.

References i, and PLearn::max().

{
    mapping_t::const_iterator it= mapping.begin();
    int max = 0;
    for(;it != mapping.end(); ++it)
    {
        if(it->second <0 || it->second - (int)it->second > 0)
            return false;
        if(max < it->second)
            max = (int)it->second;
    }
    TVec<int> v(max+1,0);
    for(it=mapping.begin();it != mapping.end(); ++it)
    {
        if(v[(int)it->second] != 0)
            return false;
        v[(int)it->second]=1;
    }
    for(int i=0;i<max;i++)
        if(v[i]==0)
            return false;
    return true;  
}

Here is the call graph for this function:

string PLearn::RealMapping::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
void PLearn::RealMapping::clear ( ) [inline]

Removes all entries in mapping. Does not change other params.

Definition at line 181 of file RealMapping.h.

{ mapping.clear(); }
void PLearn::RealMapping::declareOptions ( OptionList ol) [static, protected]

Declare options (data fields) for the class.

Redefine this in subclasses: call declareOption(...) for each option, and then call inherited::declareOptions(options). Please call the inherited method AT THE END to get the options listed in a consistent order (from most recently defined to least recently defined).

  static void MyDerivedClass::declareOptions(OptionList& ol)
  {
      declareOption(ol, "inputsize", &MyObject::inputsize_,
                    OptionBase::buildoption,
                    "The size of the input; it must be provided");
      declareOption(ol, "weights", &MyObject::weights,
                    OptionBase::learntoption,
                    "The learned model weights");
      inherited::declareOptions(ol);
  }
Parameters:
olList of options that is progressively being constructed for the current class.

Reimplemented from PLearn::Object.

Definition at line 168 of file RealMapping.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), keep_other_as_is, mapping, missing_mapsto, and other_mapsto.

{
    declareOption(
        ol, "mapping", &RealMapping::mapping, OptionBase::buildoption,
        "The mapping");

    declareOption(
        ol, "missing_mapsto", &RealMapping::missing_mapsto, OptionBase::buildoption,
        "Value to which to map missing values (can be missing value)");

    declareOption(
        ol, "keep_other_as_is", &RealMapping::keep_other_as_is,
        OptionBase::buildoption,
        "If true, values not in mapping are left as is, otherwise they're\n"
        "mapped to 'other_mapsto' (default=True)");

    declareOption(
        ol, "other_mapsto", &RealMapping::other_mapsto, OptionBase::buildoption,
        "Value to which to map values not in mapping, if 'keep_other_as_is' is\n"
        "false");
    
    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::RealMapping::declaringFile ( ) [inline, static]

Reimplemented from PLearn::Object.

Definition at line 169 of file RealMapping.h.

RealMapping * PLearn::RealMapping::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
iterator PLearn::RealMapping::end ( ) [inline]

Definition at line 239 of file RealMapping.h.

    { return mapping.end(); }
const_iterator PLearn::RealMapping::end ( ) const [inline]

Definition at line 241 of file RealMapping.h.

    { return mapping.end(); }
void PLearn::RealMapping::erase ( iterator  it) [inline]

Definition at line 243 of file RealMapping.h.

    { mapping.erase(it); }
Vec PLearn::RealMapping::getCutPoints ( ) const

If all ranges in the mapping are consecutive, return the cut points between different ranges.

e.g.: [0,1[ [1, 5[ [5, 10] ]10, 15]--> <0,1,5,10,15>.

Definition at line 381 of file RealMapping.cc.

References PLearn::TVec< T >::first(), and i.

{
    Vec v(length()+1);
    mapping_t::const_iterator it= mapping.begin();
    v[0]= it->first.low;
    for(int i= 1; it != mapping.end(); ++it, ++i)
        v[i]= it->first.high;
    return v;
}

Here is the call graph for this function:

OptionList & PLearn::RealMapping::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
OptionMap & PLearn::RealMapping::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
RemoteMethodMap & PLearn::RealMapping::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 63 of file RealMapping.cc.

{ 
void PLearn::RealMapping::keepOtherAsIs ( ) [inline]

Definition at line 215 of file RealMapping.h.

    { keep_other_as_is=true; }
pair<RealRange, real> PLearn::RealMapping::lastMapping ( ) [inline]

Definition at line 227 of file RealMapping.h.

Referenced by PLearn::StatsCollector::getBinMapping().

    { return *(mapping.rbegin()); }

Here is the caller graph for this function:

int PLearn::RealMapping::length ( ) const [inline]

Definition at line 178 of file RealMapping.h.

Referenced by PLearn::ConditionalStatsCollector::findrange().

{ return (int)mapping.size(); }

Here is the caller graph for this function:

real PLearn::RealMapping::map ( real  val) const

Definition at line 201 of file RealMapping.cc.

References PLearn::is_missing().

Referenced by PLearn::ConditionalStatsCollector::findrange().

{
    if(is_missing(val))
        return missing_mapsto;
    mapping_t::const_iterator it= mapping.lower_bound(RealRange('[',val,val,']'));
    if(it != mapping.end() && it->first.contains(val))
        return it->second;
    if(keep_other_as_is)
        return val;
    else
        return other_mapsto;      
}

Here is the call graph for this function:

Here is the caller graph for this function:

real PLearn::RealMapping::maxMappedToValue ( )

Definition at line 157 of file RealMapping.cc.

References PLearn::max().

{
    real max = -9999999;
    mapping_t::iterator it = mapping.begin();
    mapping_t::iterator itend = mapping.end();
    for(; it!=itend; ++it)
        if(it->second > max)
            max = it->second;
    return max;
}    

Here is the call graph for this function:

void PLearn::RealMapping::newwrite ( PStream out) const [virtual]

Overridden to use the specific real mapping format in raw_ascii mode.

Reimplemented from PLearn::Object.

Definition at line 255 of file RealMapping.cc.

References PLearn::endl(), PLearn::is_missing(), PLearn::PStream::outmode, and PLearn::PStream::raw_ascii.

                                             {
    switch (out.outmode) {
    case PStream::raw_ascii:
        out << "{ ";
        for(mapping_t::const_iterator it= mapping.begin();
            it != mapping.end(); ++it)
            out << it->first << " -> " << it->second << "; ";
        if(!is_missing(missing_mapsto))
            out << "MISSING -> " << missing_mapsto << "; " << endl;
        if(!keep_other_as_is)
            out << "OTHER -> " << other_mapsto;
        out << "} ";
        break;
    default:
        inherited::newwrite(out);
    }
}

Here is the call graph for this function:

bool PLearn::RealMapping::operator== ( const RealMapping rm) const

Definition at line 144 of file RealMapping.cc.

References PLearn::is_equal(), keep_other_as_is, mapping, missing_mapsto, and other_mapsto.

{
    return (rm.mapping == mapping && 
            is_equal(rm.missing_mapsto, missing_mapsto, 1, 0, 0) &&
            rm.keep_other_as_is == keep_other_as_is &&
            is_equal(rm.other_mapsto, other_mapsto, 1, 0, 0));
}

Here is the call graph for this function:

void PLearn::RealMapping::print ( ostream &  out) const [virtual]

Definition at line 275 of file RealMapping.cc.

References PLearn::endl(), and PLearn::is_missing().

{ 
    out << "{ ";
    out.precision(17);
    for(mapping_t::const_iterator it= mapping.begin(); 
        it != mapping.end(); ++it)
        out << it->first << " -> " << it->second << "; ";
    if(!is_missing(missing_mapsto))
        out << "MISSING -> " << missing_mapsto << "; " << endl;
    if(!keep_other_as_is)
        out << "OTHER -> " << other_mapsto;
    out << "} ";
}

Here is the call graph for this function:

void PLearn::RealMapping::read ( PStream in) [virtual]

Definition at line 296 of file RealMapping.cc.

References c, PLearn::clear(), PLearn::PStream::eof(), PLearn::PStream::get(), PLearn::PStream::getline(), MISSING_VALUE, PLearn::peekAfterSkipBlanks(), PLERROR, PLearn::RealRange::read(), PLearn::readFooter(), PLearn::readHeader(), PLearn::removeblanks(), PLearn::PStream::unget(), PLearn::upperstring(), and PLearn::ws().

{
    clear();//mapping.resize(0);
    missing_mapsto = MISSING_VALUE;
    keep_other_as_is = true;

    in >> ws;//skipBlanks(in);
    char c=in.get();
    bool uses_headers = false;
    
    if(c=='<')
    {
        in.unget();
        int version = readHeader(in,"RealMapping");
        if(version!=0)
            PLERROR("In RealMapping::read reading of version #%d not implemented",version);
        uses_headers = true;
        in >> ws;//skipBlanks(in);
        c = in.get();
    }

    if(c!='{')
        PLERROR("In RealMapping::read, expected a {, found a %c",c);

    while((c=peekAfterSkipBlanks(in))!='}' && !in.eof())
    {
        RealRange r;
            
        if(c==';'){c=in.get();}
        else if(c=='[' || c==']')
        {
            r.read(in);
            in >> ws;//skipBlanks(in);
            if(in.get()!='-' || in.get()!='>')
                PLERROR("Expecting -> after range specification ( range syntax example : ]100 200] -> 10 )");
            real val;
            in >> val;
            addMapping(r,val);
        }
        else if (isdigit(c) || c == '-' || c == '.')
        {
            real val0, val;
            in >> val0 >> ws;
            r= RealRange('[', val0, val0, ']');
            char c0= in.get();
            char c1= in.get();
            if(c0!='-' || c1!='>')
                PLERROR("Expecting '->' in mapping (syntax example : '100 -> 10') got '%c%c' instead",c0,c1);
            in >> val;
            addMapping(r,val);      
        }
        else 
        {
            string str;
            in.getline(str, '-');
            str=upperstring(removeblanks(str));
            if(str=="MISSING")
            {
                if(in.get()!='>')
                    PLERROR("Expecting -> after MISSING");
                real val;
                in >> val;
                setMappingForMissing(val);
            }
            else if (str=="OTHER")
            {
                if(in.get()!='>')
                    PLERROR("Expecting -> after OTHER");
                real val;
                in >> val;
                setMappingForOther(val);
            }
            else PLERROR("Unexpected string in mapping definition : %s",str.c_str());
        }
    }
    in.get();
    
    if(uses_headers)
    {
        in >> ws;//skipBlanks(in);
        readFooter(in,"RealMapping");        
    }
}

Here is the call graph for this function:

void PLearn::RealMapping::removeMapping ( const RealRange range) [inline]

Definition at line 187 of file RealMapping.h.

References PLWARNING.

Referenced by PLearn::StatsCollector::getBinMapping().

    { 
        mapping_t::iterator it= mapping.find(range);
        if(it != mapping.end())
            mapping.erase(it);
        else
            PLWARNING("In RealMapping::removeMapping  mapping not removed: does not exist.");
    }

Here is the caller graph for this function:

void PLearn::RealMapping::removeMapping ( real  x) [inline]

Definition at line 196 of file RealMapping.h.

References PLWARNING.

    {
        mapping_t::iterator it= mapping.lower_bound(RealRange('[',x,x,']'));
        if(it != mapping.end() && it->first.contains(x))
            mapping.erase(it);
        else
            PLWARNING("In RealMapping::removeMapping  mapping not removed: does not exist.");
    }
void PLearn::RealMapping::setMappingForMissing ( real  what_missing_mapsto) [inline]

Set mapping for missing value (by default it maps to MISSING_VALUE)

Definition at line 208 of file RealMapping.h.

    { missing_mapsto = what_missing_mapsto; }
void PLearn::RealMapping::setMappingForOther ( real  what_other_mapsto) [inline]

Set mapping for any other value (by default it is kept as is)

Definition at line 212 of file RealMapping.h.

Referenced by PLearn::StatsCollector::getBinMapping().

    { keep_other_as_is=false; other_mapsto = what_other_mapsto; }

Here is the caller graph for this function:

int PLearn::RealMapping::size ( ) const [inline]

Definition at line 177 of file RealMapping.h.

Referenced by PLearn::StatsCollector::getBinMapping(), PLearn::FieldConvertCommand::run(), and PLearn::vmatmain().

{ return (int)mapping.size(); }

Here is the caller graph for this function:

void PLearn::RealMapping::transform ( const Vec v) const

Definition at line 241 of file RealMapping.cc.

References PLearn::TVec< T >::data(), and PLearn::TVec< T >::length().

{
    real* p = v.data();
    int l = v.length();
    while(l--)
    {
        *p = map(*p);
        ++p;
    }
}

Here is the call graph for this function:

void PLearn::RealMapping::write ( ostream &  out) const [virtual]

Write the object to a C++ ostream.

The write method should write a complete description of the object to the given stream, that should be enough to later reconstruct it. (a somewhat human-readable ascii format is usually preferred). The new default version simply calls newwrite(...) which simply writes all the "options" declared in declareOptions, so there is no need to overload write in subclasses. Old classes that still override write should progressively be moved to the new declareOptions/build mechanism.

Deprecated:
Use the declareOption / build mechanism instead, that provides automatic serialization

Reimplemented from PLearn::Object.

Definition at line 289 of file RealMapping.cc.

References PLearn::print(), PLearn::writeFooter(), and PLearn::writeHeader().

{ 
    writeHeader(out,"RealMapping",0);
    print(out);
    writeFooter(out,"RealMapping");
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::Object.

Definition at line 169 of file RealMapping.h.

If true, values not in mapping are left as is, otherwise they're mapped to other_mapsto.

Definition at line 155 of file RealMapping.h.

Referenced by declareOptions(), PLearn::StatsCollector::getAllValuesMapping(), and operator==().

Defines mapping from real ranges to values.

Definition at line 148 of file RealMapping.h.

Referenced by declareOptions(), and operator==().

Value to which to map missing values (can be missing value)

Definition at line 151 of file RealMapping.h.

Referenced by declareOptions(), and operator==().

o_mapping contains the same mappings as 'mapping', but they are ordered so that the lower limits of ranges are in ascending order NOTE : before any access, it must be created with a call to buildOrderedMapping()

Definition at line 166 of file RealMapping.h.

Value to which to map values not inmapping, if keep_other_as_is is false.

Definition at line 159 of file RealMapping.h.

Referenced by declareOptions(), PLearn::StatsCollector::getAllValuesMapping(), and operator==().


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