Skip to content

Commit

Permalink
Updated _ppgplot.c for Numpy v2.0 C-API and bumped to v5.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
scottransom committed Jun 22, 2024
1 parent 0b7b52a commit 726c9e7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 57 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Version 5.0.2:
* Updated the C wrappers for PGPLOT for the Numpy 2.0 C API
* Python v3.9 or newer is now required.
* Several minor bug fixes, including to `injectpsr.py`, thanks to @remsforian

## Version 5.0.1:
* Minor improvements over v5.0.0
* Some clarifications and improvements to the build process
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ PRESTO is a large suite of pulsar search and analysis software developed primari

**PRESTO has discovered well over 1000 pulsars, including ~400 recycled and/or binary pulsars!**

## Version 5.0.2:
* Updated the C wrappers for PGPLOT for the Numpy 2.0 C API (with thanks to Tom Marsh)
* Python v3.9 or newer is now required.
* Several minor bug fixes, including to `injectpsr.py`, thanks to @remsforian

## Version 5.0.1:
* Minor improvements over v5.0.0
* Some clarifications and improvements to the build process
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('PRESTO', 'c',
version: '5.0.1',
version: '5.0.2',
license: 'GPL-2.0',
default_options: [
'buildtype=release',
Expand Down
13 changes: 1 addition & 12 deletions python/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('PRESTO_python', 'c', 'fortran',
version: '5.0.1',
version: '5.0.2',
license: 'GPL-2.0',
default_options: [
'buildtype=release',
Expand Down Expand Up @@ -41,17 +41,6 @@ incdir_numpy = run_command(py3,
).stdout().strip()

inc_np = include_directories(incdir_numpy)

# Don't use the deprecated NumPy C API. Define this to a fixed version instead of
# NPY_API_VERSION in order not to break compilation for released SciPy versions
# when NumPy introduces a new deprecation. Use in a meson.build file::
#
# py3.extension_module('_name',
# 'source_fname',
# numpy_nodepr_api)
#
numpy_nodepr_api = '-DNPY_NO_DEPRECATED_API=NPY_1_9_API_VERSION'

incdir_f2py = incdir_numpy / '..' / '..' / 'f2py' / 'src'
inc_f2py = include_directories(incdir_f2py)
fortranobject_c = incdir_f2py / 'fortranobject.c'
Expand Down
94 changes: 55 additions & 39 deletions python/ppgplot_src/_ppgplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
* FILE:
* _ppgplot.c
* DESCRIPTION:
* ppgplot: Python / Numeric Python interface to the PGPLOT graphical
* library.
* Tested with PGPLOT 5.2, Python 2.3.1 and Numeric 23.1, on
* Linux (v2.4.x).
* ppgplot: Python / Numeric Python interface to the PGPLOT graphical library.
* Tested with PGPLOT 5.2.2, Python 3.10+, and Numpy 1.2X/2.X on Linux
* AUTHOR(S):
* Nick Patavalis (npat@efault.net)
* Nick Patavalis (npat@efault.net), Tom Marsh, Scott Ransom
* NOTES:
* - A few ppgplot functions have not been interfaced yet.
* - The pythonic calling conventions of some functions are *not*
* identical to the original PGPLOT ones.
* - added pgpt1 15/04/2008 TRM
* - updates to docs and for Numpy 2.0 in June 2024 by SMR
*/

#include <Python.h>
Expand Down Expand Up @@ -74,62 +73,76 @@ tofloatvector (PyObject *o, float **v, npy_intp *vsz)
TRM, 12/02/09. This avoids an irritating deprecation warning from numpy.
*/
PyArrayObject* array = NULL;
array = (PyArrayObject*) PyArray_FromAny(o, PyArray_DescrFromType(NPY_FLOAT), 1, 1, NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_FORCECAST, NULL);
if(array == NULL) return NULL;
*vsz = PyArray_Size(array);
*v = (float*) PyArray_DATA(array);
array = (PyArrayObject*) PyArray_FromAny(o, PyArray_DescrFromType(NPY_FLOAT), 1, 1,
NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_FORCECAST, NULL);
if (array == NULL) return NULL;
*vsz = PyArray_Size((PyArrayObject*) array);
*v = (float*) PyArray_DATA((PyArrayObject*) array);
return (PyObject*)array;
}

/*************************************************************************/

//static PyObject *
//tofloatmatX(PyObject *o, float **m, npy_intp *nr, npy_intp *nc)
//{
// /*
// This is based on the tofloatvector() changes by TRM.
// */
// PyArrayObject* array = NULL;
// array = (PyArrayObject*) PyArray_FromAny(o, PyArray_DescrFromType(NPY_FLOAT), 1, 1,
// NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_FORCECAST, NULL);
// if(array == NULL) return NULL;
// *vsz = PyArray_Size(array);
// *v = (float*) PyArray_DATA(array);
// return (PyObject*)array;
//}

/*************************************************************************/

static PyObject *
tofloatmat(PyObject *o, float **m, int *nr, int *nc)
{
npy_intp newdims[2];
PyArrayObject *a1, *af1, *af2;
int ownedaf1=0;
char **tmpdat;

/* Check if args are arrays. */
if (!PyArray_Check(o)) {
PyErr_SetString(PpgTYPEErr,"object is not an array");
return(NULL);
PyErr_SetString(PpgTYPEErr,"object is not an array");
return(NULL);
}
a1 = (PyArrayObject *)o;
/* Check if args are matrices. */
if (a1->nd != 2) {
PyErr_SetString(PpgTYPEErr,"object is not a matrix");
return(NULL);
PyErr_SetString(PpgTYPEErr,"object is not a matrix");
return(NULL);
}

#ifdef DEBUG_TOARRAY
fprintf(stderr,"(tofloatmat): array type = %d\n",a1->descr->type_num);
#endif

switch (a1->descr->type_num) {
case PyArray_FLOAT:
af1 = a1;
break;
case PyArray_CHAR:
#ifndef USE_NUMARRAY
case PyArray_UBYTE:
#endif
// case PyArray_SBYTE:
case PyArray_SHORT:
case PyArray_INT:
#ifndef USE_NUMARRAY
case PyArray_LONG:
#endif
case PyArray_DOUBLE:
if (!(af1 = (PyArrayObject *)PyArray_Cast(a1,PyArray_FLOAT))) {
PyErr_SetString(PpgTYPEErr,"cannot cast matrix to floats");
return(NULL);
}
ownedaf1 = 1;
break;
case NPY_FLOAT:
af1 = a1;
break;
case NPY_CHAR:
case NPY_UBYTE:
case NPY_SHORT:
case NPY_INT:
case NPY_LONG:
case NPY_DOUBLE:
if (!(af1 = (PyArrayObject *)PyArray_Cast(a1,NPY_FLOAT))) {
PyErr_SetString(PpgTYPEErr,"cannot cast matrix to floats");
return(NULL);
}
ownedaf1 = 1;
break;
default:
PyErr_SetString(PpgTYPEErr,"cannot cast matrix to floats");
return(NULL);
PyErr_SetString(PpgTYPEErr,"cannot cast matrix to floats");
return(NULL);
break;
}

Expand All @@ -139,11 +152,14 @@ tofloatmat(PyObject *o, float **m, int *nr, int *nc)

af2 = af1;
/* (void *) avoids irritating gcc warning about strict aliasing */
if (PyArray_As2D((PyObject **)(void *)&af2, (char ***)&tmpdat, nr, nc, PyArray_FLOAT) == -1) {
af2 = NULL;
goto bailout;
if (PyArray_AsCArray((PyObject **)(void *)&af2, (char ***)&tmpdat,
newdims, 2, PyArray_DescrFromType(NPY_FLOAT)) == -1) {
af2 = NULL;
goto bailout;
}

*nr = (int ) newdims[0];
*nc = (int ) newdims[1];

/* WARNING: What follows is a little tricky and I dunno if I'm
really allowed to do this. On the other hand it really conserves
time and memory! So this assert statement will make sure that
Expand Down
2 changes: 0 additions & 2 deletions python/ppgplot_src/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Note: will need to fix Numpy API deprecation soon -SMR
# c_args: [numpy_nodepr_api, '-Wno-unused-variable'],
py3.extension_module('_ppgplot', '_ppgplot.c',
include_directories: inc_np,
dependencies : [py3_dep, libm, pgplot, cpgplot, x11, png],
Expand Down
6 changes: 3 additions & 3 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[build-system]
build-backend = 'mesonpy'
requires = ['meson-python', 'numpy<2.0']
requires = ['meson-python', 'numpy']

[project]
name = 'presto'
version = '5.0.1'
version = '5.0.2'
description = 'PulsaR Exploration and Search TOolkit'
requires-python = '>=3.8'
requires-python = '>=3.9'
dependencies = ['numpy', 'scipy', 'astropy', 'matplotlib']
authors = [
{name = 'Scott Ransom', email = 'sransom@nrao.edu'},
Expand Down

0 comments on commit 726c9e7

Please sign in to comment.