From 9ba8cd9319bd99a5fd753eeb77fd5845990f89a0 Mon Sep 17 00:00:00 2001 From: Navaneet Villodi <11260095+nauaneed@users.noreply.github.com> Date: Thu, 26 Sep 2024 23:38:55 +0530 Subject: [PATCH 1/5] ci(deps): install nothing separately let pip handle all the dependencies while installing this package --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6e26198..ab16e31 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -20,8 +20,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip setuptools wheel - python -m pip install -r requirements.txt - pip install . + pip install .[tests] -v + pip list - name: Run tests run: pytest -v --pyargs cyarray --benchmark-save benchmark-stats --benchmark-json benchmark-full.json --benchmark-histogram From a97d0a9f6ea5af6929629fd234497410212d0a48 Mon Sep 17 00:00:00 2001 From: Navaneet Villodi <11260095+nauaneed@users.noreply.github.com> Date: Thu, 26 Sep 2024 23:39:43 +0530 Subject: [PATCH 2/5] chore(deps): req in pyproject.toml, not setup.py --- pyproject.toml | 34 ++++++++++++++++++++++++++++++++++ setup.py | 13 ------------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5b68acd..5c90bee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,3 +6,37 @@ requires = [ "Cython", "mako" ] + +[project] +name = "cyarray" +dynamic = ["version"] +readme = "README.rst" +license = {file = "LICENSE.txt"} +dependencies = ['numpy'] +description = "A fast, typed, resizable, Cython array." +authors = [ + {name = "Cyarray Developers", email = "pysph-dev@googlegroups.com"} +] +keywords = ["Cython", "array", "resizable"] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Console", + "Intended Audience :: Developers", + "Intended Audience :: Science/Research", + "License :: OSI Approved :: BSD License", + "Natural Language :: English", + "Operating System :: MacOS :: MacOS X", + "Operating System :: Microsoft :: Windows", + "Operating System :: POSIX", + "Operating System :: Unix", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Topic :: Scientific/Engineering", + "Topic :: Scientific/Engineering :: Physics", + "Topic :: Software Development :: Libraries", +] + +[project.optional-dependencies] +docs = ["sphinx"] +tests = ["pytest", "pytest-benchmark[histogram]"] +dev = ["sphinx", "pytest", "pytest-benchmark[histogram]", "cython"] diff --git a/setup.py b/setup.py index 3801e83..721a8c4 100644 --- a/setup.py +++ b/setup.py @@ -85,13 +85,6 @@ def setup_package(): module = os.path.join('cyarray', '__init__.py') exec(compile(open(module).read(), module, 'exec'), info) - # The requirements. - install_requires = [ - 'numpy', 'mako', 'Cython', 'setuptools>=6.0' - ] - tests_require = ["pytest", "pytest-benchmark[histogram]"] - docs_require = ["sphinx"] - ext_modules = get_basic_extensions() if MODE != 'info' and _is_cythonize_default(): # Cython >= 0.25 uses cythonize to compile the extensions. This @@ -128,12 +121,6 @@ def setup_package(): ext_modules=ext_modules, include_package_data=True, cmdclass=cmdclass, - install_requires=install_requires, - extras_require={ - "docs": docs_require, - "tests": tests_require, - "dev": docs_require + tests_require, - }, zip_safe=False, platforms=['Linux', 'Mac OS-X', 'Unix', 'Windows'], classifiers=[c.strip() for c in """\ From e242bced0a076884869a3766f33afb7d2bdc84a7 Mon Sep 17 00:00:00 2001 From: Navaneet Villodi <11260095+nauaneed@users.noreply.github.com> Date: Thu, 26 Sep 2024 23:48:53 +0530 Subject: [PATCH 3/5] chore: no more depend on pre-NumPy-1.7 C-API Ref: https://cython.readthedocs.io/en/latest/src/userguide/migrating_to_cy30.html --- cyarray/carray.pyx | 206 ++++++++++++++++++++++++---------------- cyarray/carray.pyx.mako | 62 ++++++++---- 2 files changed, 166 insertions(+), 102 deletions(-) diff --git a/cyarray/carray.pyx b/cyarray/carray.pyx index 29cf108..dbf770f 100644 --- a/cyarray/carray.pyx +++ b/cyarray/carray.pyx @@ -3,6 +3,7 @@ # To make changes modify the source templates (carray.pxd.mako) and regenerate # distutils: language=c++ # cython: embedsignature=True, language_level=3 +# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION """ Implementation of resizeable arrays of different types in Cython. @@ -58,6 +59,10 @@ cdef extern from "numpy/arrayobject.h": np.ndarray PyArray_SimpleNewFromData(int, np.npy_intp*, int, void*) + void *PyArray_DATA(np.ndarray arr) noexcept nogil + + np.npy_intp *PyArray_DIMS(np.ndarray arr) noexcept nogil + # memcpy cdef extern from "stdlib.h": @@ -68,7 +73,7 @@ cdef extern from "stdlib.h": cdef extern from *: """ static void PyArray_SET_DATA(PyArrayObject *arr, char * data) { - arr->data = data; + ((PyArrayObject_fields *)arr)->data = data; } """ void PyArray_SET_DATA(np.ndarray arr, char * data) noexcept nogil @@ -161,9 +166,10 @@ cdef class BaseArray: pass cdef void c_reset(self) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *dims self.length = 0 - arr.dimensions[0] = self.length + dims = PyArray_DIMS(self._npy_array) + dims[0] = self.length cdef void c_resize(self, long size) noexcept nogil: pass @@ -203,11 +209,19 @@ cdef class BaseArray: """ cdef PyArrayObject* sarr = nparr cdef PyArrayObject* darr = self._npy_array + cdef np.npy_intp *s_dims, *d_dims + cdef void *s_data, *d_data + + s_data = PyArray_DATA(nparr) + d_data = PyArray_DATA(self._npy_array) + + s_dims = PyArray_DIMS(nparr) + d_dims = PyArray_DIMS(self._npy_array) - if sarr.data == darr.data: + if s_data == d_data: return - elif sarr.dimensions[0] <= darr.dimensions[0]: - self._npy_array[:sarr.dimensions[0]] = nparr + elif s_dims[0] <= d_dims[0]: + self._npy_array[:s_dims[0]] = nparr else: raise ValueError, 'array size mismatch' @@ -426,7 +440,7 @@ cdef class IntArray(BaseArray): cdef void c_append(self, int value) noexcept nogil: cdef long l = self.length - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if l >= self.alloc: self.c_reserve(l*2) @@ -434,10 +448,10 @@ cdef class IntArray(BaseArray): self.length += 1 # update the numpy arrays length - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_reserve(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array cdef void* data = NULL if size > self.alloc: data = aligned_realloc( @@ -452,7 +466,7 @@ cdef class IntArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cdef void c_reset(self) noexcept nogil: BaseArray.c_reset(self) @@ -462,26 +476,29 @@ cdef class IntArray(BaseArray): PyArray_SET_DATA(self._npy_array, self.data) cdef void c_resize(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims # reserve memory self.c_reserve(size) # update the lengths self.length = size - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_set_view(self, int *array, long length) noexcept nogil: """Create a view of a given raw data pointer with given length. """ + cdef np.npy_intp *arr_dims + if self._old_data == NULL: self._old_data = self.data self.data = array self.length = length - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_squeeze(self) noexcept nogil: cdef PyArrayObject* arr = self._npy_array @@ -500,7 +517,7 @@ cdef class IntArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cpdef str get_c_type(self): @@ -567,14 +584,16 @@ cdef class IntArray(BaseArray): itself. """ + cdef np.npy_intp *arr_dims + if self._parent is None: self._old_data = self.data self._parent = parent self.data = parent.data + start self.length = end - start - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef squeeze(self): """Release any unused memory. @@ -617,7 +636,7 @@ cdef class IntArray(BaseArray): cdef long inlength = index_list.size cdef np.ndarray sorted_indices cdef long id - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if inlength > self.length: return @@ -640,7 +659,8 @@ cdef class IntArray(BaseArray): for j in range(stride): self.data[id + j] = self.data[self.length - stride + j] self.length = self.length - stride - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. @@ -931,7 +951,7 @@ cdef class UIntArray(BaseArray): cdef void c_append(self, unsigned int value) noexcept nogil: cdef long l = self.length - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if l >= self.alloc: self.c_reserve(l*2) @@ -939,10 +959,10 @@ cdef class UIntArray(BaseArray): self.length += 1 # update the numpy arrays length - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_reserve(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array cdef void* data = NULL if size > self.alloc: data = aligned_realloc( @@ -957,7 +977,7 @@ cdef class UIntArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cdef void c_reset(self) noexcept nogil: BaseArray.c_reset(self) @@ -967,26 +987,29 @@ cdef class UIntArray(BaseArray): PyArray_SET_DATA(self._npy_array, self.data) cdef void c_resize(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims # reserve memory self.c_reserve(size) # update the lengths self.length = size - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_set_view(self, unsigned int *array, long length) noexcept nogil: """Create a view of a given raw data pointer with given length. """ + cdef np.npy_intp *arr_dims + if self._old_data == NULL: self._old_data = self.data self.data = array self.length = length - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_squeeze(self) noexcept nogil: cdef PyArrayObject* arr = self._npy_array @@ -1005,7 +1028,7 @@ cdef class UIntArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cpdef str get_c_type(self): @@ -1072,14 +1095,16 @@ cdef class UIntArray(BaseArray): itself. """ + cdef np.npy_intp *arr_dims + if self._parent is None: self._old_data = self.data self._parent = parent self.data = parent.data + start self.length = end - start - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef squeeze(self): """Release any unused memory. @@ -1122,7 +1147,7 @@ cdef class UIntArray(BaseArray): cdef long inlength = index_list.size cdef np.ndarray sorted_indices cdef long id - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if inlength > self.length: return @@ -1145,7 +1170,8 @@ cdef class UIntArray(BaseArray): for j in range(stride): self.data[id + j] = self.data[self.length - stride + j] self.length = self.length - stride - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. @@ -1436,7 +1462,7 @@ cdef class LongArray(BaseArray): cdef void c_append(self, long value) noexcept nogil: cdef long l = self.length - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if l >= self.alloc: self.c_reserve(l*2) @@ -1444,10 +1470,10 @@ cdef class LongArray(BaseArray): self.length += 1 # update the numpy arrays length - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_reserve(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array cdef void* data = NULL if size > self.alloc: data = aligned_realloc( @@ -1462,7 +1488,7 @@ cdef class LongArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cdef void c_reset(self) noexcept nogil: BaseArray.c_reset(self) @@ -1472,26 +1498,29 @@ cdef class LongArray(BaseArray): PyArray_SET_DATA(self._npy_array, self.data) cdef void c_resize(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims # reserve memory self.c_reserve(size) # update the lengths self.length = size - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_set_view(self, long *array, long length) noexcept nogil: """Create a view of a given raw data pointer with given length. """ + cdef np.npy_intp *arr_dims + if self._old_data == NULL: self._old_data = self.data self.data = array self.length = length - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_squeeze(self) noexcept nogil: cdef PyArrayObject* arr = self._npy_array @@ -1510,7 +1539,7 @@ cdef class LongArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cpdef str get_c_type(self): @@ -1577,14 +1606,16 @@ cdef class LongArray(BaseArray): itself. """ + cdef np.npy_intp *arr_dims + if self._parent is None: self._old_data = self.data self._parent = parent self.data = parent.data + start self.length = end - start - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef squeeze(self): """Release any unused memory. @@ -1627,7 +1658,7 @@ cdef class LongArray(BaseArray): cdef long inlength = index_list.size cdef np.ndarray sorted_indices cdef long id - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if inlength > self.length: return @@ -1650,7 +1681,8 @@ cdef class LongArray(BaseArray): for j in range(stride): self.data[id + j] = self.data[self.length - stride + j] self.length = self.length - stride - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. @@ -1941,7 +1973,7 @@ cdef class FloatArray(BaseArray): cdef void c_append(self, float value) noexcept nogil: cdef long l = self.length - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if l >= self.alloc: self.c_reserve(l*2) @@ -1949,10 +1981,10 @@ cdef class FloatArray(BaseArray): self.length += 1 # update the numpy arrays length - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_reserve(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array cdef void* data = NULL if size > self.alloc: data = aligned_realloc( @@ -1967,7 +1999,7 @@ cdef class FloatArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cdef void c_reset(self) noexcept nogil: BaseArray.c_reset(self) @@ -1977,26 +2009,29 @@ cdef class FloatArray(BaseArray): PyArray_SET_DATA(self._npy_array, self.data) cdef void c_resize(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims # reserve memory self.c_reserve(size) # update the lengths self.length = size - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_set_view(self, float *array, long length) noexcept nogil: """Create a view of a given raw data pointer with given length. """ + cdef np.npy_intp *arr_dims + if self._old_data == NULL: self._old_data = self.data self.data = array self.length = length - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_squeeze(self) noexcept nogil: cdef PyArrayObject* arr = self._npy_array @@ -2015,7 +2050,7 @@ cdef class FloatArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cpdef str get_c_type(self): @@ -2082,14 +2117,16 @@ cdef class FloatArray(BaseArray): itself. """ + cdef np.npy_intp *arr_dims + if self._parent is None: self._old_data = self.data self._parent = parent self.data = parent.data + start self.length = end - start - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef squeeze(self): """Release any unused memory. @@ -2132,7 +2169,7 @@ cdef class FloatArray(BaseArray): cdef long inlength = index_list.size cdef np.ndarray sorted_indices cdef long id - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if inlength > self.length: return @@ -2155,7 +2192,8 @@ cdef class FloatArray(BaseArray): for j in range(stride): self.data[id + j] = self.data[self.length - stride + j] self.length = self.length - stride - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. @@ -2446,7 +2484,7 @@ cdef class DoubleArray(BaseArray): cdef void c_append(self, double value) noexcept nogil: cdef long l = self.length - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if l >= self.alloc: self.c_reserve(l*2) @@ -2454,10 +2492,10 @@ cdef class DoubleArray(BaseArray): self.length += 1 # update the numpy arrays length - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_reserve(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array cdef void* data = NULL if size > self.alloc: data = aligned_realloc( @@ -2472,7 +2510,7 @@ cdef class DoubleArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cdef void c_reset(self) noexcept nogil: BaseArray.c_reset(self) @@ -2482,26 +2520,29 @@ cdef class DoubleArray(BaseArray): PyArray_SET_DATA(self._npy_array, self.data) cdef void c_resize(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims # reserve memory self.c_reserve(size) # update the lengths self.length = size - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_set_view(self, double *array, long length) noexcept nogil: """Create a view of a given raw data pointer with given length. """ + cdef np.npy_intp *arr_dims + if self._old_data == NULL: self._old_data = self.data self.data = array self.length = length - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_squeeze(self) noexcept nogil: cdef PyArrayObject* arr = self._npy_array @@ -2520,7 +2561,7 @@ cdef class DoubleArray(BaseArray): self.data = data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cpdef str get_c_type(self): @@ -2587,14 +2628,16 @@ cdef class DoubleArray(BaseArray): itself. """ + cdef np.npy_intp *arr_dims + if self._parent is None: self._old_data = self.data self._parent = parent self.data = parent.data + start self.length = end - start - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef squeeze(self): """Release any unused memory. @@ -2637,7 +2680,7 @@ cdef class DoubleArray(BaseArray): cdef long inlength = index_list.size cdef np.ndarray sorted_indices cdef long id - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if inlength > self.length: return @@ -2660,7 +2703,8 @@ cdef class DoubleArray(BaseArray): for j in range(stride): self.data[id + j] = self.data[self.length - stride + j] self.length = self.length - stride - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. diff --git a/cyarray/carray.pyx.mako b/cyarray/carray.pyx.mako index cf75c31..5f44ebb 100644 --- a/cyarray/carray.pyx.mako +++ b/cyarray/carray.pyx.mako @@ -12,6 +12,7 @@ type_info = [ # To make changes modify the source templates (carray.pxd.mako) and regenerate # distutils: language=c++ # cython: embedsignature=True, language_level=3 +# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION """ Implementation of resizeable arrays of different types in Cython. @@ -67,6 +68,10 @@ cdef extern from "numpy/arrayobject.h": np.ndarray PyArray_SimpleNewFromData(int, np.npy_intp*, int, void*) + void *PyArray_DATA(np.ndarray arr) noexcept nogil + + np.npy_intp *PyArray_DIMS(np.ndarray arr) noexcept nogil + # memcpy cdef extern from "stdlib.h": @@ -77,7 +82,7 @@ cdef extern from "stdlib.h": cdef extern from *: """ static void PyArray_SET_DATA(PyArrayObject *arr, char * data) { - arr->data = data; + ((PyArrayObject_fields *)arr)->data = data; } """ void PyArray_SET_DATA(np.ndarray arr, char * data) noexcept nogil @@ -171,9 +176,10 @@ cdef class BaseArray: pass cdef void c_reset(self) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *dims self.length = 0 - arr.dimensions[0] = self.length + dims = PyArray_DIMS(self._npy_array) + dims[0] = self.length cdef void c_resize(self, long size) noexcept nogil: pass @@ -214,11 +220,19 @@ cdef class BaseArray: """ cdef PyArrayObject* sarr = nparr cdef PyArrayObject* darr = self._npy_array + cdef np.npy_intp *s_dims, *d_dims + cdef void *s_data, *d_data + + s_data = PyArray_DATA(nparr) + d_data = PyArray_DATA(self._npy_array) + + s_dims = PyArray_DIMS(nparr) + d_dims = PyArray_DIMS(self._npy_array) - if sarr.data == darr.data: + if s_data == d_data: return - elif sarr.dimensions[0] <= darr.dimensions[0]: - self._npy_array[:sarr.dimensions[0]] = nparr + elif s_dims[0] <= d_dims[0]: + self._npy_array[:s_dims[0]] = nparr else: raise ValueError, 'array size mismatch' @@ -439,7 +453,7 @@ cdef class ${CLASSNAME}(BaseArray): cdef void c_append(self, ${ARRAY_TYPE} value) noexcept nogil: cdef long l = self.length - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if l >= self.alloc: self.c_reserve(l*2) @@ -447,10 +461,10 @@ cdef class ${CLASSNAME}(BaseArray): self.length += 1 # update the numpy arrays length - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_reserve(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array cdef void* data = NULL if size > self.alloc: data = <${ARRAY_TYPE}*>aligned_realloc( @@ -465,7 +479,7 @@ cdef class ${CLASSNAME}(BaseArray): self.data = <${ARRAY_TYPE}*>data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) cdef void c_reset(self) noexcept nogil: BaseArray.c_reset(self) @@ -475,26 +489,29 @@ cdef class ${CLASSNAME}(BaseArray): PyArray_SET_DATA(self._npy_array, self.data) cdef void c_resize(self, long size) noexcept nogil: - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims # reserve memory self.c_reserve(size) # update the lengths self.length = size - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_set_view(self, ${ARRAY_TYPE} *array, long length) noexcept nogil: """Create a view of a given raw data pointer with given length. """ + cdef np.npy_intp *arr_dims + if self._old_data == NULL: self._old_data = self.data self.data = array self.length = length - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cdef void c_squeeze(self) noexcept nogil: cdef PyArrayObject* arr = self._npy_array @@ -513,7 +530,7 @@ cdef class ${CLASSNAME}(BaseArray): self.data = <${ARRAY_TYPE}*>data self.alloc = size - arr.data = self.data + PyArray_SET_DATA(self._npy_array, self.data) ##### Python protocol ###################################### @@ -581,14 +598,16 @@ cdef class ${CLASSNAME}(BaseArray): itself. """ + cdef np.npy_intp *arr_dims + if self._parent is None: self._old_data = self.data self._parent = parent self.data = parent.data + start self.length = end - start - cdef PyArrayObject* arr = self._npy_array - arr.data = self.data - arr.dimensions[0] = self.length + PyArray_SET_DATA(self._npy_array, self.data) + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef squeeze(self): """Release any unused memory. @@ -631,7 +650,7 @@ cdef class ${CLASSNAME}(BaseArray): cdef long inlength = index_list.size cdef np.ndarray sorted_indices cdef long id - cdef PyArrayObject* arr = self._npy_array + cdef np.npy_intp *arr_dims if inlength > self.length: return @@ -654,7 +673,8 @@ cdef class ${CLASSNAME}(BaseArray): for j in range(stride): self.data[id + j] = self.data[self.length - stride + j] self.length = self.length - stride - arr.dimensions[0] = self.length + arr_dims = PyArray_DIMS(self._npy_array) + arr_dims[0] = self.length cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. From 7aabc02730f1f169a19c0adc262814510c8a12b8 Mon Sep 17 00:00:00 2001 From: Navaneet Villodi <11260095+nauaneed@users.noreply.github.com> Date: Fri, 27 Sep 2024 00:24:45 +0530 Subject: [PATCH 4/5] chore: update `raise` syntax --- cyarray/carray.pyx | 2 +- cyarray/carray.pyx.mako | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cyarray/carray.pyx b/cyarray/carray.pyx index dbf770f..b6c7bc1 100644 --- a/cyarray/carray.pyx +++ b/cyarray/carray.pyx @@ -223,7 +223,7 @@ cdef class BaseArray: elif s_dims[0] <= d_dims[0]: self._npy_array[:s_dims[0]] = nparr else: - raise ValueError, 'array size mismatch' + raise ValueError('array size mismatch') cpdef squeeze(self): """Release any unused memory. diff --git a/cyarray/carray.pyx.mako b/cyarray/carray.pyx.mako index 5f44ebb..03f92f8 100644 --- a/cyarray/carray.pyx.mako +++ b/cyarray/carray.pyx.mako @@ -192,17 +192,17 @@ cdef class BaseArray: cpdef str get_c_type(self): """Return the c data type of this array. """ - raise NotImplementedError, 'BaseArray::get_c_type' + raise NotImplementedError('BaseArray::get_c_type') cpdef reserve(self, long size): """Resizes the internal data to required size. """ - raise NotImplementedError, 'BaseArray::reserve' + raise NotImplementedError(BaseArray::reserve') cpdef resize(self, long size): """Resizes the array to the new size. """ - raise NotImplementedError, 'BaseArray::resize' + raise NotImplementedError(BaseArray::resize) cpdef np.ndarray get_npy_array(self): """Returns a numpy array of the data: do not keep its reference. @@ -234,22 +234,22 @@ cdef class BaseArray: elif s_dims[0] <= d_dims[0]: self._npy_array[:s_dims[0]] = nparr else: - raise ValueError, 'array size mismatch' + raise ValueError('array size mismatch') cpdef squeeze(self): """Release any unused memory. """ - raise NotImplementedError, 'BaseArray::squeeze' + raise NotImplementedError('BaseArray::squeeze') cpdef remove(self, np.ndarray index_list, bint input_sorted=0, int stride=1): """Remove the particles with indices in index_list. """ - raise NotImplementedError, 'BaseArray::remove' + raise NotImplementedError('BaseArray::remove') cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. """ - raise NotImplementedError, 'BaseArray::extend' + raise NotImplementedError('BaseArray::extend') cpdef align_array(self, LongArray new_indices, int stride=1): """Rearrange the array contents according to the new indices. @@ -261,23 +261,23 @@ cdef class BaseArray: cpdef reset(self): """Reset the length of the array to 0. """ - raise NotImplementedError, 'BaseArray::reset' + raise NotImplementedError('BaseArray::reset') cpdef copy_values(self, LongArray indices, BaseArray dest, int stride=1, int start=0): """Copy values of indexed particles from self to dest. """ - raise NotImplementedError, 'BaseArray::copy_values' + raise NotImplementedError('BaseArray::copy_values') cpdef copy_subset(self, BaseArray source, long start_index=-1, long end_index=-1, int stride=1): """Copy subset of values from source to self. """ - raise NotImplementedError, 'BaseArray::copy_subset' + raise NotImplementedError('BaseArray::copy_subset') cpdef update_min_max(self): """Update the min and max values of the array. """ - raise NotImplementedError, 'BaseArray::update_min_max' + raise NotImplementedError('BaseArray::update_min_max') def __len__(self): return self.length From 54ae678e01b99ae7ffd069539160bddbc104726a Mon Sep 17 00:00:00 2001 From: navaneet Date: Fri, 13 Dec 2024 19:40:43 +0530 Subject: [PATCH 5/5] fix(carray.pyx): silly errors --- cyarray/carray.pyx | 20 ++++++++++---------- cyarray/carray.pyx.mako | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cyarray/carray.pyx b/cyarray/carray.pyx index b6c7bc1..808764a 100644 --- a/cyarray/carray.pyx +++ b/cyarray/carray.pyx @@ -181,17 +181,17 @@ cdef class BaseArray: cpdef str get_c_type(self): """Return the c data type of this array. """ - raise NotImplementedError, 'BaseArray::get_c_type' + raise NotImplementedError('BaseArray::get_c_type') cpdef reserve(self, long size): """Resizes the internal data to required size. """ - raise NotImplementedError, 'BaseArray::reserve' + raise NotImplementedError('BaseArray::reserve') cpdef resize(self, long size): """Resizes the array to the new size. """ - raise NotImplementedError, 'BaseArray::resize' + raise NotImplementedError('BaseArray::resize') cpdef np.ndarray get_npy_array(self): """Returns a numpy array of the data: do not keep its reference. @@ -228,17 +228,17 @@ cdef class BaseArray: cpdef squeeze(self): """Release any unused memory. """ - raise NotImplementedError, 'BaseArray::squeeze' + raise NotImplementedError('BaseArray::squeeze') cpdef remove(self, np.ndarray index_list, bint input_sorted=0, int stride=1): """Remove the particles with indices in index_list. """ - raise NotImplementedError, 'BaseArray::remove' + raise NotImplementedError('BaseArray::remove') cpdef extend(self, np.ndarray in_array): """Extend the array with data from in_array. """ - raise NotImplementedError, 'BaseArray::extend' + raise NotImplementedError('BaseArray::extend') cpdef align_array(self, LongArray new_indices, int stride=1): """Rearrange the array contents according to the new indices. @@ -250,23 +250,23 @@ cdef class BaseArray: cpdef reset(self): """Reset the length of the array to 0. """ - raise NotImplementedError, 'BaseArray::reset' + raise NotImplementedError('BaseArray::reset') cpdef copy_values(self, LongArray indices, BaseArray dest, int stride=1, int start=0): """Copy values of indexed particles from self to dest. """ - raise NotImplementedError, 'BaseArray::copy_values' + raise NotImplementedError('BaseArray::copy_values') cpdef copy_subset(self, BaseArray source, long start_index=-1, long end_index=-1, int stride=1): """Copy subset of values from source to self. """ - raise NotImplementedError, 'BaseArray::copy_subset' + raise NotImplementedError('BaseArray::copy_subset') cpdef update_min_max(self): """Update the min and max values of the array. """ - raise NotImplementedError, 'BaseArray::update_min_max' + raise NotImplementedError('BaseArray::update_min_max') def __len__(self): return self.length diff --git a/cyarray/carray.pyx.mako b/cyarray/carray.pyx.mako index 03f92f8..058b377 100644 --- a/cyarray/carray.pyx.mako +++ b/cyarray/carray.pyx.mako @@ -197,12 +197,12 @@ cdef class BaseArray: cpdef reserve(self, long size): """Resizes the internal data to required size. """ - raise NotImplementedError(BaseArray::reserve') + raise NotImplementedError('BaseArray::reserve') cpdef resize(self, long size): """Resizes the array to the new size. """ - raise NotImplementedError(BaseArray::resize) + raise NotImplementedError('BaseArray::resize') cpdef np.ndarray get_npy_array(self): """Returns a numpy array of the data: do not keep its reference.