eddelbuettel / rcppcnpy

Rcpp bindings for NumPy files
GNU General Public License v2.0
26 stars 16 forks source link

npySave(): 3D arrays saved as 1D npy objects #32

Closed aloboa closed 4 months ago

aloboa commented 4 months ago
> A <- array(0:23, dim=c(2,3, 4)) * 1.1
> dim(A)
[1] 2 3 4
> npySave("farr.npy", A)
Saving Numeric Vector
> a <- npyLoad("farr.npy", type="numeric", dotranspose=TRUE)
> dim(a)
NULL
eddelbuettel commented 4 months ago

Hm, but please see the package docs (and tests) for examples with explictly shaped / typed NumPy objects. Here your A is not something NumPy (or the CNPy library) knows: it is simply an R vector with a dimension attribute. Where would the Python reshape() call come from?

You could try and see if reticulate helps you. RcppCNPy does not have automatic type converters. We deal with NumPy files primarily. The README is quite explicit: "Vectors and matrices of numeric types can be read or written to and from files as well as compressed files." as the package offers the facilities of the CNPy library.

aloboa commented 4 months ago

I am not sure that I understand you. In principle, I do not see why RcppCNPy should not aim to be able to properly handle 3D arrays. But feel free to close the issue if you think this feature is outside the scope.

eddelbuettel commented 4 months ago

Let me rephrase it: What RcppCNPy offers is what the CNPy library has implemented, namely the reading and writing of vectors and matrices to/from NumPy format. That covers the 1d and 2d case.

Your object is a 3d array. There is no converter in the CNPy code to fall back to, and consequently the RcppCNPy package does not claim to create a NumPy 3d array for you. Sorry.

eddelbuettel commented 4 months ago

Also maybe see #18 and its example and #23 and the section in the vignette that was added. There apparently is read support to recognise a 3d array. You can maybe pass your 3d array via reticulate to Python to obtain a NumPy 3d array. Once you save that from NumPy, I suspect RcppCNpy will be able to read back from disk.