Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion R/crop.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ NULL
#' @importFrom methods is
#' @importFrom sf st_bbox
setMethod("crop", "SpatialDataArray", \(x, y, j=1, ...) {
#x <- label(sd); y <- bb; j <- 1
if (is.matrix(y)) {
y <- .check_pol(y)
y <- st_bbox(st_polygon(list(y)))
Expand Down
63 changes: 43 additions & 20 deletions R/mask.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,56 @@ setGeneric("mask_i_by_j", \(i, j, ...) standardGeneric("mask_i_by_j"))
setMethod("mask_i_by_j",
c("SpatialDataImage", "SpatialDataLabel"),
\(i, j, how=NULL, ...) {
if (is.null(how)) {
message("Missing 'how'; defaulting to 'mean'")
how <- "mean"
}
# default to 1st matching scale
di <- lapply(data(i, NULL), dim)
dj <- lapply(data(j, NULL), dim)
ij <- outer(
ai <- axes(i, "type") == "space"
aj <- axes(j, "type") == "space"
ks <- outer(
seq_along(di),
seq_along(dj),
Vectorize(\(i, j) identical(tail(di[[i]], length(dj[[j]])), dj[[j]])))
ij <- which(ij, arr.ind=TRUE)
if (nrow(ij) == 0)
Vectorize(\(i, j) identical(di[[i]][ai], dj[[j]][aj])))
ks <- which(ks, arr.ind=TRUE)
if (nrow(ks) == 0)
stop("couldn't find shared multiscales level between label/image;",
" need at least one data() pair with identical dimensions")
ki <- ij[1, 1]
kj <- ij[1, 2]
if (is.null(how)) {
message("Missing 'how'; defaulting to 'mean'")
how <- "mean"
di <- data(i, ks[1, 1])
dj <- data(j, ks[1, 2])
# utility to aggregate 'i' channels by instance in 'j'
agg <- \(di, dj, how) {
iv <- as(di, "sparseVector")
jv <- as(dj, "sparseVector")
jv <- as.vector(jv[ok <- jv > 0])
iv <- as.vector(iv[ok])
tapply(iv, jv, how)
}
res <- if ("t" %in% axes(i, "name")) {
ts <- seq_len(dim(di)[1])
names(ts) <- paste0("t", ts)
ix <- as.list(!logical(length(dim(di))))
jx <- as.list(!logical(length(dim(dj))))
lapply(ts, \(t) {
ix[[1]] <- t; jx[[1]] <- t
.di <- do.call(`[`, c(list(di), ix))
.dj <- do.call(`[`, c(list(dj), jx))
agg(.di, .dj, how)
})
} else {
list(apply(di, 1, \(.di) agg(.di, dj, how)))
}
se <- SingleCellExperiment(lapply(res, t))
rownames(se) <- channels(i)
as <- assayNames(se)
as <- if (is.null(as)) {
how
} else {
paste0(how, "_", as)
}
.j <- as(data(j, kj), "sparseVector")
.j <- as.vector(.j[ok <- .j > 0])
mx <- apply(data(i, ki), 1, \(.i) {
.i <- as(.i, "sparseVector")
.i <- as.vector(.i[ok])
tapply(.i, .j, how)
})
colnames(mx) <- channels(i)
se <- SingleCellExperiment(list(t(mx)))
assayNames(se) <- how
assayNames(se) <- as
return(se)
})

Expand All @@ -129,7 +153,6 @@ setMethod("mask_i_by_j",
"POINT"=mutate(data(j), geometry=ST_Buffer(geometry, radius)),
data(j))
ddbs_intersects(df_j, data(i), sparse=TRUE)

}

#' @noRd
Expand Down
8 changes: 2 additions & 6 deletions R/validity.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@

.validateImage <- \(object) {
msg <- c()
axs <- axes(object)
typ <- vapply(axs, \(.) .$type, character(1))
d <- sum(typ != "time")
d <- length(axes(object))
for (k in seq_along(object)) {
x <- data(object, k)
if (length(dim(x)) != d) msg <- c(msg, paste(
Expand All @@ -62,9 +60,7 @@ setValidity2("SpatialDataImage", .validateImage)
#' @importFrom ZarrArray type
.validateLabel <- \(object) {
msg <- c()
axs <- axes(object)
typ <- vapply(axs, \(.) .$type, character(1))
d <- sum(typ == "space")
d <- length(axes(object))
for (k in seq_along(object)) {
x <- data(object, k)
if (length(dim(x)) != d) msg <- c(msg, paste(
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-sdarray.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test_that("data(),SpatialDataImage", {

test_that("SpatialDataLabel()", {
val <- sample(seq_len(12), 20*20, replace=TRUE)
mat <- array(val, dim=c(20, 20))
mat <- array(val, dim=c(20, 20, 20))
# invalid
expect_error(SpatialDataLabel(mat, 1))
expect_error(SpatialDataLabel(mat, list()))
Expand All @@ -64,13 +64,13 @@ test_that("SpatialDataLabel()", {
expect_silent(SpatialDataLabel(list(mat)))
expect_silent(SpatialDataLabel(list(mat), SpatialDataAttrs()))
# multiscale
dim <- lapply(c(20, 10, 5), \(.) rep(., 2))
dim <- lapply(c(20, 10, 5), \(.) rep(., 3))
lys <- lapply(dim, \(.) array(sample(seq_len(12), prod(.), replace=TRUE), dim=.))
expect_silent(SpatialDataLabel(lys))
})

test_that("data(),SpatialDataLabel", {
dim <- lapply(c(8, 4, 2), \(.) rep(., 2))
dim <- lapply(c(8, 4, 2), \(.) rep(., 3))
lys <- lapply(dim, \(.) array(0L, dim=.))
lab <- SpatialDataLabel(lys)
for (. in seq_along(lys))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-sdframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test_that("filter", {
n <- length(p <- point(x))
expect_length(filter(p), n)
expect_length(filter(p, genes == "x"), 0)
f <- \() filter(p, z == 1)
f <- \() filter(p, missing == 1)
expect_error(show(f()))
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-validity.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test_that("validity,SpatialDataLabel", {
x <- label(sd,1); x@data[[1]][1,1] <- v; expect_error(validObject(x))
x <- label(sd,2); x@data[[2]][1,1] <- v; expect_error(validObject(x))
}
expect_error(SpatialDataLabel(list(a <- array(integer(1), c(1,1,1)))))
expect_error(SpatialDataLabel(list(a <- array(integer(1), rep(1,7)))))
x <- label(sd,1); x@data[[1]] <- a; expect_error(validObject(x))
x <- label(sd,2); x@data[[2]] <- a; expect_error(validObject(x))
})
Expand Down
Loading