Repository navigation
Conversation
for more information, see https://pre-commit.ci
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
SpatialData inputs currently recompute element centroids and coordinate transformations on every spatial-neighbor call, even when the selected table already contains the required coordinates. This repeats input preparation when users rebuild a graph with different parameters, as reported in #1199.
Add the keyword-only parameter
coordinate_sourceto all six spatial-neighbor entry points. The default,"elements", preserves centroid computation. With"table", graph construction reads the selected table'sobsm[spatial_key]directly and skips centroid computation and coordinate transformation. AnnData inputs continue to use their existing coordinates with either value.Table mode retains region and instance association checks and requires finite, real numeric NumPy coordinates of shape
(n_obs, 2)in table observation order. Coordinates must already be in the desired coordinate system. Missing or invalid coordinates raise an error without falling back to element centroids. Withcopy=True, coordinates are copied into the existing lightweight temporary AnnData without loading the expression matrix. Withcopy=False, the original coordinate array is reused and graph results are saved to the table.The change includes shared parameter documentation, a radius-graph example, regression tests, and a reproducible benchmark comparing element coordinates, table coordinates, and the equivalent AnnData input.
How has this been tested?
copy=Falsemakes no coordinate assignments, missing keys raiseKeyErrorin every entry point, and coordinate changes by a builder leave source data unchanged withcopy=True.git diff --checkpass. Checked expanded documentation for all six interfaces and rendered the affected API documentation and benchmark README with Sphinx.Performance
Benchmarked commit
cbf140don macOS ARM64 with Python 3.13.3 and SpatialData 0.8.0. Each case uses synthetic 16-vertex polygons and identical coordinates and region groups across the three paths. Radius is 1.6,copy=True, andn_jobs=1; numerical-library thread limits are one. Imports, warmup, fixture construction, and initial coordinate preparation are excluded from graph timings. The values below are medians of five repeated calls per path, in seconds.For 100,000 cells across 20 regions, total time decreases by 35.9%. Input preparation drops from 0.28549 s to 0.03208 s (8.90x), while graph construction time remains similar. Table mode makes zero centroid calls in all cases; element mode calls it once per region on every graph call. All 51 timed graph outputs match the reference CSR structure and values, and input observations and coordinates remain unchanged. The CSV samples and summary medians were independently cross-checked, and the recorded source hashes match the tested code.
Reproduce with the benchmark script:
PYTHONPATH="$PWD/src" python benchmarks/bench_spatial_neighbors_input.py \ --output /tmp/squidpy-1199-benchmarkThe benchmark README also documents the historical baseline and the earlier Linux run, with their environments and raw measurements. The table above compares paths within the final macOS run.
Closes
Closes #1199.