Skip to content

Commit dac8625

Browse files
committed
refactoring
1 parent 5c87a39 commit dac8625

7 files changed

Lines changed: 62 additions & 75 deletions

File tree

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,5 @@ importFrom(sf,st_coordinates)
4848
importFrom(sf,st_geometry_type)
4949
importFrom(spatialdataR,channels)
5050
importFrom(spatialdataR,data_type)
51-
importFrom(spatialdataR,meta)
5251
importFrom(spatialdataR,transform)
5352
importFrom(utils,tail)

R/plotImage.R

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ NULL
160160
#' @importFrom DelayedArray realize
161161
#' @importFrom spatialdataR data_type
162162
.df_i <- \(x, k=NULL, ch=NULL, c=NULL, cl=NULL) {
163-
a <- .get_multiscale_data(x, k)
163+
a <- .get_ms_data(x, k)
164164
# max-projection over z-stacks
165165
d <- length(dim(x))
166166
if (d == 4) a <- apply(a, c(1, 3, 4), max)
@@ -171,21 +171,6 @@ NULL
171171
a <- .prep_ia(a, c, cl)
172172
}
173173

174-
#' @importFrom utils tail
175-
.get_wh <- \(x) {
176-
wh <- metadata(x)$wh
177-
if (!is.null(wh)) {
178-
df <- data.frame(x=wh[[1]], y=wh[[2]])
179-
} else {
180-
ds <- dim(data(x, 1))
181-
df <- data.frame(
182-
x=c(0, tail(ds, 1)),
183-
y=c(0, tail(ds, 2)[1]))
184-
}
185-
wh <- list(w=df$x, h=df$y)
186-
return(wh)
187-
}
188-
189174
#' @importFrom ggplot2 guides geom_point geom_blank annotation_raster
190175
#' @importFrom ggplot2 scale_color_identity scale_x_continuous scale_y_reverse
191176
.gg_i <- \(x, w, h, pal=NULL) {
@@ -219,16 +204,8 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl
219204
nms <- unlist(channels(y))[idx <- .ch_idx(y, ch)]
220205
pal <- pal[seq_along(idx)]; names(pal) <- nms
221206
}
222-
# multi-scale adjustment
207+
# physical space mapping
223208
wh <- .get_wh(y)
224-
if (wh$w[2] == tail(dim(y), 1) ||
225-
wh$h[2] == tail(dim(y), 2)[1]) {
226-
ts <- .get_multiscale_scale(y)
227-
tx <- tail(ts, 1)
228-
ty <- tail(ts, 2)[1]
229-
} else tx <- ty <- 1
230-
wh$w[2] <- wh$w[2]*tx
231-
wh$h[2] <- wh$h[2]*ty
232209
.gg_i(df, wh$w, wh$h, pal)
233210
})
234211

@@ -238,5 +215,5 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl
238215
plotSpatialData <- \() ggplot() + coord_sf(expand=FALSE, reverse="y") + .theme
239216
# `annotation_raster` plots the array the same way it is printed, i.e., with the
240217
# row 1 at the top, which means we need to flip the y-axis to have the correct axis labels.
241-
# We tried flipping the image itself but it means everything gets out of alignement if
218+
# We tried flipping the image itself but it means everything gets out of alignment if
242219
# the user sets `scale_y_reverse()` themselves.

R/plotLabel.R

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
6969
y <- transform(y, j)
7070

