Skip to content

Commit

Permalink
Merge pull request #240 from jiajic/defer
Browse files Browse the repository at this point in the history
feat: `doDeferred()`
  • Loading branch information
jiajic authored Oct 22, 2024
2 parents d47a88c + 8cc4dec commit a6d1641
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
12 changes: 7 additions & 5 deletions R/classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -1660,14 +1660,16 @@ giottoLargeImage <- setClass(
#' @title S4 giottoAffineImage Class
#' @description
#' Class extending `giottoLargeImage`. When `shear()` or `spin()` operations
#' are performed on
#'
#'
#' are performed on a `giottoLargeImage`, this class is instantiated. It
#' provides a way of storing the affine transformation and also lazily
#' performing it when required for a plotting preview. It is possible to force
#' the deferred affine transform using `doDeferred()` and return a processed
#' `giottoLargeImage`.
#' @slot affine contains `affine2d` object allowing lazily performed spatial
#' transforms
#' @slot funs list of functions associated with the object. Primarily to
#' perform the delayed/lazy operations
#' @returns giottoAffineImage
#' perform the delayed/lazy operation
#' @returns `giottoAffineImage`
setClass(
"giottoAffineImage",
contains = c("giottoLargeImage"),
Expand Down
5 changes: 5 additions & 0 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ setGeneric("copy",
)


# lazy operations ####
setGeneric("doDeferred", function(x, ...) standardGeneric("doDeferred"))



# spatial operations ####
setGeneric(
"calculateOverlap",
Expand Down
25 changes: 21 additions & 4 deletions R/images.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,19 @@ get_adj_rescale_img <- function(
# can be loaded in with terra or used with getOption("viewer")() downstream
# based on magick:::image_preview()
# accepts a single `magick-image` object
.magick_preview <- function(x, tempname = "preview") {
# only returns depth 8 images. DO NOT use for analyzed values
.magick_preview <- function(x,
basename = "preview",
filename = NULL) {
stopifnot(inherits(x, "magick-image"))
stopifnot(length(x) == 1L)
format <- tolower(magick::image_info(x[1])$format)
tmp <- file.path(tempdir(), paste(tempname, format, sep = "."))
if (is.null(filename)) {
filename <- file.path(tempdir(), paste(basename, format, sep = "."))
}
vmsg(.is_debug = TRUE, "`.magick_preview()` saving as", format)
magick::image_write(x, path = tmp, format = format, depth = 8)
return(tmp)
magick::image_write(x, path = filename, format = format, depth = 8)
return(filename)
}

#' @title addGiottoImageMG
Expand Down Expand Up @@ -2743,6 +2748,18 @@ add_img_array_alpha <- function(




# doDeferred ####

#' @export
setMethod(
"doDeferred", signature("giottoAffineImage"),
function(x, size = 5e5, filename = NULL, ...) {
x@funs$realize_magick(filename = filename, size = size, ...)
})



# converters ####


Expand Down
4 changes: 2 additions & 2 deletions R/methods-initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ setMethod("initialize", signature("giottoAffineImage"), function(.Object, ...) {
.Object@extent <- .ext_to_num_vec(ext(d))
}

.Object@funs$realize_magick <- function(tempname = "preview", size = 5e5) {
.Object@funs$realize_magick <- function(filename = NULL, size = 5e5) {
mg <- .gaffine_realize_magick(.Object, size = size)
gimg <- .magick_preview(mg@mg_object, tempname = tempname) %>%
gimg <- .magick_preview(mg@mg_object, filename = filename) %>%
createGiottoLargeImage()
ext(gimg) <- ext(.Object)

Expand Down

0 comments on commit a6d1641

Please sign in to comment.