Skip to content

Vectorize and parallelize the DE methods that support it - #1051

Merged
Zethson merged 1 commit into
mainfrom
feat/parallel-de-methods
Jul 25, 2026
Merged

Vectorize and parallelize the DE methods that support it#1051
Zethson merged 1 commit into
mainfrom
feat/parallel-de-methods

Conversation

@Zethson

@Zethson Zethson commented Jul 25, 2026

Copy link
Copy Markdown
Member

Closes #613.

Some parallelization but mostly vectorization improvements

Closes #613.

The per-variable Python loops in the simple tests and in `Statsmodels.fit` were slow for two reasons that parallelism alone does not address, so this fixes those first and then distributes what is left over jobs.

`SimpleComparisonBase._compare_single_group` sliced each variable with `x0[:, self.adata.var_names == var]`, building a mask of length `n_vars` per variable, and called the scalar scipy test once per variable.
`TTest` and `WilcoxonTest` now implement a `_test_vectorized` hook that tests all variables in a single call via scipy's `axis` argument, which is 38x faster on a 200x4000 matrix and gives identical p-values, test statistics and log fold changes.
Variables are processed in blocks bounded to ~64 MB so that densifying stays cheap on sparse input, and the blocks are distributed over jobs when there is more than one.

Methods without a vectorized counterpart, in practice `PermutationTest`, keep the per-variable path, which is now parallel.
Each variable gets its own random stream derived from the `rng` passed via `test_kwargs`, so seeded results are reproducible and do not depend on `n_jobs`.

`Statsmodels.fit` extracted its response vector with `sc.get.obs_df` per variable, which built a DataFrame each time and cost about 5x the model fitting it fed.
Extracting the column directly makes `fit` ~6x faster before any parallelism, and the fits are now distributed over jobs in blocks.

`n_jobs` defaults to None, i.e. joblib's default of a single job, so nothing spawns worker processes unless asked.
Since the backend is resolved by joblib, wrapping a call in `joblib.parallel_config` is enough to send the work to a dask cluster.

The joblib progress-bar wrapper is adapted from scirpy.

Signed-off-by: Lukas Heumos <lukas.heumos@posteo.net>
@Zethson
Zethson force-pushed the feat/parallel-de-methods branch from eb3131b to a4da8dc Compare July 25, 2026 22:05
@Zethson
Zethson merged commit 08a1051 into main Jul 25, 2026
19 checks passed
@Zethson
Zethson deleted the feat/parallel-de-methods branch July 25, 2026 22:13
@codecov-commenter

codecov-commenter commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.90476% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.75%. Comparing base (6152d32) to head (a4da8dc).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pertpy/_parallel.py 72.72% 9 Missing ⚠️
...ols/_differential_gene_expression/_simple_tests.py 94.87% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1051      +/-   ##
==========================================
- Coverage   78.76%   78.75%   -0.01%     
==========================================
  Files          51       52       +1     
  Lines        7167     7236      +69     
==========================================
+ Hits         5645     5699      +54     
- Misses       1522     1537      +15     
Files with missing lines Coverage Δ
...ools/_differential_gene_expression/_statsmodels.py 100.00% <100.00%> (ø)
...ols/_differential_gene_expression/_simple_tests.py 93.16% <94.87%> (-4.62%) ⬇️
pertpy/_parallel.py 72.72% <72.72%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Parallelize DE methods that support it

2 participants