DataSource.cxx
Go to the documentation of this file.
1 
12 // for distance defect
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #else
16 #ifdef _MSC_VER
17 #include "msdevstudio/MSconfig.h"
18 #define std::isnan _std::isnan
19 #endif
20 #endif
21 
22 //To have std::isnan.
23 #ifdef __APPLE__
24 #include <cstdlib>
25 #define _GLIBCPP_USE_C99 1
26 #endif
27 
28 #include "DataSource.h"
29 
30 #include "axes/Range.h"
31 #include "pattern/Observer.h"
32 #include "pattern/string_convert.h"
33 
34 #include <algorithm>
35 #include <functional>
36 #include <stdexcept>
37 
38 #include <cmath>
39 
40 #include <cassert>
41 
42 #ifdef __APPLE__
43 using std::std::isnan;
44 #endif
45 
46 #ifdef ITERATOR_MEMBER_DEFECT
47 using namespace std;
48 #else
49 using std::distance;
50 using std::runtime_error;
51 using std::string;
52 using std::vector;
53 #endif
54 
55 using namespace hippodraw;
56 
57 DataSource::
58 DataSource ( const std::string & name )
59  : m_ds_name ( name ),
60  m_title(),
61  m_is_null ( false )
62 {
63 }
64 
66 DataSource ( const char * name )
67  : m_ds_name ( name ),
68  m_title(),
69  m_is_null ( false )
70 {
71 }
72 
74 DataSource ( const DataSource & ds )
75  : Observable (),
76  m_ds_name ( ds.m_ds_name ),
77  m_title( ds.m_title ),
78  m_labels ( ds.m_labels ),
79  m_is_null ( false ),
80  m_shape ( ds.m_shape )
81 {
82 }
83 
85 DataSource ( const std::vector < std::string > & labels )
86  : m_labels ( labels ),
87  m_is_null ( false )
88 {
89 }
90 
93  : m_ds_name ( ),
94  m_title(),
95  m_is_null ( false )
96 {
97 }
98 
100 DataSource ( bool yes )
101  : m_ds_name ( "null" ),
102  m_title ( "Not valid DataSource" ),
103  m_is_null ( yes )
104 {
105 }
106 
108 {
110 }
111 
112 void
114 copyPrivate ( const DataSource & other )
115 {
116  m_ds_name = other.m_ds_name;
117  m_is_null = other.m_is_null;
118  m_labels = other.m_labels;
119  m_shape = other.m_shape;
120  m_title = other.m_title;
121 }
122 
123 bool
125 isNull () const
126 {
127  return m_is_null;
128 }
129 
130 void DataSource::setName ( const std::string & name )
131 {
132  m_ds_name = name;
133  notifyObservers ();
134 }
135 
136 const string & DataSource::getName () const
137 {
138  return m_ds_name;
139 }
140 
141 const string & DataSource::title () const
142 {
143  return m_title;
144 }
145 
146 void DataSource::setTitle ( const std::string & title )
147 {
148  m_title = title;
149  notifyObservers ();
150 }
151 
152 void
154 addLabel ( const std::string & label )
155 {
156  m_labels.push_back ( label );
157 }
158 
159 bool
161 setLabelAt( const std::string & s, unsigned int i )
162 {
163  if ( i >= m_labels.size() ) return false;
164  m_labels[i] = s;
165  notifyObservers ();
166 
167  return true;
168 }
169 
170 const vector <string> &
172 getLabels () const
173 {
174  return m_labels;
175 }
176 
177 const string &
179 getLabelAt ( unsigned int i ) const
180 {
181  if ( isNull() ) return title ();
182 
183  if ( m_labels.size () < i ) {
184  string what ( "DataSource: argument out of range" );
185  throw runtime_error ( what );
186  }
187 
188  return m_labels[i];
189 }
190 
191 int
193 indexOf ( const std::string & label ) const
194 {
195  int index = -1;
196  vector< string >::const_iterator first
197  = find ( m_labels.begin(), m_labels.end(), label );
198 
199  if ( first != m_labels.end() ) {
200 #ifdef DISTANCE_DEFECT
201  index = first - m_labels.begin();
202 #else
203  index = distance ( m_labels.begin(), first );
204 #endif
205  }
206 
207  return index;
208 }
209 
210 bool
212 isValidColumn ( unsigned int index ) const
213 {
214  unsigned int size = columns();
215  if ( index >= size ) {
216  string what ( "DataSource: Index `" );
217  what += String::convert ( index );
218  what += "' out of range 0 to ";
219  what += String::convert ( size - 1 );
220  throw runtime_error ( what );
221  }
222 
223  return true;
224 }
225 
226 const vector < double > &
228 getColumn ( unsigned int index ) const
229 {
230  isValidColumn ( index ); // will throw exception if bad
231  unsigned int size = rows ();
232 
233  m_array.resize ( size, 0.0 );
234  for ( unsigned int i = 0; i < size; i++ ) {
235  m_array [ i ] = valueAt ( i, index );
236  }
237 
238  return m_array;
239 }
240 
241 const vector < double > &
243 getColumn ( const std::string & label ) const
244 {
245  int index = indexOf ( label );
246  if ( index < 0 ) {
247  const string what ( "DataSource: Attempt to get column whose label"
248  " doesn't exist" );
249  throw runtime_error ( what );
250  }
251 
252  return getColumn ( index );
253 }
254 
255 bool
257 isValidLabel ( const std::string & label ) const
258 {
259  vector< string >::const_iterator first
260  = find ( m_labels.begin(), m_labels.end(), label );
261  bool yes = first != m_labels.end();
262 
263  return yes;
264 }
265 
266 void
268 throwIfInvalidLabel ( const std::string & label ) const
269 {
270  bool yes = isValidLabel ( label );
271  if ( yes == false ) {
272  string what ( "DataSource: `" );
273  what += label;
274  what += "' not found in this data source";
275  throw runtime_error ( what );
276  }
277 }
278 
279 void
281 throwIfInvalidRowSize ( const std:: vector < double > & row )
282 {
283  unsigned int size = columns ();
284  unsigned int cols = row.size (); // for Mac OS
285  if ( size != cols ) {
286  string what ( "DataSource: Attempt to add row of size `" );
287  what += String::convert ( cols );
288  what += "' to DataSource with `";
289  what += String::convert ( size );
290  what += "' columns";
291  throw runtime_error ( what );
292  }
293 }
294 
295 void
297 setLabels ( const std::vector < std::string > & v )
298 {
299  m_labels = v;
300 
301  notifyObservers ();
302 }
303 
304 void
306 setShape ( std::vector < unsigned int > & shape )
307 {
308  m_shape = shape;
309 }
310 
311 const vector < unsigned int > &
313 getShape () const
314 {
315  return m_shape;
316 }
317 
318 void
320 fillShape ( std::vector < int > & shape, unsigned int column ) const
321 {
322  shape.clear ();
323  shape.push_back ( rows () );
324 }
325 
326 void
328 fillShape ( std::vector < int > & v,
329  const std::string & label ) const
330 {
331  int index = indexOf ( label );
332  if ( index < 0 ) {
333  string what ( "DataSource: No column with label `" );
334  what += label;
335  what += "' exists";
336  throw runtime_error ( what );
337  }
338  fillShape ( v, index );
339 }
340 
341 
342 unsigned int
344 {
345  return m_shape.size();
346 }
347 
348 void
350 replaceColumn ( unsigned int,
351  const std::vector < double > & array )
352 {
353  string what ( "DataSource: The type of data source does not support "
354  "replacing a column." );
355  throw runtime_error ( what );
356 }
357 
358 void
360 replaceColumn ( const std::string & label,
361  const std::vector < double > & array )
362 {
363  int index = indexOf ( label );
364 
365  if ( index < 0 ) { // column doesn't exist
366  string what ( "DataSource: Attempt to replace column `" );
367  what += label;
368  what += "' which does not exist.";
369  throw runtime_error ( what );
370  }
371  else {
372  replaceColumn ( index, array );
373  }
374 }
375 
376 void
378 replaceColumn ( const std::string &,
379  const std::vector < double > &,
380  const std::vector < intptr_t > & )
381 {
382  string what ( "DataSource: This type of data source does not support"
383  " the notion of shape." );
384  throw runtime_error ( what );
385 }
386 
387 
388 int
390 addColumn ( const std::string &,
391  const std::vector < double > & )
392 {
393  string what ( "DataSource: This type of data source does not support "
394  "adding a column." );
395  throw runtime_error ( what );
396 }
397 
398 int
400 addColumn ( const std::string &,
401  const std::vector < double > &,
402  const std::vector < intptr_t > & )
403 {
404  string what ( "DataSource: This type of data source does not support"
405  " the notion of shape." );
406  throw runtime_error ( what );
407 }
408 
409 
413 bool
415 fillRange ( unsigned int column, Range & range ) const
416 {
417  assert ( column < columns () );
418  bool isValid = true;
419 
420  unsigned int size = rows ();
421  if ( size > 0 ) {
422  bool valid = false;
423  double min = DBL_MAX;
424  double max = DBL_MIN;
425  double pos = DBL_MAX;
426  for ( unsigned int i = 0; i < size; i++ ) {
427  double x = valueAt ( i, column );
428 
429  if ( x != HUGE_VAL &&
430  x != -HUGE_VAL &&
431  std::isnan ( x ) == false ) {
432  min = std::min ( min, x );
433  max = std::max ( max, x );
434  if ( x > 0.0 ) pos = std::min ( pos, x );
435  valid = true;
436  }
437  else {
438  isValid = false;
439  }
440  }
441  if ( valid == true ) {
442  range.setRange ( min, max, pos );
443  }
444  }
445 
446  return isValid;
447 }
448 
449 bool
451 isMultiDimensional( const std::string & ) const
452 {
453  return false;
454 }
455 
456 bool
458 isUseable( const std::string & ) const
459 {
460  return true;
461 }
462 
463 bool
465 setReleventIndex( const std::string &,
466  const std::vector< unsigned int > & )
467 {
468  // Do not call this fucntion unless implemented in base class
469  assert( 0 );
470 
471  return false;
472 }
473 
474 unsigned int
476 indexOfMaxElement ( unsigned int column ) const
477 {
478  assert ( column < columns () );
479 
480  unsigned int size = rows ();
481  unsigned int index = 0;
482  double m = valueAt ( 0, column );
483 
484  for ( unsigned int i = 1; i < size; i++ ) {
485  double v = valueAt ( i, column );
486  if ( v > m ) {
487  index = i;
488  m = v;
489  }
490  }
491 
492  return index;
493 }
494 
495 unsigned int
497 indexOfMinElement ( unsigned int column ) const
498 {
499  assert ( column < columns () );
500 
501  unsigned int size = rows ();
502  unsigned int index = 0;
503  double m = valueAt ( 0, column );
504 
505  for ( unsigned int i = 1; i < size; i++ ) {
506  double v = valueAt ( i, column );
507  if ( v < m ) {
508  index = i;
509  m = v;
510  }
511  }
512 
513  return index;
514 }
515 
516 double
518 sum ( unsigned int column ) const
519 {
520  assert ( column < columns () );
521 
522  unsigned int size = rows ();
523  double sum = 0.0;
524 
525  for ( unsigned int i = 0; i < size; i++ ) {
526  sum += valueAt ( i, column );
527  }
528 
529  return sum;
530 }
531 
532 const vector < string > &
535 {
536  return m_dups;
537 }
538 
539 double
541 valueAtNoCache ( unsigned int row, unsigned int column ) const
542 {
543  return valueAt ( row, column );
544 }
545 
546 void
548 addRow ( const std::vector < double > & row )
549 {
550  string what ( "This data source is not capable of adding rows" );
551  throw runtime_error ( what );
552 }
553 
554 void
556 eraseRow ( unsigned int index )
557 {
558  string what ("This data source is not capable of erasing rows" );
559  throw runtime_error ( what );
560 }
561 
562 
563 void
565 checkWidth ( const DataSource * source )
566 {
567  unsigned int ncolumns = source -> columns ();
568  if ( ncolumns != columns () ) {
569  string what ( "DataSource: Number of columns of source (" );
570  what += String::convert ( ncolumns );
571  what += ") not equal to current (";
572  what += String::convert ( columns() );
573  what += ").";
574  throw runtime_error ( what );
575  }
576 }
577 
578 void
580 append ( const DataSource * source )
581 {
582  checkWidth ( source ); // throws runtime_error
583 
584  unsigned int size = source -> rows ();
585  for ( unsigned int i = 0; i < size; i++ ) {
586  const vector < double > & src_row = source -> getRow ( i );
587  addRow ( src_row );
588  }
589 }
590 
591 double *
593 doubleArrayAt ( unsigned int row, unsigned int column ) const
594 {
595  string what ( "DataSource: This data source is not capable of containing\n"
596  "an array in a column." );
597  throw runtime_error ( what );
598 }
599 
600 void
602 expandIfNeeded ( const std::vector < std::string > & labels ) const
603 {
604 
605 }
virtual unsigned int indexOfMaxElement(unsigned int index) const
Returns row index of the maximum element in a column for the given column.
Definition: DataSource.cxx:476
virtual bool setReleventIndex(const std::string &column, const std::vector< unsigned int > &index)
In case we are dealing with multidimensional data in rows of this column we would like to deal with o...
Definition: DataSource.cxx:465
std::vector< unsigned int > m_shape
The shape of the data.
Definition: DataSource.h:96
virtual ~DataSource()
The destructor.
Definition: DataSource.cxx:107
Part of an implementation of the Observable-Observer pattern based on the example in the GOF Patterns...
Definition: Observable.h:39
virtual const std::string & getLabelAt(unsigned int index) const
Returns the label for the column at index index.
Definition: DataSource.cxx:179
virtual void checkWidth(const DataSource *source)
Checks the number of columns.
Definition: DataSource.cxx:565
virtual double valueAt(unsigned int row, unsigned int column) const =0
Returns the value in the table in position given by the row and column indexes.
virtual void eraseRow(unsigned int index)
Erase a row from the data source.
Definition: DataSource.cxx:556
virtual void addRow(const std::vector< double > &row)
Adds a row to the data source.
Definition: DataSource.cxx:548
virtual int indexOf(const std::string &label) const
Returns true if the specified column labeled label has been filled.
Definition: DataSource.cxx:193
virtual const std::vector< unsigned int > & getShape() const
Returns the shape of the data elements.
Definition: DataSource.cxx:313
virtual void willDelete(const Observable *)
Notifies this Observer object that one of its Observable objects is about to be deleted.
Definition: Observer.cxx:25
virtual void expandIfNeeded(const std::vector< std::string > &labels) const
Expand the multidimension column if needed.
Definition: DataSource.cxx:602
unsigned int getRank() const
Returns the rank of the data source.
Definition: DataSource.cxx:343
virtual bool isUseable(const std::string &column) const
Returns true if the column is usable.
Definition: DataSource.cxx:458
virtual void setTitle(const std::string &title)
Sets the title of the data source to title.
Definition: DataSource.cxx:146
virtual void setShape(std::vector< unsigned int > &shape)
Sets the shape of the data elements.
Definition: DataSource.cxx:306
std::string m_ds_name
The name of the data source .
Definition: DataSource.h:66
virtual void copyPrivate(const DataSource &other)
Copies the contents of the other DataSource.
Definition: DataSource.cxx:114
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: Observable.cxx:93
std::vector< double > m_array
A temporary array that can be returned by const reference.
Definition: DataSource.h:88
std::string m_title
The title.
Definition: DataSource.h:70
Namespace for HippoDraw.
Definition: AxesType.cxx:21
virtual bool setLabelAt(const std::string &s, unsigned int index)
Changes the label of a data column to s.
Definition: DataSource.cxx:161
std::vector< std::string > m_labels
The labels of the vectors of the data.
Definition: DataSource.h:74
void setName(const std::string &name)
Sets the name of the data source.
Definition: DataSource.cxx:130
bool isValidLabel(const std::string &label) const
Returns true if label is a valid label for a column in the DataSource.
Definition: DataSource.cxx:257
The namespace for conversion to string.
virtual double valueAtNoCache(unsigned int row, unsigned int column) const
Returns the value in the table in position given by the row and column indexes without storing it int...
Definition: DataSource.cxx:541
Base class for DataSource.
Definition: DataSource.h:55
virtual void addLabel(const std::string &label)
Adds a new label for a column.
Definition: DataSource.cxx:154
intp size(numeric::array arr)
Definition: num_util.cpp:296
void throwIfInvalidRowSize(const std::vector< double > &row)
Throws a DataSourceException if the size of the row is not equal to the number of columns...
Definition: DataSource.cxx:281
virtual const std::vector< double > & getRow(unsigned int) const =0
Returns a const reference to slice along the axis known as a row.
const std::string & title() const
Returns a const reference to the title of the data source.
Definition: DataSource.cxx:141
STL namespace.
void setRange(double low, double high, double pos)
Changes the current Range.
Definition: Range.cxx:126
bool isNull() const
Returns true if the receiving objects is a null object.
Definition: DataSource.cxx:125
Expresses a range of values.
Definition: Range.h:33
hippodraw::DataSource class interface.
virtual void replaceColumn(const std::string &label, const std::vector< double > &array)
Replaces data in column label with contents of array.
Definition: DataSource.cxx:360
virtual int addColumn(const std::string &label, const std::vector< double > &data, const std::vector< intptr_t > &shape)
Adds column vector.
Definition: DataSource.cxx:400
virtual double * doubleArrayAt(unsigned int row, unsigned int column) const
Returns an array at row and column.
Definition: DataSource.cxx:593
virtual bool isMultiDimensional(const std::string &column) const
Returns true if the column contains multidimensional data.
Definition: DataSource.cxx:451
DataSource()
The default constructor creating an data source with 0 columns.
Definition: DataSource.cxx:92
virtual void setLabels(const std::vector< std::string > &v)
Assigns the label to each column from the vector of strings @ v.
Definition: DataSource.cxx:297
virtual const std::vector< std::string > & getLabels() const
Returns the list of available labels.
Definition: DataSource.cxx:172
virtual void fillShape(std::vector< int > &v, unsigned int column) const
Fills the vector with the shape of a column.
Definition: DataSource.cxx:320
bool isValidColumn(unsigned int index) const
Returns true if index is valid, otherwise throws a DataSourceException.
Definition: DataSource.cxx:212
const std::string & getName() const
Returns the name of the data source.
Definition: DataSource.cxx:136
virtual void throwIfInvalidLabel(const std::string &label) const
Throws a DataSourceException object if label is not a valid label for this DataSource.
Definition: DataSource.cxx:268
virtual bool fillRange(unsigned int column, Range &) const
Fills the Range object from data indexed by column.
Definition: DataSource.cxx:415
virtual unsigned int rows() const =0
Returns the number of rows.
virtual void append(const DataSource *source)
Appends the contents of the DataSource source.
Definition: DataSource.cxx:580
bool m_is_null
A flag to indicate the object is null.
Definition: DataSource.h:82
virtual const std::vector< double > & getColumn(const std::string &name) const
Returns the data in the column with label name.
Definition: DataSource.cxx:243
virtual double sum(unsigned int column) const
Returns the sum of all the elements in the sequence of column column.
Definition: DataSource.cxx:518
string convert(int i)
Converts an integer to a string.
const std::vector< std::string > & getDuplicatedLabels() const
Returns the column labels that were duplicated, or an empty vector.
Definition: DataSource.cxx:534
Observer class interface.
column
The column indices for 2 dimension data point tuple.
virtual unsigned int indexOfMinElement(unsigned int index) const
Returns row index of the minimum element in a column for the given column.
Definition: DataSource.cxx:497
hippodraw::Range class interface
std::vector< std::string > m_dups
A list of labels that duplicated others in a DataSource file.
Definition: DataSource.h:100
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317

Generated for HippoDraw Class Library by doxygen