Skip to content

Commit 3b534af

Browse files
authored
Merge pull request #89 from TileDB-Inc/refactor-extension
Extension refactoring
2 parents 7a2c362 + 458dd09 commit 3b534af

File tree

15 files changed

+382
-1085
lines changed

15 files changed

+382
-1085
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,27 @@ jobs:
2525
- uses: actions/checkout@v2
2626
- uses: conda-incubator/setup-miniconda@v2
2727
with:
28-
auto-update-conda: true
2928
python-version: ${{ matrix.python-version }}
29+
mamba-version: "*"
3030
activate-environment: test
3131
channels: conda-forge
3232
auto-activate-base: false
3333

3434
- name: Linux dependencies
3535
if: matrix.os == 'ubuntu-latest'
3636
shell: bash -l {0}
37-
run: |
38-
sudo apt-get install gcc-multilib -y
37+
run: sudo apt-get install gcc-multilib -y
3938

4039
- name: Dependencies
4140
shell: bash -l {0}
42-
run: |
43-
conda install --yes --quiet -c conda-forge scikit-build numpy python=${{ matrix.python-version }} compilers cython ninja pdal pytest conda-build -y
41+
run: mamba install --yes --quiet -c conda-forge scikit-build numpy python=${{ matrix.python-version }} cython pdal pytest
4442

4543
- name: Install
4644
shell: bash -l {0}
4745
run: |
4846
python setup.py install -- -G "Ninja" -DWITH_TESTS=ON
49-
5047
pdal --drivers
5148
52-
53-
# pip install . --build-option="-DWITH_TESTS=ON"
54-
5549
- name: Test
5650
shell: bash -l {0}
5751
run: |
@@ -63,7 +57,6 @@ jobs:
6357
$SKPATH/pdal_io_numpy_test
6458
py.test
6559
66-
6760
windows:
6861
name: ${{ matrix.os }} - ${{ matrix.python-version }}
6962

@@ -79,21 +72,17 @@ jobs:
7972
- uses: conda-incubator/setup-miniconda@v2
8073
with:
8174
channels: conda-forge
82-
auto-update-conda: true
8375
python-version: ${{ matrix.python-version }}
8476

85-
8677
- name: Dependencies
8778
shell: cmd /C CALL "{0}"
8879
run: |
89-
9080
call conda activate test
91-
conda install --yes --quiet -c conda-forge scikit-build numpy python=${{ matrix.python-version }} compilers cython ninja pdal pytest conda-build -y
81+
conda install --yes --quiet -c conda-forge scikit-build numpy python=${{ matrix.python-version }} cython pdal pytest
9282
9383
- name: Install
9484
shell: cmd /C CALL "{0}"
9585
run: |
96-
9786
call conda activate test
9887
where python
9988
python setup.py develop -- -G "Ninja" -DWITH_TESTS=ON
@@ -127,13 +116,12 @@ jobs:
127116
- uses: conda-incubator/setup-miniconda@v2
128117
with:
129118
channels: conda-forge
130-
auto-update-conda: true
131119
python-version: ${{ matrix.python-version }}
120+
mamba-version: "*"
132121

133122
- name: Dependencies
134123
shell: bash -l {0}
135-
run: |
136-
conda install --yes --quiet -c conda-forge scikit-build numpy python=${{ matrix.python-version }} compilers cython ninja pdal pytest conda-build -y
124+
run: mamba install --yes --quiet -c conda-forge scikit-build numpy python=${{ matrix.python-version }} cython pdal pytest
137125

138126
- name: sdist
139127
shell: bash -l {0}

pdal/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
set(EXTENSION_SRC
22
PyArray.cpp
33
PyArray.hpp
4-
PyMesh.cpp
5-
PyMesh.hpp
64
PyDimension.hpp
75
PyPipeline.cpp
86
PyPipeline.hpp)

pdal/PyArray.cpp

Lines changed: 5 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
#include "PyArray.hpp"
3636
#include <pdal/io/MemoryViewReader.hpp>
37-
3837
#include <numpy/arrayobject.h>
3938

4039
namespace pdal
@@ -90,11 +89,6 @@ std::string toString(PyObject *pname)
9089

9190
} // unnamed namespace
9291

