pachterlab / SpatialFeatureExperiment

Extension of SpatialExperiment with sf
https://pachterlab.github.io/SpatialFeatureExperiment
Artistic License 2.0
43 stars 7 forks source link

Optimize `sfc` affine transformation #32

Closed lambdamoses closed 7 months ago

lambdamoses commented 9 months ago

The code using the binary arithmetic operators on sfc is slow when the number of geometries is large, which is bad news when there're hundreds of thousands of cells:

https://github.com/pachterlab/SpatialFeatureExperiment/blob/efed2e7c15a7949b9614c66a963d8e30a88d3f2c/R/geometry_operation.R#L627

From profiling, the slow part is the mapply called in the sf package, which is essentially an R loop that is not parallelized:

https://github.com/r-spatial/sf/blob/bd1940f68dd7607b7e9098ccc32c99e273c30a60/R/arith.R#L160

Try optimizing it by using st_coordinates() to get the coordinates, directly apply matrix multiplication for the linear transformation, and then use df2sf() (now using the really fast sfheaders package behind the scene) to turn it back to sf. Before going back to sf, I wonder if the sweep() step used to apply the translation is slow. I also wonder if this optimization issue should actually be opened in the sf repo instead of here.

lambdamoses commented 7 months ago

Right now the speedup would not work for GEOMETRY which has a mixture of different geometry types because st_coordinates would not work. I'm waiting for official performance improvements in sf itself as in https://github.com/r-spatial/sf/issues/2376