Skip to content

Commit

Permalink
Prepare for the missing values in version 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Oct 3, 2023
1 parent 5e072f7 commit 4249d95
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(.stageObject)
export(.writeMetadata)
export(acquireFile)
export(acquireMetadata)
export(addMissingPlaceholderAttribute)
export(addMissingStringPlaceholderAttribute)
export(altLoadObject)
export(altLoadObjectFunction)
Expand Down
7 changes: 4 additions & 3 deletions R/loadDataFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ loadDataFrame <- function(info, project, include.nested=TRUE, parallel=TRUE) {
df <- make_zero_col_DFrame(nrow=nrows)
} else {
raw <- h5read(path, prefix("data"))
version_above_1 <- isTRUE(info$data_frame$version > 1)

# Replacing NAs for strings.
# Replacing placeholders with NAs.
for (i in names(raw)) {
current <- raw[[i]]
if (is.character(current)) {
if (version_above_1 || is.character(current)) {
attr <- h5readAttributes(path, prefix(paste0("data/", i)))
replace.na <- attr[["missing-value-placeholder"]]
if (!is.null(replace.na)) {
if (!is.null(replace.na) && !is.na(replace.na)) {
raw[[i]][current == replace.na] <- NA
}
}
Expand Down
24 changes: 16 additions & 8 deletions R/stageDataFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
#'
#' @export
#' @aliases
#' addMissingStringPlaceholderAttribute
#' addMissingPlaceholderAttribute
#' chooseMissingStringPlaceholder
#' addMissingStringPlaceholderAttribute
#' .addMissingStringPlaceholderAttribute
#' .chooseMissingStringPlaceholder
#'
Expand Down Expand Up @@ -180,7 +181,7 @@ setMethod("stageObject", "DataFrame", function(x, dir, path, child=FALSE, df.nam
opath <- paste0(opath, ".h5")
ofile <- file.path(dir, opath)
skippable <- vapply(meta, function(x) x$type == "other", TRUE)
.write_hdf5_data_frame(x, skippable, "contents", ofile)
.write_hdf5_data_frame(x, skippable, "contents", ofile, .version=.version)
schema <- "hdf5_data_frame/v1.json"
extra[[1]]$group <- "contents"

Expand Down Expand Up @@ -229,7 +230,7 @@ setMethod("stageObject", "DataFrame", function(x, dir, path, child=FALSE, df.nam
})

#' @importFrom rhdf5 h5write h5createGroup h5createFile
.write_hdf5_data_frame <- function(x, skippable, host, ofile) {
.write_hdf5_data_frame <- function(x, skippable, host, ofile, .version) {
h5createFile(ofile)
prefix <- function(x) paste0(host, "/", x)
h5createGroup(ofile, host)
Expand All @@ -248,16 +249,20 @@ setMethod("stageObject", "DataFrame", function(x, dir, path, child=FALSE, df.nam
}

missing.placeholder <- NULL
if (is.character(current) && anyNA(current)) {
missing.placeholder <- chooseMissingStringPlaceholder(current)
current[is.na(current)] <- missing.placeholder
if (anyNA(current)) {
if (is.character(current) && anyNA(current)) {
missing.placeholder <- chooseMissingStringPlaceholder(current)
current[is.na(current)] <- missing.placeholder
} else if (.version > 1) {
missing.placeholder <- as(NA, storage.mode(current))
}
}

data.name <- as.character(i - 1L)
h5write(current, ofile, prefix(paste0("data/", data.name)))

if (!is.null(missing.placeholder)) {
addMissingStringPlaceholderAttribute(ofile, prefix(paste0("data/", data.name)), missing.placeholder)
addMissingPlaceholderAttribute(ofile, prefix(paste0("data/", data.name)), missing.placeholder)
}
}

Expand All @@ -282,7 +287,7 @@ chooseMissingStringPlaceholder <- function(x) {

#' @export
#' @importFrom rhdf5 H5Fopen H5Fclose H5Dopen H5Dclose h5writeAttribute
addMissingStringPlaceholderAttribute <- function(file, path, placeholder) {
addMissingPlaceholderAttribute <- function(file, path, placeholder) {
fhandle <- H5Fopen(file)
on.exit(H5Fclose(fhandle), add=TRUE)
dhandle <- H5Dopen(fhandle, path)
Expand All @@ -295,5 +300,8 @@ addMissingStringPlaceholderAttribute <- function(file, path, placeholder) {
#' @export
.chooseMissingStringPlaceholder <- function(...) chooseMissingStringPlaceholder(...)

#' @export
addMissingStringPlaceholderAttribute <- function(...) addMissingPlaceholderAttribute(...)

#' @export
.addMissingStringPlaceholderAttribute <- function(...) addMissingStringPlaceholderAttribute(...)
8 changes: 6 additions & 2 deletions man/stageDataFrame.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4249d95

Please sign in to comment.