Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NumArrayTuple.cxx

Go to the documentation of this file.
00001 
00012 #include "NumArrayTuple.h"
00013 
00014 #include "axes/Range.h"
00015 #include "datasrcs/DataSourceException.h"
00016 
00017 #include "num_util.h"
00018 
00019 #include <algorithm>
00020 #include <numeric>
00021 
00022 using namespace boost::python;
00023 
00024 using std::string;
00025 using std::vector;
00026 
00027 NumArrayTuple::NumArrayTuple ()
00028   : DataSource ()
00029 {
00030 }
00031 
00032 NumArrayTuple::~NumArrayTuple()
00033 {
00034 }
00035 
00036 void
00037 NumArrayTuple::
00038 copy ( const DataSource & )
00039 {
00040   assert ( false );
00041 }
00042 
00043 void
00044 NumArrayTuple::
00045 notifyObservers ( ) const
00046 {
00047   Observable::notifyObservers ();
00048 }
00049 
00050 unsigned int
00051 NumArrayTuple::
00052 rows() const
00053 {
00054   unsigned int size = 0;
00055   if ( m_data.empty () == false ) {
00056     const numeric::array & array = m_data[0];
00057 
00058     size = num_util::size ( array );
00059   }
00060 
00061   return size;
00062 }
00063 
00064 bool
00065 NumArrayTuple::
00066 empty () const
00067 {
00068   return rows () == 0;
00069 }
00070 
00074 double
00075 NumArrayTuple::
00076 valueAt( unsigned int row, unsigned int column ) const
00077 {
00078 #ifdef HAVE_NUMERIC
00079   PyGILState_STATE state = PyGILState_Ensure ();
00080 #endif
00081 
00082   assert ( column < m_data.size () );
00083 
00084   const numeric::array & array = m_data[column];
00085 
00086   int size = num_util::size ( array );
00087   assert ( row < static_cast < unsigned int > ( size ) );
00088 
00089   object result = array[row];
00090 
00091   double value = extract < double > ( result );
00092 #ifdef HAVE_NUMERIC
00093   PyGILState_Release ( state );
00094 #endif
00095 
00096   return value;
00097 }
00098 
00102 const std::vector < double > &
00103 NumArrayTuple::
00104 getRow ( unsigned int row ) const
00105 {
00106   unsigned int size = m_data.size();
00107   m_row.resize ( size );
00108   for ( unsigned int column = 0; column < size; column++ ) {
00109     m_row [ column ] = valueAt ( row, column );
00110   }
00111 
00112   return m_row;
00113 }
00114 
00117 int
00118 NumArrayTuple::
00119 addColumn ( const std::string & label,
00120             boost::python::numeric::array array )
00121 {
00122   // Check if label already exists.
00123   int index = indexOf ( label );
00124   if ( index >= 0 ) {
00125     string what ( "NumArrayTuple Attempt to add a column whose label"
00126                   " is same as other column." );
00127     throw DataSourceException ( what );
00128   }
00129 
00130   unsigned int new_size = num_util::size ( array );
00131   // Check if column has right size.
00132   if ( m_data.empty () == false ) {
00133     unsigned int old_size = rows ();
00134 
00135     if ( old_size != 0 && old_size != new_size ) {
00136       string what ( "NumArrayTuple Attempt to add a column whose size"
00137                     " is not equal to other columns." );
00138       throw DataSourceException ( what );
00139     }
00140   }
00141   m_data.push_back ( array );
00142   addLabel ( label );
00143 
00144   return m_data.size() - 1;
00145 }
00146 
00147 void
00148 NumArrayTuple::
00149 replaceColumn ( unsigned int col, 
00150                 boost::python::numeric::array array )
00151 {
00152   unsigned int size = columns ();
00153   if ( col >= size ) {
00154     const string what ( "NunArrayTuple: column doesn't exist" );
00155     throw DataSourceException ( what );
00156   }
00157 
00158   const numeric::array old_array = m_data[col];
00159   int old_size = num_util::size ( old_array );
00160   int new_size = num_util::size ( array );
00161 
00162   if ( old_size != 0 && old_size != new_size ) {
00163     const string what ( "NumArrayTuple: Attempt to replace column with one "
00164                         "whose size is not equal to other columns." );
00165     throw DataSourceException ( what );
00166   }
00167   m_data[col] = array;
00168 
00169   notifyObservers ();
00170 }
00171 
00172 void
00173 NumArrayTuple::
00174 replaceColumn ( const std::string & column,
00175                 boost::python::numeric::array array )
00176 {
00177   unsigned int index = indexOf ( column );
00178 
00179   replaceColumn ( index, array );
00180 }
00181 
00182 numeric::array
00183 NumArrayTuple::
00184 getNumArray( unsigned int index ) const
00185 {
00186   unsigned int size = columns ();
00187   if ( index >= size ) {
00188     const string what ( "NunArrayTuple: column doesn't exist" );
00189     throw DataSourceException ( what );
00190   }
00191   return m_data[index];
00192 }
00193 
00194 numeric::array
00195 NumArrayTuple::
00196 getNumArray( const std::string & label ) const
00197 {
00198    unsigned int index = indexOf ( label );
00199    return getNumArray( index );
00200 }
00201 
00202 void
00203 NumArrayTuple::
00204 setShape ( std::vector < unsigned int > & shape )
00205 {
00206   unsigned int size = 1;
00207   for ( unsigned int i = 0; i < shape.size(); i++ ) {
00208     size *= shape[i];
00209   }
00210 
00211   m_shape = shape;
00212 }
00213 
00214 const vector < unsigned int > &
00215 NumArrayTuple::
00216 getShape () const
00217 {
00218   return m_shape;
00219 }
00220 
00221 unsigned int
00222 NumArrayTuple::getRank () const
00223 {
00224   return m_shape.size();
00225 }
00226 void
00227 NumArrayTuple::
00228 clear ()
00229 {
00230   assert ( false );
00231 }
00232 
00233 void
00234 NumArrayTuple::
00235 reserve ( unsigned int count )
00236 {
00237   assert ( false );
00238 }
00239 
00240 void
00241 NumArrayTuple::
00242 addRow ( const std::vector < double > & row )
00243 {
00244   assert ( false );
00245 }
00246 
00247 double 
00248 NumArrayTuple::
00249 operator [] (  std::vector < unsigned int > & indices ) const
00250 {
00251   assert ( false );
00252   return 0.;
00253 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3