Skip to content

Commit 110e8e7

Browse files
authored
axes() arg to get name/type/unit; centroids,label: >2D, multiscale adjustment
* add axes() arg to return name/type/unit * >2D & multi-scale adjustment in centroids,label
1 parent ce16614 commit 110e8e7

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

R/CTutils.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#' @aliases axes CTlist CTname CTtype CTdata addCT rmvCT
44
#'
55
#' @param x \code{SpatialData}, an element, or \code{SpatialDataAttrs}.
6+
#' @param y NULL (default) returns a list where each element is
7+
#' an axis: a list with name/type/unit (e.g., x/space/micrometer);
8+
#' \code{y="name/type/unit"} extracts specific data over all axiis.
69
#' @param i for \code{CTpath}, source node label; else, string or
710
#' scalar integer giving the name or index of a coordinate space.
811
#' @param name character(1); name of coordinate space
@@ -51,11 +54,13 @@ NULL
5154

5255
#' @rdname CTutils
5356
#' @export
54-
setMethod("axes", "SpatialDataAttrs", \(x, ...) {
57+
setMethod("axes", "SpatialDataAttrs", \(x, y=NULL, ...) {
5558
ms <- multiscales(x)
5659
if (!is.null(ms)) x <- ms[[1]]
5760
if (is.null(x <- x$axes)) stop("couldn't find 'axes'")
58-
return(x)
61+
if (is.null(y)) return(x)
62+
y <- match.arg(y, c("name", "type", "unit"))
63+
vapply(x, `[[`, character(1), y)
5964
})
6065

6166
# CTlist/data/type/name() ----
@@ -105,7 +110,7 @@ setMethod("CTname", "SpatialDataAttrs", \(x, ...) {
105110

106111
#' @rdname CTutils
107112
#' @export
108-
setMethod("axes", "SpatialDataElement", \(x, ...) axes(meta(x), ...))
113+
setMethod("axes", "SpatialDataElement", \(x, y=NULL, ...) axes(meta(x), y, ...))
109114

110115
#' @rdname CTutils
111116
#' @export

R/centroids.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,34 @@ setMethod("centroids", "SpatialDataLabel", \(x,
3636
as=c("data.frame", "matrix")) {
3737
as <- match.arg(as)
3838
y <- data(x)
39+
if (length(dim(y)) > 2) {
40+
# max-projection
41+
ax <- match(c("y", "x"), axes(x, "name"))
42+
y <- apply(y, ax, max)
43+
}
3944
y <- as(y, "dgCMatrix")
4045
i <- summary(y)
4146
# flip dimensions so that columns=x, rows=y
42-
# TODO: should these be offset by 0.5?
4347
i[, c(1, 2)] <- i[, c(2, 1)]-0.5
4448
xy <- tapply(i[, -3], i[[3]], colMeans)
4549
xy <- do.call(rbind, xy)
4650
xy <- cbind(xy, as.integer(rownames(xy)))
4751
dimnames(xy) <- list(NULL, c("x", "y", "i"))
52+
# multi-scale adjustment
53+
sf <- .get_multiscale_scale(x)
54+
xy[,1] <- xy[,1]*tail(sf, 1)
55+
xy[,2] <- xy[,2]*tail(sf, 2)[1]
56+
# offset
57+
wh <- metadata(x)$wh
58+
if (!is.null(wh)) {
59+
xy[,1] <- xy[,1]+wh[[1]][1]
60+
xy[,2] <- xy[,2]+wh[[2]][1]
61+
}
62+
# output
4863
if (as == "matrix") return(xy)
4964
xy <- as.data.frame(xy)
50-
xy$i <- factor(xy$i); xy
65+
xy$i <- factor(xy$i)
66+
return(xy)
5167
})
5268

5369
#' @export

man/CTutils.Rd

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

0 commit comments

Comments
 (0)