NumArrayTuple.cxx
Go to the documentation of this file.
1 
12 // include first to avoid _POSIX_C_SOURCE warning.
13 #include <boost/python.hpp>
14 
15 // for have numarray etc
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #define PY_ARRAY_UNIQUE_SYMBOL HippoPyArrayHandle
21 #define NO_IMPORT_ARRAY
22 
23 #include "NumArrayTuple.h"
24 
25 #include "axes/Range.h"
26 
27 #include "num_util.h"
28 
29 #include <algorithm>
30 #include <stdexcept>
31 
32 using namespace boost::python;
33 
34 using std::runtime_error;
35 using std::string;
36 using std::vector;
37 
38 using namespace hippodraw;
39 
40 NumArrayTuple::NumArrayTuple ()
41  : DataSource ()
42 {
43 }
44 
46 {
47 }
48 
49 void
51 copy ( const DataSource & )
52 {
53  assert ( false );
54 }
55 
56 void
58 notifyObservers ( ) const
59 {
61 }
62 
63 unsigned int
65 rows() const
66 {
67  unsigned int size = 0;
68 
69  if ( m_data.empty () == false ) {
70  numeric::array na = getNumArray ( 0 );
71  size = static_cast < unsigned int > ( num_util::get_dim ( na, 0 ) );
72  }
73 
74  return size;
75 }
76 
77 bool
79 empty () const
80 {
81  return rows () == 0;
82 }
83 
87 double
89 valueAt( unsigned int row, unsigned int column ) const
90 {
91 #if HAVE_NUMPY || HAVE_NUMERIC
92  PyGILState_STATE state = PyGILState_Ensure ();
93 #endif
94 
95  assert ( column < m_data.size () );
96 
97  const numeric::array array = m_data[column];
98 
99  int size = num_util::size ( array );
100  assert ( row < static_cast < unsigned int > ( size ) );
101 
102  object result = array[row];
103 
104  double value = extract < double > ( result );
105 #if HAVE_NUMPY || HAVE_NUMERIC
106  PyGILState_Release ( state );
107 #endif
108 
109  return value;
110 }
111 
115 const std::vector < double > &
117 getRow ( unsigned int row ) const
118 {
119  unsigned int size = m_data.size();
120  m_row.resize ( size );
121  for ( unsigned int column = 0; column < size; column++ ) {
122  m_row [ column ] = valueAt ( row, column );
123  }
124 
125  return m_row;
126 }
127 
130 int
132 addColumn ( const std::string & label,
133  boost::python::numeric::array array )
134 {
135  // Check if label already exists.
136  int index = indexOf ( label );
137  if ( index >= 0 ) {
138  string what ( "NumArrayTuple Attempt to add a column whose label, `");
139  what += label;
140  what += "', is same as existing column.";
141  throw runtime_error ( what );
142  }
143 
144  unsigned int new_size = num_util::get_dim ( array, 0 );
145  // Check if column has right size.
146  if ( m_data.empty () == false ) {
147  unsigned int old_size = rows ();
148 
149  if ( old_size != 0 && old_size != new_size ) {
150  string what ( "NumArrayTuple Attempt to add a column whose size"
151  " is not equal to other columns." );
152  throw runtime_error ( what );
153  }
154  }
155  m_data.push_back ( array );
156  addLabel ( label );
157 
158  return m_data.size() - 1;
159 }
160 
161 void
163 replaceColumn ( unsigned int col,
164  boost::python::numeric::array array )
165 {
166  unsigned int size = columns ();
167  if ( col >= size ) {
168  const string what ( "NunArrayTuple: column doesn't exist" );
169  throw runtime_error ( what );
170  }
171 
172  const numeric::array old_array = m_data[col];
173  int old_size = num_util::size ( old_array );
174  int new_size = num_util::size ( array );
175 
176  if ( old_size != 0 && old_size != new_size ) {
177  const string what ( "NumArrayTuple: Attempt to replace column with one "
178  "whose size is not equal to other columns." );
179  throw runtime_error ( what );
180  }
181  m_data[col] = array;
182 
183  notifyObservers ();
184 }
185 
186 void
188 replaceColumn ( const std::string & column,
189  boost::python::numeric::array array )
190 {
191  unsigned int index = indexOf ( column );
192 
193  replaceColumn ( index, array );
194 }
195 
196 numeric::array
198 getNumArray( unsigned int index ) const
199 {
200  unsigned int size = columns ();
201  if ( index >= size ) {
202  const string what ( "NunArrayTuple: column doesn't exist" );
203  throw runtime_error ( what );
204  }
205  return m_data[index];
206 }
207 
208 numeric::array
210 getNumArray( const std::string & label ) const
211 {
212  unsigned int index = indexOf ( label );
213  return getNumArray( index );
214 }
215 
216 void
218 setShape ( std::vector < unsigned int > & shape )
219 {
220  m_shape = shape;
221 }
222 
223 const vector < unsigned int > &
225 getShape () const
226 {
227  return m_shape;
228 }
229 
230 void
232 fillShape ( std::vector < intptr_t > & shape, unsigned int column ) const
233 {
234  shape.clear ();
235  numeric::array na = getNumArray ( column );
236 
237  shape = num_util::shape ( na );
238 }
239 
240 void
243 {
244  assert ( false );
245 }
246 
247 void
249 reserve ( unsigned int ) //row )
250 {
251  assert ( false );
252 }
253 
254 double
256 operator [] ( std::vector < unsigned int > & ) const // indices ) const
257 {
258  assert ( false );
259  return 0.;
260 }
std::vector< boost::python::numeric::array > m_data
The numarray objects that contains the data.
Definition: NumArrayTuple.h:61
virtual void setShape(std::vector< unsigned int > &shape)
Sets the shape of the data elements.
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: Observable.cxx:93
virtual bool empty() const
Returns true, if NumArrayTuple is empty, i.e.
virtual void notifyObservers() const
Notifies observers.
virtual ~NumArrayTuple()
The destructor.
virtual void addLabel(const std::string &label)
Adds a new label for a column.
Definition: DataSource.cxx:154
column
The column indices for 2 dimension data point tuple.
intp get_dim(boost::python::numeric::array arr, int dimnum)
Returns the size of a specific dimension.
Definition: num_util.cpp:335
std::vector< double > m_row
A temporary array of data from one row of each column.
Definition: NumArrayTuple.h:67
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317
virtual void copy(const DataSource &)
Raises exception because with this release making a copy is not supported.
virtual const std::vector< double > & getRow(unsigned int index) const
Returns a const reference to slice along the axis known as a row.
std::vector< unsigned int > m_shape
The shape of the data.
Definition: DataSource.h:96
intp size(numeric::array arr)
Definition: num_util.cpp:296
virtual double operator[](std::vector< unsigned int > &indices) const
Raises assertion as this method is not implemented yet.
void replaceColumn(unsigned int index, boost::python::numeric::array array)
Replaces the column indexed by index with the array.
boost::python::numeric::array getNumArray(const std::string &label) const
Return the reference to the desired numarray by column label.
hippodraw::NumArrayTuple class interface.
virtual void clear()
Raises assertion as this method is not implemented yet.
hippodraw::Range class interface
virtual int indexOf(const std::string &label) const
Returns true if the specified column labeled label has been filled.
Definition: DataSource.cxx:193
virtual unsigned int rows() const
Returns the size of the slice for the next to last dimension.
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
return index
Definition: PickTable.cxx:182
int addColumn(const std::string &label, boost::python::numeric::array array)
Adds a column to the end of the NumArrayTuple.
virtual void fillShape(std::vector< intptr_t > &v, unsigned int column) const
Fills the vector with the shape of a column.
virtual double valueAt(unsigned int row, unsigned int column) const
const std::vector< unsigned int > & getShape() const
Returns the shape of the data elements.
virtual void reserve(unsigned int count)
Raises assertion as this method is not implemented yet.
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen