From 63643d9840f71f3436e41a36d0dd78ce88ee9bce Mon Sep 17 00:00:00 2001 From: HelenaLC Date: Mon, 8 Jun 2026 21:36:54 +0200 Subject: [PATCH] refine image/label show (#239) --- R/misc.R | 27 +++++++++++++++++++++------ R/utils.R | 12 ++++++++---- tests/testthat/test-misc.R | 12 ++++++++---- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/R/misc.R b/R/misc.R index f66aab29..59c41735 100644 --- a/R/misc.R +++ b/R/misc.R @@ -86,16 +86,31 @@ NULL #' @rdname misc setMethod("show", "SpatialData", .showSpatialData) +.csv <- \(x) paste(x, collapse=",") +.csv_one <- \(x) sprintf("(%s)", .csv(x)) +.csv_all <- \(x) vapply(x, .csv_one, character(1)) + #' @importFrom S4Vectors coolcat -.showArray <- function(object) { - n.object <- length(object@data) - cat("class: ", class(object), ifelse(n.object > 1, "(MultiScale)", ""),"\n") - scales <- vapply(object@data, \(x) paste0(dim(x), collapse=","), character(1)) - coolcat("Scales (%d): (%s)", scales) +.showLabel <- function(object) { + x <- axes(object, "name") + x <- sprintf("(%sd: %s)", length(x), paste(x, collapse="")) + cat("class:", class(object), x, "\n") + x <- .csv_all(lapply(object@data, dim)) + coolcat("levels(%d): %s", x) + x <- .csv_all(.get_ms_scale(object, NULL)) + coolcat("scales(%d): %s", x) +} +.showImage <- function(object) { + .showLabel(object) + x <- channels(object) + cat(sprintf("channels(%s): (%s)", length(x), .csv(x)), "\n") } #' @rdname misc -setMethod("show", "SpatialDataArray", .showArray) +setMethod("show", "SpatialDataLabel", .showLabel) + +#' @rdname misc +setMethod("show", "SpatialDataImage", .showImage) #' @importFrom S4Vectors coolcat .showPoint <- function(object) { diff --git a/R/utils.R b/R/utils.R index 30627fb1..b8469ea5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -191,11 +191,15 @@ # get scale factors between 'multiscales' levels # (returns numeric vector, one value per dimension) -.get_ms_scale <- \(x) { +.get_ms_scale <- \(x, i=1) { ms <- .get_ms(x) - ds <- ms$datasets[[1]] - ct <- ds$coordinateTransformations[[1]] - return(unlist(ct$scale)) + ds <- ms$datasets + sf <- lapply(ds, \(.) { + ct <- .$coordinateTransformations[[1]] + unlist(ct$scale) + }) + if (is.null(i)) return(sf) + if (length(i) == 1) sf[[i]] else sf[i] } # find indices with equal spatial extents diff --git a/tests/testthat/test-misc.R b/tests/testthat/test-misc.R index 4159d027..42a5a1d7 100644 --- a/tests/testthat/test-misc.R +++ b/tests/testthat/test-misc.R @@ -48,17 +48,21 @@ test_that("show(SpatialData)", { test_that("show(SpatialDataElement)", { # image x <- image(sd, 1) + y <- axes(x, "name") ok <- c( - "class: SpatialDataImage", - sprintf("Scales \\(%d\\):", length(data(x, NULL))), + "class: SpatialDataImage", + sprintf("\\(%dd: %s\\)", length(y), paste(y, collapse="")), + sprintf("levels\\(%d\\):", length(data(x, NULL))), sprintf("(%s)", paste(dim(x), collapse=","))) fn(x, ok) # label x <- label(sd, 1) + y <- axes(x, "name") ok <- c( - "class: SpatialDataLabel", - sprintf("Scales \\(%d\\):", length(data(x, NULL))), + "class: SpatialDataLabel", + sprintf("\\(%dd: %s\\)", length(y), paste(y, collapse="")), + sprintf("levels\\(%d\\):", length(data(x, NULL))), sprintf("(%s)", paste(dim(x), collapse=","))) fn(x, ok)