diff --git a/src/interfaces/python/CMakeLists.txt b/src/interfaces/python/CMakeLists.txt index ebee08be5ff..e58c2df24bd 100644 --- a/src/interfaces/python/CMakeLists.txt +++ b/src/interfaces/python/CMakeLists.txt @@ -1,7 +1,7 @@ # find python library for the found python interpreter # i.e. versions should match! FIND_PACKAGE(PythonLibs ${PYTHON_VERSION_STRING} EXACT REQUIRED) -FIND_PACKAGE(NumPy REQUIRED) +FIND_PACKAGE(NumPy 1.7 REQUIRED) SET(HAVE_PYTHON 1) diff --git a/src/interfaces/python/CustomKernel_protocols.i b/src/interfaces/python/CustomKernel_protocols.i index 1c08fbdc86c..a471e9febcf 100644 --- a/src/interfaces/python/CustomKernel_protocols.i +++ b/src/interfaces/python/CustomKernel_protocols.i @@ -209,7 +209,7 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx) ret=(PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 1, shape+1, strides+1, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); if (ret==NULL) goto fail; @@ -297,7 +297,7 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi ret=(PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 2, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); if (ret==NULL) goto fail; @@ -454,7 +454,7 @@ static PyObject* class_name ## _getsubscript(PyObject *self, PyObject *key, bool ret=(PyObject *) PyArray_NewFromDescr(&PyArray_Type, descr, ndims, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); } diff --git a/src/interfaces/python/DenseFeatures_protocols.i b/src/interfaces/python/DenseFeatures_protocols.i index 10ab666b494..072d14e314b 100644 --- a/src/interfaces/python/DenseFeatures_protocols.i +++ b/src/interfaces/python/DenseFeatures_protocols.i @@ -203,7 +203,7 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx) ret=(PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 1, shape+1, strides+1, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); if (ret==NULL) goto fail; @@ -291,7 +291,7 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi ret=(PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 2, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); if (ret==NULL) goto fail; @@ -448,7 +448,7 @@ static PyObject* class_name ## _getsubscript(PyObject *self, PyObject *key, bool ret=(PyObject *) PyArray_NewFromDescr(&PyArray_Type, descr, ndims, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); } diff --git a/src/interfaces/python/DenseLabels_protocols.i b/src/interfaces/python/DenseLabels_protocols.i index 41925c1ab0c..24520164345 100644 --- a/src/interfaces/python/DenseLabels_protocols.i +++ b/src/interfaces/python/DenseLabels_protocols.i @@ -202,7 +202,7 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx, bool get ret=(PyObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 0, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); } @@ -289,7 +289,7 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi ret=(PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 1, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); if (ret==NULL) goto fail; diff --git a/src/interfaces/python/SGVector_protocols.i b/src/interfaces/python/SGVector_protocols.i index 38daa255369..7a42c26616e 100644 --- a/src/interfaces/python/SGVector_protocols.i +++ b/src/interfaces/python/SGVector_protocols.i @@ -201,7 +201,7 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx, bool get ret=(PyObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 0, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); } @@ -288,7 +288,7 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi ret=(PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, descr, 1, shape, strides, data, - NPY_FARRAY | NPY_WRITEABLE, + NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, (PyObject *) self); if (ret==NULL) goto fail; diff --git a/src/interfaces/python/swig_typemaps.i b/src/interfaces/python/swig_typemaps.i index 8f930b09e56..80a7f9a1dff 100644 --- a/src/interfaces/python/swig_typemaps.i +++ b/src/interfaces/python/swig_typemaps.i @@ -29,6 +29,7 @@ #include #undef _POSIX_C_SOURCE +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION extern "C" { #include #include @@ -37,10 +38,8 @@ extern "C" { /* Functions to extract array attributes. */ static bool is_array(PyObject* a) { return (a) && PyArray_Check(a); } -static int array_type(PyObject* a) { return (int) PyArray_TYPE(a); } -static int array_dimensions(PyObject* a) { return ((PyArrayObject *)a)->nd; } -static int array_size(PyObject* a, int i) { return ((PyArrayObject *)a)->dimensions[i]; } -static bool array_is_contiguous(PyObject* a) { return PyArray_ISCONTIGUOUS(a); } +static int array_type(const PyObject* a) { return (int) PyArray_TYPE((const PyArrayObject*)a); } +static int array_dimensions(const PyObject* a) { return PyArray_NDIM((const PyArrayObject *)a); } /* Given a PyObject, return a string describing its type. */ @@ -96,18 +95,18 @@ static const char* typecode_string(int typecode) { * If array is NULL or dimensionality or typecode does not match * return NULL */ -static PyObject* make_contiguous(PyObject* ary, int* is_new_object, - int dims, int typecode, bool force_copy=false) +static PyArrayObject* make_contiguous( + PyObject* ary, int* is_new_object, int dims, int typecode, bool force_copy=false) { PyObject* array; - if (PyArray_ISFARRAY(ary) && !force_copy) + if (PyArray_ISFARRAY((PyArrayObject*)ary) && !force_copy) { array = ary; *is_new_object = 0; } else { - array=PyArray_FromAny((PyObject*)ary, NULL,0,0, NPY_FARRAY|NPY_ENSURECOPY, NULL); + array=PyArray_FromAny((PyObject*)ary, NULL,0,0, NPY_ARRAY_FARRAY|NPY_ARRAY_ENSURECOPY, NULL); *is_new_object = 1; } @@ -151,7 +150,7 @@ static PyObject* make_contiguous(PyObject* ary, int* is_new_object, return NULL; } - return array; + return (PyArrayObject*)array; } /* End John Hunter translation (with modifications by Bill Spotz) */ @@ -244,11 +243,11 @@ static bool vector_from_numpy(SGVector& sg_vec, PyObject* obj, int typecod } int is_new_object; - PyObject* array = make_contiguous(obj, &is_new_object, 1,typecode, true); + PyArrayObject* array = make_contiguous(obj, &is_new_object, 1,typecode, true); if (!array) return false; - ((PyArrayObject*) array)->flags &= (-1 ^ NPY_OWNDATA); + PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA); type* vec = (type*) PyArray_BYTES(array); int32_t vlen = PyArray_DIM(array,0); Py_DECREF(array); @@ -268,8 +267,8 @@ static bool vector_to_numpy(PyObject* &obj, SGVector sg_vec, int typecode) { void* copy=get_copy(sg_vec.vector, sizeof(type)*size_t(sg_vec.vlen)); obj = PyArray_NewFromDescr(&PyArray_Type, - descr, 1, &dims, NULL, copy, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) obj)->flags |= NPY_OWNDATA; + descr, 1, &dims, NULL, copy, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) obj, NPY_ARRAY_OWNDATA); } return descr!=NULL; @@ -285,14 +284,14 @@ static bool matrix_from_numpy(SGMatrix& sg_matrix, PyObject* obj, int type } int is_new_object; - PyObject* array = make_contiguous(obj, &is_new_object, 2,typecode, true); + PyArrayObject* array = make_contiguous(obj, &is_new_object, 2,typecode, true); if (!array) return false; sg_matrix = shogun::SGMatrix((type*) PyArray_BYTES(array), PyArray_DIM(array,0), PyArray_DIM(array,1), true); - ((PyArrayObject*) array)->flags &= (-1 ^ NPY_OWNDATA); + PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA); Py_DECREF(array); return true; @@ -308,8 +307,8 @@ static bool matrix_to_numpy(PyObject* &obj, SGMatrix sg_matrix, int typeco { void* copy=get_copy(sg_matrix.matrix, sizeof(type)*size_t(sg_matrix.num_rows)*size_t(sg_matrix.num_cols)); obj = PyArray_NewFromDescr(&PyArray_Type, - descr, 2, dims, NULL, (void*) copy, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) obj)->flags |= NPY_OWNDATA; + descr, 2, dims, NULL, (void*) copy, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) obj, NPY_ARRAY_OWNDATA); } return descr!=NULL; @@ -325,7 +324,7 @@ static bool array_from_numpy(SGNDArray& sg_array, PyObject* obj, int typec } int is_new_object; - PyObject* array = make_contiguous(obj, &is_new_object, -1,typecode, true); + PyArrayObject* array = make_contiguous(obj, &is_new_object, -1,typecode, true); if (!array) return false; @@ -342,7 +341,7 @@ static bool array_from_numpy(SGNDArray& sg_array, PyObject* obj, int typec sg_array = SGNDArray((type*) PyArray_BYTES(array), temp_dims, ndim); - ((PyArrayObject*) array)->flags &= (-1 ^ NPY_OWNDATA); + PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA); Py_DECREF(array); return true; @@ -369,8 +368,8 @@ static bool array_to_numpy(PyObject* &obj, SGNDArray sg_array, int typecod { void* copy=get_copy(sg_array.array, sizeof(type)*size_t(n)); obj = PyArray_NewFromDescr(&PyArray_Type, - descr, sg_array.num_dims, dims, NULL, (void*) copy, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) obj)->flags |= NPY_OWNDATA; + descr, sg_array.num_dims, dims, NULL, (void*) copy, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) obj, NPY_ARRAY_OWNDATA); } #ifdef _MSC_VER delete[] dims; @@ -436,7 +435,7 @@ static bool string_from_strpy(SGStringList& sg_strings, PyObject* obj, int if (::is_array(o) && array_dimensions(o)==1 && array_type(o) == typecode) { int is_new_object=0; - PyObject* array = make_contiguous(o, &is_new_object, 1, typecode); + PyArrayObject* array = make_contiguous(o, &is_new_object, 1, typecode); if (!array) return false; @@ -516,8 +515,8 @@ static bool string_to_strpy(PyObject* &obj, SGStringList sg_strings, int t npy_intp dims = str[i].slen; s = PyArray_NewFromDescr(&PyArray_Type, - descr, 1, &dims, NULL, (void*) data, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) s)->flags |= NPY_OWNDATA; + descr, 1, &dims, NULL, (void*) data, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) s, NPY_ARRAY_OWNDATA); } else return false; @@ -605,21 +604,21 @@ static bool spmatrix_from_numpy(SGSparseMatrix& sg_matrix, PyObject* obj, /* get indptr array */ int is_new_object_indptr=0; - PyObject* array_indptr = make_contiguous(indptr, &is_new_object_indptr, 1, NPY_INT32); + PyArrayObject* array_indptr = make_contiguous(indptr, &is_new_object_indptr, 1, NPY_INT32); if (!array_indptr) return false; int32_t* bytes_indptr=(int32_t*) PyArray_BYTES(array_indptr); int32_t len_indptr = PyArray_DIM(array_indptr,0); /* get indices array */ int is_new_object_indices=0; - PyObject* array_indices = make_contiguous(indices, &is_new_object_indices, 1, NPY_INT32); + PyArrayObject* array_indices = make_contiguous(indices, &is_new_object_indices, 1, NPY_INT32); if (!array_indices) return false; int32_t* bytes_indices=(int32_t*) PyArray_BYTES(array_indices); int32_t len_indices = PyArray_DIM(array_indices,0); /* get data array */ int is_new_object_data=0; - PyObject* array_data = make_contiguous(data, &is_new_object_data, 1, typecode); + PyArrayObject* array_data = make_contiguous(data, &is_new_object_data, 1, typecode); if (!array_data) return false; type* bytes_data=(type*) PyArray_BYTES(array_data); int32_t len_data = PyArray_DIM(array_data,0); @@ -719,17 +718,17 @@ static bool spmatrix_to_numpy(PyObject* &obj, SGSparseMatrix sg_matrix, in npy_intp indptr_dims = num_vec+1; indptr_py = PyArray_NewFromDescr(&PyArray_Type, - descr, 1, &indptr_dims, NULL, (void*) indptr, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) indptr_py)->flags |= NPY_OWNDATA; + descr, 1, &indptr_dims, NULL, (void*) indptr, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) indptr_py, NPY_ARRAY_OWNDATA); npy_intp dims = nnz; indices_py = PyArray_NewFromDescr(&PyArray_Type, - descr, 1, &dims, NULL, (void*) indices, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) indices_py)->flags |= NPY_OWNDATA; + descr, 1, &dims, NULL, (void*) indices, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) indices_py, NPY_ARRAY_OWNDATA); data_py = PyArray_NewFromDescr(&PyArray_Type, - descr_data, 1, &dims, NULL, (void*) data, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) data_py)->flags |= NPY_OWNDATA; + descr_data, 1, &dims, NULL, (void*) data, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) data_py, NPY_ARRAY_OWNDATA); PyTuple_SetItem(tuple, 0, data_py); PyTuple_SetItem(tuple, 1, indices_py); @@ -778,12 +777,13 @@ static bool spvector_to_numpy(PyObject* &obj, SGSparseVector sg_vector, in } indices_py = PyArray_NewFromDescr(&PyArray_Type, - descr, 1, &dims, NULL, (void*) indices, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) indices_py)->flags |= NPY_OWNDATA; + descr, 1, &dims, NULL, (void*) indices, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + + PyArray_ENABLEFLAGS((PyArrayObject*) indices_py, NPY_ARRAY_OWNDATA); data_py = PyArray_NewFromDescr(&PyArray_Type, - descr_data, 1, &dims, NULL, (void*) data, NPY_FARRAY | NPY_WRITEABLE, NULL); - ((PyArrayObject*) data_py)->flags |= NPY_OWNDATA; + descr_data, 1, &dims, NULL, (void*) data, NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEABLE, NULL); + PyArray_ENABLEFLAGS((PyArrayObject*) data_py, NPY_ARRAY_OWNDATA); PyTuple_SetItem(tuple, 0, data_py); PyTuple_SetItem(tuple, 1, indices_py);