Skip to content

Commit ffc91e9

Browse files
committed
air and lint
1 parent 4471bb7 commit ffc91e9

21 files changed

+484
-406
lines changed

R/BFArray.R

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,34 @@
1515
#'
1616
#' @export
1717
#' @return A BFArray object
18-
#'
18+
#'
1919
#' @examples
2020
#' # get image
2121
#' library(RBioFormats)
22-
#' img.file <- system.file("extdata",
23-
#' "xy_12bit__plant.ome.tiff",
22+
#' img.file <- system.file("extdata",
23+
#' "xy_12bit__plant.ome.tiff",
2424
#' package = "ImageArray")
2525
#' bfa <- BFArray(img.file, series = 1, resolution = 2)
2626
#' dim(bfa)
2727
#' type(bfa)
2828
BFArray <- function(image.file, series, resolution) {
29-
3029
# check RBioFormats
31-
if(!requireNamespace("RBioFormats"))
30+
if (!requireNamespace("RBioFormats")) {
3231
stop("Please install RBioFormats: BiocManager::install('RBioFormats')")
33-
32+
}
33+
3434
# get metadata
35-
meta <- RBioFormats::read.metadata(
35+
meta.data <- RBioFormats::read.metadata(
3636
file = image.file,
3737
filter.metadata = TRUE,
3838
proprietary.metadata = TRUE
3939
)
40-
len_meta <- vapply(meta@.Data, length, integer(1))
41-
meta@.Data <- meta@.Data[which(len_meta > 0)]
40+
len_meta <- vapply(meta.data@.Data, length, integer(1))
41+
meta.data@.Data <- meta.data@.Data[which(len_meta > 0)]
4242

4343
# get shape
4444
series_res_meta <- vapply(
45-
meta@.Data,
45+
meta.data@.Data,
4646
function(x) {
4747
if (!is.null(cm <- x$coreMetadata)) {
4848
x <- cm
@@ -60,7 +60,7 @@ BFArray <- function(image.file, series, resolution) {
6060
shape <- vapply(
6161
c("sizeX", "sizeY", "sizeC"),
6262
function(x) {
63-
md <- meta@.Data[[series_index]]
63+
md <- meta.data@.Data[[series_index]]
6464
if (!is.null(cm <- md$coreMetadata)) {
6565
md <- cm
6666
}
@@ -77,7 +77,7 @@ BFArray <- function(image.file, series, resolution) {
7777
shape = shape,
7878
type = "double"
7979
)
80-
return(.BFArray(seed = seed))
80+
.BFArray(seed = seed)
8181
} else {
8282
stop("Specified resolution was not found in the image!")
8383
}
@@ -110,22 +110,15 @@ setMethod("dim", "BFArraySeed", function(x) x@shape)
110110
setMethod("type", "BFArraySeed", function(x) x@type)
111111

112112
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113-
### extract_array()
113+
### extract_array
114114
###
115115

116116
#' @importFrom EBImage imageData
117117
.extract_array_from_BFArraySeed <- function(x, index) {
118-
119118
# check RBioFormats
120-
if(!requireNamespace("RBioFormats"))
119+
if (!requireNamespace("RBioFormats")) {
121120
stop("Please install RBioFormats: BiocManager::install('RBioFormats')")
122-
123-
# get metadata
124-
meta <- RBioFormats::read.metadata(
125-
file = x@filepath,
126-
filter.metadata = TRUE,
127-
proprietary.metadata = TRUE
128-
)
121+
}
129122

130123
# check for index length
131124
if (length(index) > 3) {
@@ -136,11 +129,11 @@ setMethod("type", "BFArraySeed", function(x) x@type)
136129
ind <- mapply(
137130
function(x, y) {
138131
if (is.null(x)) {
139-
return(seq_len(y))
132+
seq_len(y)
140133
} else if (length(x) == 0) {
141-
return(integer(0))
134+
integer(0)
142135
} else if (length(x) > 0) {
143-
return(x)
136+
x
144137
}
145138
},
146139
index,
@@ -155,8 +148,9 @@ setMethod("type", "BFArraySeed", function(x) x@type)
155148
type(res) <- x@type
156149
} else {
157150
subset_list <- list(X = ind[[1]], Y = ind[[2]])
158-
if (length(len_ind) == 3)
151+
if (length(len_ind) == 3) {
159152
subset_list <- c(subset_list, list(C = ind[[3]]))
153+
}
160154
res <- RBioFormats::read.image(
161155
file = x@filepath,
162156
series = x@series,
@@ -166,10 +160,10 @@ setMethod("type", "BFArraySeed", function(x) x@type)
166160
res <- EBImage::imageData(res)
167161
if (length(dim(res)) != 3) {
168162
res <- array(res, dim = c(dim(res), 1))
169-
}
163+
}
170164
}
171165

172-
return(res)
166+
res
173167
}
174168

175169
setMethod("extract_array", "BFArraySeed", .extract_array_from_BFArraySeed)

R/ImageArray.R

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ NULL
7373
#'
7474
#' @export
7575
setMethod(
76-
f = '[',
77-
signature = c('ImageArray', "numeric", "numeric"),
76+
f = "[",
77+
signature = c("ImageArray", "numeric", "numeric"),
7878
definition = function(x, i, j, ..., drop = FALSE) {
7979
crop(x, ind = list(i, j))
8080
}
@@ -85,10 +85,10 @@ setMethod(
8585
#'
8686
#' @export
8787
setMethod(
88-
f = '[[',
89-
signature = c('ImageArray', "numeric"),
88+
f = "[[",
89+
signature = c("ImageArray", "numeric"),
9090
definition = function(x, i) {
91-
return(x@levels[[i]])
91+
x@levels[[i]]
9292
}
9393
)
9494

@@ -97,28 +97,35 @@ setMethod(
9797
#'
9898
#' @export
9999
setMethod(
100-
f = '[[<-',
101-
signature = c('ImageArray', "numeric"),
100+
f = "[[<-",
101+
signature = c("ImageArray", "numeric"),
102102
definition = function(x, i, ..., value) {
103103
x@levels[[i]] <- value
104-
return(x)
104+
x
105105
}
106106
)
107107

108108
#' @importFrom S4Vectors coolcat
109109
#' @noRd
110110
setMethod(
111-
f = 'show',
112-
signature = c('ImageArray'),
111+
f = "show",
112+
signature = c("ImageArray"),
113113
definition = function(object) {
114-
cat(class(x = object), "Object",
115-
paste0(
116-
"(", paste(object@meta[["axes"]], collapse = ","), ")"
117-
),
118-
"\n")
119-
scales <- vapply(object@levels,
120-
\(x) sprintf("(%s)", paste0(dim(x), collapse=",")),
121-
character(1))
114+
cat(
115+
class(x = object),
116+
"Object",
117+
paste0(
118+
"(",
119+
paste(object@meta[["axes"]], collapse = ","),
120+
")"
121+
),
122+
"\n"
123+
)
124+
scales <- vapply(
125+
object@levels,
126+
\(x) sprintf("(%s)", paste0(dim(x), collapse = ",")),
127+
character(1)
128+
)
122129
S4Vectors::coolcat("Scales (%d): %s", scales)
123130
}
124131
)
@@ -142,7 +149,7 @@ setMethod("length", signature = "ImageArray", function(x) length(x@levels))
142149
#'
143150
#' A function for creating objects of ImageArray class
144151
#'
145-
#' @param meta the metadata of the ImageArray object.
152+
#' @param meta the metadata of the ImageArray object.
146153
#' @param levels levels of the pyramid image, typically a vector of integers
147154
#' starting with 1
148155
#'
@@ -197,7 +204,7 @@ createBFArray <- function(
197204
#' @param max.pixel.threshold the maximum width
198205
#' and height pixel dimension that the lowest level of the image pyramid
199206
#' should have, thus the image will be downscaled two folds until both width
200-
#' and height is below the threshold. Default is 700 pixels.
207+
#' and height is below the threshold. Default is 700 pixels.
201208
#' If \code{n.levels} is provided, this parameter will be ignored.
202209
#' @param verbose verbose
203210
#'
@@ -238,17 +245,19 @@ createMagickArray <- function(
238245
}
239246

240247
# create image levels
241-
if (verbose)
248+
if (verbose) {
242249
.img_create_msg(dim(image), 1)
250+
}
243251
image_data <- magick::image_data(image, channels = "rgb")
244252
storage.mode(image_data) <- "integer"
245253
image_list <- list(DelayedArray::DelayedArray(as.array(image_data)))
246254
if (n.levels > 1) {
247255
cur_image <- image
248256
for (i in 2:n.levels) {
249257
dim_image <- ceiling(dim_image / 2)
250-
if (verbose)
258+
if (verbose) {
251259
.img_create_msg(dim_image, 1)
260+
}
252261
cur_image <- magick::image_resize(
253262
cur_image,
254263
geometry = magick::geometry_size_percent(50),
@@ -275,7 +284,7 @@ createMagickArray <- function(
275284
#' @param max.pixel.threshold the maximum width
276285
#' and height pixel dimension that the lowest level of the image pyramid
277286
#' should have, thus the image will be downscaled two folds until both width
278-
#' and height is below the threshold. Default is 700 pixels.
287+
#' and height is below the threshold. Default is 700 pixels.
279288
#' If \code{n.levels} is provided, this parameter will be ignored.
280289
#' @param verbose verbose
281290
#'
@@ -309,9 +318,10 @@ createEBImageArray <- function(
309318

310319
# create image levels
311320
meta <- list(axes = c("x", "y", "c"))
312-
if (verbose)
321+
if (verbose) {
313322
.img_create_msg(dim_image, 1)
314-
img_perm <- if(length(dim(image)) == 2) c(1,2) else c(1, 2, 3)
323+
}
324+
img_perm <- if (length(dim(image)) == 2) c(1, 2) else c(1, 2, 3)
315325
meta[["axes"]] <- meta[["axes"]][img_perm]
316326
img_perm <- stats::setNames(img_perm, meta[["axes"]])
317327
img <- aperm(image, img_perm)
@@ -320,9 +330,9 @@ createEBImageArray <- function(
320330
cur_image <- image
321331
for (i in 2:n.levels) {
322332
dim_image <- ceiling(dim_image / 2)
323-
if (verbose)
333+
if (verbose) {
324334
.img_create_msg(dim_image, i)
325-
resize_factor <- dim_image
335+
}
326336
cur_image <- EBImage::resize(
327337
cur_image,
328338
w = dim_image[1],
@@ -346,13 +356,13 @@ createEBImageArray <- function(
346356
#' @param n.levels the number of levels of the pyramidal image,
347357
#' typical an integer starting from 1
348358
#' @param series the series IDs of the pyramidal image,
349-
#' typical an integer starting from 1.
359+
#' typical an integer starting from 1.
350360
#' @param resolution the resolution IDs of the pyramidal image,
351-
#' typical an integer starting from 1.
361+
#' typical an integer starting from 1.
352362
#' @param max.pixel.threshold the maximum width
353363
#' and height pixel dimension that the lowest level of the image pyramid
354364
#' should have, thus the image will be downscaled two folds until both width
355-
#' and height is below the threshold. Default is 700 pixels.
365+
#' and height is below the threshold. Default is 700 pixels.
356366
#' If \code{n.levels} is provided, this parameter will be ignored.
357367
#' @param engine the package to use for each image layer: either
358368
#' \code{EBImage} or \code{magick-image}
@@ -447,15 +457,15 @@ createImageArray <- function(
447457
#' @param replace Should the existing file be
448458
#' removed or not
449459
#' @param n.levels the number of levels if the image supposed to be
450-
#' pyramidal.
460+
#' pyramidal.
451461
#' @param chunkdim The dimensions of the chunks
452462
#' to use for writing the data to disk.
453463
#' @param level The compression level to use for
454464
#' writing the data to disk.
455465
#' @param engine the package to use for each image layer: either
456466
#' \code{EBImage} or \code{magick-image}
457467
#' @param verbose verbose
458-
#' @param ... additional parameters passed to
468+
#' @param ... additional parameters passed to
459469
#' \link[ImageArray]{createImageArray}.
460470
#'
461471
#' @importFrom HDF5Array writeHDF5Array
@@ -534,8 +544,9 @@ writeImageArray <- function(
534544
rhdf5::h5createFile(ondisk_path)
535545
}
536546
# TODO: is there a better way to check existing groups
537-
if(!name %in% c("", "/"))
547+
if (!name %in% c("", "/")) {
538548
rhdf5::h5createGroup(ondisk_path, group = name)
549+
}
539550
},
540551
ZarrImageArray = {
541552
dir.zarr <- gsub(paste0(basename(ondisk_path), "$"), "", ondisk_path)
@@ -548,7 +559,7 @@ writeImageArray <- function(
548559
ax <- axes(image_list)
549560
for (i in seq_len(length(image_list@levels))) {
550561
img <- image_list[[i]]
551-
562+
552563
# write array
553564
switch(
554565
format,
@@ -566,7 +577,7 @@ writeImageArray <- function(
566577
)
567578
},
568579
ZarrImageArray = {
569-
chunk_dim <- stats::setNames(dim(img),ax)
580+
chunk_dim <- stats::setNames(dim(img), ax)
570581
chunk_dim["x"] <- min(chunk_dim["x"], 2000)
571582
chunk_dim["y"] <- min(chunk_dim["y"], 2000)
572583
image_list[[i]] <-
@@ -583,15 +594,15 @@ writeImageArray <- function(
583594
}
584595

585596
# return
586-
return(image_list)
597+
image_list
587598
}
588599

589600
####
590601
# Auxiliary ####
591602
####
592603

593604
#' @noRd
594-
.img_create_msg <- function(dim_img, i){
605+
.img_create_msg <- function(dim_img, i) {
595606
cat(paste0(
596607
"Creating level ",
597608
i,

R/allgenerics.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ setGeneric("negate", function(object, ...) standardGeneric("negate"))
66
setGeneric("flip", function(object, ...) standardGeneric("flip"))
77
setGeneric("flop", function(object, ...) standardGeneric("flop"))
88
setGeneric("modulate", function(object, ...) standardGeneric("modulate"))
9-
setGeneric("axes", function(object, ...) standardGeneric("axes"))
9+
setGeneric("axes", function(object, ...) standardGeneric("axes"))

0 commit comments

Comments
 (0)