Skip to content

added py::ellipsis() method for slicing of multidimensional NumPy arrays #1502

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 2 commits into from
Aug 28, 2018
Merged
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
20 changes: 20 additions & 0 deletions docs/advanced/pycpp/numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,23 @@ uses of ``py::array``:

The file :file:`tests/test_numpy_array.cpp` contains additional examples
demonstrating the use of this feature.

Ellipsis
========

Python 3 provides a convenient ``...`` ellipsis notation that is often used to
slice multidimensional arrays. For instance, the following snippet extracts the
middle dimensions of a tensor with the first and last index set to zero.

.. code-block:: python

a = # a NumPy array
b = a[0, ..., 0]

The function ``py::ellipsis()`` function can be used to perform the same
operation on the C++ side:

.. code-block:: cpp

py::array a = /* A NumPy array */;
py::array b = a[py::make_tuple(0, py::ellipsis(), 0)];
11 changes: 11 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,9 @@ inline bool PyIterable_Check(PyObject *obj) {
}

inline bool PyNone_Check(PyObject *o) { return o == Py_None; }
#if PY_MAJOR_VERSION >= 3
inline bool PyEllipsis_Check(PyObject *o) { return o == Py_Ellipsis; }
#endif

inline bool PyUnicode_Check_Permissive(PyObject *o) { return PyUnicode_Check(o) || PYBIND11_BYTES_CHECK(o); }

Expand Down Expand Up @@ -967,6 +970,14 @@ class none : public object {
none() : object(Py_None, borrowed_t{}) { }
};

#if PY_MAJOR_VERSION >= 3
class ellipsis : public object {
public:
PYBIND11_OBJECT(ellipsis, object, detail::PyEllipsis_Check)
ellipsis() : object(Py_Ellipsis, borrowed_t{}) { }
};
#endif

class bool_ : public object {
public:
PYBIND11_OBJECT_CVT(bool_, object, PyBool_Check, raw_bool)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def assert_equal_ref(mat):


def assert_sparse_equal_ref(sparse_mat):
assert_equal_ref(sparse_mat.todense())
assert_equal_ref(sparse_mat.toarray())


def test_fixed():
Expand Down
6 changes: 6 additions & 0 deletions tests/test_numpy_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,10 @@ TEST_SUBMODULE(numpy_array, sm) {
std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
return a;
});

#if PY_MAJOR_VERSION >= 3
sm.def("index_using_ellipsis", [](py::array a) {
return a[py::make_tuple(0, py::ellipsis(), 0)];
});
#endif
}
6 changes: 6 additions & 0 deletions tests/test_numpy_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,9 @@ def test_array_create_and_resize(msg):
a = m.create_and_resize(2)
assert(a.size == 4)
assert(np.all(a == 42.))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flake8 error in CI:

./tests/test_numpy_array.py:412:1: E302 expected 2 blank lines, found 1
def test_index_using_ellipsis():


@pytest.unsupported_on_py2
def test_index_using_ellipsis():
a = m.index_using_ellipsis(np.zeros((5, 6, 7)))
assert a.shape == (6,)