Skip to content

Commit b00cc4e

Browse files
authored
Numpy 2.1+ support (#11)
* pin to numpy < 2.1 * no 3.13 yet * set numpy 2.0 pin * numpy1 * restructure defines and undef NPY_API_SYMBOL_ATTRIBUTE * undo pin * parameterize numpy * doh * exclude 2.1 for python 3.9 * use _import_array as before for numpy 1.x * cleanup and fix osx flat_namespace thing * put back NPY_API_SYMBOL_ATTRIBUTE * set min CMake version to 3.13 * bump ver to 1.6.2
1 parent 9b8199f commit b00cc4e

File tree

10 files changed

+57
-45
lines changed

10 files changed

+57
-45
lines changed

.github/environment.yml

+4
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ dependencies:
88
- compilers
99
- python
1010
- pip
11+
- scikit-build-core
12+
- pybind11
13+
- cmake
14+
- ninja

.github/workflows/build.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ defaults:
1717

1818
jobs:
1919
build:
20-
name: Build ${{ matrix.os }} py${{ matrix.python-version }}
20+
name: ${{ matrix.os }} py${{ matrix.python-version }} numpy${{ matrix.numpy-version }}
2121
runs-on: ${{ matrix.os }}
2222

2323
strategy:
2424
fail-fast: true
2525
matrix:
2626
os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
2727
python-version: ['3.9', '3.10', '3.11', '3.12']
28+
numpy-version: ['1.26', '2.0', '2.1']
29+
exclude:
30+
- python-version: 3.9
31+
numpy-version: 2.1
2832

2933
steps:
3034
- name: Check out
@@ -40,6 +44,10 @@ jobs:
4044
auto-update-conda: true
4145
environment-file: .github/environment.yml
4246

47+
- name: Install Numpy ${{ matrix.numpy-version }}
48+
shell: bash -l {0}
49+
run: |
50+
mamba install numpy=${{ matrix.numpy-version }}
4351
4452
- name: Install
4553
shell: bash -l {0}

CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
cmake_minimum_required(VERSION 3.11.0)
2-
project(pdal-python-plugins)
1+
cmake_minimum_required(VERSION 3.13.0)
32
project(pdal-python-plugins VERSION ${SKBUILD_PROJECT_VERSION}
43
DESCRIPTION "PDAL Python Plugins"
54
HOMEPAGE_URL "https://github.com/PDAL/python-plugins")

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies = [
2929
"numpy >= 1.22"
3030
]
3131

32-
version="1.6.1"
32+
version="1.6.2"
3333

3434
[project.optional-dependencies]
3535
test = [ ]

src/pdal/io/NumpyReader.cpp

+27-25
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include <pdal/util/Algorithm.hpp>
4444
#include <pdal/util/Extractor.hpp>
4545

46-
#include "../plang/Environment.hpp"
46+
4747

4848
#if NPY_ABI_VERSION < 0x02000000
4949
#define PyDataType_FIELDS(descr) ((descr)->fields)
@@ -138,10 +138,13 @@ void NumpyReader::setArray(PyObject* array)
138138
Py_XINCREF(m_array);
139139
}
140140

141+
141142
PyArrayObject* load_npy_file(std::string const& filename)
142143
{
143144

144145
PyObject *py_filename = PyUnicode_FromString(filename.c_str());
146+
if (!py_filename)
147+
throw pdal::pdal_error(plang::getTraceback());
145148
PyObject *numpy_module = PyImport_ImportModule("numpy");
146149
if (!numpy_module)
147150
throw pdal::pdal_error(plang::getTraceback());
@@ -165,7 +168,11 @@ PyArrayObject* load_npy_file(std::string const& filename)
165168
if (!array)
166169
throw pdal::pdal_error(plang::getTraceback());
167170

168-
return reinterpret_cast<PyArrayObject*>(array);
171+
172+
PyArrayObject* nparray = reinterpret_cast<PyArrayObject*>(array);
173+
if (!PyArray_Check(array))
174+
throw pdal_error("Numpy file did not return an array!");
175+
return nparray;
169176
}
170177

