Skip to content

Commit 7a368bd

Browse files
committed
code cleaning
1 parent 9199ed2 commit 7a368bd

8 files changed

Lines changed: 59 additions & 2409 deletions

File tree

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
export(plotSpatialData)
44
export(scalebar)
55
exportMethods(plotImage)
6-
exportMethods(plotLabel)
76
exportMethods(plotPoint)
87
exportMethods(plotShape)
98
import(spatialdataR)

R/plotImage.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,26 @@ NULL
161161
#' @importFrom spatialdataR data_type
162162
.df_i <- \(x, k=NULL, ch=NULL, t=NULL, c=NULL, cl=NULL, z=NULL) {
163163
a <- .get_ms_data(x, k)
164-
axisNames <- axes(x=x, y="name")
164+
axisNames <- axes(x, "name")
165165
# 2D max-projection
166166
a <- .project(x, a)
167167
axisNames <- axisNames[axisNames != "z"]
168+
ti <- which(axisNames == "t")
169+
tn <- length(ti)
168170
# subset channels and timepoint of interest
169-
tidx <- which(axisNames=="t")
170-
if (length(tidx)>0) {
171+
if (tn) {
171172
if (is.null(t)) {
172173
t <- 1
173-
}
174-
if (length(t)>1) {
174+
} else if (length(t) > 1) {
175175
stop("Only a single timepoint can be selected")
176176
}
177177
}
178178
a <- .subset_array_by_axes(a=a, axisNames=axisNames,
179179
c=.ch_idx(x, ch), t=t, drop=FALSE)
180180
# remove time axis if it exists
181-
dim(a) <- dim(a)[axisNames != "t"]
182-
if (length(tidx)>0) {
183-
axisNames <- axisNames[-tidx]
181+
if (tn) {
182+
dim(a) <- dim(a)[axisNames != "t"]
183+
axisNames <- axisNames[-ti]
184184
}
185185
# if no channel axis, add dummy axis
186186
if (!("c" %in% axisNames)) {

R/plotLabel.R

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@
4545
#'
4646
#' # coloring by 'assay' data
4747
#' p + plotLabel(x, i, c="channel_1_sum")
48-
NULL
49-
50-
#' @rdname plotLabel
51-
#' @importFrom grDevices hcl.colors colorRampPalette
52-
#' @importFrom S4Vectors metadata
53-
#' @importFrom rlang .data
48+
#'
5449
#' @importFrom methods as
50+
#' @importFrom rlang .data
51+
#' @importFrom S4Vectors metadata
52+
#' @importFrom SingleCellExperiment colData
53+
#' @importFrom grDevices hcl.colors colorRampPalette
5554
#' @importFrom ggplot2 scale_fill_manual scale_fill_gradientn
5655
#' @importFrom ggplot2 aes theme unit guides guide_legend geom_tile
57-
#'
58-
#' @importFrom SingleCellExperiment colData
59-
#' @export
56+
#'
6057
setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, t=NULL, c=NULL,
6158
a=0.5, pal=NULL, nan=NA, assay=1, z=NULL) {
6259

R/scalebar.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#' @importFrom methods is
2929
#' @export
3030
scalebar <- function(x, len, col="red", lwd=1, xrel=0.05, yrel=0.05) {
31+
# validity
3132
if (!is(x, "SpatialDataArray"))
3233
stop("'x' should be a 'SpatialDataArray' object, i.e., an",
3334
" image or label element from a 'SpatialData' object")

R/utils.R

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# convenience functions until this is fixed/exported by 'SpatialData'
1+
# internal helper for null-coalescing
2+
`%||%` <- \(a, b) if (is.null(a)) b else a
23

34
#' @importFrom methods is
45
#' @importFrom SingleCellExperiment int_metadata
@@ -65,19 +66,16 @@
6566
.project <- \(x, y, z=NULL) {
6667
# max-projection over z-stacks
6768
axisNames <- axes(x, y="name")
68-
zidx <- which(axisNames=="z")
69-
if (length(zidx)>0) {
69+
zidx <- which(axisNames == "z")
70+
if (length(zidx) > 0) {
7071
if (is.null(z)) {
7172
# max-projection across z-slices
7273
y <- apply(y, seq_along(dim(x))[-zidx], max)
7374
} else {
74-
if (length(z)>1) {
75-
stop("Only a single z-plane can be selected")
76-
}
75+
if (length(z) > 1) stop("only a single z-plane can be selected")
7776
# subset target z-slice
78-
y <- .subset_array_by_axes(a=y, axisNames=axisNames,
79-
z=z, drop=FALSE)
80-
dim(y) <- dim(y)[axisNames!="z"]
77+
y <- .subset_array_by_axes(a=y, axisNames=axisNames, z=z, drop=FALSE)
78+
dim(y) <- dim(y)[axisNames != "z"]
8179
}
8280
}
8381
y
@@ -116,9 +114,9 @@
116114
}
117115

118116
.subset_array_by_axes <- \(a, axisNames, ..., drop=FALSE) {
119-
if (length(dim(a)) != length(axisNames)) {
120-
stop("axisNames must have the same length as the number of dimensions of x")
121-
}
117+
# this should never be trigger as object validity should prevent it
118+
ok <- length(dim(a)) == length(axisNames)
119+
if (!ok) stop("'length(axes(x))' must equal 'length(dim(x))'")
122120
specs <- list(...)
123121
idx <- lapply(axisNames, \(nm) {
124122
if (!is.null(specs[[nm]])) {
@@ -130,49 +128,4 @@
130128
do.call("[", c(list(a), idx, list(drop=drop)))
131129
}
132130

133-
.unit_map <- c(micrometer="\U03BCm",
134-
micron="\U03BCm")
135-
136-
#' Create scalebar for image
137-
#'
138-
#' @param x A \code{SpatialDataArray} object.
139-
#' @param l A numeric scalar giving the length of the scalebar (in global
140-
#' coordinates). The unit will be extracted from the metadata of \code{x}.
141-
#' @param xrel,yrel Numeric scalars between 0 and 1 indicating the relative
142-
#' x and y position of the scalebar.
143-
#' @param color Character scalar indicating the color to use for the scalebar.
144-
#' @param linewidth Numeric scalar indicating the line width to use for the
145-
#' scalebar.
146-
#'
147-
#' @examples
148-
#' x <- file.path("extdata", "blobs.zarr")
149-
#' x <- system.file(x, package="spatialdataR")
150-
#' x <- readSpatialData(x, tables=FALSE)
151-
#' plotSpatialData() +
152-
#' plotImage(x, i=2) +
153-
#' scalebar(image(x, i=2), l=10)
154-
#'
155-
#' @importFrom ggplot2 annotate
156-
#' @export
157-
scalebar <- function(x, l, xrel=0.05, yrel=0.05,
158-
color="red", linewidth=1) {
159-
unit <- axes(x)[[which(axes(x, y="name")=="x")]]$unit
160-
if (unit %in% names(.unit_map)) {
161-
unit <- .unit_map[unit]
162-
}
163-
wh <- .get_wh(x)
164-
if (xrel<=0.5) {
165-
xmin <- diff(wh$w) * xrel + wh$w[1]
166-
xmax <- diff(wh$w) * xrel + wh$w[1] + l
167-
} else {
168-
xmin <- wh$w[2] - diff(wh$w) * (1 - xrel) - l
169-
xmax <- wh$w[2] - diff(wh$w) * (1 - xrel)
170-
}
171-
y <- wh$h[2] - diff(wh$h) * yrel
172-
list(annotate(geom="segment", x=xmin, xend=xmax, y=y, yend=y,
173-
color=color, linewidth=linewidth),
174-
annotate(geom="text", x=(xmin+xmax)/2, y=y,
175-
vjust=ifelse(yrel>0.5,1.5,-0.5),
176-
color=color, label=paste0(l, unit)))
177-
}
178-
131+
.unit_map <- c(micrometer="\U03BCm", micron="\U03BCm")

man/plotLabel.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scalebar.Rd

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

tests/testthat/_snaps/plotSpatialData/overlays.svg

Lines changed: 0 additions & 2315 deletions
This file was deleted.

0 commit comments

Comments
 (0)