| Title: | Read 'NumPy' '.npy' and '.npz' Files |
|---|---|
| Description: | Lightweight way to read 'NumPy' '.npy' and '.npz' files in R. All data types supported by 'NumPy', with all sizes (converted internally to R native size), both C and 'Fortran' order, and any shape, up to an arbitrary number of dimensions, are supported. |
| Authors: | Hugo Gruson [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-4094-1476>), Mike Smith [aut, cph] (Original author of portions of the C code migrated from the Rarr package), German Network for Bioinformatics Infrastructure - de.NBI [fnd] |
| Maintainer: | Hugo Gruson <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1.9004 |
| Built: | 2026-10-12 23:10:55 UTC |
| Source: | https://github.com/Bisaloo/grumpy |
This is a replacement for readBin() that can handle the various data types
and endianness specified in the .npy file header.
convert_bytes_to_array(bytes, what, shape, size, endian)convert_bytes_to_array(bytes, what, shape, size, endian)
bytes |
A raw vector containing the bytes to convert |
what |
A character specifying the base type to convert to (e.g.,
|
shape |
A numeric vector with desired shape of the output array |
size |
A numeric value with the number of bytes per element for the specified type |
endian |
The endianness of the data ( |
An R array containing the converted data, with the specified shape and data type.
x <- matrix(c(3L, 6L, 2L, 1L, 12L, 0L), nrow = 2, ncol = 3) x y <- writeBin(c(x), raw()) |> convert_bytes_to_array( "int", shape = c(2L, 3L), size = 4L, endian = "little" ) y dim(y) is.array(y) storage.mode(y)x <- matrix(c(3L, 6L, 2L, 1L, 12L, 0L), nrow = 2, ncol = 3) x y <- writeBin(c(x), raw()) |> convert_bytes_to_array( "int", shape = c(2L, 3L), size = 4L, endian = "little" ) y dim(y) is.array(y) storage.mode(y)
Parse a NumPy Array-protocol type string
parse_npy_datatype(descr)parse_npy_datatype(descr)
descr |
A NumPy dtype description string, or a list of such strings for structured dtypes |
If a list is passed to descr, each element can be of length 1, or of
length 2 in which case the first element corresponds to the name of the field
and the second to its dtype.
A list containing the parsed data type information, including the base type, the number of bytes, and the endianness
parse_npy_datatype(">i8") parse_npy_datatype("|b1") # A structured datatype where each element has 3 components, all integers, # named "r", "g" and "b". parse_npy_datatype(list(c("r", "<i8"), c("g", "<i8"), c("b", "<i8")))parse_npy_datatype(">i8") parse_npy_datatype("|b1") # A structured datatype where each element has 3 components, all integers, # named "r", "g" and "b". parse_npy_datatype(list(c("r", "<i8"), c("g", "<i8"), c("b", "<i8")))
.npy fileRead a .npy file
read_npy(file, lazy = FALSE, ...)read_npy(file, lazy = FALSE, ...)
file |
Path to the |
lazy |
If |
... |
Ignored. Reserved for future use. |
An array containing the data from the .npy file
# Array of integers. NumPy "<i4" dtype read_npy( system.file("extdata", "test_int32.npy", package = "grumpy") ) # Array of logicals. NumPy "|b1" dtype read_npy( system.file("extdata", "test_bool.npy", package = "grumpy") ) # Array of numerics. NumPy "<f8" dtype read_npy( system.file("extdata", "test_float64.npy", package = "grumpy") ) # Array of strings. NumPy "<U" dtype read_npy( system.file("extdata", "test_str_unicode.npy", package = "grumpy") ) # "Array" of lists. Numpy structured dtype read_npy( system.file("extdata", "test_structured.npy", package = "grumpy") )# Array of integers. NumPy "<i4" dtype read_npy( system.file("extdata", "test_int32.npy", package = "grumpy") ) # Array of logicals. NumPy "|b1" dtype read_npy( system.file("extdata", "test_bool.npy", package = "grumpy") ) # Array of numerics. NumPy "<f8" dtype read_npy( system.file("extdata", "test_float64.npy", package = "grumpy") ) # Array of strings. NumPy "<U" dtype read_npy( system.file("extdata", "test_str_unicode.npy", package = "grumpy") ) # "Array" of lists. Numpy structured dtype read_npy( system.file("extdata", "test_structured.npy", package = "grumpy") )
.npz fileRead a .npz file
read_npz(file, arrays = NULL)read_npz(file, arrays = NULL)
file |
Path to the |
arrays |
Optional character vector of array names or numeric vector of
indices to read from the |
A named list of arrays containing the data from the .npz file
read_npz( system.file("extdata", "test.npz", package = "grumpy") ) read_npz( system.file("extdata", "test.npz", package = "grumpy"), arrays = "x" )read_npz( system.file("extdata", "test.npz", package = "grumpy") ) read_npz( system.file("extdata", "test.npz", package = "grumpy"), arrays = "x" )