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

ndarray equivalent of numpy.astype? #493

Closed
drewm1980 opened this issue Sep 24, 2018 · 4 comments
Closed

ndarray equivalent of numpy.astype? #493

drewm1980 opened this issue Sep 24, 2018 · 4 comments

Comments

@drewm1980
Copy link

drewm1980 commented Sep 24, 2018

How does one convert the type of an array? We're trying to go from an owned Array2<u32> to an owned Array2<usize>.

We've tried to_owned(), into_owned(), and from(), and can't find anything that works. Do we need to resort to iterators for this?

As a documentation feature request, could you please add the answer to the "ndarray_for_numpy_users" page?

The wider context is that we're trying to get our data out of python using the rust-numpy crate:

https://github.com/rust-numpy/rust-numpy

but their example doesn't get the data all the way into an owned ndarray; it's still a view into a custom type wrapping data that is still owned by a python interpreter.

It already took us hours to get this far:

    let a = pyarray.as_array().unwrap();
    let b:ArrayView2<i32> = a.into_dimensionality::<Ix2>().unwrap();
    let c:ndarray::Array2<i32> = b.to_owned();

I'm hoping you can get us the rest of the way there, or maybe even point us to a conversion path that only incurs one copy rather than two...

@jturner314
Copy link
Member

A safe way to perform the conversion is arr.mapv(|elem| elem as usize), which copies the data. Instead of copying the data, you could use unsafe code to perform an in-place cast of the array, but getting it correct without undefined behavior is tricky.

For your specific use-case (getting an array of usize from rust-numpy), I recommend a different approach: add support for usize directly to rust-numpy instead of trying to cast an array of u32 to an array of usize. This avoids the need for a copy and doesn't require any tricky casts. At first glance, it looks like all that's necessary is a few lines at the end of rust-numpy/src/types.rs to implement TypeNum for usize.

@jturner314
Copy link
Member

By the way, depending on what you're trying to do, you may also be interested in ndarray-npy. For example, I like to do my calculations with ndarray and dump the results to .npy/.npz files with ndarray-npy. I can then easily read the results with NumPy and plot with Maptlotlib.

@drewm1980
Copy link
Author

Thanks for the response; we will try mapv out tomorrow. I was expecting a copy anyway since usize is 64 bit on our current target, but maybe the view pyarray returns will work with mapv. Thanks also for the recommendation for ndarray-npy; with a rapidly evolving ecosystem recommendations for what works are always valuable!

@jturner314
Copy link
Member

I was expecting a copy anyway since usize is 64 bit on our current target

Yeah, if the new element type is larger than the old element type, you won't be able to avoid a copy.

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

2 participants