Skip to content

Commit fa70d70

Browse files
committed
make multiscale adjustments
1 parent 2da47cf commit fa70d70

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

R/plotImage.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ NULL
172172
}
173173

174174
#' @importFrom utils tail
175-
#' @importFrom spatialdataR transform
176175
.get_wh <- \(x) {
177176
wh <- metadata(x)$wh
178177
if (!is.null(wh)) {
@@ -184,10 +183,6 @@ NULL
184183
y=c(0, tail(ds, 2)[1]))
185184
}
186185
wh <- list(w=df$x, h=df$y)
187-
# multi-scale adjustment
188-
t <- .get_multiscale_scale(x)
189-
wh$w[2] <- wh$w[2]*t[length(t)]
190-
wh$h[2] <- wh$h[2]*t[length(t)-1]
191186
return(wh)
192187
}
193188

@@ -213,7 +208,6 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl
213208
if (is.numeric(j))
214209
j <- CTname(y)[j]
215210
y <- transform(y, j)
216-
wh <- .get_wh(y)
217211
if (.is_rgb(y)) {
218212
# RGB: we plot everything by default and we don't normalize
219213
ch <- ch %||% channels(y)
@@ -225,6 +219,16 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl
225219
nms <- unlist(channels(y))[idx <- .ch_idx(y, ch)]
226220
pal <- pal[seq_along(idx)]; names(pal) <- nms
227221
}
222+
# multi-scale adjustment
223+
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
228232
.gg_i(df, wh$w, wh$h, pal)
229233
})
230234

R/plotLabel.R

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ NULL
5858
#' @export
5959
setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
6060
a=0.5, pal=c("red", "green"), nan=NA, assay=1, z=NULL) {
61-
6261
if (is.numeric(i)) i <- labelNames(x)[i]
6362
i <- match.arg(i, labelNames(x))
6463
y <- label(x, i)
@@ -72,10 +71,12 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
7271
ym <- .get_multiscale_data(y, k)
7372
if (length(dim(ym)) > 2) {
7473
if (is.null(z)) {
74+
# max-projection across z-slices
7575
nm <- vapply(axes(y), \(.) .$name, character(1))
7676
yx <- match(c("y", "x"), nm)
7777
ym <- apply(ym, yx, max)
7878
} else {
79+
# subset target z-slice
7980
ym <- ym[z,,]
8081
}
8182
}
@@ -84,19 +85,23 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
8485
# and thus save memory by not plotting all pixels
8586
idx <- BiocGenerics::which(ym != 0L, arr.ind=TRUE)
8687

87-
# offset & multi-scale adjustment
88+
# offset & multi-scale adjustment
89+
ds <- dim(ym)
8890
wh <- .get_wh(y)
89-
t <- .get_multiscale_scale(y)
90-
tx <- t[length(t)]
91-
ty <- t[length(t)-1L]
92-
.x <- tx*idx[, 2L]
93-
.y <- ty*idx[, 1L]
94-
mx <- dim(ym)[2]*tx
95-
my <- dim(ym)[1]*ty
91+
if (wh$w[2] == tail(dim(y), 1) ||
92+
wh$h[2] == tail(dim(y), 2)[1]) {
93+
ts <- .get_multiscale_scale(y)
94+
tx <- tail(ts, 1)
95+
ty <- tail(ts, 2)[1]
96+
} else tx <- ty <- 1
97+
nx <- tail(ds, 1)
98+
ny <- tail(ds, 2)[1]
99+
sx <- (diff(wh$w)/nx)*tx
100+
sy <- (diff(wh$h)/ny)*ty
96101
df <- data.frame(
97-
x = .x*(wh$w[2]/mx)+wh$w[1],
98-
y = .y*(wh$h[2]/my)+wh$h[1],
99-
z = ym[idx])
102+
x=wh$w[1]+idx[,2L]*sx,
103+
y=wh$h[1]+idx[,1L]*sy,
104+
z=ym[idx])
100105

101106
aes <- aes(.data[["x"]], .data[["y"]])
102107
if (!is.null(c)) {

0 commit comments

Comments
 (0)