171178
PyArrayObject* load_npy_script(std::string const& source,
@@ -196,6 +203,9 @@ PyArrayObject* load_npy_script(std::string const& source,
196203

197204
Py_XDECREF(scriptArgs);
198205

206+
if (!PyArray_Check(array))
207+
throw pdal_error("Numpy script did not return an array!");
208+
199209
return reinterpret_cast<PyArrayObject*>(array);
200210
}
201211

@@ -350,32 +360,24 @@ Dimension::Id NumpyReader::registerDim(PointLayoutPtr layout,
350360
return id;
351361
}
352362

353-
namespace
354-
{
355363

356-
357-
Dimension::Type getType(PyArray_Descr *dtype, const std::string& name)
364+
void NumpyReader::createFields(PointLayoutPtr layout)
358365
{
359-
if (!dtype)
360-
throw pdal_error("Can't fetch data type for numpy field.");
361366

362-
Dimension::Type pdalType =
363-
plang::Environment::getPDALDataType(dtype->type_num);
364-
if (pdalType == Dimension::Type::None)
367+
auto getPDALType = [](int type_num, const std::string& name)
365368
{
366-
std::ostringstream oss;
367-
oss << "Unable to map dimension '" << name << "' because its "
368-
"type '" << dtype->type_num <<"' is not mappable to PDAL";
369-
throw pdal_error(oss.str());
370-
}
371-
return pdalType;
372-
}
373-
374-
} // unnamed namespace
375-
369+
Dimension::Type pdalType =
370+
plang::Environment::getPDALDataType(type_num);
371+
if (pdalType == Dimension::Type::None)
372+
{
373+
std::ostringstream oss;
374+
oss << "Unable to map dimension '" << name << "' because its "
375+
"type '" << type_num <<"' is not mappable to PDAL";
376+
throw pdal_error(oss.str());
377+
}
378+
return pdalType;
379+
};
376380

377-
void NumpyReader::createFields(PointLayoutPtr layout)
378-
{
379381
Dimension::Id id;
380382
Dimension::Type type;
381383
int offset;
@@ -388,7 +390,7 @@ void NumpyReader::createFields(PointLayoutPtr layout)
388390
// Array isn't structured - just a bunch of data.
389391
if (m_numFields <= 0)
390392
{
391-
type = getType(m_dtype, m_defaultDimension);
393+
type = getPDALType(m_dtype->type_num, m_defaultDimension);
392394
id = registerDim(layout, m_defaultDimension, type);
393395
m_fields.push_back({id, type, 0});
394396
}
@@ -415,7 +417,7 @@ void NumpyReader::createFields(PointLayoutPtr layout)
415417

416418
// Get type.
417419
PyArray_Descr* dt = (PyArray_Descr *)PySequence_Fast_GET_ITEM(tup, 0);
418-
type = getType(dt, name);
420+
type = getPDALType(dt->type_num, name);
419421

420422
char byteorder = dt->byteorder;
421423
int elsize = (int) PyDataType_ELSIZE(dt);

src/pdal/io/NumpyReader.hpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@
3737
#include <pdal/Reader.hpp>
3838
#include <pdal/Streamable.hpp>
3939

40+
#include "../plang/Environment.hpp"
4041
#include "../plang/Invocation.hpp"
4142

42-
#include <Python.h>
43-
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
44-
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
45-
#define NO_IMPORT_ARRAY
46-
#include <numpy/arrayobject.h>
43+
#define NO_IMPORT_ARRAY // Already have it from Environment.hpp
44+
#include <numpy/ndarrayobject.h>
4745

4846
#include <memory>
4947

src/pdal/plang/Environment.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@
3939
#include "Environment.hpp"
4040
#include "Redirector.hpp"
4141

42-
#define NPY_TARGET_VERSION NPY_1_22_API_VERSION
43-
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
4442

45-
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
46-
47-
#include <numpy/arrayobject.h>
43+
#include <numpy/ndarrayobject.h>
4844
#include <pdal/util/FileUtils.hpp>
4945
#include <pdal/util/Utils.hpp>
5046

@@ -145,9 +141,12 @@ Environment::Environment()
145141
// the return.
146142
auto initNumpy = []()
147143
{
148-
// #undef NUMPY_IMPORT_ARRAY_RETVAL
149-
// #define NUMPY_IMPORT_ARRAY_RETVAL VOID
144+
145+
#if NPY_ABI_VERSION < 0x02000000
150146
_import_array();
147+
#else
148+
PyArray_ImportNumPyAPI();
149+
#endif
151150
return ;
152151
};
153152

@@ -165,7 +164,6 @@ Environment::Environment()
165164
throw pdal_error("unable to add redirector module!");
166165
}
167166

168-
169167
initNumpy();
170168
PyImport_ImportModule("redirector");
171169

src/pdal/plang/Environment.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
#include "Script.hpp"
5050
#include "gil.hpp"
5151

52+
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_NPARRAY_API
53+
#define NPY_TARGET_VERSION NPY_1_22_API_VERSION
54+
#define NPY_API_SYMBOL_ATTRIBUTE
55+
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
56+
5257
namespace pdal
5358
{
5459
namespace plang

src/pdal/plang/Invocation.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@
3636

3737
#include <pdal/util/Algorithm.hpp>
3838

39-
#define NPY_NO_DEPRECATED_API NPY_1_22_API_VERSION
4039
#define NO_IMPORT_ARRAY
41-
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
42-
#include <numpy/arrayobject.h>
40+
#include <numpy/ndarrayobject.h>
4341

4442
namespace
4543
{

src/pdal/test/data/1.2-with-color.npy

48 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)