-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from jiajic/gg_image
fix: rework image plotting
- Loading branch information
Showing
7 changed files
with
869 additions
and
843 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: GiottoVisuals | ||
Title: Visuals for the Giotto spatial biology analysis ecosystem | ||
Version: 0.1.3 | ||
Version: 0.1.4 | ||
Authors@R: c( | ||
person("Ruben", "Dries", email = "[email protected]", | ||
role = c("aut", "cre")), | ||
|
@@ -82,6 +82,8 @@ Collate: | |
'colorRamp2.R' | ||
'color_palettes.R' | ||
'dd.R' | ||
'generics.R' | ||
'gg_annotation_raster.R' | ||
'gg_info_layers.R' | ||
'globals.R' | ||
'gstop.R' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# internals | ||
setOldClass("gg") | ||
setGeneric("gg_annotation_raster", function(ggobj, gimage, ...) standardGeneric("gg_annotation_raster")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
|
||
#' @name gg_annotation_raster | ||
#' @keywords internal | ||
#' @title Append image to ggplot as annotation_raster | ||
#' @param ggobj ggplot2 `gg` object | ||
#' @param gimage `giottoLargeImage`, `giottoImage` or `list` thereof | ||
#' @return `gg` object with images to plot appended as annotation rasters | ||
NULL | ||
|
||
#' @rdname gg_annotation_raster | ||
setMethod( | ||
"gg_annotation_raster", | ||
signature(ggobj = "gg", gimage = "list"), | ||
function(ggobj, gimage, ...) | ||
{ | ||
for(i in seq_along(gimage)) { | ||
ggobj <- gg_annotation_raster(ggobj, gimage[[i]], ...) | ||
} | ||
return(ggobj) | ||
} | ||
) | ||
|
||
#' @rdname gg_annotation_raster | ||
setMethod( | ||
"gg_annotation_raster", | ||
signature(ggobj = "gg", gimage = "giottoImage"), | ||
function(ggobj, gimage, ...) | ||
{ | ||
# extract min and max from object | ||
my_xmax = gimage@minmax[1] | ||
my_xmin = gimage@minmax[2] | ||
my_ymax = gimage@minmax[3] | ||
my_ymin = gimage@minmax[4] | ||
|
||
# convert giotto image object into array | ||
img_array = as.numeric(gimage@mg_object[[1]]) | ||
|
||
# extract adjustments from object | ||
xmax_b = gimage@boundaries[1] | ||
xmin_b = gimage@boundaries[2] | ||
ymax_b = gimage@boundaries[3] | ||
ymin_b = gimage@boundaries[4] | ||
|
||
# append to ggobj | ||
ggobj <- ggobj + annotation_raster( | ||
img_array, | ||
xmin = my_xmin-xmin_b, xmax = my_xmax+xmax_b, | ||
ymin = my_ymin-ymin_b, ymax = my_ymax+ymax_b | ||
) | ||
|
||
# TODO geom_raster to accommodate single-channel | ||
return(ggobj) | ||
} | ||
) | ||
|
||
#' @rdname gg_annotation_raster | ||
setMethod( | ||
"gg_annotation_raster", | ||
signature(ggobj = "gg", gimage = "giottoLargeImage"), | ||
function(ggobj, gimage, ...) | ||
{ | ||
# get plotting minmax | ||
extent = terra::ext(gimage@raster_object)[1:4] | ||
xmin = extent[['xmin']] | ||
xmax = extent[['xmax']] | ||
ymin = extent[['ymin']] | ||
ymax = extent[['ymax']] | ||
|
||
# convert raster object into array with 3 channels | ||
img_array = terra::as.array(gimage@raster_object) | ||
|
||
# TODO: check if required, fixes NaN values | ||
# replacing NA's by zero or another value directly in raster object? | ||
# raster[is.na(raster[])] <- 0 | ||
if (is.nan(max(img_array[,,1]))) { | ||
img_array[,,1][is.nan(img_array[,,1])] = max(img_array[,,1], na.rm = TRUE) | ||
} | ||
|
||
if(dim(img_array)[3] > 1) { | ||
if(is.nan(max(img_array[,,2]))) { | ||
img_array[,,2][is.nan(img_array[,,2])] = max(img_array[,,2], na.rm = TRUE) | ||
} | ||
} | ||
|
||
if(dim(img_array)[3] > 2) { | ||
if(is.nan(max(img_array[,,3]))) { | ||
img_array[,,3][is.nan(img_array[,,3])] = max(img_array[,,3], na.rm = TRUE) | ||
} | ||
} | ||
|
||
img_array = img_array/max(img_array, na.rm = TRUE) | ||
if(dim(img_array)[3] == 1) { | ||
img_array_RGB = array(NA, dim = c(dim(img_array)[1:2],3)) | ||
img_array_RGB[,,1:3] = img_array | ||
} else { | ||
img_array_RGB = img_array | ||
} | ||
|
||
# handle NA values | ||
img_array_RGB[is.na(img_array_RGB)] <- 0 | ||
|
||
# append to ggobj | ||
ggobj <- ggobj + annotation_raster( | ||
img_array_RGB, | ||
xmin = xmin, xmax = xmax, | ||
ymin = ymin, ymax = ymax | ||
) | ||
|
||
# TODO geom_raster to accommodate single-channel | ||
return(ggobj) | ||
} | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.