Mercurial > hg > vampy
comparison PyTypeInterface.cpp @ 66:5664fe298af2
Update to Python 2.7 and clean up the build (avoid using deprecated NumPy API, fix compiler warnings)
author | Chris Cannam |
---|---|
date | Mon, 17 Nov 2014 09:37:59 +0000 |
parents | 74703a562ce3 |
children | 6c755f3e1173 |
comparison
equal
deleted
inserted
replaced
65:0df94e3f0fdb | 66:5664fe298af2 |
---|---|
1 /* -*- c-basic-offset: 8 indent-tabs-mode: t -*- */ | |
1 /* | 2 /* |
2 | 3 |
3 * Vampy : This plugin is a wrapper around the Vamp plugin API. | 4 * Vampy : This plugin is a wrapper around the Vamp plugin API. |
4 * It allows for writing Vamp plugins in Python. | 5 * It allows for writing Vamp plugins in Python. |
5 | 6 |
12 #include <Python.h> | 13 #include <Python.h> |
13 | 14 |
14 #ifdef HAVE_NUMPY | 15 #ifdef HAVE_NUMPY |
15 #define PY_ARRAY_UNIQUE_SYMBOL VAMPY_ARRAY_API | 16 #define PY_ARRAY_UNIQUE_SYMBOL VAMPY_ARRAY_API |
16 #define NO_IMPORT_ARRAY | 17 #define NO_IMPORT_ARRAY |
18 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION | |
17 #include "numpy/arrayobject.h" | 19 #include "numpy/arrayobject.h" |
18 #endif | 20 #endif |
19 | 21 |
20 #include "PyTypeInterface.h" | 22 #include "PyTypeInterface.h" |
21 #include "PyRealTime.h" | 23 #include "PyRealTime.h" |
38 static std::map<std::string, eSampleTypes> sampleKeys; | 40 static std::map<std::string, eSampleTypes> sampleKeys; |
39 static std::map<std::string, eFeatureFields> ffKeys; | 41 static std::map<std::string, eFeatureFields> ffKeys; |
40 static bool isMapInitialised = false; | 42 static bool isMapInitialised = false; |
41 | 43 |
42 /* Note: NO FUNCTION IN THIS CLASS SHOULD ALTER REFERENCE COUNTS | 44 /* Note: NO FUNCTION IN THIS CLASS SHOULD ALTER REFERENCE COUNTS |
43 (EXCEPT FOR TEMPORARY PYTHON OBJECTS)! */ | 45 (EXCEPT FOR TEMPORARY PYTHON OBJECTS)! */ |
44 | 46 |
45 PyTypeInterface::PyTypeInterface() : | 47 PyTypeInterface::PyTypeInterface() : |
46 m_strict(false), | 48 m_strict(false), |
47 m_error(false), | 49 m_error(false), |
48 m_numpyInstalled(false), | 50 m_numpyInstalled(false), |
696 return Output; | 698 return Output; |
697 } | 699 } |
698 #endif | 700 #endif |
699 | 701 |
700 PyArrayObject* pyArray = (PyArrayObject*) pyValue; | 702 PyArrayObject* pyArray = (PyArrayObject*) pyValue; |
701 PyArray_Descr* descr = pyArray->descr; | 703 PyArray_Descr* descr = PyArray_DESCR(pyArray); |
702 | 704 |
703 /// check raw data and descriptor pointers | 705 /// check raw data and descriptor pointers |
704 if (pyArray->data == 0 || descr == 0) { | 706 if (PyArray_DATA(pyArray) == 0 || descr == 0) { |
705 std::string msg = "NumPy array with NULL data or descriptor pointer encountered."; | 707 std::string msg = "NumPy array with NULL data or descriptor pointer encountered."; |
706 setValueError(msg,m_strict); | 708 setValueError(msg,m_strict); |
707 #ifdef _DEBUG | 709 #ifdef _DEBUG |
708 cerr << "PyTypeInterface::PyArray_To_FloatVector failed. Error: " << msg << endl; | 710 cerr << "PyTypeInterface::PyArray_To_FloatVector failed. Error: " << msg << endl; |
709 #endif | 711 #endif |
710 return Output; | 712 return Output; |
711 } | 713 } |
712 | 714 |
713 /// check dimensions | 715 /// check dimensions |
714 if (pyArray->nd != 1) { | 716 if (PyArray_NDIM(pyArray) != 1) { |
715 std::string msg = "NumPy array must be a one dimensional vector."; | 717 std::string msg = "NumPy array must be a one dimensional vector."; |
716 setValueError(msg,m_strict); | 718 setValueError(msg,m_strict); |
717 #ifdef _DEBUG | 719 #ifdef _DEBUG |
718 cerr << "PyTypeInterface::PyArray_To_FloatVector failed. Error: " << msg << " Dims: " << (int) pyArray->nd << endl; | 720 cerr << "PyTypeInterface::PyArray_To_FloatVector failed. Error: " << msg << " Dims: " << (int) pyArray->nd << endl; |
719 #endif | 721 #endif |
723 #ifdef _DEBUG_VALUES | 725 #ifdef _DEBUG_VALUES |
724 cerr << "PyTypeInterface::PyArray_To_FloatVector: Numpy array verified." << endl; | 726 cerr << "PyTypeInterface::PyArray_To_FloatVector: Numpy array verified." << endl; |
725 #endif | 727 #endif |
726 | 728 |
727 /// check strides (useful if array is not continuous) | 729 /// check strides (useful if array is not continuous) |
728 size_t strides = *((size_t*) pyArray->strides); | 730 size_t strides = *((size_t*) PyArray_STRIDES(pyArray)); |
729 | 731 |
730 /// convert the array | 732 /// convert the array |
731 switch (descr->type_num) | 733 switch (descr->type_num) |
732 { | 734 { |
733 case NPY_FLOAT : // dtype='float32' | 735 case NPY_FLOAT : // dtype='float32' |
734 return PyArray_Convert<float,float>(pyArray->data,pyArray->dimensions[0],strides); | 736 return PyArray_Convert<float,float>(PyArray_DATA(pyArray),PyArray_DIMS(pyArray)[0],strides); |
735 case NPY_DOUBLE : // dtype='float64' | 737 case NPY_DOUBLE : // dtype='float64' |
736 return PyArray_Convert<float,double>(pyArray->data,pyArray->dimensions[0],strides); | 738 return PyArray_Convert<float,double>(PyArray_DATA(pyArray),PyArray_DIMS(pyArray)[0],strides); |
737 case NPY_INT : // dtype='int' | 739 case NPY_INT : // dtype='int' |
738 return PyArray_Convert<float,int>(pyArray->data,pyArray->dimensions[0],strides); | 740 return PyArray_Convert<float,int>(PyArray_DATA(pyArray),PyArray_DIMS(pyArray)[0],strides); |
739 case NPY_LONG : // dtype='long' | 741 case NPY_LONG : // dtype='long' |
740 return PyArray_Convert<float,long>(pyArray->data,pyArray->dimensions[0],strides); | 742 return PyArray_Convert<float,long>(PyArray_DATA(pyArray),PyArray_DIMS(pyArray)[0],strides); |
741 default : | 743 default : |
742 std::string msg = "Unsupported value type in NumPy array object."; | 744 std::string msg = "Unsupported value type in NumPy array object."; |
743 setValueError(msg,m_strict); | 745 setValueError(msg,m_strict); |
744 #ifdef _DEBUG | 746 #ifdef _DEBUG |
745 cerr << "PyTypeInterface::PyArray_To_FloatVector failed. Error: " << msg << endl; | 747 cerr << "PyTypeInterface::PyArray_To_FloatVector failed. Error: " << msg << endl; |