Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFE's imgRaster method doesn't follow the generic's contract #64

Open
LTLA opened this issue Jan 31, 2025 · 1 comment
Open

SFE's imgRaster method doesn't follow the generic's contract #64

LTLA opened this issue Jan 31, 2025 · 1 comment

Comments

@LTLA
Copy link

LTLA commented Jan 31, 2025

The contract for the SpatialExperiment::imgRaster() generic expects a raster object to be returned. This is not satisfied by the imgRaster() metbods in SpatialFeatureExperiment, which return instances of various other classes like SpatRaster and Image as per

#' @export
setMethod("imgRaster", "SpatRasterImage", function(x) as(x, "SpatRaster"))
#' @export
setMethod("imgRaster", "BioFormatsImage", function(x, resolution = 4L) {
toExtImage(x, resolution) |> imgRaster()
})
#' @export
setMethod("imgRaster", "ExtImage", function(x) as(x, "Image"))

AFAICT these are not subclasses of raster.

This discrepancy causes problems when an SFE is supplied to a function that accepts a SpatialExperiment. Functions calling imgRaster(imgData(spe)$data[[i]]) expect to get a raster object back, and fail when they are confronted with something else.

It would be best if your imgRaster() methods called as.raster() to comply with the generic contract. Of course, you can have other functions to return SpatRaster and Image directly, but those should not be called imgRaster().

@lambdamoses
Copy link
Collaborator

Sorry I didn't know that. Initially I thought the purpose of imgRaster() was just to access the image itself, so in early versions of SFE, imgRaster SpatRaster method would return the original SpatRaster object when SpatRasterImage did not directly inherit from SpatRaster. Later I changed SpatRasterImage to directly inherit from SpatRaster just because it's kind of annoying to have to call imgRaster before any terra functions can be applied to the image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants