Skip to content

Commit ce16614

Browse files
authored
scale matching in mask(); fix channels() (#232)
* match scales in mask() * channels() bug fix * fix [,sdArray docs
1 parent 95343c1 commit ce16614

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

R/mask.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,25 @@ setGeneric("mask_i_by_j", \(i, j, ...) standardGeneric("mask_i_by_j"))
9393
setMethod("mask_i_by_j",
9494
c("SpatialDataImage", "SpatialDataLabel"),
9595
\(i, j, how=NULL, ...) {
96-
.wh <- \(.) {
97-
ds <- dim(.); if (length(ds) == 3) ds <- ds[-1]
98-
metadata(.)$wh %||% list(c(0, ds[2]), c(0, ds[1]))
99-
}
100-
stopifnot(
101-
"image/label width mismatch"=.wh(i)[[1]] == .wh(j)[[1]],
102-
"image/label height mismatch"=.wh(i)[[2]] == .wh(j)[[2]])
96+
di <- lapply(data(i, NULL), dim)
97+
dj <- lapply(data(j, NULL), dim)
98+
ij <- outer(
99+
seq_along(di),
100+
seq_along(dj),
101+
Vectorize(\(i, j) identical(tail(di[[i]], length(dj[[j]])), dj[[j]])))
102+
ij <- which(ij, arr.ind=TRUE)
103+
if (nrow(ij) == 0)
104+
stop("couldn't find shared multiscales level between label/image;",
105+
" need at least one data() pair with identical dimensions")
106+
ki <- ij[1, 1]
107+
kj <- ij[1, 2]
103108
if (is.null(how)) {
104109
message("Missing 'how'; defaulting to 'mean'")
105110
how <- "mean"
106111
}
107-
.j <- as(data(j), "sparseVector")
112+
.j <- as(data(j, kj), "sparseVector")
108113
.j <- as.vector(.j[ok <- .j > 0])
109-
mx <- apply(data(i), 1, \(.i) {
114+
mx <- apply(data(i, ki), 1, \(.i) {
110115
.i <- as(.i, "sparseVector")
111116
.i <- as.vector(.i[ok])
112117
tapply(.i, .j, how)

R/sdArray.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ setMethod("data_type", "DelayedArray", \(x) {
131131
v <- tryCatch(.ome_ver(x), error=\(e) NULL)
132132
if (is.null(v)) return()
133133
if (v == "0.5") x <- x$ome
134-
unlist(x$omero$channels)
134+
# NOTE: can't use 'vapply' as we
135+
# have encountered integer 'label's
136+
x <- x$omero$channels
137+
x$label %||% unlist(lapply(x, `[[`, "label"))
135138
}
136139

137140
#' @export
@@ -173,8 +176,6 @@ setMethod("channels", "SpatialDataElement", \(x, ...) stop("only 'images' have c
173176
# https://github.com/Huber-group-EMBL/Rarr/blob/1795c676e2ac81a9ba2a592c7210cc59036544b6/R/utils.R#L74-L79
174177
.sub <- \(x, ix) rlang::inject(x[!!!ix, drop=FALSE])
175178

176-
#' @exportMethod [
177-
#' @rdname SpatialDataArray
178179
#' @importFrom utils head tail
179180
.sub_sda <- \(x, yx, z=list()) {
180181
#x <- label(sd); yx <- list(1:10, 1:10); z <- list()
@@ -208,13 +209,17 @@ setMethod("channels", "SpatialDataElement", \(x, ...) stop("only 'images' have c
208209
x
209210
}
210211

212+
#' @exportMethod [
213+
#' @rdname SpatialDataArray
211214
setMethod("[", "SpatialDataImage", \(x, i, j, k, ..., drop=FALSE) {
212215
if (missing(i)) i <- TRUE
213216
if (missing(j)) j <- TRUE else if (isFALSE(j)) j <- 0 else .check_jk(j, "j")
214217
if (missing(k)) k <- TRUE else if (isFALSE(k)) k <- 0 else .check_jk(k, "k")
215218
.sub_sda(x, yx=list(j, k), z=list(i))
216219
})
217220

221+
#' @exportMethod [
222+
#' @rdname SpatialDataArray
218223
setMethod("[", "SpatialDataLabel", \(x, i, j, ..., drop=FALSE) {
219224
if (missing(i)) i <- TRUE else if (isFALSE(i)) i <- 0 else .check_jk(i, "i")
220225
if (missing(j)) j <- TRUE else if (isFALSE(j)) j <- 0 else .check_jk(j, "j")

man/SpatialDataArray.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-mask.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ test_that("mask,sdImage,sdLabel", {
4646
expect_equivalent(
4747
assay(tables(y)[[2]]),
4848
assay(tables(x)[[1]]))
49+
50+
# no matching scale
51+
.i <- image(x, "blobs_multiscale_image")
52+
.i@data <- lapply(.i@data, \(.) .[,,-1])
53+
.x <- x; image(.x, i) <- .i
54+
expect_error(mask(.x, i, j))
4955
})
5056

5157
test_that("mask w/ transform", {
@@ -58,7 +64,7 @@ test_that("mask w/ transform", {
5864
l <- list(1,.1,.1); t <- "scale"
5965
a <- addCT(a, name=t, type=t, data=l)
6066
y <- x; y[[layer(y, i)]][[i]] <- a
61-
expect_error(mask(y, i, j, t))
67+
expect_no_error(mask(y, i, j, t))
6268

6369
# aligned
6470
l <- c(list(1), CTdata(b, t <- "scale"))

0 commit comments

Comments
 (0)