Machine Learning Library
CDatasetItem.h
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef CDATASETITEM_H
27 #define CDATASETITEM_H
28 
29 #include "CVector.h"
30 #include "CDenseVector.h"
31 #include "assert.h"
32 #include "stdio.h"
33 #include "CObject.h"
34 #include "CArchiv.h"
35 #include "CObjectFactory.h"
42 template<class Type>
43 class CDatasetItem :public CObject<Type> {
44 protected:
47  int iId;
48 
49 public:
51  {
52  ptInput = new CDenseVector<Type>(1);
54  iId=-1;
55  };
56 
57  CDatasetItem(int iDimInput, int iDimOutput) : CObject<Type>() {
58 #ifdef SAFE
59  assert(iDimInput >= 0);
60  assert(iDimOutput >= 0);
61 #endif
62  ptInput = new CDenseVector<Type>(iDimInput);
63  ptOutput = new CDenseVector<Type>(iDimOutput);
64  iId=-1;
65  };
66 
67 
68  CDatasetItem(const CVector<Type>& rtInput,const CVector<Type>& rtOutput) : CObject<Type>()
69  {
70  ptInput = rtInput.clone();
71  ptOutput = rtOutput.clone();
72  iId=-1;
73  };
74 
75  CDatasetItem(const CVector<Type>& rtInput,const CVector<Type>& rtOutput,const int iID) : CObject<Type>()
76  {
77  ptInput = rtInput.clone();
78  ptOutput = rtOutput.clone();
79  iId = iID;
80  };
81 
82  CDatasetItem(const CDatasetItem<Type>& rtItem) : CObject<Type>()
83  {
84  this->ptInput = rtItem.ptInput->clone();
85  this->ptOutput= rtItem.ptOutput->clone();
86  this->iId = rtItem.iId;
87  };
88 
89  virtual string className() const { return string("CDatasetItem");};
91  {
92  if(this == &rtItem)
93  return *this;
94  delete this->ptInput;
95  delete this->ptOutput;
96  this->ptInput = rtItem.ptInput->clone();
97  this->ptOutput= rtItem.ptOutput->clone();
98  this->iId = rtItem.iId;
99  return *this;
100  };
101 
102 
106  {
107  ptInput = in;
108  ptOutput = out;
109  iId=-1;
110  }
111 
113  {
114  if(sizeof(Type) == sizeof(double))
115  return new CDatasetItem<double>(*((CDatasetItem<double>*)this));
116 
117  return new CDatasetItem<double>(ptInput->toDouble(),
118  ptOutput->toDouble());
119  };
120 
121 
123  {
124  if(sizeof(Type) == sizeof(float))
125  return new CDatasetItem<float>(*((CDatasetItem<float>*) this));
126 
127  return new CDatasetItem<float>(ptInput->toFloat(),
128  ptOutput->toFloat());
129 
130  }
131 
132  virtual ~CDatasetItem() {
133  if(ptInput)
134  delete ptInput;
135  if(ptOutput)
136  delete ptOutput;
137  };
138 
139 
140  void setInputVector(const CVector<Type>& rtVector)
141  {
142  if(ptInput) {
143  delete ptInput;
144  }
145  ptInput = rtVector.clone();
146  };
147 
148 
149  void setOutputVector(const CVector<Type>& rtVector)
150  {
151  if(ptOutput) {
152  delete ptOutput;
153  }
154  ptOutput = rtVector.clone();
155  };
156 
157 
161  inline const CVector<Type>& getInputVector() const
162  {
163  return *ptInput;
164  };
165 
166  inline const CVector<Type>& inputVector() const
167  {
168  return *ptInput;
169  };
170 
171 
176  {
177  return *ptInput;
178  };
179 
181  {
182  return *ptInput;
183  };
184 
185 
186  inline CVector<Type>* inputVectorPtr() const {
187  return ptInput;
188  }
189 
190 
194  inline const CVector<Type>& getOutputVector() const
195  {
196  return *ptOutput;
197  }
198  inline const CVector<Type>& outputVector() const
199  {
200  return *ptOutput;
201  }
206  {
207  return *ptOutput;
208  };
210  {
211  return *ptOutput;
212  };
213 
214  inline CVector<Type>* outputVectorPtr() const {
215  return ptOutput;
216  }
217 
221  inline Type getInputComponent(int iIndex) const
222  {
223  return ptInput->getElement(iIndex);
224  };
225  inline Type inputComponent(int iIndex) const
226  {
227  return ptInput->getElement(iIndex);
228  };
229 
233  inline Type getOutputComponent(int iIndex) const
234  {
235  return ptOutput->getElement(iIndex);
236  };
237  inline Type outputComponent(int iIndex) const
238  {
239  return ptOutput->getElement(iIndex);
240  };
241 
242  /* inline void setInputComponent(int iIndex, Type tValue)
243  {
244  ptInput->setElement(iIndex, tValue);
245  };
246 
247 
248  inline void setOutputComponent(int iIndex, Type tValue)
249  {
250  ptOutput->setElement(iIndex, tValue);
251  }; */
252 
253  inline int inputDimension() const
254  {
255  return ptInput->dimension();
256  };
257 
258 
259  inline int outputDimension() const
260  {
261  return ptOutput->dimension();
262  };
263 
264  virtual inline bool operator== (const CDatasetItem<Type>& right) const
265  {
266  if((int)this->id() == (int)right.id())
267  return 1;
268  else
269  return 0;
270  };
271 
272  virtual inline bool operator!= (const CDatasetItem<Type>& right) const
273  {
274  if((int)this->id() != (int)right.id())
275  return 1;
276  else
277  return 0;
278  };
279 
280  virtual inline bool operator> (const CDatasetItem<Type>& right) const
281  {
282  if((int)this->id() < (int)right.id())
283  return 0;
284  else
285  return 1;
286  };
287 
288 
289  virtual inline bool operator< (const CDatasetItem<Type>& right) const
290  {
291  if((int)this->id() > (int)right.id())
292  return 0;
293  else
294  return 1;
295  };
296 
297  inline int id() const {return iId;};
298 
299  inline void setId(int iId) { this->iId = iId;};
300 
301 
308  bool serialize(fstream& stream, IO_MODE mode) {
309  CObject<Type>::serialize(stream, mode);
310  if(mode == WRITE) {
311  stream.write((char*) &iId, sizeof(int));
312  stream << ptInput->className() << endl;
313  ptInput->serialize(stream, mode);
314  stream << ptOutput->className() << endl;
315  ptOutput->serialize(stream, mode);
316  return true;
317  } else {
318  string str;
319  stream.read((char*) &iId, sizeof(int));
320  getline(stream, str);
322  ptInput->serialize(stream, mode);
323  getline(stream, str);
325  ptOutput->serialize(stream, mode);
326  return true;
327  }
328  return false;
329  }
330 
331 
332  bool serialize2(CArchiv& tA) {
333  string str;
335  return false;
336  if( tA.isReading()) {
337  if(ptInput)
338  delete ptInput;
339  if(ptOutput)
340  delete ptOutput;
341  tA >> iId;
342  tA >> str;
344  if(!ptInput)
345  ML_CRITICAL_MSG("Unable to produce CVector object");
346  tA >> *ptInput;
347  tA >> str;
349  if(!ptOutput)
350  ML_CRITICAL_MSG("Unable to produce CVector object");
351  tA >> *ptOutput;
352  return true;
353  } else {
354  tA << iId; tA.flush();
355  tA << ptInput->className(); tA.flush();
356  tA << *ptInput; tA.flush();
357  tA << ptOutput->className(); tA.flush();
358  tA << *ptOutput; tA.flush();
359  return true;
360  }
361  return false;
362  }
363 
364 
365 };
366 #endif
bool serialize2(CArchiv &tA)
Definition: CDatasetItem.h:332
CVector< Type > * inputVectorPtr() const
Definition: CDatasetItem.h:186
virtual bool isReading() const
Definition: CArchiv.h:25
CDatasetItem(const CDatasetItem< Type > &rtItem)
Definition: CDatasetItem.h:82
void setId(int iId)
Definition: CDatasetItem.h:299
Type outputComponent(int iIndex) const
Definition: CDatasetItem.h:237
Definition: CObject.h:38
int outputDimension() const
Definition: CDatasetItem.h:259
IO_MODE
Definition: CObject.h:38
Type inputComponent(int iIndex) const
Definition: CDatasetItem.h:225
virtual CDatasetItem< float > * convertToFloat()
Definition: CDatasetItem.h:122
virtual string className() const
Returns the class name.
Definition: CDatasetItem.h:89
CVector< Type > & getInputVector()
Definition: CDatasetItem.h:175
virtual bool operator!=(const CDatasetItem< Type > &right) const
Definition: CDatasetItem.h:272
virtual bool serialize(fstream &stream, IO_MODE mode=READ)
Read/write from binary stream.
const CVector< Type > & getOutputVector() const
Definition: CDatasetItem.h:194
CDatasetItem()
Definition: CDatasetItem.h:50
CDatasetItem(int iDimInput, int iDimOutput)
Definition: CDatasetItem.h:57
virtual ~CDatasetItem()
Definition: CDatasetItem.h:132
CVector< Type > & inputVector()
Definition: CDatasetItem.h:180
#define ML_CRITICAL_MSG(x)
Definition: Macros.h:136
void setInputVector(const CVector< Type > &rtVector)
Definition: CDatasetItem.h:140
bool serialize(fstream &stream, IO_MODE mode)
Definition: CDatasetItem.h:308
static CObjectFactory * instance()
int inputDimension() const
Definition: CDatasetItem.h:253
CDatasetItem(const CVector< Type > &rtInput, const CVector< Type > &rtOutput, const int iID)
Definition: CDatasetItem.h:75
virtual bool operator==(const CDatasetItem< Type > &right) const
Definition: CDatasetItem.h:264
CDatasetItem(const CVector< Type > &rtInput, const CVector< Type > &rtOutput)
Definition: CDatasetItem.h:68
Single item of a dataset consisting of a pair of input and out vectors.
Definition: CDatasetItem.h:43
Type getInputComponent(int iIndex) const
Definition: CDatasetItem.h:221
CVector< Type > & outputVector()
Definition: CDatasetItem.h:209
Base class of object serialization.
Definition: CArchiv.h:19
virtual CDatasetItem< double > * convertToDouble()
Definition: CDatasetItem.h:112
Type getOutputComponent(int iIndex) const
Definition: CDatasetItem.h:233
Template object for vectors of single and double precision and integer.
Definition: CDenseVector.h:37
int iId
Definition: CDatasetItem.h:47
templatized vector for numerical applications
Definition: CMatrix.h:39
Base class for all object.
Definition: CObject.h:51
CVector< Type > * outputVectorPtr() const
Definition: CDatasetItem.h:214
int id() const
Definition: CDatasetItem.h:297
DATATYPE dataType() const
Returns the template data type.
CVector< Type > * ptInput
Definition: CDatasetItem.h:45
virtual CDatasetItem< Type > & operator=(const CDatasetItem< Type > &rtItem)
Definition: CDatasetItem.h:90
void * produceObject(const char *acObjectName, DATATYPE tType)
virtual bool operator>(const CDatasetItem< Type > &right) const
Definition: CDatasetItem.h:280
const CVector< Type > & outputVector() const
Definition: CDatasetItem.h:198
CDatasetItem(CVector< Type > *in, CVector< Type > *out)
Definition: CDatasetItem.h:105
const CVector< Type > & inputVector() const
Definition: CDatasetItem.h:166
virtual void flush()
Definition: CArchiv.h:26
CVector< Type > & getOutputVector()
Definition: CDatasetItem.h:205
CVector< Type > * ptOutput
Definition: CDatasetItem.h:46
virtual CVector< Type > * clone() const =0
destructor
const CVector< Type > & getInputVector() const
Definition: CDatasetItem.h:161
void setOutputVector(const CVector< Type > &rtVector)
Definition: CDatasetItem.h:149