Skip to content
Snippets Groups Projects
Commit da4e844e authored by Jean-Karim Heriche's avatar Jean-Karim Heriche
Browse files

Notification for lack of ome-zarr support in producing galleries.

parent 3202dac6
No related branches found
No related tags found
No related merge requests found
Pipeline #49056 failed
......@@ -35,22 +35,22 @@ explore_gallery_server <-function(input, output, session, rv, session_parent){
dtmp <- rv$data[rv$selectedRows,]
}
imgPaths <- as.character(rv$data[rv$selectedRows, get(rv$fileCol1)])
# Too many images or objects can take too long to process or blow up resources
# but also are probably not so useful.
max.objects <- 100
if(nrow(dtmp)>max.objects) {
# Reduce size by taking a sample
sample.idx <- sample(nrow(dtmp), max.objects)
dtmp <- dtmp[sample.idx,]
imgPaths <- imgPaths[sample.idx]
}
slice.def <- list()
img.metadata <- list()
if(class(rv$imgRoot)[1] == "s3_bucket"){ # Images are in a S3 bucket
if(any(grepl("\\.ome\\.zarr$", imgPaths))) { # NGFF, use omeRarr
showNotification("Galleries from ome-zarr files not supported yet", duration = 10, type="warning")
return(NULL)
} else {
if(any(grepl("\\.ome\\.zarr$", imgPaths))) { # NGFF, use omeRarr
showNotification("Galleries from ome-zarr files not supported yet", duration = 10, type="warning")
return(NULL)
} else {
# Too many images or objects can take too long to process or blow up resources
# but also are probably not so useful.
max.objects <- 100
if(nrow(dtmp)>max.objects) {
# Reduce size by taking a sample
sample.idx <- sample(nrow(dtmp), max.objects)
dtmp <- dtmp[sample.idx,]
imgPaths <- imgPaths[sample.idx]
}
slice.def <- list()
img.metadata <- list()
if(class(rv$imgRoot)[1] == "s3_bucket"){ # Images are in a S3 bucket
file.list <- c()
for(i in 1:length(imgPaths)) {
file.list[i] <- save_object(object = imgPaths[i], bucket = rv$imgRoot,
......@@ -58,121 +58,122 @@ explore_gallery_server <-function(input, output, session, rv, session_parent){
file = tempfile())
}
}
} else if(!is.null(rv$imgRoot)) { # Images are on a filesystem
rootDir <- file.path(rv$imgRoot)
file.list <- file.path(rootDir, imgPaths)
}
if(!is.null(rv$roiFrame) && rv$roiFrame != "") {
## Figure out what the 3rd dimension is from first image
metadata <- read.metadata(file.list[[1]])$coreMetadata
if(metadata$sizeZ == 1 && metadata$sizeT > 1) {
frameName <- 'T'
} else if(metadata$sizeZ > 1 && metadata$sizeT == 1) {
frameName <- 'Z'
}
## Slice definition for each image
for(i in 1:nrow(dtmp)) {
slice.def[[i]] <- list()
slice.def[[i]][[frameName]] = rv$selectedFrame[i]
else if(!is.null(rv$imgRoot)) { # Images are on a filesystem
rootDir <- file.path(rv$imgRoot)
file.list <- file.path(rootDir, imgPaths)
}
}
# Do we have objects or images?
# If images, simply tile them
# If objects, we need to crop them out first then tile them
tile.w <- 128
tile.h <- 128
if(!is.null(rv$roiX ) && !is.null(rv$roiY) && rv$roiX != "" && rv$roiY != "") {
# Assume we have objects
obj <- list()
for(i in 1:nrow(dtmp)) {
metadata <- read.metadata(file.list[[i]])$coreMetadata
x <- round(dtmp[i, get(rv$roiX)])
dx <- c((x-floor(tile.w/2)) : (x+ceiling(tile.w/2)-1))
y <- round(dtmp[i, get(rv$roiY)])
dy <- c((y-floor(tile.h/2)) : (y+ceiling(tile.h/2)-1))
# Crop image, taking care of object near image borders
dx <- dx[dx>0 & dx<metadata$sizeX]
dy <- dy[dy>0 & dy<metadata$sizeY]
if(length(dx) == 0 || length(dy) == 0) {
showNotification("Object not in selected image. Check that anchor point coordinates come from the selected image.", duration = 10, type="error")
return(NULL)
if(!is.null(rv$roiFrame) && rv$roiFrame != "") {
## Figure out what the 3rd dimension is from first image
metadata <- read.metadata(file.list[[1]])$coreMetadata
if(metadata$sizeZ == 1 && metadata$sizeT > 1) {
frameName <- 'T'
} else if(metadata$sizeZ > 1 && metadata$sizeT == 1) {
frameName <- 'Z'
}
## Slice definition for each image
for(i in 1:nrow(dtmp)) {
slice.def[[i]] <- list()
slice.def[[i]][[frameName]] = rv$selectedFrame[i]
}
slice.def[[i]]$X <- dx
slice.def[[i]]$Y <- dy
obj[[i]] <- read.image(file.list[[i]], subset = slice.def[[i]])
}
obj <- lapply(obj, EBImage::normalize, separate = TRUE)
I <- combine(obj)
} else { # We deal with whole images
depth <- 1 # Images may have variable number of slices
for(f in file.list) {
img.metadata <- read.metadata(f)
# Adjust tile dimensions to images
tile.w <- max(tile.w, floor(img.metadata$coreMetadata$sizeX/10))
tile.h <- max(tile.h, floor(img.metadata$coreMetadata$sizeY/10))
depth <- max(depth, img.metadata$coreMetadata$sizeZ)
}
# We may still have too many images after the sample taking above
# which doesn't account for size
final.size <- tile.w*tile.h*length(file.list) # in number of pixels
threshold <- 1e8 # pixels
if(final.size>threshold) {
# Reduce size by taking a sample
# Make sample size adaptive
sample.size <- floor(threshold/(1.2*tile.w*tile.h))
file.list <- sample(file.list, sample.size)
}
img <- list()
for(i in 1:length(file.list)) {
img.file <- file.list[[i]]
if(!is.na(img.file)) {
img[[i]] <- resize(read.image(img.file, subset = slice.def), w = tile.w)
if(dim(img[[i]])[2] > tile.h) {
tile.h <- dim(img[[i]])[2]
# Do we have objects or images?
# If images, simply tile them
# If objects, we need to crop them out first then tile them
tile.w <- 128
tile.h <- 128
if(!is.null(rv$roiX ) && !is.null(rv$roiY) && rv$roiX != "" && rv$roiY != "") {
# Assume we have objects
obj <- list()
for(i in 1:nrow(dtmp)) {
metadata <- read.metadata(file.list[[i]])$coreMetadata
x <- round(dtmp[i, get(rv$roiX)])
dx <- c((x-floor(tile.w/2)) : (x+ceiling(tile.w/2)-1))
y <- round(dtmp[i, get(rv$roiY)])
dy <- c((y-floor(tile.h/2)) : (y+ceiling(tile.h/2)-1))
# Crop image, taking care of object near image borders
dx <- dx[dx>0 & dx<metadata$sizeX]
dy <- dy[dy>0 & dy<metadata$sizeY]
if(length(dx) == 0 || length(dy) == 0) {
showNotification("Object not in selected image. Check that anchor point coordinates come from the selected image.", duration = 10, type="error")
return(NULL)
}
if(length(dim(img[[i]]))>2 && dim(img[[i]])[3] > depth) {
depth <- dim(img[[i]])[3]
slice.def[[i]]$X <- dx
slice.def[[i]]$Y <- dy
obj[[i]] <- read.image(file.list[[i]], subset = slice.def[[i]])
}
obj <- lapply(obj, EBImage::normalize, separate = TRUE)
I <- combine(obj)
} else { # We deal with whole images
depth <- 1 # Images may have variable number of slices
for(f in file.list) {
img.metadata <- read.metadata(f)
# Adjust tile dimensions to images
tile.w <- max(tile.w, floor(img.metadata$coreMetadata$sizeX/10))
tile.h <- max(tile.h, floor(img.metadata$coreMetadata$sizeY/10))
depth <- max(depth, img.metadata$coreMetadata$sizeZ)
}
# We may still have too many images after the sample taking above
# which doesn't account for size
final.size <- tile.w*tile.h*length(file.list) # in number of pixels
threshold <- 1e8 # pixels
if(final.size>threshold) {
# Reduce size by taking a sample
# Make sample size adaptive
sample.size <- floor(threshold/(1.2*tile.w*tile.h))
file.list <- sample(file.list, sample.size)
}
img <- list()
for(i in 1:length(file.list)) {
img.file <- file.list[[i]]
if(!is.na(img.file)) {
img[[i]] <- resize(read.image(img.file, subset = slice.def), w = tile.w)
if(dim(img[[i]])[2] > tile.h) {
tile.h <- dim(img[[i]])[2]
}
if(length(dim(img[[i]]))>2 && dim(img[[i]])[3] > depth) {
depth <- dim(img[[i]])[3]
}
}
}
# Ensure all images have the same size
if(length(dim(img[[1]])) == 2) {
dims <- c(tile.w, tile.h)
} else {
dims <- c(tile.w, tile.h, depth)
}
I <- list()
for(i in 1:length(img)) {
a <- as.Image(array(0, dim = dims))
for(j in 1:length(dim(a))) {
dimnames(img[[i]])[[j]] <- 1:dim(img[[i]])[j]
dimnames(a)[[j]] <- 1:dim(a)[j]
}
abind::afill(a) <- img[[i]]
I[[i]] <- a
}
I <- abind(I, along = (length(dim(I[[1]]))+1))
}
# Ensure all images have the same size
if(length(dim(img[[1]])) == 2) {
dims <- c(tile.w, tile.h)
} else {
dims <- c(tile.w, tile.h, depth)
I <- EBImage::normalize(I, separate = FALSE)
img.per.row <- 8
if(nrow(dtmp) < img.per.row) {
img.per.row <- nrow(dtmp)
}
I <- list()
for(i in 1:length(img)) {
a <- as.Image(array(0, dim = dims))
for(j in 1:length(dim(a))) {
dimnames(img[[i]])[[j]] <- 1:dim(img[[i]])[j]
dimnames(a)[[j]] <- 1:dim(a)[j]
if(length(dim(I)) == 3) {
I <- tile(I, nx = img.per.row)
} else { # Tile each channel separately
# Assume we have 4D (since if 5D, one of t or z should have been fixed)
img <- list()
for(k in 1:dim(I)[3]) {
img[[k]] <- as.Image(I[,,k,])
# if colorMode(I) > 0, prevent the third dimension
# from being interpreted as channels
colorMode(img[[k]]) <- Grayscale
}
abind::afill(a) <- img[[i]]
I[[i]] <- a
}
I <- abind(I, along = (length(dim(I[[1]]))+1))
}
I <- EBImage::normalize(I, separate = FALSE)
img.per.row <- 8
if(nrow(dtmp) < img.per.row) {
img.per.row <- nrow(dtmp)
}
if(length(dim(I)) == 3) {
I <- tile(I, nx = img.per.row)
} else { # Tile each channel separately
# Assume we have 4D (since if 5D, one of t or z should have been fixed)
img <- list()
for(k in 1:dim(I)[3]) {
img[[k]] <- as.Image(I[,,k,])
# if colorMode(I) > 0, prevent the third dimension
# from being interpreted as channels
colorMode(img[[k]]) <- Grayscale
I <- combine(lapply(img, tile, nx = img.per.row))
}
I <- combine(lapply(img, tile, nx = img.per.row))
}
I
}
I
}
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment