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: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
^touchstone$
^\.lintr$
^cran-comments\.md$
^CRAN-SUBMISSION$
^data-raw$
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ BugReports: https://github.com/Bisaloo/grumpy/issues
Imports:
jsonlite
Config/roxygen2/version: 8.0.0
Depends:
R (>= 3.5)
57 changes: 20 additions & 37 deletions R/read_npy.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,52 +123,35 @@ parse_npy_datatype <- function(descr) {
)
)
}
descr_components <- regmatches(
descr,
regexec("^([<>|]?)([a-zA-Z])([0-9]*)$", descr)
)[[1L]]
endian <- if (descr_components[2L] %in% c("", "|")) {
NA_character_
} else {
switch(
descr_components[2L],
`<` = "little",
`>` = "big",
stop(
"Invalid endianness: ",
descr_components[1L],
call. = FALSE
if (startsWith(descr, "|S")) {
return(
list(
endian = NA_character_,
base_type = "string",
nbytes = as.integer(sub("|S", "", descr, fixed = TRUE))
)
)
}
if (startsWith(descr, "<U") || startsWith(descr, ">U")) {
charlen <- as.integer(sub("^[<>]U", "", descr))
return(
list(
endian = if (startsWith(descr, "<")) "little" else "big",
base_type = "unicode",
nbytes = charlen * 4L
)
)
}
python_type <- descr_components[3L]
n <- as.integer(descr_components[4L])

type_map <- list(
f = list(base_type = "float", size = n),
i = list(base_type = "int", size = n),
u = list(base_type = "uint", size = n),
`?` = list(base_type = "bool", size = 1L),
b = list(base_type = "bool", size = 1L),
a = list(base_type = "string", size = n),
S = list(base_type = "string", size = n),
U = list(base_type = "unicode", size = n * 4L),
c = list(base_type = "complex", size = n),
m = list(base_type = "timedelta", size = n),
M = list(base_type = "datetime", size = n),
V = list(base_type = "other", size = n),
O = list(base_type = "py_object", size = NA_integer_)
)

entry <- type_map[[python_type]]
entry <- supported_types[[descr]]
if (is.null(entry)) {
stop("Unsupported data type: ", descr_components[1L], call. = FALSE)
stop("Unsupported data type: ", descr, call. = FALSE)
}

return(list(
endian = endian,
endian = entry$endian,
base_type = entry$base_type,
nbytes = entry$size
nbytes = entry$nbytes
))
}

Expand Down
Binary file added R/sysdata.rda
Binary file not shown.
33 changes: 33 additions & 0 deletions data-raw/supported_types.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## code to prepare `supported_types` dataset goes here
supported_types <- list(
">f2" = list(base_type = "float", nbytes = 2L, endian = "big"),
"<f2" = list(base_type = "float", nbytes = 2L, endian = "little"),
">f4" = list(base_type = "float", nbytes = 4L, endian = "big"),
"<f4" = list(base_type = "float", nbytes = 4L, endian = "little"),
">f8" = list(base_type = "float", nbytes = 8L, endian = "big"),
"<f8" = list(base_type = "float", nbytes = 8L, endian = "little"),
"|i1" = list(base_type = "int", nbytes = 1L, endian = NA_character_),
">i2" = list(base_type = "int", nbytes = 2L, endian = "big"),
"<i2" = list(base_type = "int", nbytes = 2L, endian = "little"),
">i4" = list(base_type = "int", nbytes = 4L, endian = "big"),
"<i4" = list(base_type = "int", nbytes = 4L, endian = "little"),
">i8" = list(base_type = "int", nbytes = 8L, endian = "big"),
"<i8" = list(base_type = "int", nbytes = 8L, endian = "little"),
"|u1" = list(base_type = "uint", nbytes = 1L, endian = NA_character_),
">u2" = list(base_type = "uint", nbytes = 2L, endian = "big"),
"<u2" = list(base_type = "uint", nbytes = 2L, endian = "little"),
">u4" = list(base_type = "uint", nbytes = 4L, endian = "big"),
"<u4" = list(base_type = "uint", nbytes = 4L, endian = "little"),
">u8" = list(base_type = "uint", nbytes = 8L, endian = "big"),
"<u8" = list(base_type = "uint", nbytes = 8L, endian = "little"),
"|?1" = list(base_type = "bool", nbytes = 1L, endian = NA_character_),
"|b1" = list(base_type = "bool", nbytes = 1L, endian = NA_character_),
"|O" = list(
base_type = "py_object",
nbytes = NA_integer_,
endian = NA_character_
)
) |>
list2env(hash = TRUE, parent = emptyenv())

usethis::use_data(supported_types, internal = TRUE, overwrite = TRUE)
Loading