comparison PyTypeInterface.h @ 32:a8231788216c vampy2

Vampy2: accept numpy array return types.
author fazekasgy
date Mon, 21 Sep 2009 13:56:28 +0000
parents 4f1894c7591b
children c4da8d559872
comparison
equal deleted inserted replaced
31:4f1894c7591b 32:a8231788216c
5 5
6 */ 6 */
7 7
8 #ifndef _PY_TYPE_INTERFACE_H_ 8 #ifndef _PY_TYPE_INTERFACE_H_
9 #define _PY_TYPE_INTERFACE_H_ 9 #define _PY_TYPE_INTERFACE_H_
10
11 #include "vamp-sdk/Plugin.h"
12 #include <Python.h> 10 #include <Python.h>
11 #ifdef HAVE_NUMPY
12 #include "arrayobject.h"
13 #endif
13 #include "PyExtensionModule.h" 14 #include "PyExtensionModule.h"
14 #include <vector> 15 #include <vector>
15 #include <queue> 16 #include <queue>
16 #include <string> 17 #include <string>
18 #include "vamp-sdk/Plugin.h"
17 //#include <typeinfo> 19 //#include <typeinfo>
18 20
19 21
20 using std::cerr; 22 using std::cerr;
21 using std::endl; 23 using std::endl;
71 duration, 73 duration,
72 values, 74 values,
73 label 75 label
74 }; 76 };
75 77
76
77 /// sutructure of NumPy array interface:
78 /// this is all we need to support numpy without direct dependency
79 typedef struct {
80 int two; /* contains the integer 2 -- simple sanity check */
81 int nd; /* number of dimensions */
82 char typekind; /* kind in array --- character code of typestr */
83 int itemsize; /* size of each element */
84 int flags; /* flags indicating how the data should be interpreted */
85 /* must set ARR_HAS_DESCR bit to validate descr */
86 Py_intptr_t *shape; /* A length-nd array of shape information */
87 Py_intptr_t *strides; /* A length-nd array of stride information */
88 void *data; /* A pointer to the first element of the array */
89 PyObject *descr; /* NULL or data-description (same as descr key */
90 /* of __array_interface__) -- must set ARR_HAS_DESCR */
91 /* flag or this will be ignored. */
92 } PyArrayInterface;
93
94 /* C++ mapping of PyNone Type*/ 78 /* C++ mapping of PyNone Type*/
95 typedef struct NoneType {}; 79 typedef struct NoneType {};
96 80
97 class PyTypeInterface 81 class PyTypeInterface
98 { 82 {
136 PyObject *PyValue_From_CValue(bool) const; 120 PyObject *PyValue_From_CValue(bool) const;
137 121
138 // Sequence types 122 // Sequence types
139 std::vector<std::string> PyValue_To_StringVector (PyObject*) const; 123 std::vector<std::string> PyValue_To_StringVector (PyObject*) const;
140 std::vector<float> PyValue_To_FloatVector (PyObject*) const; 124 std::vector<float> PyValue_To_FloatVector (PyObject*) const;
125 std::vector<float> PyList_To_FloatVector (PyObject*) const;
141 126
142 // Numpy types 127 // Numpy types
143 float* getNumPyObjectData(PyObject *object, int &length) const; 128 #ifdef HAVE_NUMPY
129 std::vector<float> PyArray_To_FloatVector (PyObject *pyValue) const;
130 #endif
144 131
145 132
146 /* Template functions */ 133 /* Template functions */
147 134
148 135
149 /// Common wrappers to set a value in one of these structs. (to be used in template functions) 136 /// Common wrappers to set values in Vamp API structs. (to be used in template functions)
150 void SetValue(Vamp::Plugin::OutputDescriptor& od, std::string& key, PyObject* pyValue) const; 137 void SetValue(Vamp::Plugin::OutputDescriptor& od, std::string& key, PyObject* pyValue) const;
151 void SetValue(Vamp::Plugin::ParameterDescriptor& od, std::string& key, PyObject* pyValue) const; 138 void SetValue(Vamp::Plugin::ParameterDescriptor& od, std::string& key, PyObject* pyValue) const;
152 bool SetValue(Vamp::Plugin::Feature& od, std::string& key, PyObject* pyValue) const; 139 bool SetValue(Vamp::Plugin::Feature& od, std::string& key, PyObject* pyValue) const;
153 PyObject* GetDescriptor_As_Dict(PyObject* pyValue) const 140 PyObject* GetDescriptor_As_Dict(PyObject* pyValue) const
154 { 141 {
223 list.push_back(element); 210 list.push_back(element);
224 } 211 }
225 return list; 212 return list;
226 } 213 }
227 214
215 /// Convert DTYPE type 1D NumpyArray to std::vector<RET>
216 template<typename RET, typename DTYPE>
217 std::vector<RET> PyTypeInterface::PyArray_Convert(char* raw_data_ptr, long length) const
218 {
219 std::vector<RET> rValue;
220 DTYPE* data = (DTYPE*) raw_data_ptr;
221 for (long i = 0; i<length; ++i){
222 #ifdef _DEBUG
223 cerr << "value: " << (RET)data[i] << endl;
224 #endif
225 rValue.push_back((RET)data[i]);
226 }
227 return rValue;
228 }
228 229
229 //Vamp specific types 230 //Vamp specific types
230 231
231 Vamp::Plugin::FeatureSet PyValue_To_FeatureSet(PyObject*) const; 232 Vamp::Plugin::FeatureSet PyValue_To_FeatureSet(PyObject*) const;
232 inline void PyValue_To_rValue(PyObject *pyValue, Vamp::Plugin::FeatureSet &r) const 233 inline void PyValue_To_rValue(PyObject *pyValue, Vamp::Plugin::FeatureSet &r) const
251 { std::string tmp = this->PyValue_To_String(pyValue); 252 { std::string tmp = this->PyValue_To_String(pyValue);
252 if(!m_error) defValue = tmp; } 253 if(!m_error) defValue = tmp; }
253 /*used by templates where we expect no return value, if there is one it will be ignored*/ 254 /*used by templates where we expect no return value, if there is one it will be ignored*/
254 inline void PyValue_To_rValue(PyObject *pyValue, NoneType &defValue) const 255 inline void PyValue_To_rValue(PyObject *pyValue, NoneType &defValue) const
255 { if (m_strict && pyValue != Py_None) 256 { if (m_strict && pyValue != Py_None)
256 setValueError("Strict conversion error: expected 'None' type.",m_strict); 257 setValueError("Strict conversion error: Expected 'None' type.",m_strict);
257 } 258 }
258 259
259 /* convert sequence types to Vamp List types */ 260 /* convert sequence types to Vamp List types */
260 inline void PyValue_To_rValue(PyObject *pyValue, Vamp::Plugin::OutputList &r) const 261 inline void PyValue_To_rValue(PyObject *pyValue, Vamp::Plugin::OutputList &r) const
261 { r = this->PyValue_To_VampList<Vamp::Plugin::OutputList,Vamp::Plugin::OutputDescriptor>(pyValue); } 262 { r = this->PyValue_To_VampList<Vamp::Plugin::OutputList,Vamp::Plugin::OutputDescriptor>(pyValue); }
299 public: 300 public:
300 const bool& error; 301 const bool& error;
301 302
302 }; 303 };
303 304
305 #ifdef NUMPY_REFERENCE
306 /// This should be all we need to compile without direct dependency,
307 /// but we don't do that. (it may not work on some platforms)
308 typedef struct {
309 int two; /* contains the integer 2 -- simple sanity check */
310 int nd; /* number of dimensions */
311 char typekind; /* kind in array --- character code of typestr */
312 int itemsize; /* size of each element */
313 int flags; /* flags indicating how the data should be interpreted */
314 /* must set ARR_HAS_DESCR bit to validate descr */
315 Py_intptr_t *shape; /* A length-nd array of shape information */
316 Py_intptr_t *strides; /* A length-nd array of stride information */
317 void *data; /* A pointer to the first element of the array */
318 PyObject *descr; /* NULL or data-description (same as descr key */
319 /* of __array_interface__) -- must set ARR_HAS_DESCR */
320 /* flag or this will be ignored. */
321 } PyArrayInterface;
322
323 typedef struct PyArrayObject {
324 PyObject_HEAD
325 char *data; /* pointer to raw data buffer */
326 int nd; /* number of dimensions, also called ndim */
327 npy_intp *dimensions; /* size in each dimension */
328 npy_intp *strides; /* bytes to jump to get to the
329 next element in each dimension */
330 PyObject *base; /* This object should be decref'd
331 upon deletion of array */
332 /* For views it points to the original array */
333 /* For creation from buffer object it points
334 to an object that shold be decref'd on
335 deletion */
336 /* For UPDATEIFCOPY flag this is an array
337 to-be-updated upon deletion of this one */
338 PyArray_Descr *descr; /* Pointer to type structure */
339 int flags; /* Flags describing array -- see below*/
340 PyObject *weakreflist; /* For weakreferences */
341 } PyArrayObject;
342
343 typedef struct _PyArray_Descr {
344 PyObject_HEAD
345 PyTypeObject *typeobj; /* the type object representing an
346 instance of this type -- should not
347 be two type_numbers with the same type
348 object. */
349 char kind; /* kind for this type */
350 char type; /* unique-character representing this type */
351 char byteorder; /* '>' (big), '<' (little), '|'
352 (not-applicable), or '=' (native). */
353 char hasobject; /* non-zero if it has object arrays
354 in fields */
355 int type_num; /* number representing this type */
356 int elsize; /* element size for this type */
357 int alignment; /* alignment needed for this type */
358 struct _arr_descr \
359 *subarray; /* Non-NULL if this type is
360 is an array (C-contiguous)
361 of some other type
362 */
363 PyObject *fields; /* The fields dictionary for this type */
364 /* For statically defined descr this
365 is always Py_None */
366
367 PyObject *names; /* An ordered tuple of field names or NULL
368 if no fields are defined */
369
370 PyArray_ArrFuncs *f; /* a table of functions specific for each
371 basic data descriptor */
372 } PyArray_Descr;
373
374 enum NPY_TYPES { NPY_BOOL=0,
375 NPY_BYTE, NPY_UBYTE,
376 NPY_SHORT, NPY_USHORT,
377 NPY_INT, NPY_UINT,
378 NPY_LONG, NPY_ULONG,
379 NPY_LONGLONG, NPY_ULONGLONG,
380 NPY_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE,
381 NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE,
382 NPY_OBJECT=17,
383 NPY_STRING, NPY_UNICODE,
384 NPY_VOID,
385 NPY_NTYPES,
386 NPY_NOTYPE,
387 NPY_CHAR, /* special flag */
388 NPY_USERDEF=256 /* leave room for characters */
389 };
390 #endif /*NUMPY_REFERENCE*/
304 #endif 391 #endif