Skip to content

feat: parallelize per-library spatial graph construction#1144

Closed
Marius1311 wants to merge 1 commit into
scverse:mainfrom
quadbio:feat/parallel-spatial-neighbors
Closed

feat: parallelize per-library spatial graph construction#1144
Marius1311 wants to merge 1 commit into
scverse:mainfrom
quadbio:feat/parallel-spatial-neighbors

Conversation

@Marius1311
Copy link
Copy Markdown

Add n_jobs parameter to spatial_neighbors() to compute per-library graphs in parallel via joblib. Defaults to 1 (sequential, no behavior change). Set to -1 to use all CPUs.

When library_key is set, each library's graph is already computed independently, so this is a trivially parallel workload. For datasets with many libraries (e.g., multi-sample spatial transcriptomics), this gives a near-linear speedup.

IMPORTANT: Please search among the Pull requests before creating one.

Description

How has this been tested?

Closes

Add `n_jobs` parameter to `spatial_neighbors()` to compute per-library
graphs in parallel via joblib. Defaults to 1 (sequential, no behavior
change). Set to -1 to use all CPUs.

When `library_key` is set, each library's graph is already computed
independently, so this is a trivially parallel workload. For datasets
with many libraries (e.g., multi-sample spatial transcriptomics), this
gives a near-linear speedup.
@Marius1311 Marius1311 force-pushed the feat/parallel-spatial-neighbors branch from 89f8be4 to 5eda868 Compare March 23, 2026 16:50
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.08%. Comparing base (9690a55) to head (5eda868).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1144      +/-   ##
==========================================
+ Coverage   74.05%   74.08%   +0.03%     
==========================================
  Files          39       39              
  Lines        6495     6503       +8     
  Branches     1122     1122              
==========================================
+ Hits         4810     4818       +8     
  Misses       1230     1230              
  Partials      455      455              
Files with missing lines Coverage Δ
src/squidpy/gr/_build.py 88.88% <100.00%> (+0.36%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@selmanozleyen
Copy link
Copy Markdown
Member

Thank you but we are working on making updates to spatial_neighbours: #1147 Stay tuned, after these changes your version should be trivial to implement!

@grst
Copy link
Copy Markdown
Contributor

grst commented Jun 2, 2026

Hi @Marius1311,

#1147 has been merged.
Your implementation would have to be adapted accordingly, to parallelize this section:

if library_key is not None:
mats: list[tuple[Any, Any]] = []
ixs: list[int] = []
for lib in libs:
ixs.extend(np.where(adata.obs[library_key] == lib)[0])
mats.append(builder.build(adata[adata.obs[library_key] == lib].obsm[spatial_key]))
adj, dst = builder.combine(mats, ixs)

Do you want to work on this?

@Marius1311
Copy link
Copy Markdown
Author

thanks @grst! In my own tests, my parallelization actually didn't lead to any speed improvements, not sure why!

@Marius1311
Copy link
Copy Markdown
Author

So we can close this if you like.

@grst
Copy link
Copy Markdown
Contributor

grst commented Jun 2, 2026

I observed something similar, and I think a large part of the runtime is spent on subsetting the spatialdata/anndata object before any nearest neighbor code is even triggered.

I still think it should be possible to optimize this, but then it's likely not a trivial fix as thought initially.

@grst
Copy link
Copy Markdown
Contributor

grst commented Jun 3, 2026

Closing in favor of #1198

@grst grst closed this Jun 3, 2026
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

Successfully merging this pull request may close these issues.

3 participants