diff --git a/DESCRIPTION b/DESCRIPTION index cfebfb4..04e3098 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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")), diff --git a/NEWS.md b/NEWS.md index 7aad59f..8ce11e2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/read_npy.R b/R/read_npy.R index 18537e7..054d847 100644 --- a/R/read_npy.R +++ b/R/read_npy.R @@ -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)) ) @@ -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, diff --git a/tests/testthat/test-read_npy.R b/tests/testthat/test-read_npy.R index c38c404..b5ccf39 100644 --- a/tests/testthat/test-read_npy.R +++ b/tests/testthat/test-read_npy.R @@ -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) )