Vectorize and parallelize the DE methods that support it - #1051
Merged
Conversation
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
force-pushed
the
feat/parallel-de-methods
branch
from
July 25, 2026 22:05
eb3131b to
a4da8dc
Compare
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
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.
Closes #613.
Some parallelization but mostly vectorization improvements