ListTuple.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 #ifdef HAVE_CONFIG_H
16 // for have smp and qt4
17 #include "config.h"
18 #endif
19 
20 #include "ListTuple.h"
21 
22 #include "axes/Range.h"
23 
24 #include <stdexcept>
25 
26 using namespace boost::python;
27 
28 using std::runtime_error;
29 using std::string;
30 using std::vector;
31 
32 // namespace
33 // {
34 // /** @note For multi-processor machines, need the to get the the
35 // Python global interpreter lock when compiled with Qt 4 on Mac OS
36 // X and maybe other systems. However, with Qt 3, obtaining the
37 // lock seems to lock up the system.
38 
39 // The implementation of this
40 // member function is taken from
41 // http://docs.python.org/api.thread.html
42 // */
43 // unsigned int getSize ( const boost::python::list & sequence )
44 // {
45 // #ifdef HAVE_QT4
46 // #ifdef HAVE_SMP
47 // PyGILState_STATE gstate;
48 // gstate = PyGILState_Ensure ();
49 // #endif
50 // #endif
51 
52 // object obj = sequence.attr ( "__len__" ) ();
53 // unsigned int size = extract < unsigned int > ( obj );
54 
55 // #ifdef HAVE_QT4
56 // #ifdef HAVE_SMP
57 // PyGILState_Release ( gstate );
58 // #endif
59 // #endif
60 
61 // return size;
62 // }
63 // }
64 
65 using namespace hippodraw;
66 
67 ListTuple::ListTuple ()
68  : DataSource ()
69 {
70 }
71 
73 {
74 }
75 
76 unsigned int
78 getSize ( const boost::python::list & sequence )
79 {
80 #ifdef HAVE_QT4
81 #ifdef HAVE_SMP
82  PyGILState_STATE gstate;
83  gstate = PyGILState_Ensure ();
84 #endif
85 #endif
86 
87  object obj = sequence.attr ( "__len__" ) ();
88  unsigned int size = extract < unsigned int > ( obj );
89 
90 #ifdef HAVE_QT4
91 #ifdef HAVE_SMP
92  PyGILState_Release ( gstate );
93 #endif
94 #endif
95 
96  return size;
97 }
98 
99 void
101 copy ( const DataSource & )
102 {
103  assert ( false );
104 }
105 
106 void
109 {
111 }
112 
113 unsigned int
115 rows() const
116 {
117  unsigned int size = 0;
118  if ( m_data.empty () == false ) {
119  const boost::python::list & seq = m_data[0];
120 
121  size = getSize ( seq );
122  }
123 
124  return size;
125 }
126 
127 bool
129 empty () const
130 {
131  return rows () == 0;
132 }
133 
134 double
136 valueAt( unsigned int row, unsigned int column ) const
137 {
138  assert ( column < m_data.size () );
139  double value = 0;
140 
141  const boost::python::list & seq = m_data[column];
142 
143  unsigned int size = getSize ( seq );
144 
145  assert ( row < size );
146 
147  object result = seq[row];
148  value = extract < double > ( result );
149 
150  return value;
151 }
152 
156 const std::vector < double > &
158 getRow ( unsigned int row ) const
159 {
160  unsigned int size = m_data.size();
161  m_row.resize ( size );
162  for ( unsigned int column = 0; column < size; column++ ) {
163  m_row [ column ] = valueAt ( row, column );
164  }
165 
166  return m_row;
167 }
168 
169 bool
172 {
173  bool yes = true;
174  unsigned int size = getSize ( array );
175  for ( unsigned int i = 0; i < size; i++ ) {
176  object obj = array[i];
177  extract < double > check ( obj );
178  if ( check.check() == false ) {
179  yes = false;
180  break;
181  }
182  }
183  return yes;
184 }
185 
188 int
190 addColumn ( const std::string & label,
191  boost::python::list array )
192 {
193  // Check if label already exists.
194  int index = indexOf ( label );
195  if ( index >= 0 ) {
196  string what ( "ListTuple Attempt to add a column whose label"
197  " is same as other column." );
198  throw runtime_error ( what );
199  }
200 
201  unsigned int new_size = getSize ( array );
202  // Check if column has right size.
203  if ( m_data.empty () == false ) {
204  unsigned int old_size = rows ();
205 
206  if ( old_size != 0 && old_size != new_size ) {
207  string what ( "ListTuple Attempt to add a column whose size"
208  " is not equal to other columns." );
209  throw runtime_error ( what );
210  }
211  }
212  if ( isAcceptable ( array ) == false ) {
213  string what ( "ListTuple: Attempt to add a column with one or more"
214  " elements not convertable to float" );
215  throw runtime_error ( what );
216  }
217 
218  m_data.push_back ( array );
219  addLabel ( label );
220 
221  return m_data.size() - 1;
222 }
223 
224 void
226 replaceColumn ( unsigned int col,
227  boost::python::list array )
228 {
229  unsigned int size = columns ();
230  if ( col >= size ) {
231  const string what ( "NunArrayTuple: column doesn't exist" );
232  throw runtime_error ( what );
233  }
234 
235  const boost::python::list & old_array = m_data[col];
236  int old_size = getSize ( old_array );
237  int new_size = getSize ( array );
238 
239  if ( old_size != 0 && old_size != new_size ) {
240  const string what ( "ListTuple: Attempt to replace column with one "
241  "whose size is not equal to other columns." );
242  throw runtime_error ( what );
243  }
244  m_data[col] = array;
245 
246  notifyObservers ();
247 }
248 
249 void
251 replaceColumn ( const std::string & column,
252  boost::python::list array )
253 {
254  unsigned int index = indexOf ( column );
255 
256  replaceColumn ( index, array );
257 }
258 
259 void
261 setShape ( std::vector < unsigned int > & shape )
262 {
263  m_shape = shape;
264 }
265 
266 const vector < unsigned int > &
268 getShape () const
269 {
270  return m_shape;
271 }
272 void
275 {
276  assert ( false );
277 }
278 
279 void
281 reserve ( unsigned int )
282 {
283  assert ( false );
284 }
285 
286 double
288 operator [] ( std::vector < unsigned int > & /* indices */ ) const
289 {
290  assert ( false );
291  return 0.0;
292 }
unsigned int i
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: Observable.cxx:93
virtual void clear()
Raises assertion because the contained Python list should not be changed.
Definition: ListTuple.cxx:274
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.
virtual void reserve(unsigned int count)
Raises assertion because the contained Python list should not be changed.
Definition: ListTuple.cxx:281
const std::vector< unsigned int > & getShape() const
Returns the shape of the data elements.
Definition: ListTuple.cxx:268
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317
virtual double operator[](std::vector< unsigned int > &indices) const
Raises assertion because this method is not implemented yet.
Definition: ListTuple.cxx:288
return yes
Definition: CanvasView.cxx:883
virtual void notifyObservers() const
Notifies observers.
Definition: ListTuple.cxx:108
std::vector< unsigned int > m_shape
The shape of the data.
Definition: DataSource.h:96
virtual void setShape(std::vector< unsigned int > &shape)
Sets the shape of the data elements.
Definition: ListTuple.cxx:261
hippodraw::ListTuple class interface.
intp size(numeric::array arr)
Definition: num_util.cpp:296
bool isAcceptable(const boost::python::list &seq)
Returns true if all the elements of the list seq can be converted to type double, otherwise returns f...
Definition: ListTuple.cxx:171
void replaceColumn(unsigned int index, boost::python::list array)
Replaces the column indexed by index with the array.
Definition: ListTuple.cxx:226
virtual void copy(const DataSource &)
Raises an exception as with this release copying is not supported.
Definition: ListTuple.cxx:101
std::vector< boost::python::list > m_data
The numarray objects that contains the data.
Definition: ListTuple.h:47
virtual double valueAt(unsigned int row, unsigned int column) const
Returns the value in the table in position given by the row and column indexes.
Definition: ListTuple.cxx:136
int addColumn(const std::string &label, boost::python::list seq)
Adds a column to the end of the ListTuple.
Definition: ListTuple.cxx:190
virtual unsigned int rows() const
Returns the size of the slice for the next to last dimension.
Definition: ListTuple.cxx:115
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
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
unsigned static int getSize(const boost::python::list &sequence)
Definition: ListTuple.cxx:78
std::vector< double > m_row
A temporary array of data from one row of each column.
Definition: ListTuple.h:53
return index
Definition: PickTable.cxx:182
virtual bool empty() const
Returns true, if ListTuple is empty, i.e.
Definition: ListTuple.cxx:129
A wrapper class for Python list objects.
Definition: python.dir:41
virtual ~ListTuple()
The destructor.
Definition: ListTuple.cxx:72
Base class for DataSource.
Definition: DataSource.h:55
virtual const std::vector< double > & getRow(unsigned int index) const
Returns a const reference to slice along the axis known as a row.
Definition: ListTuple.cxx:158

Generated for HippoDraw Class Library by doxygen