iRoCS Toolbox  1.1.0
GlobalIDFV.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: GlobalIDFV
26 ** $RCSfile$
27 ** $Revision: 907 $$Name$
28 ** $Date: 2006-10-06 15:50:05 +0200 (Fri, 06 Oct 2006) $
29 ** Copyright: LGPL $Author: fehr $
30 ** Description: globalID feature vector. Feature vector consisting of single
31 ** entry namely a globalID. Reading in ASCII-Mode consists of
32 ** two entries per line: one label and one globalID
33 ** As methods like square() are missing, common SVM-kernel like
34 ** Kernel_RBF will not work with this FV. The FV can work with
35 ** kernels like Kernel_DS_TRIA, Kernel_DS_RBF, Kernel_FILE and
36 ** other file-based kernels, where the global ID is taken
37 ** as 'coordinates' into these matrix-files
38 **
39 **-------------------------------------------------------------------------
40 **
41 ** $Log$
42 ** Revision 1.3 2006/10/06 13:50:05 fehr
43 ** linear model optimizer added
44 **
45 ** Revision 1.2 2005/07/19 13:42:59 haasdonk
46 ** adapted header for automatic CVS-Information extraction
47 **
48 **
49 **************************************************************************/
50 
51 #ifndef GLOBALIDFV_HH
52 #define GLOBALIDFV_HH
53 
54 #ifdef HAVE_CONFIG_H
55 #include <config.hh>
56 #endif
57 
58 #include <list>
59 #include <string>
60 #include <vector>
61 #include <iostream>
62 #include <cctype>
63 using std::isspace; // sorry gcc 2.95.4 has no isspace in namespace std
64 
65 #include "adjustUniqueIDs.hh"
66 #include "svm_defines.hh"
67 
68 namespace svt
69 {
70  class GlobalIDFV
71  {
72  public:
73  typedef std::vector<double>::iterator iterator;
74  typedef std::vector<double>::const_iterator const_iterator;
75  typedef std::vector<double>::reference reference;
76  typedef std::vector<double>::const_reference const_reference;
77  typedef std::vector<double>::size_type size_type;
78 
80  : pLabel(0),
81  _uniqueID( MAX_BELIEVABLE_UNIQUE_ID + 1),
82  pGlobalID(0)
83  {};
84 
85  void setLabel( double value)
86  {
87  pLabel=value;
88  };
89 
90  double getLabel() const
91  {
92  return pLabel;
93  };
94 
95  void setUniqueID( unsigned int uid)
96  {
97  _uniqueID = uid;
98  }
99 
100 
101  unsigned int uniqueID() const
102  {
103  return _uniqueID;
104  }
105 
106 
107  void setGlobalID( unsigned int uid)
108  {
109  pGlobalID = uid;
110  }
111 
112 
113  unsigned int getGlobalID() const
114  {
115  return pGlobalID;
116  }
117 
118 
119 // reference operator[](
120 // int index)
121 // {
122 // pSquareValid=false;
123 // return pFeatures[index];
124 // };
125 
126 // const_reference operator[](
127 // int index) const
128 // {
129 // return pFeatures[index];
130 // };
131 
132 // const_iterator begin() const
133 // {
134 // return pFeatures.begin();
135 // };
136 // iterator begin()
137 // {
138 // pSquareValid=false;
139 // return pFeatures.begin();
140 // };
141 
142 // const_iterator end() const
143 // {
144 // return pFeatures.end();
145 // };
146 // iterator end()
147 // {
148 // pSquareValid=false;
149 // return pFeatures.end();
150 // };
151 
152  size_type size() const
153  {
154  return 0;
155  };
156 
157 // void resize(
158 // size_type newSize)
159 // {
160 // pFeatures.resize(newSize);
161 // pSquareValid=false;
162 // };
163 
164 // void setZero() // fixme: STL name nachgucken
165 // {
166 // for (iterator p=begin();
167 // p!=end();
168 // )
169 // {
170 // *p=0.;
171 // ++p;
172 // }
173 // pSquareValid=false;
174 // };
175 
176 // double square() const
177 // {
178 // if(!pSquareValid)
179 // {
180 // pSquare=0.;
181 // for (const_iterator p=begin();
182 // p!=end();
183 // )
184 // {
185 // pSquare+=*p * *p;
186 // ++p;
187 // }
188 
189 // pSquareValid=true;
190 // }
191 
192 // return pSquare;
193 // };
194 
195 // double dotProduct(
196 // const svt::GlobalIDFV& fv) const
197 // {
198 // double sum=0.;
199 
200 // const_iterator i=begin();
201 // const_iterator j=fv.begin();
202 // for (;
203 // i!=end();
204 // )
205 // {
206 // sum+=(*i) * (*j);
207 // ++i;
208 // ++j;
209 // }
210 
211 // return sum;
212 // };
213 
214  void readWithoutLabel( std::istream& is)
215  {
216  if( is.good())
217  {
218  //skip whitespace, stop if non-number character or newline occurs
219  char c;
220  do
221  {
222  if( is.rdbuf()->sgetc() == EOF) return;
223  is.get(c);
224  if( !is) return;
225  } while( isspace(c) && c != '\n');
226 
227  is.putback(c);
228  if( !isdigit(c) && c != '-' && c != '+' && c != '.') return;
229  is >> pGlobalID;
230  }
231  }
232 
233 
234  void writeWithoutLabel( std::ostream& os) const
235  {
236  os << " " << pGlobalID << " ";
237  }
238 
239  static const char* helpPipeFormat()
240  {
241  return "<label><ws><feature_0>\n"
242  "where <ws> is any number of white spaces except for newline\n"
243  "example:\n"
244  "-1 1001";
245  }
246 
247 
248  bool operator==( const GlobalIDFV& fv) const
249  {
250  return( pGlobalID == fv.pGlobalID);
251  }
252 
253  // this does not make sense - but a dummy is needed
254  void operator+=( const GlobalIDFV& fv)
255  {}
256 
257  // this does not make sense - but a dummy is needed
258  void operator*=( double factor)
259  {}
260 
261  // this does not make sense - but a dummy is needed
262  void setZero()
263  {}
264 
265  void resize(int i)
266  {}
267 
268  friend std::ostream& operator<<(std::ostream& os, const svt::GlobalIDFV& fv);
269  friend std::istream& operator>>(std::istream& is, svt::GlobalIDFV& fv);
270 
271  private:
272  double pLabel;
273  unsigned int _uniqueID;
274  unsigned int pGlobalID;
275  };
276 
277  inline
278  std::ostream& operator<<(std::ostream& os, const svt::GlobalIDFV& fv)
279  {
280  os << fv.pLabel;
281  fv.writeWithoutLabel( os);
282  return os;
283  }
284 
285  inline
286  std::istream& operator>>(std::istream& is, svt::GlobalIDFV& fv)
287  {
288  // read label
289  double label;
290  is >> label;
291  fv.setLabel(label);
292  fv.readWithoutLabel( is);
293  return is;
294  }
295 
296 }
297 
298 #endif
299 
300 
301 
void setGlobalID(unsigned int uid)
Definition: GlobalIDFV.hh:107
std::vector< double >::reference reference
Definition: GlobalIDFV.hh:75
static const char * helpPipeFormat()
Definition: GlobalIDFV.hh:239
void operator+=(const GlobalIDFV &fv)
Definition: GlobalIDFV.hh:254
void setUniqueID(unsigned int uid)
Definition: GlobalIDFV.hh:95
unsigned int uniqueID() const
Definition: GlobalIDFV.hh:101
std::vector< double >::const_iterator const_iterator
Definition: GlobalIDFV.hh:74
std::vector< double >::const_reference const_reference
Definition: GlobalIDFV.hh:76
const unsigned int MAX_BELIEVABLE_UNIQUE_ID
Definition: svm_defines.hh:63
std::vector< double >::size_type size_type
Definition: GlobalIDFV.hh:77
void operator*=(double factor)
Definition: GlobalIDFV.hh:258
friend std::istream & operator>>(std::istream &is, svt::GlobalIDFV &fv)
Definition: GlobalIDFV.hh:286
size_type size() const
Definition: GlobalIDFV.hh:152
void readWithoutLabel(std::istream &is)
Definition: GlobalIDFV.hh:214
bool operator==(const GlobalIDFV &fv) const
Definition: GlobalIDFV.hh:248
unsigned int getGlobalID() const
Definition: GlobalIDFV.hh:113
void setLabel(double value)
Definition: GlobalIDFV.hh:85
std::vector< double >::iterator iterator
Definition: GlobalIDFV.hh:73
friend std::ostream & operator<<(std::ostream &os, const svt::GlobalIDFV &fv)
Definition: GlobalIDFV.hh:278
void writeWithoutLabel(std::ostream &os) const
Definition: GlobalIDFV.hh:234
double getLabel() const
Definition: GlobalIDFV.hh:90
void resize(int i)
Definition: GlobalIDFV.hh:265