Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

RealMapping.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: RealMapping.cc,v 1.20 2004/07/21 16:30:50 chrish42 Exp $ 00038 * This file is part of the PLearn library. 00039 ******************************************************* */ 00040 00041 #include "RealMapping.h" 00042 #include <algorithm> 00043 #include <plearn/io/fileutils.h> 00044 //#include <sstream> 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 // char s[50]; 00083 // sprintf(s,"%c%f %f%c",leftbracket,low,high,rightbracket); 00084 // return s; 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 // Now call the parent class' declareOptions 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 //PStream perr(&cerr); 00172 //perr << "BEFORE " << mapping << endl; 00173 //perr << "ADDING " << range << "->" << val << endl; 00174 mapping[range]= val; 00175 //perr << "AFTER " << mapping << endl; 00176 // cout<<"adding :"<<range<<" = "<<val<<endl; 00177 // cout<<"MAPPING:"<<*this<<endl; 00178 } 00179 // { mapping.push_back(make_pair(range,val)); } 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();//mapping.resize(0); 00230 missing_mapsto = MISSING_VALUE; 00231 keep_other_as_is = true; 00232 00233 in >> ws;//skipBlanks(in); 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;//skipBlanks(in); 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;//skipBlanks(in); 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;//skipBlanks(in); 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 // checks that the values which the 'n' ranges are mapped to span from 0..n-1 and that these values are integers 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 } // end of namespace PLearn

Generated on Tue Aug 17 16:03:32 2004 for PLearn by doxygen 1.3.7