93-
Array::Array() : m_array(nullptr)
94-
{
95-
if (_import_array() < 0)
96-
throw pdal_error("Could not import numpy.core.multiarray.");
97-
}
9892

9993
Array::Array(PyArrayObject* array) : m_array(array), m_rowMajor(true)
10094
{
@@ -163,131 +157,21 @@ Array::Array(PyArrayObject* array) : m_array(array), m_rowMajor(true)
163157

164158
Array::~Array()
165159
{
166-
if (m_array)
167-
Py_XDECREF((PyObject *)m_array);
160+
Py_XDECREF(m_array);
168161
}
169162

170163

171-
void Array::update(PointViewPtr view)
172-
{
173-
if (m_array)
174-
Py_XDECREF((PyObject *)m_array);
175-
m_array = nullptr; // Just in case of an exception.
176-
177-
Dimension::IdList dims = view->dims();
178-
npy_intp size = view->size();
179-
180-
PyObject *dtype_dict = (PyObject*)buildNumpyDescription(view);
181-
if (!dtype_dict)
182-
throw pdal_error("Unable to build numpy dtype "
183-
"description dictionary");
184-
185-
PyArray_Descr *dtype = nullptr;
186-
if (PyArray_DescrConverter(dtype_dict, &dtype) == NPY_FAIL)
187-
throw pdal_error("Unable to build numpy dtype");
188-
Py_XDECREF(dtype_dict);
189-
190-
// This is a 1 x size array.
191-
m_array = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype,
192-
1, &size, 0, nullptr, NPY_ARRAY_CARRAY, nullptr);
193-
194-
// copy the data
195-
DimTypeList types = view->dimTypes();
196-
for (PointId idx = 0; idx < view->size(); idx++)
197-
{
198-
char *p = (char *)PyArray_GETPTR1(m_array, idx);
199-
view->getPackedPoint(types, idx, p);
200-
}
201-
}
202-
203-
204-
//ABELL - Who's responsible for incrementing the ref count?
205-
PyArrayObject *Array::getPythonArray() const
206-
{
207-
return m_array;
208-
}
209-
210-
PyObject* Array::buildNumpyDescription(PointViewPtr view) const
211-
{
212-
// Build up a numpy dtype dictionary
213-
//
214-
// {'formats': ['f8', 'f8', 'f8', 'u2', 'u1', 'u1', 'u1', 'u1', 'u1',
215-
// 'f4', 'u1', 'u2', 'f8', 'u2', 'u2', 'u2'],
216-
// 'names': ['X', 'Y', 'Z', 'Intensity', 'ReturnNumber',
217-
// 'NumberOfReturns', 'ScanDirectionFlag', 'EdgeOfFlightLine',
218-
// 'Classification', 'ScanAngleRank', 'UserData',
219-
// 'PointSourceId', 'GpsTime', 'Red', 'Green', 'Blue']}
220-
//
221-
222-
Dimension::IdList dims = view->dims();
223-
224-
PyObject* dict = PyDict_New();
225-
PyObject* sizes = PyList_New(dims.size());
226-
PyObject* formats = PyList_New(dims.size());
227-
PyObject* titles = PyList_New(dims.size());
228-
229-
for (size_t i = 0; i < dims.size(); ++i)
230-
{
231-
Dimension::Id id = dims[i];
232-
Dimension::Type t = view->dimType(id);
233-
npy_intp stride = view->dimSize(id);
234-
235-
std::string name = view->dimName(id);
236-
237-
std::string kind("i");
238-
Dimension::BaseType b = Dimension::base(t);
239-
if (b == Dimension::BaseType::Unsigned)
240-
kind = "u";
241-
else if (b == Dimension::BaseType::Signed)
242-
kind = "i";
243-
else if (b == Dimension::BaseType::Floating)
244-
kind = "f";
245-
else
246-
throw pdal_error("Unable to map kind '" + kind +
247-
"' to PDAL dimension type");
248-
249-
std::stringstream oss;
250-
oss << kind << stride;
251-
PyObject* pySize = PyLong_FromLong(stride);
252-
PyObject* pyTitle = PyUnicode_FromString(name.c_str());
253-
PyObject* pyFormat = PyUnicode_FromString(oss.str().c_str());
254-
255-
PyList_SetItem(sizes, i, pySize);
256-
PyList_SetItem(titles, i, pyTitle);
257-
PyList_SetItem(formats, i, pyFormat);
258-
}
259-
260-
PyDict_SetItemString(dict, "names", titles);
261-
PyDict_SetItemString(dict, "formats", formats);
262-
263-
return dict;
264-
}
265-
266-
bool Array::rowMajor() const
267-
{
268-
return m_rowMajor;
269-
}
270-
271-
Array::Shape Array::shape() const
272-
{
273-
return m_shape;
274-
}
275-
276-
const Array::Fields& Array::fields() const
277-
{
278-
return m_fields;
279-
}
280-
281164
ArrayIter& Array::iterator()
282165
{
283-
ArrayIter *it = new ArrayIter(*this);
166+
ArrayIter *it = new ArrayIter(m_array);
284167
m_iterators.push_back(std::unique_ptr<ArrayIter>(it));
285168
return *it;
286169
}
287170

