Skip to content
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

numpy arrays scalar #1224

Closed
fbriol opened this issue Dec 25, 2017 · 4 comments
Closed

numpy arrays scalar #1224

fbriol opened this issue Dec 25, 2017 · 4 comments

Comments

@fbriol
Copy link

fbriol commented Dec 25, 2017

Hello I need different functions taking as arguments a value of « numpy arrays scalar » type.

It is possible to write prototypes like this:

PYBIND11_MODULE(np_scalar, m) {
    m.def("overloaded", [](py::array_t<double>) {
        return "double";
    });
    m.def("overloaded", [](py::array_t<std::int8_t>) {
        return "int8";
    });
    m.def("overloaded", [](py::array_t<std::int16_t>) {
        return "int16";
    });
}

However, this method does not allow to intercept scalars, for example:

>>> import np_scalar
>>> np_scalar.overloaded(numpy.array([1], dtype='int8'))
'int8'
>>> np_scalar.overloaded(numpy.int8(1))
'double'

How do we intercept numpy arrays scalar?

Thank you very much for your help.

@chhenning
Copy link

I'm not an expert but can you do:

m.def("overloaded", [](py::array a)
{
    auto buf = a.request();
    auto fmt = buf.format;

    if (fmt == "int32")
    {
        return "int32";
    }
    else if (fmt == "float64")
    {
        return "float64";
    }
    else
    {
        return "unknown";
    }
});

@fbriol
Copy link
Author

fbriol commented Dec 25, 2017

This doesn't work because this prototype only accepts array, not scalar.

m.def("overloaded", [](py::array a)
{
    auto buf = a.request();
    auto fmt = buf.format;

    if (fmt == "i")
    {
        return "int32";
    }
    else if (fmt == "h")
    {
        return "int16";
    }
    else
    {
        return "unknown";
    }
});

>>> import np_scalar
>>> np_scalar.overloaded(numpy.array([1], dtype="int32"))
'int32'
>>> np_scalar.overloaded(numpy.int32(1))
.../...
TypeError: overloaded(): incompatible function arguments. The following argument types are 
supported:
    1. (arg0: array) -> str

@vanossj
Copy link
Contributor

vanossj commented Dec 28, 2017

I was just doing a similar thing. It should work works (in numpy >= 1.15.0) with py::buffer instead of py::array
However numpy doesn't expose the buffer info of scalers correctly, numpy/numpy#10265, it always reports them as byte arrays.

m.def("overloaded", [](py::buffer a)
{
    auto buf = a.request();
    if (buf.ndim == 0)
    {
        return "scaler of type " + buf.format;
    }
    else
    {
        return "ndarray of type " + buf.format;
    }
});

Numpy < 1.15.0

>>> import np_scalar
>>> np_scalar.overloaded(numpy.array([1], dtype="int32"))
'ndarray of type i'
>>> np_scalar.overloaded(numpy.int32(1))
'ndarray of type B'

Numpy >= 1.15.0

>>> import np_scalar
>>> np_scalar.overloaded(numpy.array([1], dtype="int32"))
'ndarray of type i'
>>> np_scalar.overloaded(numpy.int32(1))
'scaler of type i'

As a workaround you could use ctypes to specify scalers

>>> import ctypes
>>> np_scalar.overloaded(ctypes.c_int32(1))
'scaler of type <i'

edited for numpy 1.15 release

ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 3, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 3, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 5, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 7, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 12, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
ax3l added a commit to ax3l/openPMD-api that referenced this issue Sep 14, 2018
`set_attribute` requires numpy 1.15.0+ in order to handle
scalars properly.

Refs:
  numpy/numpy#10265
  pybind/pybind11#1224 (comment)
@eacousineau
Copy link
Contributor

@fbriol If you're still monitoring this issue, can you take a gander at the solutions (/ workarounds) in these two issues?

#1776
#1785

Both were for leveraging NumPy to gain access to "off the beaten path" floats, like float128 and float16. One of the workarounds I posted was making a npy_scalar_caster - however, that's only useful if you want to capture a specific type, and it could shadow (or error out) if you try and use it with already supported types like float or double:
https://github.com/eacousineau/repro/blob/5e6acf6/python/pybind11/custom_tests/test_numpy_issue1785.cc#L21-L47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants