Skip to content

Commit 34a1c1d

Browse files
committed
implement multiscale label visualization
1 parent 9fd1260 commit 34a1c1d

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ importFrom(ggplot2,element_line)
2727
importFrom(ggplot2,element_text)
2828
importFrom(ggplot2,geom_point)
2929
importFrom(ggplot2,geom_polygon)
30-
importFrom(ggplot2,geom_tile)
30+
importFrom(ggplot2,geom_raster)
3131
importFrom(ggplot2,ggplot)
3232
importFrom(ggplot2,guide_legend)
3333
importFrom(ggplot2,guides)

R/plotImage.R

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,11 @@ plotSpatialData <- \() ggplot() + scale_y_reverse() + .theme
159159
return(NULL)
160160
}
161161

162-
.guess_scale <- \(x, w, h) {
163-
n <- length(dim(x))
164-
i <- ifelse(n == 3, -1, TRUE)
165-
d <- vapply(x@data, dim, numeric(n))
166-
d <- apply(d, 2, \(.) sum(abs(.[i]-c(h, w))))
167-
which.min(d)
168-
}
169-
170-
.get_img_data <- \(x, k=NULL, w=800, h=800) {
171-
if (!is.null(k)) return(data(x, k))
172-
data(x, .guess_scale(x, w, h))
173-
}
174-
175162
#' @importFrom methods as
176163
#' @importFrom grDevices rgb
177164
#' @importFrom DelayedArray realize
178165
.df_i <- \(x, k=NULL, ch=NULL, c=NULL, cl=NULL) {
179-
a <- .get_img_data(x, k)
166+
a <- .get_multiscale_data(x, k)
180167
ch <- .ch_idx(x, ch)
181168
if (!.is_rgb(x))
182169
a <- a[ch, , , drop=FALSE]

R/plotLabel.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ NULL
4545
#' @importFrom methods as
4646
#' @importFrom ggplot2
4747
#' scale_fill_manual scale_fill_gradientn
48-
#' aes geom_tile theme unit guides guide_legend
48+
#' aes geom_raster theme unit guides guide_legend
4949
#' @export
5050
setMethod("plotLabel", "SpatialData", \(x, i=1, c=NULL,
5151
a=0.5, pal=c("red", "green"), nan=NA, assay=1) {
5252
if (is.numeric(i)) i <- labelNames(x)[i]
5353
i <- match.arg(i, labelNames(x))
54-
y <- as.matrix(as(data(label(x, i)), "DelayedArray"))
54+
y <- as.matrix(.get_multiscale_data(label(x, i)))
5555
df <- data.frame(x=c(col(y)), y=c(row(y)), z=c(y))
5656
aes <- aes(.data[["x"]], .data[["y"]])
5757
if (!is.null(c)) {
@@ -81,5 +81,5 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, c=NULL,
8181
theme(legend.position="none"),
8282
scale_fill_manual(NULL, values=pal))
8383
}
84-
list(thm, do.call(geom_tile, list(data=df, mapping=aes, alpha=a)))
84+
list(thm, do.call(geom_raster, list(data=df, mapping=aes, alpha=a)))
8585
})

R/utils.R

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@
4545
.DTYPE_MAX_VALUES <- c("uint8" = 255,
4646
"uint16" = 65535,
4747
"uint32" = 4294967295,
48-
"uint64" = 2^64 - 1)
48+
"uint64" = 2^64 - 1)
49+
50+
# guess scale of image or label
51+
.guess_scale <- \(x, w, h) {
52+
n <- length(dim(x))
53+
i <- ifelse(n == 3, -1, TRUE)
54+
d <- vapply(x@data, dim, numeric(n))
55+
d <- apply(d, 2, \(.) sum(abs(.[i]-c(h, w))))
56+
which.min(d)
57+
}
58+
59+
# get multiscale
60+
.get_multiscale_data <- \(x, k=NULL, w=800, h=800) {
61+
if (!is.null(k)) return(data(x, k))
62+
data(x, .guess_scale(x, w, h))
63+
}

tests/testthat/Rplots.pdf

3.96 KB
Binary file not shown.

tests/testthat/test-plotArray.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ test_that(".guess_scale", {
2222
dim <- lapply(c(6, 3), \(.) c(3, rep(., 2))), \(.)
2323
array(sample(seq_len(255), prod(.), replace=TRUE), dim=.)))
2424
# manual scale
25-
expect_identical(.get_img_data(img, k=1), lys[[1]])
26-
expect_identical(.get_img_data(img, k=2), lys[[2]])
25+
expect_identical(.get_multiscale_data(img, k=1), lys[[1]])
26+
expect_identical(.get_multiscale_data(img, k=2), lys[[2]])
2727
# automatic scale
28-
expect_identical(.get_img_data(img, k=NULL, w=5, h=7), lys[[1]])
29-
expect_identical(.get_img_data(img, k=NULL, w=2, h=2), lys[[2]])
28+
expect_identical(.get_multiscale_data(img, k=NULL, w=5, h=7), lys[[1]])
29+
expect_identical(.get_multiscale_data(img, k=NULL, w=2, h=2), lys[[2]])
3030
})
3131

3232
test_that("plotImage()", {

0 commit comments

Comments
 (0)