Skip to content

Commit 07fb6f0

Browse files
authored
Add checks for array contiguity (#199)
Fixes #198.
1 parent 6d476e3 commit 07fb6f0

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

xcsf/pybind_wrapper.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ class XCS
340340
// access input
341341
const py::buffer_info buf_x = X.request();
342342
const py::buffer_info buf_y = Y.request();
343+
// check array contiguity
344+
// https://github.com/pybind/pybind11/discussions/4211#discussioncomment-3905115
345+
const int C_CONTIGUOUS =
346+
py::detail::npy_api::constants::NPY_ARRAY_C_CONTIGUOUS_;
347+
if (!(C_CONTIGUOUS == (X.flags() & C_CONTIGUOUS)) ||
348+
!(C_CONTIGUOUS == (Y.flags() & C_CONTIGUOUS))) {
349+
throw std::invalid_argument("X and Y must be C-contiguous");
350+
}
343351
// check input shape
344352
if (buf_x.ndim < 1 || buf_x.ndim > 2) {
345353
throw std::invalid_argument("X must be 1 or 2-D array");
@@ -584,6 +592,13 @@ class XCS
584592
predict(const py::array_t<double> X, const py::object &cover)
585593
{
586594
const py::buffer_info buf_x = X.request();
595+
// check array contiguity
596+
// https://github.com/pybind/pybind11/discussions/4211#discussioncomment-3905115
597+
const int C_CONTIGUOUS =
598+
py::detail::npy_api::constants::NPY_ARRAY_C_CONTIGUOUS_;
599+
if (!(C_CONTIGUOUS == (X.flags() & C_CONTIGUOUS))) {
600+
throw std::invalid_argument("X must be C-contiguous");
601+
}
587602
if (buf_x.ndim < 1 || buf_x.ndim > 2) {
588603
throw std::invalid_argument("predict(): X must be 1 or 2-D array");
589604
}

0 commit comments

Comments
 (0)