Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FlowUtils (and Flowkit) compatible with NumPy >= 2.0 #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[build-system]
requires = ['setuptools>=61.0', 'oldest-supported-numpy']
requires = ['setuptools>=61.0', 'numpy>=2']
build-backend = 'setuptools.build_meta'
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
with open("README.md", "r") as fh:
long_description = fh.read()


logicle_extension = Extension(
'flowutils.logicle_c',
sources=[
'src/flowutils/logicle_c_ext/_logicle.c',
'src/flowutils/logicle_c_ext/logicle.c'
],
include_dirs=[np.get_include(), 'src/flowutils/logicle_c_ext'],
extra_compile_args=['-std=c99']
extra_compile_args=['-std=c99', '-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION']
)

gating_extension = Extension(
Expand All @@ -32,7 +33,7 @@
'src/flowutils/gating_c_ext/gate_helpers.c'
],
include_dirs=[np.get_include(), 'src/flowutils/gating_c_ext'],
extra_compile_args=['-std=c99']
extra_compile_args=['-std=c99', '-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION']
)

setup(
Expand All @@ -49,7 +50,7 @@
license='BSD',
url="https://github.com/whitews/flowutils",
ext_modules=[logicle_extension, gating_extension],
install_requires=['numpy>=1.20,<2'],
install_requires=['numpy>=2.0,<3.0'],
classifiers=[
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.11',
Expand Down
57 changes: 41 additions & 16 deletions src/flowutils/gating_c_ext/_gate_helpers.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION // to avoid a warning
#include <numpy/arrayobject.h>
#include "gate_helpers.h"

Expand All @@ -12,7 +13,11 @@ static PyObject *wrap_calc_wind_count(PyObject *self, PyObject *args) {
return NULL;
}

PyObject *poly_vert_array = PyArray_FROM_OTF(poly_vertices, NPY_DOUBLE, NPY_IN_ARRAY);
PyArrayObject *poly_vert_array = (PyArrayObject *) PyArray_FROM_OTF(poly_vertices, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!poly_vert_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert poly_vertices to NumPy array");
return NULL;
}
double *poly_vertices_c = (double *) PyArray_DATA(poly_vert_array);

// now we can call our function!
Expand All @@ -34,10 +39,18 @@ static PyObject *wrap_points_in_polygon(PyObject *self, PyObject *args) {
return NULL;
}

PyObject *poly_vert_array = PyArray_FROM_OTF(poly_vertices, NPY_DOUBLE, NPY_IN_ARRAY);
PyArrayObject *poly_vert_array = (PyArrayObject *) PyArray_FROM_OTF(poly_vertices, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!poly_vert_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert poly_vertices to NumPy array");
return NULL;
}
double *poly_vertices_c = (double *) PyArray_DATA(poly_vert_array);

PyObject *points_array = PyArray_FROM_OTF(points, NPY_DOUBLE, NPY_IN_ARRAY);
PyArrayObject *points_array = (PyArrayObject *) PyArray_FROM_OTF(points, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!points_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert points to NumPy array");
return NULL;
}
double *points_c = (double *) PyArray_DATA(points_array);

// now we can call our function!
Expand Down Expand Up @@ -67,28 +80,40 @@ static PyMethodDef module_methods[] = {

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef gatingdef = {
PyModuleDef_HEAD_INIT,
"gating_c",
NULL,
-1,
module_methods
PyModuleDef_HEAD_INIT,
"gating_c",
NULL,
-1,
module_methods
};
#endif

#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_gating_c(void) {
PyObject *m = PyModule_Create(&gatingdef);
if (m == NULL) {
return NULL;
}

import_array();
if (PyErr_Occurred()) {
Py_DECREF(m);
return NULL;
}

return m;
}
#else
PyMODINIT_FUNC initgating_c(void) {
PyObject *m = Py_InitModule3("gating_c", module_methods, NULL);
#endif

if (m == NULL) {
return NULL;
return;
}

import_array();
if (PyErr_Occurred()) {
Py_DECREF(m);
return;
}

#if PY_MAJOR_VERSION >= 3
return m;
return;
}
#endif
}
2 changes: 1 addition & 1 deletion src/flowutils/gating_c_ext/gate_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int * points_in_polygon(int *wind_counts, double *poly_vertices, int vert_count,

This implementation is based on the C implementation here:

http://geomalgorithms.com/a03-_inclusion.html
http://geomalgorithms.com/a03-_inclusion.html % This website seems to be a scam now ?

Original copyright notice:
Copyright 2000 softSurfer, 2012 Dan Sunday
Expand Down
81 changes: 43 additions & 38 deletions src/flowutils/logicle_c_ext/_logicle.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION // to avoid a warning
#include <numpy/arrayobject.h>
#include "logicle.h"

Expand All @@ -11,12 +12,10 @@ static PyObject *wrap_logicle_scale(PyObject *self, PyObject *args) {
return NULL;
}

// read the numpy array
PyObject *x_array = PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_IN_ARRAY);

PyArrayObject *x_array = (PyArrayObject *) PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
// throw exception if the array doesn't exist
if (x_array == NULL) {
Py_XDECREF(x_array);
if (!x_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert x to NumPy array");
return NULL;
}

Expand All @@ -29,7 +28,7 @@ static PyObject *wrap_logicle_scale(PyObject *self, PyObject *args) {
// now we can call our function!
logicle_scale(t, w, m, a, xc, n);

return x_array;
return (PyObject *) x_array;
}

static PyObject *wrap_logicle_inverse(PyObject *self, PyObject *args) {
Expand All @@ -42,11 +41,9 @@ static PyObject *wrap_logicle_inverse(PyObject *self, PyObject *args) {
}

// read the numpy array
PyObject *x_array = PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_IN_ARRAY);

// throw exception if the array doesn't exist
if (x_array == NULL) {
Py_XDECREF(x_array);
PyArrayObject *x_array = (PyArrayObject *) PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!x_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert x to NumPy array");
return NULL;
}

Expand All @@ -59,7 +56,7 @@ static PyObject *wrap_logicle_inverse(PyObject *self, PyObject *args) {
// now we can call our function!
logicle_inverse(t, w, m, a, xc, n);

return x_array;
return (PyObject *) x_array;
}

static PyObject *wrap_hyperlog_scale(PyObject *self, PyObject *args) {
Expand All @@ -72,11 +69,9 @@ static PyObject *wrap_hyperlog_scale(PyObject *self, PyObject *args) {
}

// read the numpy array
PyObject *x_array = PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_IN_ARRAY);

// throw exception if the array doesn't exist
if (x_array == NULL) {
Py_XDECREF(x_array);
PyArrayObject *x_array = (PyArrayObject *) PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!x_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert x to NumPy array");
return NULL;
}

Expand All @@ -89,7 +84,7 @@ static PyObject *wrap_hyperlog_scale(PyObject *self, PyObject *args) {
// now we can call our function!
hyperlog_scale(t, w, m, a, xc, n);

return x_array;
return (PyObject *) x_array;
}

static PyObject *wrap_hyperlog_inverse(PyObject *self, PyObject *args) {
Expand All @@ -102,11 +97,9 @@ static PyObject *wrap_hyperlog_inverse(PyObject *self, PyObject *args) {
}

// read the numpy array
PyObject *x_array = PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_IN_ARRAY);

// throw exception if the array doesn't exist
if (x_array == NULL) {
Py_XDECREF(x_array);
PyArrayObject *x_array = (PyArrayObject *) PyArray_FROM_OTF(x, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!x_array) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert x to NumPy array");
return NULL;
}

Expand All @@ -119,7 +112,7 @@ static PyObject *wrap_hyperlog_inverse(PyObject *self, PyObject *args) {
// now we can call our function!
hyperlog_inverse(t, w, m, a, xc, n);

return x_array;
return (PyObject *) x_array;
}


Expand All @@ -133,29 +126,41 @@ static PyMethodDef module_methods[] = {

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef logicledef = {
PyModuleDef_HEAD_INIT,
"logicle_c",
NULL,
-1,
module_methods
PyModuleDef_HEAD_INIT,
"logicle_c",
NULL,
-1,
module_methods
};
#endif

#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_logicle_c(void) {
PyObject *m = PyModule_Create(&logicledef);
if (m == NULL) {
return NULL;
}

import_array();
if (PyErr_Occurred()) {
Py_DECREF(m);
return NULL;
}

return m;
}
#else
PyMODINIT_FUNC initlogicle_c(void) {
PyObject *m = Py_InitModule3("logicle_c", module_methods, NULL);
#endif

if (m == NULL) {
return NULL;
return;
}

import_array();
import_array();
if (PyErr_Occurred()) {
Py_DECREF(m);
return;
}

#if PY_MAJOR_VERSION >= 3
return m;
#endif
return;
}
#endif