Skip to content

view for numpy arrays #987

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

Merged
merged 42 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
6f8f47c
reshape
Aug 6, 2017
a08f012
more tests
Aug 6, 2017
36ed3a1
Update numpy.h
Aug 6, 2017
5fa0335
Update test_numpy_array.py
Aug 6, 2017
63c3c14
array view
Aug 6, 2017
1724c5b
test
Aug 6, 2017
15da69a
Update test_numpy_array.cpp
Aug 6, 2017
29921fd
Merge pull request #1 from ncullen93/npyreshape
Aug 6, 2017
43d0360
Merge branch 'master' into npyview
Aug 6, 2017
49a3b74
Update numpy.h
Aug 6, 2017
fdf05e6
Update numpy.h
Aug 6, 2017
afe7e75
Update test_numpy_array.cpp
Aug 6, 2017
2c11c12
Merge branch 'master' of https://github.com/pybind/pybind11 into npyr…
Skylion007 Jul 15, 2021
0fb238d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 15, 2021
9366583
Merge branch 'master' of https://github.com/pybind/pybind11 into npyr…
Skylion007 Jul 28, 2021
d1ffc0f
Fix merge bug
Skylion007 Jul 28, 2021
612e01e
Merge branch 'npyreshape' of https://github.com/ncullen93/pybind11 in…
Skylion007 Jul 28, 2021
4b31ca7
Make clang-tidy happy
Skylion007 Jul 28, 2021
2a486f5
Add xfail for PyPy
Skylion007 Jul 28, 2021
56663b5
Fix casting issue
Skylion007 Jul 28, 2021
56ac34c
Merge branch 'npyreshape' into npyview
Skylion007 Jul 28, 2021
71c61ab
Fix formatting
Skylion007 Jul 28, 2021
98aed44
Apply clang-tidy
Skylion007 Jul 28, 2021
5e93212
Address reviews on additional tests
Skylion007 Aug 8, 2021
7685e21
Fix ordering
Skylion007 Aug 8, 2021
32c0cef
Do a little more reordering
Skylion007 Aug 8, 2021
521a261
Fix typo
Skylion007 Aug 8, 2021
b4d2094
Try improving tests
Skylion007 Aug 8, 2021
cf143ae
Fix error in reshape
Skylion007 Aug 9, 2021
fb92dc4
Add one more reshape test
Skylion007 Aug 9, 2021
599a707
Merge branch 'npyreshape' into npyview
Skylion007 Aug 9, 2021
36a2d5b
Fix bugs and add test
Skylion007 Aug 9, 2021
2a116f3
Relax test
Skylion007 Aug 9, 2021
127b400
Merge branch 'master' into npyreshape
rwgk Aug 24, 2021
487404b
streamlining new tests; removing a few stray msg
rwgk Aug 24, 2021
bd12951
Merge branch 'npyreshape' into npyview
Skylion007 Aug 26, 2021
6e87f27
Merge branch 'master' of https://github.com/pybind/pybind11 into npyview
Skylion007 Aug 26, 2021
0181034
Fix style revert
Skylion007 Aug 26, 2021
38fffba
Fix clang-tidy
Skylion007 Aug 26, 2021
7e8e5ec
Misc tweaks:
rwgk Aug 26, 2021
4acb4f5
Merge branch 'master' into npyview
rwgk Aug 26, 2021
e6cb8e6
Partial clang-format-diff.
rwgk Aug 26, 2021
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
18 changes: 18 additions & 0 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ struct npy_api {
int (*PyArray_SetBaseObject_)(PyObject *, PyObject *);
PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int, int);
PyObject* (*PyArray_Newshape_)(PyObject*, PyArray_Dims*, int);
PyObject* (*PyArray_View_)(PyObject*, PyObject*, PyObject*);

private:
enum functions {
Expand All @@ -216,6 +217,7 @@ struct npy_api {
API_PyArray_DescrNewFromType = 96,
API_PyArray_Newshape = 135,
API_PyArray_Squeeze = 136,
API_PyArray_View = 137,
API_PyArray_DescrConverter = 174,
API_PyArray_EquivTypes = 182,
API_PyArray_GetArrayParamsFromObject = 278,
Expand Down Expand Up @@ -248,6 +250,7 @@ struct npy_api {
DECL_NPY_API(PyArray_DescrNewFromType);
DECL_NPY_API(PyArray_Newshape);
DECL_NPY_API(PyArray_Squeeze);
DECL_NPY_API(PyArray_View);
DECL_NPY_API(PyArray_DescrConverter);
DECL_NPY_API(PyArray_EquivTypes);
DECL_NPY_API(PyArray_GetArrayParamsFromObject);
Expand Down Expand Up @@ -802,6 +805,21 @@ class array : public buffer {
return new_array;
}

/// Create a view of an array in a different data type.
/// This function may fundamentally reinterpret the data in the array.
/// It is the responsibility of the caller to ensure that this is safe.
/// Only supports the `dtype` argument, the `type` argument is omitted,
/// to be added as needed.
array view(const std::string &dtype) {
auto &api = detail::npy_api::get();
auto new_view = reinterpret_steal<array>(api.PyArray_View_(
m_ptr, dtype::from_args(pybind11::str(dtype)).release().ptr(), nullptr));
if (!new_view) {
throw error_already_set();
}
return new_view;
}

/// Ensure that the argument is a NumPy array
/// In case of an error, nullptr is returned and the Python error is cleared.
static array ensure(handle h, int ExtraFlags = 0) {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_numpy_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ TEST_SUBMODULE(numpy_array, sm) {
return a;
});

sm.def("array_view",
[](py::array_t<uint8_t> a, const std::string &dtype) { return a.view(dtype); });

sm.def("reshape_initializer_list", [](py::array_t<int> a, size_t N, size_t M, size_t O) {
return a.reshape({N, M, O});
});
Expand Down
15 changes: 15 additions & 0 deletions tests/test_numpy_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,21 @@ def test_array_create_and_resize():
assert np.all(a == 42.0)


def test_array_view():
a = np.ones(100 * 4).astype("uint8")
a_float_view = m.array_view(a, "float32")
assert a_float_view.shape == (100 * 1,) # 1 / 4 bytes = 8 / 32

a_int16_view = m.array_view(a, "int16") # 1 / 2 bytes = 16 / 32
assert a_int16_view.shape == (100 * 2,)


def test_array_view_invalid():
a = np.ones(100 * 4).astype("uint8")
with pytest.raises(TypeError):
m.array_view(a, "deadly_dtype")


def test_reshape_initializer_list():
a = np.arange(2 * 7 * 3) + 1
x = m.reshape_initializer_list(a, 2, 7, 3)
Expand Down