Skip to content

Commit

Permalink
Merge branch 'dev' into dev2
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajic committed May 21, 2024
2 parents 12769a0 + e9eaece commit 7d134b2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ exportMethods("prov<-")
exportMethods("spatUnit<-")
exportMethods(activeFeatType)
exportMethods(activeSpatUnit)
exportMethods(as.matrix)
exportMethods(as.points)
exportMethods(as.polygons)
exportMethods(as.sf)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

## new
- `as()` conversion from `giottoLargeImage` to `array`
- `as.matrix()` method for `spatLocsObj()`


# GiottoClass 0.3.0 (2024/05/13)
Expand Down
39 changes: 39 additions & 0 deletions R/methods-coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ NULL
#' data.table::as.data.table(g)
NULL

#' @title Coerce to matrix
#' @name as.matrix
#' @description Coerce to matrix
#' @param x object to coerce
#' @param id_rownames logical. Retain the spatial IDs as the rownames
#' @param \dots additional params to pass (none implemented)
#' @examples
#' sl <- GiottoData::loadSubObjectMini("spatLocsObj")
#' m <- as.matrix(sl)
#' @family As coercion functions
#' @returns matrix
NULL


#' @title Coerce to SpatVector polygons
#' @name as.polygons
Expand Down Expand Up @@ -107,6 +120,30 @@ as.data.table.giottoPoints <- function(x, ...) {



# to matrix ####


#' @rdname as.matrix
#' @export
setMethod("as.matrix", signature("spatLocsObj"), function(
x, id_rownames = TRUE, ...) {

x <- x[] # drop to DT
spat_cols <- c("sdimx", "sdimy", "sdimz")
spat_cols <- spat_cols %in% colnames(x)

m <- x[, spat_cols, with = FALSE] %>%
as.matrix()

if (id_rownames) {
rownames(m) <- x$cell_ID
}
return(m)
})




# image types ####

methods::setAs("giottoLargeImage", "giottoImage", function(from) {
Expand Down Expand Up @@ -137,6 +174,8 @@ methods::setAs("giottoLargeImage", "giottoImage", function(from) {
return(mImg)
})

# TODO redo this as `as.array`.
# Careful: There are already usages of this `as()` method in the code
methods::setAs("giottoLargeImage", "array", function(from) {
.spatraster_sample_values(
raster_object = from,
Expand Down
1 change: 1 addition & 0 deletions man/as.data.table.Rd

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

34 changes: 34 additions & 0 deletions man/as.matrix.Rd

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

1 change: 1 addition & 0 deletions man/as.points.Rd

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

1 change: 1 addition & 0 deletions man/as.polygons.Rd

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

1 change: 1 addition & 0 deletions man/r_spatial_conversions.Rd

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

0 comments on commit 7d134b2

Please sign in to comment.