44# '
55# ' @description ...
66# '
7- # ' @param x \code{\link{SpatialData}} object.
7+ # ' @param x \code{\link[spatialdataR] {SpatialData}} object.
88# ' @param i element to use from a given layer.
9- # ' @param j name of target coordinate system.
10- # ' @param k index of the scale of an image ; by default (NULL), will auto-select
9+ # ' @param j index or name of target coordinate system.
10+ # ' @param k index of the scale to render ; by default (NULL), will auto-select
1111# ' scale in order to minimize memory-usage and blurring for a target size of
1212# ' 800 x 800px; use Inf to plot the lowest resolution available.
1313# ' @param ch image channel(s) to be used for plotting (defaults to
1818# ' specifies channel-wise contrast limits - defaults to [0, 1] for all
1919# ' (ignored when \code{image(x, i)} is an RGB image;
2020# ' for convenience, any NULL = [0, 1], and n = [0, n]).
21+ # ' @param t,z integer scalar to indicate a specific time- or z-slice;
22+ # ' if left unspecified (default NULL), will perform a max-projection.
2123# '
2224# ' @return ggplot
2325# '
8486 n <- length(c )
8587 if (n < d ) stop(
8688 " Only " , n , " default colors available, " ,
87- " but" , d , " are needed; please specify 'c'" )
89+ " but " , d , " are needed; please specify 'c'" )
8890 c <- c [seq_len(d )]
8991 }
9092 }
117119 if (dt %in% names(.DTYPE_MAX_VALUES )) {
118120 a <- a / .DTYPE_MAX_VALUES [dt ]
119121 } else if (max(a ) > 1 ) {
120- for ( i in seq_len( d ) )
121- a [ i ,,] <- a [ i ,,] / max( a [ i ,,] )
122+ maxs <- apply( a , 1 , max )
123+ a <- sweep( a , MARGIN = 1 , STATS = maxs , FUN = " / " )
122124 }
123125 return (a )
124126}
@@ -159,33 +161,56 @@ NULL
159161# ' @importFrom methods as
160162# ' @importFrom DelayedArray realize
161163# ' @importFrom spatialdataR data_type
162- .df_i <- \(x , k = NULL , ch = NULL , c = NULL , cl = NULL ) {
164+ .df_i <- \(x , k = NULL , ch = NULL , t = NULL , c = NULL , cl = NULL , z = NULL ) {
163165 a <- .get_ms_data(x , k )
166+ axisNames <- axes(x , " name" )
164167 # 2D max-projection
165168 a <- .project(x , a )
166- # subset channels of interest
167- a <- a [.ch_idx(x , ch ),,,drop = FALSE ]
169+ axisNames <- axisNames [axisNames != " z" ]
170+ ti <- which(axisNames == " t" )
171+ tn <- length(ti )
172+ # subset channels and timepoint of interest
173+ if (tn ) {
174+ if (is.null(t )) {
175+ t <- 1
176+ } else if (length(t ) > 1 ) {
177+ stop(" Only a single timepoint can be selected" )
178+ }
179+ }
180+ a <- .subset_array_by_axes(a = a , axisNames = axisNames ,
181+ c = .ch_idx(x , ch ), t = t , drop = FALSE )
182+ # remove time axis if it exists
183+ if (tn ) {
184+ dim(a ) <- dim(a )[axisNames != " t" ]
185+ axisNames <- axisNames [- ti ]
186+ }
187+ # if no channel axis, add dummy axis
188+ if (! (" c" %in% axisNames )) {
189+ dim(a ) <- c(1 , dim(a ))
190+ axisNames <- c(" c" , axisNames )
191+ }
168192 a <- .norm_ia(a , data_type(x ))
169193 # color merging & contrasts
170194 a <- .prep_ia(a , c , cl )
171195}
172196
197+ # ' @importFrom rlang .data
173198# ' @importFrom ggplot2 guides geom_point geom_blank annotation_raster
174199# ' @importFrom ggplot2 scale_color_identity scale_x_continuous scale_y_reverse
175200.gg_i <- \(x , w , h , pal = NULL ) {
176201 l <- if (! is.null(names(pal ))) list (
177202 guides(col = guide_legend(override.aes = list (alpha = 1 , size = 2 ))),
178203 geom_point(aes(col = .data $ foo ), data.frame (foo = pal ), x = 0 , y = 0 , alpha = 0 ))
179204 list (l ,
180- geom_blank(aes(x = x , y = y ), data.frame (x = w , y = h )),
205+ geom_blank(aes(x = .data $ x , y = .data $ y ), data.frame (x = w , y = h )),
181206 annotation_raster(x , w [1 ],w [2 ], h [2 ],h [1 ], interpolate = FALSE ),
182207 scale_color_identity(NULL , guide = " legend" , breaks = pal , labels = names(pal )),
183208 ggnewscale :: new_scale_color())
184209}
185210
186211# ' @rdname plotImage
187212# ' @export
188- setMethod ("plotImage ", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl=NULL) {
213+ setMethod ("plotImage ", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl=NULL, t=NULL, z=NULL ) {
189214 if (is.numeric(i ))
190215 i <- imageNames(x )[i ]
191216 y <- image(x , i )
@@ -197,7 +222,7 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl
197222 ch <- ch %|| % channels(y )
198223 cl <- cl %|| % c(0 , 1 / 3 )
199224 }
200- df <- .df_i(y , k , ch , c , cl )
225+ df <- .df_i(y , k , ch , t , c , cl , z )
201226 pal <- c %|| % .DEFAULT_COLORS
202227 if (dim(y )[1 ] > 1 && ! .is_rgb(y )) {
203228 nms <- unlist(channels(y ))[idx <- .ch_idx(y , ch )]
0 commit comments