Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: grumpy
Title: Read 'NumPy' '.npy' and '.npz' Files
Version: 0.1.1.9002
Version: 0.1.1.9003
Authors@R: c(
person("Hugo", "Gruson", , "hugo.gruson+R@normalesup.org", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0002-4094-1476")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# grumpy (development version)

* Elements of structured datatypes are now named if names were provided during
the dataset creation.
* `read_npz()` now adds names to the list ir returns. The names are the names
of the bundled `.npy` files without the file extension.

Expand Down
14 changes: 12 additions & 2 deletions R/read_npy.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ parse_npy_datatype <- function(descr) {
types <- lapply(descr, function(field) {
parse_npy_datatype(field[[2L]])
})
nms <- vapply(
descr,
function(field) {
field[[1L]]
},
character(1L)
)
return(
list(
base_type = vapply(types, function(x) x$base_type, character(1L)),
base_type = setNames(
vapply(types, function(x) x$base_type, character(1L)),
nms
),
nbytes = vapply(types, function(x) x$nbytes, integer(1L)),
endian = vapply(types, function(x) x$endian, character(1L))
)
Expand Down Expand Up @@ -239,7 +249,7 @@ convert_bytes_to_array <- function(bytes, what, shape, size, endian) {
field_start <- c(0L, cumsum(size)[-length(size)])

# Convert each field via strided index extraction
res_fields <- vector("list", length(what))
res_fields <- vector("list", length(what)) |> setNames(names(what))
for (i in seq_along(what)) {
starts <- seq(
field_start[[i]] + 1L,
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-read_npy.R
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ test_that("structured arrays work", {
expect_identical(
array(
list(
list(1L, 3.14, "Alice"),
list(2L, 2.71, "Bob"),
list(3L, 1.62, "Charlie"),
list(4L, 0.0, "Dave"),
list(5L, -1.0, "Eve"),
list(6L, 2.0, "Frank"),
list(7L, 33.12, "Grace"),
list(8L, 13.9, "Hugo")
list(id = 1L, value = 3.14, name = "Alice"),
list(id = 2L, value = 2.71, name = "Bob"),
list(id = 3L, value = 1.62, name = "Charlie"),
list(id = 4L, value = 0.0, name = "Dave"),
list(id = 5L, value = -1.0, name = "Eve"),
list(id = 6L, value = 2.0, name = "Frank"),
list(id = 7L, value = 33.12, name = "Grace"),
list(id = 8L, value = 13.9, name = "Hugo")
),
dim = c(2L, 4L)
)
Expand Down
Loading