00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
#include "RealMapping.h"
00042 
#include <algorithm>
00043 
#include <plearn/io/fileutils.h>    
00044 
00045 
#include "stringutils.h"  
00046 
#include <plearn/io/PStream.h>
00047 
00048 
namespace PLearn {
00049 
using namespace std;
00050 
00051 
00052 PLEARN_IMPLEMENT_OBJECT(
RealMapping, 
"ONE LINE DESCR", 
"NO HELP");
00053 
00054 PStream& 
operator<<(
PStream& out, 
const RealRange& x) 
00055 { 
00056   out.
put(
x.leftbracket);
00057   out << 
x.low << 
x.high;
00058   out.
put(
x.rightbracket);
00059   
return out;
00060 }
00061 
00062 PStream& 
operator>>(
PStream& in, 
RealRange &x) 
00063 { 
00064   in.
skipBlanksAndCommentsAndSeparators();
00065   
x.leftbracket = in.
get();
00066   in.
skipBlanksAndComments();
00067   in >> 
x.low;
00068   in.
skipBlanksAndComments();
00069   in >> 
x.high;
00070   in.
skipBlanksAndComments();
00071   
x.rightbracket = in.
get();
00072   
x.checkbrackets();
00073   
return in;
00074 }
00075 
00076   string RealRange::getString()
 const 
00077 
  {
00078     ostringstream s;
00079     s << 
leftbracket << 
low << 
' ' << 
high << 
rightbracket;
00080     
return s.str();
00081 
00082 
00083 
00084 
00085   }  
00086 
00087 
00088 bool RealRange::contains(
real val)
 const
00089 
{ 
return (
val>=
low) && (
val<=
high) && (
val!=
low || 
leftbracket==
'[') && (
val!=
high || 
rightbracket==
']'); }
00090 
00091 bool RealRange::operator<(
real x)
 const
00092 
{ 
return high < 
x || 
high == 
x && 
rightbracket == 
'['; }
00093 
00094 bool RealRange::operator>(
real x)
 const
00095 
{ 
return low > 
x || 
low == 
x && 
leftbracket == 
']'; }
00096 
00097 bool RealRange::operator<(
const RealRange& x)
 const
00098 
{ 
return high < 
x.low || (
high == 
x.low && (
rightbracket==
'[' || 
x.leftbracket==
']')); }
00099 
00100 bool RealRange::operator>(
const RealRange& x)
 const
00101 
{ 
return low > 
x.high || (
low == 
x.high && (
leftbracket==
']' || 
x.rightbracket==
'[')); }
00102 
00103 bool RealRange::operator==(
const RealRange& rr)
 const
00104 
{
00105   
return (rr.
low == 
low && 
00106           rr.
high == 
high && 
00107           rr.
leftbracket == 
leftbracket && 
00108           rr.
rightbracket == 
rightbracket);
00109 }
00110 
00111 
00112 
00113 
00114   bool RealMapping::operator==(
const RealMapping& rm)
 const
00115 
  {
00116     
return (
rm.mapping == 
mapping && 
00117              (
rm.missing_mapsto == 
missing_mapsto || (isnan(
missing_mapsto)&&isnan(
rm.missing_mapsto))) &&
00118              
rm.keep_other_as_is == 
keep_other_as_is &&
00119             (
rm.other_mapsto == 
other_mapsto || (isnan(
other_mapsto)&&isnan(
rm.other_mapsto))));
00120   }
00121 
00122   bool operator<(
RealMapping::single_mapping_t a, RealMapping::single_mapping_t b)
00123   {
00124     
return a.first<b.first;
00125   }
00126 
00127   real RealMapping::maxMappedToValue()
00128   {
00129     
real max = -9999999;
00130     mapping_t::iterator it = 
mapping.begin();
00131     mapping_t::iterator itend = 
mapping.end();
00132     
for(; it!=itend; ++it)
00133       
if(it->second > 
max)
00134         
max = it->second;
00135     
return max;
00136   }    
00137 
00138 void RealMapping::declareOptions(
OptionList& ol) 
00139 {
00140   
declareOption(ol, 
"mapping", &RealMapping::mapping, OptionBase::buildoption,
00141                 
"The mapping");
00142 
00143   
00144   inherited::declareOptions(ol);
00145 }
00146 
00147   void RealMapping::buildOrderedMapping()
00148   { 
00149     
o_mapping.
resize(0);
00150     
for(
iterator it = 
begin();it!=
end();++it)
00151       
o_mapping.
push_back(*it);
00152     sort(
o_mapping.
begin(),
o_mapping.
end());
00153   }
00154 
00155   real RealMapping::map(
real val)
 const
00156 
  {
00157     
if(
is_missing(
val))
00158       
return missing_mapsto;
00159     mapping_t::const_iterator it= 
mapping.lower_bound(
RealRange(
'[',
val,
val,
']'));
00160     
if(it != 
mapping.end() && it->first.contains(
val))
00161       
return it->second;
00162     
if(
keep_other_as_is)
00163       
return val;
00164     
else
00165       
return other_mapsto;      
00166   }
00167 
00168 
00169   void RealMapping::addMapping(
const RealRange& range, 
real val)
00170     {
00171       
00172       
00173       
00174       
mapping[range]= 
val; 
00175       
00176       
00177       
00178     }
00179       
00180 
00181 
00182   int RealMapping::binnumber(
real val)
 const
00183 
  {
00184     
if(
is_missing(
val))
00185       
return -2;
00186     mapping_t::const_iterator it = 
mapping.begin();
00187     mapping_t::const_iterator itend = 
mapping.end();
00188     
for(
int num=0; it!=itend; ++it,num++)
00189       
if(it->first.contains(
val))
00190         
return num;
00191       
return -1;      
00192   }
00193 
00194 
00195   void RealMapping::transform(
const Vec& v)
 const
00196 
  {
00197     
real* p = v.
data();
00198     
int l = v.
length();
00199     
while(l--)
00200       {
00201         *p = 
map(*p);
00202         ++p;
00203       }
00204   }
00205 
00206   void RealMapping::print(ostream& out)
 const
00207 
  { 
00208     out << 
"{ ";
00209     out.precision(17);
00210     
for(mapping_t::const_iterator it= 
mapping.begin(); 
00211         it != 
mapping.end(); ++it)
00212       out << it->first << 
" -> " << it->second << 
"; ";
00213     
if(!
is_missing(
missing_mapsto))
00214       out << 
"MISSING -> " << 
missing_mapsto << 
"; " << 
endl;
00215     
if(!
keep_other_as_is)
00216       out << 
"OTHER -> " << 
other_mapsto;
00217     out << 
"} ";
00218   }
00219 
00220   void RealMapping::write(ostream& out)
 const
00221 
  { 
00222     
writeHeader(out,
"RealMapping",0);
00223     
print(out);
00224     
writeFooter(out,
"RealMapping");
00225   }
00226 
00227   void RealMapping::read(istream& in)
00228   {
00229     
clear();
00230     
missing_mapsto = 
MISSING_VALUE;
00231     
keep_other_as_is = 
true;
00232 
00233     in >> 
ws;
00234     
char c=in.
get();
00235     
bool uses_headers = 
false;
00236     
00237     
if(c==
'<')
00238       {
00239         in.unget();
00240         
int version = 
readHeader(in,
"RealMapping");
00241         
if(version!=0)
00242           
PLERROR(
"In RealMapping::read reading of version #%d not implemented",version);
00243         uses_headers = 
true;
00244         in >> 
ws;
00245         c = in.
get();
00246       }
00247 
00248     
if(c!=
'{')
00249       
PLERROR(
"In RealMapping::read, expected a {, found a %c",c);
00250 
00251     
while((c=
peekAfterSkipBlanks(in))!=
'}' && !in.eof())
00252       {
00253         
RealRange r;
00254             
00255         
if(c==
';'){c=in.get();}
00256         
else if(c==
'[' || c==
']')
00257           {
00258             r.
read(in);
00259             in >> 
ws;
00260             
if(in.get()!=
'-' || in.get()!=
'>')
00261                 
PLERROR(
"Expecting -> after range specification ( range syntax example : ]100 200] -> 10 )");
00262             
real val;
00263             in >> 
val;
00264             
addMapping(r,
val);
00265           }
00266         
else if (isdigit(c) || c == 
'-' || c == 
'.')
00267         {
00268           
real val0, 
val;
00269           in >> val0 >> 
ws;
00270           r= 
RealRange(
'[', val0, val0, 
']');
00271           
if(in.get()!=
'-' || in.get()!=
'>')
00272             
PLERROR(
"Expecting -> after range specification ( range syntax example : ]100 200] -> 10 )");
00273           in >> 
val;
00274           
addMapping(r,
val);        
00275         }
00276         
else 
00277         {
00278           
string str;
00279           getline(in,str,
'-');
00280           str=
upperstring(
removeblanks(str));
00281           
if(str==
"MISSING")
00282           {
00283             
if(in.get()!=
'>')
00284               
PLERROR(
"Expecting -> after MISSING");
00285             
real val;
00286             in >> 
val;
00287             
setMappingForMissing(
val);
00288           }
00289           
else if (str==
"OTHER")
00290           {
00291             
if(in.get()!=
'>')
00292               
PLERROR(
"Expecting -> after OTHER");
00293             
real val;
00294             in >> 
val;
00295             
setMappingForOther(
val);
00296           }
00297           
else PLERROR(
"Unexpected string in mapping definition : %s",str.c_str());
00298         }
00299       }
00300     in.get();
00301     
00302     
if(uses_headers)
00303       {
00304         in >> 
ws;
00305         
readFooter(in,
"RealMapping");        
00306       }
00307   }
00308 
00309 
00310 Vec RealMapping::getCutPoints()
 const
00311 
{
00312   
Vec v(
length()+1);
00313   mapping_t::const_iterator it= 
mapping.begin();
00314   v[0]= it->
first.low;
00315   
for(
int i= 1; it != 
mapping.end(); ++it, ++i)
00316     v[i]= it->
first.high;
00317   
return v;
00318 }
00319 
00320 
00321 bool RealMapping::checkConsistency()
00322 {
00323   mapping_t::const_iterator it= 
mapping.begin();
00324   
int max = 0;
00325   
for(;it != 
mapping.end(); ++it)
00326   {
00327     
if(it->second <0 || it->second - (
int)it->second > 0)
00328       
return false;
00329     
if(
max < it->second)
00330       
max = (
int)it->second;
00331   }
00332   
TVec<int> v(
max+1,0);
00333   
for(it=
mapping.begin();it != 
mapping.end(); ++it)
00334   {
00335     
if(v[(
int)it->second] != 0)
00336       
return false;
00337     v[(
int)it->second]=1;
00338   }
00339   
for(
int i=0;i<
max;i++)
00340     
if(v[i]==0)
00341       
return false;
00342   
return true;  
00343 }
00344 
00345 }