288-
ArrayIter::ArrayIter(Array& array)
171+
172+
ArrayIter::ArrayIter(PyArrayObject* np_array)
289173
{
290-
m_iter = NpyIter_New(array.getPythonArray(),
174+
m_iter = NpyIter_New(np_array,
291175
NPY_ITER_EXTERNAL_LOOP | NPY_ITER_READONLY | NPY_ITER_REFS_OK,
292176
NPY_KEEPORDER, NPY_NO_CASTING, NULL);
293177
if (!m_iter)
@@ -324,16 +208,6 @@ ArrayIter& ArrayIter::operator++()
324208
return *this;
325209
}
326210

327-
ArrayIter::operator bool () const
328-
{
329-
return !m_done;
330-
}
331-
332-
char * ArrayIter::operator * () const
333-
{
334-
return *m_data;
335-
}
336-
337211
} // namespace python
338212
} // namespace pdal
339213

pdal/PyArray.hpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@
3434

3535
#pragma once
3636

37-
#include <numpy/ndarraytypes.h>
38-
3937
#include <pdal/PointView.hpp>
4038
#include <pdal/io/MemoryViewReader.hpp>
41-
42-
#include <utility>
39+
#include <numpy/ndarraytypes.h>
4340

4441
namespace pdal
4542
{
@@ -48,51 +45,42 @@ namespace python
4845

4946
class ArrayIter;
5047

48+
5149
class PDAL_DLL Array
5250
{
5351
public:
5452
using Shape = std::array<size_t, 3>;
5553
using Fields = std::vector<MemoryViewReader::Field>;
5654

57-
// Create an array for reading data from PDAL.
58-
Array();
59-
60-
// Create an array for writing data to PDAL.
6155
Array(PyArrayObject* array);
62-
6356
~Array();
64-
void update(PointViewPtr view);
65-
PyArrayObject *getPythonArray() const;
66-
bool rowMajor() const;
67-
Shape shape() const;
68-
const Fields& fields() const;
69-
ArrayIter& iterator();
7057

58+
bool rowMajor() const { return m_rowMajor; };
59+
Shape shape() const { return m_shape; }
60+
const Fields& fields() const { return m_fields; };
61+
ArrayIter& iterator();
7162

7263
private:
73-
inline PyObject* buildNumpyDescription(PointViewPtr view) const;
74-
75-
7664
PyArrayObject* m_array;
77-
Array& operator=(Array const& rhs);
7865
Fields m_fields;
7966
bool m_rowMajor;
8067
Shape m_shape {};
8168
std::vector<std::unique_ptr<ArrayIter>> m_iterators;
8269
};
8370

71+
8472
class ArrayIter
8573
{
8674
public:
8775
ArrayIter(const ArrayIter&) = delete;
8876
ArrayIter() = delete;
8977

90-
ArrayIter(Array& array);
78+
ArrayIter(PyArrayObject*);
9179
~ArrayIter();
9280

9381
ArrayIter& operator++();
94-
operator bool () const;
95-
char *operator * () const;
82+
operator bool () const { return !m_done; }
83+
char* operator*() const { return *m_data; }
9684

9785
private:
9886
NpyIter *m_iter;

0 commit comments

Comments
 (0)