How to get ownedrep of ndarray array?

Hi all,

I am reading on how to call rust from python. I am using pyo3 as interface. As a starting point I am trying to send a simple numpy array to the rust function. Code for passing the numpy array to rust function, and corresponding rust function is at here.

Now when I am trying to compile it, I get the following errors,

error[E0277]: the trait bound `numpy::array::PyArray<f64, ndarray::dimension::dim::Dim<[usize; 1]>>: pyo3::conversion::FromPyObject<'_>` is not satisfied
 --> pylib/src/lib.rs:8:1
  |
8 | #[pymodinit]
  | ^^^^^^^^^^^^ the trait `pyo3::conversion::FromPyObject<'_>` is not implemented for `numpy::array::PyArray<f64, ndarray::dimension::dim::Dim<[usize; 1]>>`
  |
  = help: the following implementations were found:
            <&'a numpy::array::PyArray<T, D> as pyo3::conversion::FromPyObject<'a>>
  = note: required by `pyo3::objectprotocol::ObjectProtocol::extract`

error[E0308]: mismatched types
  --> pylib/src/lib.rs:23:19
   |
23 |         sum_array(&data)
   |                   ^^^^^ expected struct `ndarray::OwnedRepr`, found struct `ndarray::ViewRepr`
   |
   = note: expected type `&ndarray::ArrayBase<ndarray::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 1]>>`
              found type `&ndarray::ArrayBase<ndarray::ViewRepr<&f64>, ndarray::dimension::dim::Dim<[usize; 1]>>`

error: aborting due to 2 previous errors

I haven't understood the first error I haven't understood it. Actually it was a copy paste from pointprocess-rust repo. I thought it should work in my case too.

In the second error I don't understand how to get a ownedarray to a pyarray object.

Thank you for the help.

Hi there,

the second issue is quite easily resolved: we may use the to_owned method:

sum_array(&data.to_owned())

However, I'm not sure about the first issue.
PyArray1 should implement FromPyObject.

Hope I could help you at least a bit.
Best,
ambiso

1 Like

Cool. That solves the second issue. Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.