7171
# get array data
72-
ym <- .get_multiscale_data(y, k)
72+
ym <- .get_ms_data(y, k)
7373
if (length(dim(ym)) > 2) {
7474
if (is.null(z)) {
7575
# max-projection across z-slices
@@ -86,19 +86,13 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
8686
# and thus save memory by not plotting all pixels
8787
idx <- BiocGenerics::which(ym != 0L, arr.ind=TRUE)
8888

89-
# offset & multi-scale adjustment
89+
# physical space mapping
9090
ds <- dim(ym)
9191
wh <- .get_wh(y)
92-
if (wh$w[2] == tail(dim(y), 1) ||
93-
wh$h[2] == tail(dim(y), 2)[1]) {
94-
ts <- .get_multiscale_scale(y)
95-
tx <- tail(ts, 1)
96-
ty <- tail(ts, 2)[1]
97-
} else tx <- ty <- 1
9892
nx <- tail(ds, 1)
9993
ny <- tail(ds, 2)[1]
100-
sx <- (diff(wh$w)/nx)*tx
101-
sy <- (diff(wh$h)/ny)*ty
94+
sx <- diff(wh$w)/nx
95+
sy <- diff(wh$h)/ny
10296
df <- data.frame(
10397
x=wh$w[1]+idx[,2L]*sx,
10498
y=wh$h[1]+idx[,1L]*sy,

R/utils.R

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,46 @@
4747

4848
# guess scale of image or label
4949
.guess_scale <- \(x, w, h) {
50-
i <- match(c("y", "x"), vapply(axes(x), \(.) .$name, character(1)))
51-
d <- vapply(x@data, dim, numeric(length(dim(x))))
52-
d <- apply(d, 2, \(.) sum(abs(.[i]-c(h, w))))
53-
which.min(d)
50+
i <- match(c("y", "x"), vapply(axes(x), \(.) .$name, character(1)))
51+
d <- vapply(x@data, dim, numeric(length(dim(x))))
52+
d <- apply(d, 2, \(.) sum(abs(.[i]-c(h, w))))
53+
which.min(d)
5454
}
5555

5656
# get multiscale
57-
.get_multiscale_data <- \(x, k=NULL, w=800, h=800) {
58-
if (!is.null(k)) return(data(x, k))
59-
data(x, .guess_scale(x, w, h))
57+
.get_ms_data <- \(x, k=NULL, w=800, h=800) {
58+
if (!is.null(k)) return(data(x, k))
59+
data(x, .guess_scale(x, w, h))
6060
}
6161

62-
#' @importFrom spatialdataR meta
63-
.get_multiscale_scale <- \(x) {
64-
ms <- spatialdataR:::multiscales(meta(x))[[1]]
65-
ds <- ms$datasets[[1]]
66-
ct <- ds$coordinateTransformations[[1]]
67-
return(unlist(ct$scale))
62+
#' @importFrom utils tail
63+
.raw_wh <- \(x) {
64+
wh <- metadata(x)$wh
65+
if (!is.null(wh)) {
66+
df <- data.frame(x=wh[[1]], y=wh[[2]])
67+
} else {
68+
ds <- dim(data(x, 1))
69+
df <- data.frame(
70+
x=c(0, tail(ds, 1)),
71+
y=c(0, tail(ds, 2)[1]))
72+
}
73+
wh <- list(w=df$x, h=df$y)
74+
return(wh)
75+
}
76+
77+
# map index to physical space
78+
# through multi-scale adjustment
79+
.get_wh <- \(x) {
80+
wh <- .raw_wh(x)
81+
if (wh$w[2] == tail(dim(x), 1) ||
82+
wh$h[2] == tail(dim(x), 2)[1]) {
83+
ts <- spatialdataR:::.get_ms_scale(x)
84+
tx <- tail(ts, 1)
85+
ty <- tail(ts, 2)[1]
86+
} else {
87+
tx <- ty <- 1
88+
}
89+
wh$w[2] <- wh$w[2]*tx
90+
wh$h[2] <- wh$h[2]*ty
91+
return(wh)
6892
}

tests/testthat/Rplots.pdf

12.5 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_multiscale_data(img, k=1), lys[[1]])
26-
expect_identical(.get_multiscale_data(img, k=2), lys[[2]])
25+
expect_identical(.get_ms_data(img, k=1), lys[[1]])
26+
expect_identical(.get_ms_data(img, k=2), lys[[2]])
2727
# automatic scale
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]])
28+
expect_identical(.get_ms_data(img, k=NULL, w=5, h=7), lys[[1]])
29+
expect_identical(.get_ms_data(img, k=NULL, w=2, h=2), lys[[2]])
3030
})
3131

3232
test_that("plotImage()", {

tests/testthat/test-plotImage.R

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,26 @@ test_that(".check_cl", {
5050
expect_error(.check_cl(list(NULL, NULL, 0), 3)) # zero scalar
5151
})
5252

53-
dir.create(td <- tempfile())
54-
x <- MulticancerSteinbock(target=td)
55-
a <- data(image(x)[seq_len(3), seq_len(100), seq_len(100)], 1)
53+
# mock multiplex image
54+
l <- 4; m <- 80; n <- 120
55+
a <- as(array(runif(l*m*n), c(l,m,n)), "ZarrArray")
56+
y <- SpatialDataImage(list(a), SpatialDataAttrs(type="image", dim=2, nch=l))
57+
x <- SpatialData(list(y))
5658

5759
test_that(".norm_ia", {
5860
# valid data type
5961
dt <- data_type(a)
6062
b <- .norm_ia(realize(a), dt)
6163
expect_equal(
64+
tolerance=1e-3,
6265
apply(b, 1, range),
63-
replicate(3, c(0, 1)))
66+
replicate(l, c(0, 1)))
6467
# invalid data type
6568
b <- .norm_ia(realize(a), "")
6669
expect_equal(
70+
tolerance=1e-3,
6771
apply(b, 1, range),
68-
replicate(3, c(0, 1)))
72+
replicate(l, c(0, 1)))
6973
})
7074

7175
test_that(".prep_ia", { testthat::skip()
@@ -78,20 +82,9 @@ test_that(".prep_ia", { testthat::skip()
7882
expect_length(dim(b), 2)
7983
expect_is(b[1,1], "character")
8084
# colors
81-
cmy <- c("cyan", "magenta", "yellow")
82-
b <- .prep_ia(a, ch, c=cmy)
83-
expect_equal(dim(a), dim(b))
84-
expect_equal(
85-
apply(b, 1, range),
86-
replicate(d, c(0, 1)))
87-
# lower contrast lim.
88-
lim <- list(c(0.5, 1), NULL, NULL)
89-
b <- .prep_ia(a, ch, cl=lim)
90-
expect_identical(b[-1,], a[-1,,])
91-
expect_true(sum(b[1,] == 0) > sum(a[1,,] == 0))
92-
# upper contrast lim.
93-
lim <- list(c(0, 0.5), NULL, NULL)
94-
b <- .chs2rgb(a, ch, cl=lim)
95-
fac <- mean(b[1,,]/a[1,,], na.rm=TRUE)
96-
expect_equal(fac, 2, tolerance=0.05)
85+
pal <- colors()[seq_len(l)]
86+
b <- .prep_ia(a, c=pal)
87+
expect_equal(dim(a)[-1], dim(b))
88+
expect_is(b, "matrix")
89+
expect_is(c(b), "character")
9790
})

0 commit comments

Comments
 (0)