Skip to content

Conflicting types - Unable to transform a ndarray into a PyArray #168

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

Closed
Matti88 opened this issue Dec 15, 2020 · 8 comments
Closed

Conflicting types - Unable to transform a ndarray into a PyArray #168

Matti88 opened this issue Dec 15, 2020 · 8 comments

Comments

@Matti88
Copy link

Matti88 commented Dec 15, 2020

Hello team. While working with the library I wanted to pass out a PyArray from a function but I am unable to convert a ndarray into a PyArray the following is the error code:

mismatched types

expected struct `ndarray::ArrayBase`, found a different struct `ndarray::ArrayBase`

note: expected struct `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<_>, _>`
         found struct `ndarray::ArrayBase<ndarray::OwnedRepr<i32>, ndarray::Dim<[usize; 2]>>`
note: perhaps two different versions of crate `ndarray` are being used?rustc(E0308)
lib.rs(76, 58): expected struct `ndarray::ArrayBase`, found a different struct `ndarray::ArrayBase`

yielded from the simple code

let mut return_arr  = Array2::<i32>::zeros((10, 10));
let return_Second_ = PyArray::from_owned_array(_py , return_arr);

I was not able to find in ndarray documentation the type:
ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<_>, _>

@kngwyu
Copy link
Member

kngwyu commented Dec 15, 2020

I guess this is because of version mismatch.
What is in your Cargo.toml?

@Matti88
Copy link
Author

Matti88 commented Dec 16, 2020

Hello @kngwyu ,

here is what is in my cargo:

[lib]
name = "algo_match"
crate-type = ["cdylib"]

[dependencies.pyo3]
version = "0.12.4"
features = ["extension-module"]


[dependencies]
polars = "0.9.0"
numpy = "0.12"
ndarray = { version = "0.13", features = ["rayon"] }
num = "0.3.1"

@Matti88
Copy link
Author

Matti88 commented Dec 16, 2020

I guess this is because of version mismatch.
What is in your Cargo.toml?

I guess you would like to see the code for replication?

@relf
Copy link

relf commented Dec 16, 2020

I guess I have the same kind of problem.

error[E0308]: mismatched types
  --> src\lib.rs:18:32
   |
18 |             lhs: Some(LHS::new(&xlimits.as_array().to_owned()).seed(seed)),
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `ndarray::ArrayBase`, found a different struct `ndarray::ArrayBase`
   |
   = note: expected reference `&ndarray::ArrayBase<_, ndarray::dimension::dim::Dim<[usize; 2]>>`
              found reference `&ndarray::ArrayBase, ndarray::dimension::dim::Dim<[usize; 2]>>`
   = note: perhaps two different versions of crate `ndarray` are being used?

error[E0599]: no method named `into_pyarray` found for struct `ndarray::ArrayBase, ndarray::dimension::dim::Dim<[usize; 2]>>` in the current scope
  --> src\lib.rs:28:42
   |
28 |             .map(|m| m.sample(n_samples).into_pyarray(gil.python()).to_owned());
   |                                          ^^^^^^^^^^^^ method not found in `ndarray::ArrayBase, ndarray::dimension::dim::Dim<[usize; 2]>>`

Indeed it seems to be related to ndarray 0.13 vs 0.14, as ndarray-linalg is still with ndarray=0.13 even if I specify 0.13 in my Cargo.toml, it pulls 0.13 and 0.14. I do not understand why 0.14 is installed.

@kngwyu, I've seen the linalg example which works with ndarray=0.14 on a special ndarray-linalg branch of yours.
So I tried to build it with ndarray="0.13" and ndarray-linalg = "0.12" and I get:

(base) D:\rlafage\workspace\rust-numpy\examples\linalg>cargo build
   Compiling autocfg v1.0.1
   Compiling proc-macro2 v1.0.24
   Compiling unicode-xid v0.2.1
   Compiling winapi-x86_64-pc-windows-gnu v0.4.0
   Compiling syn v1.0.54
   Compiling winapi v0.3.9
   Compiling libc v0.2.81
   Compiling proc-macro-hack v0.5.19
   Compiling serde v1.0.118
   Compiling rand_core v0.4.2
   Compiling rawpointer v0.2.1
   Compiling cfg-if v1.0.0
   Compiling inventory v0.1.10
   Compiling smallvec v1.5.1
   Compiling scopeguard v1.1.0
   Compiling unindent v0.1.7
   Compiling pyo3 v0.12.4
   Compiling ndarray v0.13.1
   Compiling ndarray v0.14.0
   Compiling blas-src v0.2.1
   Compiling lapack-src v0.6.0
   Compiling blas-src v0.6.1
   Compiling cfg-if v0.1.10
   Compiling num-traits v0.2.14
   Compiling num-complex v0.2.4
   Compiling num-integer v0.1.44
   Compiling matrixmultiply v0.2.4
   Compiling instant v0.1.9
   Compiling rand_core v0.3.1
   Compiling lock_api v0.4.2
   Compiling cblas-sys v0.1.4
   Compiling lapacke-sys v0.1.4
   Compiling quote v1.0.7
   Compiling num-complex v0.3.1
   Compiling approx v0.3.2
   Compiling paste-impl v0.1.18
   Compiling rand v0.5.6
   Compiling parking_lot_core v0.8.1
   Compiling parking_lot v0.11.1
   Compiling paste v0.1.18
   Compiling lapacke v0.2.0
   Compiling cauchy v0.2.2
   Compiling pyo3-derive-backend v0.12.4
   Compiling ndarray-linalg v0.12.1
   Compiling inventory-impl v0.1.10
   Compiling indoc-impl v0.3.6
   Compiling ctor v0.1.16
   Compiling ghost v0.1.2
   Compiling pyo3cls v0.12.4
   Compiling indoc v0.3.6
   Compiling numpy v0.12.1 (D:\rlafage\workspace\rust-numpy)
   Compiling numpy-linalg-example v0.1.0 (D:\rlafage\workspace\rust-numpy\examples\linalg)
error[E0599]: no method named `inv` found for struct `ndarray::ArrayBase, ndarray::dimension::dim::Dim<[usize; 2]>>` in the current scope
  --> examples\linalg\src\lib.rs:12:14
   |
12 |             .inv()
   |              ^^^ method not found in `ndarray::ArrayBase, ndarray::dimension::dim::Dim<[usize; 2]>>`

warning: unused import: `ndarray_linalg::solve::Inverse`
 --> examples\linalg\src\lib.rs:1:5
  |
1 | use ndarray_linalg::solve::Inverse;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `IntoPyArray`
 --> examples\linalg\src\lib.rs:2:13
  |
2 | use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2};
  |             ^^^^^^^^^^^

error: aborting due to previous error; 2 warnings emitted

For more information about this error, try `rustc --explain E0599`.
error: could not compile `numpy-linalg-example`

To learn more, run the command again with --verbose.

(base) D:\rlafage\workspace\rust-numpy\examples\linalg>

It reproduces the ndarray 0.13 and 0.14 duplicate installation (and fails because of the mixture I suppose)
The modified Cargo.toml is:

[package]
name = "numpy-linalg-example"
version = "0.1.0"
authors = ["Yuji Kanagawa "]
edition = "2018"

[lib]
name = "rust_linalg"
crate-type = ["cdylib"]

[dependencies]
numpy = { path = "../.." }
ndarray = "0.13.1"
# ndarray-linalg = { git = "https://github.com/kngwyu/ndarray-linalg", branch = "ndarray-014", features = ["openblas-static"] }
ndarray-linalg = "0.12"

[dependencies.pyo3]
version = "0.12"
features = ["extension-module"]

So is there a way to make it work with ndarray-linalg=0.12 or should we wait for your pull request to be merged (I tried to use your ndarray-linalg branch but it ends up with several ndarray-linalg versions installation)?
In any case thank you for this project and your help.

@morenol
Copy link

morenol commented Dec 16, 2020

I also had this problem. In my case, I also fixed this by upgrading ndarray to 0.14

smartcorelib/smartcore#39

@Matti88
Copy link
Author

Matti88 commented Dec 17, 2020

I also had this problem. In my case, I also fixed this by upgrading ndarray to 0.14

smartcorelib/smartcore#39

I have performed the same change and it worked just fine. I would consider it solved (somehow)

@relf
Copy link

relf commented Dec 17, 2020

Yes, if you can bump ndarray=0.14 it is all fine but with a ndarray-linalg dependency depending on ndarray=0.13 it is not possible. This topic was also discussed in #165 and from my standpoint, it is still an open issue. At the moment, I consider the example linalg is not really working as it relies on an unofficial ndarray-linalg version. Hopefully ndarray-linalg will catch up with rust-ndarray/ndarray-linalg/pull/258 🙏

@kngwyu
Copy link
Member

kngwyu commented Dec 19, 2020

Thank you @relf @morenol for your helps. I'm now closing this.

is there a way to make it work with ndarray-linalg=0.12

If you have a nightly toolchain, cargo +nightly update -Z minimal-versions would work.
But actually specifying ndarray >= 0.13 was my mistake. I should have used the more restricted version. Sorry about that.

@kngwyu kngwyu closed this as completed Dec 19, 2020
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