Mercurial > hg > vampy-host
comparison PyTypeConversions.h @ 28:1175e814954e
Pruning & tidying
author | Chris Cannam |
---|---|
date | Wed, 26 Nov 2014 10:54:48 +0000 |
parents | fb6519598734 |
children |
comparison
equal
deleted
inserted
replaced
27:fb6519598734 | 28:1175e814954e |
---|---|
54 #include <vector> | 54 #include <vector> |
55 #include <queue> | 55 #include <queue> |
56 #include <string> | 56 #include <string> |
57 #include <sstream> | 57 #include <sstream> |
58 #include <iostream> | 58 #include <iostream> |
59 | |
60 #ifdef HAVE_NUMPY | |
61 enum eArrayDataType { | |
62 dtype_float32 = (int) NPY_FLOAT, | |
63 dtype_complex64 = (int) NPY_CFLOAT | |
64 }; | |
65 #endif | |
66 | |
67 /* C++ mapping of PyNone Type */ | |
68 struct NoneType {}; | |
69 | 59 |
70 // Data | 60 // Data |
71 class ValueError | 61 class ValueError |
72 { | 62 { |
73 public: | 63 public: |
74 ValueError() {} | 64 ValueError() {} |
75 ValueError(std::string m, bool s) : message(m),strict(s) {} | 65 ValueError(std::string m) : message(m) {} |
76 std::string location; | 66 std::string location; |
77 std::string message; | 67 std::string message; |
78 bool strict; | |
79 std::string str() const { | 68 std::string str() const { |
80 return (location.empty()) ? message : message + "\nLocation: " + location;} | 69 return (location.empty()) ? message : message + "\nLocation: " + location;} |
81 template<typename V> ValueError &operator<< (const V& v) | 70 template<typename V> ValueError &operator<< (const V& v) |
82 { | 71 { |
83 std::ostringstream ss; | 72 std::ostringstream ss; |
91 { | 80 { |
92 public: | 81 public: |
93 PyTypeConversions(); | 82 PyTypeConversions(); |
94 ~PyTypeConversions(); | 83 ~PyTypeConversions(); |
95 | 84 |
96 ValueError getError() const; | 85 ValueError getError() const; |
97 std::string PyValue_Get_TypeName(PyObject*) const; | |
98 float PyValue_To_Float(PyObject*) const; | |
99 | 86 |
100 // Sequence types | |
101 std::vector<std::string> PyValue_To_StringVector (PyObject*) const; | |
102 std::vector<float> PyValue_To_FloatVector (PyObject*) const; | 87 std::vector<float> PyValue_To_FloatVector (PyObject*) const; |
88 std::vector<float> PyArray_To_FloatVector (PyObject *) const; | |
103 std::vector<float> PyList_To_FloatVector (PyObject*) const; | 89 std::vector<float> PyList_To_FloatVector (PyObject*) const; |
104 | 90 |
105 PyObject *PyValue_From_StringVector(const std::vector<std::string> &) const; | 91 PyObject *PyValue_From_StringVector(const std::vector<std::string> &) const; |
106 | 92 PyObject *PyArray_From_FloatVector(const std::vector<float> &) const; |
107 // Numpy types | 93 |
108 std::vector<float> PyArray_To_FloatVector (PyObject *pyValue) const; | 94 private: |
109 PyObject *FloatVector_To_PyArray(const std::vector<float> &) const; // Copying the data | 95 std::string PyValue_Get_TypeName(PyObject*) const; |
96 float PyValue_To_Float(PyObject*) const; | |
110 | 97 |
111 /// Convert DTYPE type 1D NumpyArray to std::vector<RET> | 98 /// Convert DTYPE type 1D NumpyArray to std::vector<RET> |
112 template<typename RET, typename DTYPE> | 99 template<typename RET, typename DTYPE> |
113 std::vector<RET> PyArray_Convert(void* raw_data_ptr, long length, size_t strides) const | 100 std::vector<RET> PyArray_Convert(void* raw_data_ptr, |
114 { | 101 int length, |
115 std::vector<RET> rValue; | 102 size_t strides) const { |
103 | |
104 std::vector<RET> v(length); | |
116 | 105 |
117 /// check if the array is continuous, if not use strides info | 106 /// check if the array is continuous, if not use strides info |
118 if (sizeof(DTYPE)!=strides) { | 107 if (sizeof(DTYPE) != strides) { |
119 #ifdef _DEBUG_VALUES | 108 #ifdef _DEBUG_VALUES |
120 cerr << "Warning: discontinuous numpy array. Strides: " << strides << " bytes. sizeof(dtype): " << sizeof(DTYPE) << endl; | 109 cerr << "Warning: discontinuous numpy array. Strides: " << strides << " bytes. sizeof(dtype): " << sizeof(DTYPE) << endl; |
121 #endif | 110 #endif |
122 char* data = (char*) raw_data_ptr; | 111 char* data = (char*) raw_data_ptr; |
123 for (long i = 0; i<length; ++i){ | 112 for (int i = 0; i < length; ++i){ |
124 rValue.push_back((RET)(*((DTYPE*)data))); | 113 v[i] = (RET)(*((DTYPE*)data)); |
125 #ifdef _DEBUG_VALUES | 114 data += strides; |
126 cerr << "value: " << (RET)(*((DTYPE*)data)) << endl; | |
127 #endif | |
128 data+=strides; | |
129 } | 115 } |
130 return rValue; | 116 return v; |
131 } | 117 } |
132 | 118 |
133 DTYPE* data = (DTYPE*) raw_data_ptr; | 119 DTYPE* data = (DTYPE*) raw_data_ptr; |
134 for (long i = 0; i<length; ++i){ | 120 for (int i = 0; i < length; ++i){ |
135 #ifdef _DEBUG_VALUES | 121 v[i] = (RET)data[i]; |
136 cerr << "value: " << (RET)data[i] << endl; | |
137 #endif | |
138 rValue.push_back((RET)data[i]); | |
139 } | 122 } |
140 return rValue; | |
141 } | |
142 | 123 |
143 /// this is a special case. numpy.float64 has an array conversions but no array descriptor | 124 return v; |
144 std::vector<float> PyArray0D_Convert(PyArrayInterface *ai) const | |
145 { | |
146 std::vector<float> rValue; | |
147 if ((ai->typekind) == *"f") | |
148 rValue.push_back((float)*(double*)(ai->data)); | |
149 else { | |
150 setValueError("Unsupported NumPy data type.",m_strict); | |
151 return rValue; | |
152 } | |
153 #ifdef _DEBUG_VALUES | |
154 cerr << "value: " << rValue[0] << endl; | |
155 #endif | |
156 return rValue; | |
157 } | 125 } |
158 | 126 |
159 private: | 127 private: |
160 bool m_strict; | |
161 mutable bool m_error; | 128 mutable bool m_error; |
162 mutable std::queue<ValueError> m_errorQueue; | 129 mutable std::queue<ValueError> m_errorQueue; |
163 | 130 |
164 void setValueError(std::string,bool) const; | 131 void setValueError(std::string) const; |
165 ValueError& lastError() const; | 132 ValueError& lastError() const; |
133 | |
166 public: | 134 public: |
167 const bool& error; | 135 const bool& error; |
168 | |
169 }; | 136 }; |
170 | 137 |
171 #endif | 138 #endif |