-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'saveterrards' into devel
- Loading branch information
Showing
7 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#' Save SpatialFeatureExperiment as RDS file | ||
#' | ||
#' Saving SFE objects as RDS files is complicated by the \code{SpatRaster} class | ||
#' of the images. If present, the images need to be wrapped with the \code{\link{wrap}} | ||
#' function in \code{terra} before serializing the SFE object. Otherwise the | ||
#' images will be invalid pointers when the RDS is reloaded. If the image does | ||
#' not fit in memory and its file source is unknown, then it will be written to | ||
#' a temporary file, which is reloaded when the RDS file is loaded. When an SFE | ||
#' object with images is read from an RDS file, the images will not be unwrapped | ||
#' until necessary. | ||
#' | ||
#' @inheritParams base::saveRDS | ||
#' @param object A \code{SpatialFeatureExperiment} object. | ||
#' @return Invisibly \code{NULL}. | ||
#' @importFrom terra wrap unwrap | ||
#' @export | ||
#' @examples | ||
#' outdir <- system.file("extdata", package = "SpatialFeatureExperiment") | ||
#' samples <- file.path(outdir, paste0("sample0", 1:2)) | ||
#' sfe <- read10xVisiumSFE(samples, type = "sparse", data = "filtered") | ||
#' saveRDS(sfe, "foo.rds") | ||
#' | ||
setMethod("saveRDS", "SpatialFeatureExperiment", | ||
function(object, file = "", ascii = FALSE, version = NULL, | ||
compress = TRUE, refhook = NULL) { | ||
if (is.null(imgData(object))) | ||
base::saveRDS(object, file = file, ascii = ascii, | ||
version = version, compress = compress, | ||
refhook = refhook) | ||
else { | ||
for (i in seq_len(nrow(imgData(object)))) { | ||
img <- int_metadata(object)$imgData$data[[i]] | ||
if (is(imgRaster(img), "SpatRaster")) | ||
img@image <- wrap(img@image) | ||
int_metadata(object)$imgData$data[[i]] <- img | ||
# Then I need to add unwrap to functions that use the image | ||
} | ||
base::saveRDS(object, file = file, ascii = ascii, | ||
version = version, compress = compress, | ||
refhook = refhook) | ||
} | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
outdir <- system.file("extdata", package = "SpatialFeatureExperiment") | ||
samples <- file.path(outdir, paste0("sample0", 1:2)) | ||
sfe <- read10xVisiumSFE(samples, type = "sparse", data = "filtered") | ||
|
||
test_that("Save SFE with SpatRaster images as RDS", { | ||
saveRDS(sfe, "foo.rds") | ||
sfe2 <- readRDS("foo.rds") | ||
imgs <- imgData(sfe2)$data | ||
classes <- vapply(imgs, function(x) class(imgRaster(x)), FUN.VALUE = character(1)) | ||
expect_true(all(classes == "PackedSpatRaster")) | ||
expect_s4_class(sfe2, "SpatialFeatureExperiment") | ||
unlink("foo.rds") | ||
}) | ||
|
||
test_that("Save SFE without images", { | ||
imgData(sfe) <- NULL | ||
saveRDS(sfe, "bar.rds") | ||
sfe2 <- readRDS("bar.rds") | ||
expect_equal(sfe, sfe2) | ||
unlink("bar.rds") | ||
}) |