Skip to content

Commit

Permalink
crop() method for giottoLargeImage
Browse files Browse the repository at this point in the history
- refactor portion of `createGiottoLargeImage()` for usage with `crop()` workflow
- add `crop()` import and method
- run document
  • Loading branch information
jiajic committed Sep 19, 2023
1 parent 4ab4d39 commit 53c5c7a
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 23 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Collate:
'methods-spin.R'
'methods-transpose.R'
'methods-wrap.R'
'methods_crop.R'
'provenance.R'
'python_environment.R'
'save_load.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ exportMethods(as.sf)
exportMethods(centroids)
exportMethods(colnames)
exportMethods(copy)
exportMethods(crop)
exportMethods(dim)
exportMethods(ext)
exportMethods(featIDs)
Expand Down Expand Up @@ -351,6 +352,7 @@ importMethodsFrom(Matrix,t)
importMethodsFrom(terra,"ext<-")
importMethodsFrom(terra,as.data.frame)
importMethodsFrom(terra,centroids)
importMethodsFrom(terra,crop)
importMethodsFrom(terra,ext)
importMethodsFrom(terra,flip)
importMethodsFrom(terra,ncol)
Expand Down
41 changes: 18 additions & 23 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ createGiottoObject = function(expression,
## evaluate if h5_file exists
if(!is.null(h5_file)) {
if(file.exists(h5_file)) {
wrap_msg("'", h5_file, "'",
wrap_msg("'", h5_file, "'",
" file already exists and will be replaced", sep = "")
file.remove(h5_file)
} else {
wrap_msg("Initializing file ", "'", h5_file, "'", sep = "")
}
}

### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
gobject = setExpression(gobject = gobject,
x = expression_data,
Expand Down Expand Up @@ -2526,31 +2526,22 @@ createGiottoLargeImage = function(raster_object,


## 5. Get image characteristics by sampling
sampleValues = stats::na.omit(terra::spatSample(raster_object,
size = 5000, # Defines the rough maximum of pixels allowed when resampling
method = 'regular',
value = TRUE))
if(nrow(sampleValues) == 0) {
sample_values = spatraster_sample_values(raster_object, size = 5000, verbose = verbose)

if(nrow(sample_values) == 0) {
if(verbose == TRUE) cat('No values discovered when sampling for image characteristics')
} else {
# get intensity range
srMinmax = suppressWarnings(terra::minmax(raster_object))
if(sum(is.infinite(srMinmax)) == 0) { # pull minmax values from terra spatRaster obj if img was small enough for them to be calculated
g_imageL@max_intensity = srMinmax[2]
g_imageL@min_intensity = srMinmax[1]
} else { # pull minmax values from sampled subset if img was too large
intensityRange = range(sampleValues)
g_imageL@max_intensity = intensityRange[2]
g_imageL@min_intensity = intensityRange[1]
}

# find estimated intensity range
intensity_range = spatraster_intensity_range(raster_object = raster_object,
sample_values = sample_values)
g_imageL@min_intensity = intensity_range[['min']]
g_imageL@max_intensity = intensity_range[['max']]

# find out if image is int or floating point
is_int = identical(sampleValues, round(sampleValues))
if(is_int == TRUE) {
g_imageL@is_int = TRUE
} else {
g_imageL@is_int = FALSE
}
is_int = spatraster_is_int(raster_object = raster_object,
sample_values = sample_values)
g_imageL@is_int = is_int
}


Expand All @@ -2563,6 +2554,10 @@ createGiottoLargeImage = function(raster_object,
}






#' @title createGiottoLargeImageList
#' @name createGiottoLargeImageList
#' @description Creates a list of large giotto images that can be added to a Giotto object. Generates deep copy of SpatRaster
Expand Down
67 changes: 67 additions & 0 deletions R/images.R
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,73 @@ plot_auto_largeImage_resample = function(gobject,



#' @title Sample values from SpatRaster
#' @name spatraster_sample_values
#' @param raster_object terra SpatRaster to sample from
#' @param size rough maximum of pixels allowed when resampling
#' @param verbose be verbose
#' @keywords internal
spatraster_sample_values = function(raster_object, size = 5000, verbose = TRUE) {
res = stats::na.omit(
terra::spatSample(
raster_object,
size = size,
method = 'regular',
value = TRUE)
)

if(nrow(res) == 0) {
if(isTRUE(verbose)) cat('No values discovered when sampling for image characteristics')
}

res
}




#' @title Find SpatRaster intensity range
#' @name spatraster_intensity_range
#' @keywords internal
#' @noRd
#' @return named numeric vector of min then max detected values
spatraster_intensity_range = function(
raster_object,
sample_values = spatraster_sample_values(raster_object)
) {
# get intensity range
srMinmax = suppressWarnings(terra::minmax(raster_object))
if(sum(is.infinite(srMinmax)) == 0) { # pull minmax values from terra spatRaster obj if img was small enough for them to be calculated
res = c(srMinmax[1], srMinmax[2])
} else { # pull minmax values from sampled subset if img was too large
intensityRange = range(sample_values)
res = c(intensityRange[1],intensityRange[2])
}

names(res) = c('min', 'max')
return(res)
}




#' @title Find SpatRaster int or floating point
#' @name spatraster_is_int
#' @keywords internal
#' @noRd
#' @return logical
spatraster_is_int = function(
raster_object,
sample_values = spatraster_sample_values(raster_object)
) {
# find out if image is int or floating point
identical(sample_values, round(sample_values))
}






#' @title Plot smoothed curve of giotto largeImage intensity values
#' @name density_giottoLargeImage
Expand Down
23 changes: 23 additions & 0 deletions R/methods_crop.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


#' @name crop-generic
#' @title Crop to a spatial subset
#' @description see [terra::crop]. Object x will be cropped using object y.
#' @param x object
#' @param y any object that has a SpatExtent or returns a SpatExtent
#' @param ... additional params to pass to terra::crop
NULL



#' @describeIn crop-generic Crop a giottoLargeImage
#' @export
setMethod('crop', signature('giottoLargeImage'), function(x, y, ...) {
x@raster_object = terra::crop(x@raster_object, y)
x@extent = ext(x@raster_object)
intensity_range = spatraster_intensity_range(x@raster_object)
x@min_intensity = intensity_range[['min']]
x@max_intensity = intensity_range[['max']]

x
})
1 change: 1 addition & 0 deletions R/package_imports.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' @importMethodsFrom terra plot
#' @importMethodsFrom terra wrap
#' @importMethodsFrom terra vect
#' @importMethodsFrom terra crop
#' @importMethodsFrom terra as.data.frame
#' @importMethodsFrom terra nrow ncol
#' @importClassesFrom terra SpatExtent
Expand Down
24 changes: 24 additions & 0 deletions man/crop-generic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/ext-generic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/spatraster_sample_values.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 53c5c7a

Please sign in to comment.