Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
like-for-like steps and still loses the standard workflow by 1.2–2.5x, to seeded
UMAP and presto's Wilcoxon. With Seurat's optimiser on both sides, clustering finds
the same number of clusters on all four datasets, at 1.1–1.2x Seurat's time from
8,000 cells up. DESeq2 has a row for the first time, 3.9x faster than Seurat's, but
pydeseq2 starts one worker process per core, and they took the process tree from
under 1 GB to 5.2 GB. The M4 Pro sweep is kept in `results_m4pro/`, and the report's
two findings from it are labelled as such.
8,000 cells up. DESeq2 has a row for the first time, 2.5x faster than Seurat's on
half its memory. The M4 Pro sweep is kept in `results_m4pro/`, and the report's two
findings from it are labelled as such.

### Fixed

Expand Down Expand Up @@ -490,6 +489,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The DE tutorial's fold-change figure was titled "13,712 genes, 1,172 shared
cells", typed in before the feature renaming and the new clusters. The title is
now counted from the data: 13,714 genes and 1,183 cells.
- **`find_markers(test_use="deseq2")` runs in one process.** pydeseq2 starts a joblib
worker per CPU core, once for its data set and again for its test, and the workers
outlive the call. Re-measuring the performance report found them taking the process
tree from under 1 GB to 5.2 GB on PBMC 3k's DE bench to save 1.5 s, and doubling
the out-of-core tutorial's peak to 11.5 GB. DESeq2 runs in one R process and the
answer does not depend on the worker count, so truecell now gives pydeseq2 one. The
bench's call takes 4.0 s where it took 2.5 s, still 2.5x faster than Seurat's, and
peaks at 1.2 GB. `tests/test_deseq2_seurat_parity.py` fails if any of pydeseq2's
parallel sections asks for more than one job.

## [1.2.0] - 2026-08-10

Expand Down
26 changes: 26 additions & 0 deletions tests/test_deseq2_seurat_parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ def test_the_fit_tracks_deseq2_step_by_step():
assert nan_pattern == np.isnan(_values("pvalue")).tolist()


def test_the_fit_runs_in_one_process(monkeypatch):
"""DESeq2 runs in one R process; pydeseq2 starts a joblib worker per core.

Those workers outlive the call. On the benchmark's PBMC 3k DE call they took the
process tree from under 1 GB to 5.1 GB to save 1.6 s, for the same output. So
every parallel section pydeseq2 opens must ask for one job.
"""
import pydeseq2.default_inference as inference

jobs = []
real_parallel = inference.Parallel

def recording_parallel(*args, **kwargs):
jobs.append(kwargs.get("n_jobs"))
return real_parallel(*args, **kwargs)

monkeypatch.setattr(inference, "Parallel", recording_parallel)
genes, samples, groups = REF["genes"], np.array(REF["samples"]), np.array(REF["groups"])
counts = pd.DataFrame(np.array(REF["counts"], dtype=np.int64), index=genes, columns=samples)
order = list(samples[groups == "A"]) + list(samples[groups == "B"])
_deseq2.fit(counts.loc[REF["tested_features"], order].T,
np.isin(order, samples[groups == "A"]))
assert jobs, "pydeseq2 opened no parallel section, so nothing was checked"
assert set(jobs) == {1}, jobs


LOW = REF["low_count"]


Expand Down
9 changes: 8 additions & 1 deletion truecell/_deseq2.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def fit(counts: pd.DataFrame, group1: np.ndarray):
p-values, NaN where ``results()``' Cook's cutoff removes them, as R gives NA.
"""
from pydeseq2.dds import DeseqDataSet
from pydeseq2.default_inference import DefaultInference
from pydeseq2.ds import DeseqStats

values = counts.to_numpy()
Expand All @@ -144,11 +145,17 @@ def fit(counts: pd.DataFrame, group1: np.ndarray):
group1 = np.asarray(group1, dtype=bool)
metadata = pd.DataFrame(
{"condition": np.where(group1, "group1", "group2")}, index=counts.index)
# One process, as DESeq2 runs in R. Left to itself, pydeseq2 starts a joblib
# worker per CPU core for each object below, and the workers outlive the call:
# on the benchmark's PBMC 3k DE call they took the process tree from under 1 GB
# to 5.1 GB to save 1.6 s. The answer does not depend on the worker count.
inference = DefaultInference(n_cpus=1)
# max_disp is DESeq2's maxDisp, max(10, samples). pydeseq2 enforces that maximum
# itself; passing it keeps the rule from depending on that.
dds = DeseqDataSet(
counts=counts, metadata=metadata, design="~condition", refit_cooks=False,
max_disp=max(10.0, float(counts.shape[0])), min_disp=MIN_DISP, quiet=True,
inference=inference,
)
dds.fit_size_factors(fit_type="ratio")
dds.fit_genewise_dispersions()
Expand All @@ -175,7 +182,7 @@ def fit(counts: pd.DataFrame, group1: np.ndarray):
dds.calculate_cooks()

stats = DeseqStats(dds, contrast=["condition", "group1", "group2"], alpha=0.05,
quiet=True)
quiet=True, inference=inference)
# nbinomWaldTest floors the fitted means at minmu before weighting them;
# DeseqStats.run_wald_test does not, so run its Wald test with the floor.
design = stats.design_matrix.to_numpy(dtype=float)
Expand Down
86 changes: 42 additions & 44 deletions tutorials/benchmark/PERFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Reproduce with `bash tutorials/benchmark/sweep.sh` (41 minutes here).
> clusters on all four datasets (9, 12, 16 and 8; truecell 1.0.0 found 8, 9, 15
> and 7), at close to Seurat's cost — see
> [2.5](#25-clustering-runs-seurats-optimiser). And `find_markers(test_use="deseq2")`
> is Seurat's per-cell test now, so DESeq2 has a row for the first time, with a
> memory cost worth knowing about ([section 5](#5-named-operations)).
> is Seurat's per-cell test now, so DESeq2 has a row for the first time
> ([section 5](#5-named-operations)).

---

Expand All @@ -51,16 +51,16 @@ Seurat's Wilcoxon is presto's C++.
| VST feature selection | | **1.7–2.5x faster** |
| Scaling; exact neighbours | 1.1–1.8x and 1.5–3.2x faster on two datasets each | 1.1–1.6x and 1.1–3.0x faster on the other two |
| Clustering, Seurat's optimiser on both sides | | **1.1–1.2x faster** at ≥8k cells, 2.3x at 2.7k |
| Differential expression, 7 of the 8 shared tests | **1.7–8.2x faster** | |
| Wilcoxon — `de_wilcox` and `find_all_markers` | | **1.6–3.6x faster** (presto) |
| Differential expression, 7 of the 8 shared tests | **1.7–8.9x faster** | |
| Wilcoxon — `de_wilcox` and `find_all_markers` | | **1.6–3.7x faster** (presto) |
| Seeded UMAP | | **1.7–3.9x faster** |
| Harmony | **1.6x faster** | |
| CCA integration | | **3.1x faster** |
| RPCA integration | **1.9x faster** | |
| SCTransform | **1.4x faster**, half the memory | |
| Moran's I | **77x faster**, and the only one without an *n* limit | |
| Peak memory, standard workflow | **1.04–1.4x lighter** at ≥8k cells | 1.5x lighter at 2.7k |
| Peak memory, the DE bench | | **2.0x lighter**: pydeseq2 starts a worker process per core (section 5) |
| Peak memory, the DE bench | **2.0x lighter**, with DESeq2 in one process (section 5) | |

The tally is generated by `make_report.py`, not counted by hand.

Expand Down Expand Up @@ -416,18 +416,18 @@ Median of 2 timed repeats per arm, warm-up discarded. *Shared pipeline* excludes

| Step | Truecell | Seurat | Faster by | Truecell peak RSS | Seurat peak RSS | Truecell result | Seurat result |
|---|---:|---:|---:|---:|---:|---:|---:|
| `import` | 0.44s | — | — | 143 MB | — | | |
| `prep` | 1.01s | 1.91s | **1.9x** | 863 MB | 909 MB | 9 | 9 |
| `de_wilcox` | 0.18s | 0.05s | 3.6x slower | 864 MB | 936 MB | 1254 | 1254 |
| `de_t` | 0.22s | 0.75s | **3.3x** | 864 MB | 1126 MB | 1254 | 1254 |
| `de_bimod` | 0.11s | 0.72s | **6.3x** | 865 MB | 992 MB | 1254 | 1254 |
| `de_LR` | 1.44s | 2.41s | **1.7x** | 916 MB | 1053 MB | 1254 | 1254 |
| `de_negbinom` | 2.38s | 6.25s | **2.6x** | 922 MB | 1108 MB | 1254 | 1254 |
| `de_roc` | 0.19s | 1.54s | **8.2x** | 922 MB | 1145 MB | 1254 | 1254 |
| `de_MAST` | 1.55s | 6.37s | **4.1x** | 924 MB | 1709 MB | 1254 | 1254 |
| `de_DESeq2` | 2.50s | 9.67s | **3.9x** | 5175 MB | 2543 MB | 2347 | 2347 |
| `library_load` | — | 1.21s | — | — | 475 MB | | |
| **shared pipeline** | **9.6s** | **29.7s** | **3.1x** | **5177 MB** | **2543 MB** | | |
| `import` | 0.45s | — | — | 140 MB | — | | |
| `prep` | 1.01s | 1.98s | **2.0x** | 863 MB | 1005 MB | 9 | 9 |
| `de_wilcox` | 0.19s | 0.05s | 3.7x slower | 864 MB | 1005 MB | 1254 | 1254 |
| `de_t` | 0.23s | 0.78s | **3.4x** | 864 MB | 1157 MB | 1254 | 1254 |
| `de_bimod` | 0.11s | 0.73s | **6.4x** | 865 MB | 992 MB | 1254 | 1254 |
| `de_LR` | 1.46s | 2.48s | **1.7x** | 916 MB | 1099 MB | 1254 | 1254 |
| `de_negbinom` | 2.37s | 6.36s | **2.7x** | 923 MB | 1143 MB | 1254 | 1254 |
| `de_roc` | 0.19s | 1.67s | **8.9x** | 923 MB | 1174 MB | 1254 | 1254 |
| `de_MAST` | 1.57s | 5.92s | **3.8x** | 924 MB | 1829 MB | 1254 | 1254 |
| `de_DESeq2` | 4.00s | 10.01s | **2.5x** | 1190 MB | 2395 MB | 2347 | 2347 |
| `library_load` | — | 1.26s | — | — | 419 MB | | |
| **shared pipeline** | **11.1s** | **30.0s** | **2.7x** | **1190 MB** | **2395 MB** | | |

Median of 2 timed repeats per arm, warm-up discarded. *Shared pipeline* excludes interpreter start-up.

Expand Down Expand Up @@ -470,20 +470,19 @@ completely unaffected by the BLAS. Both arms test the same two groups of cells
(truecell writes the assignment, R adopts it). The seven tests that apply the
fold-change pre-filter test the same 1,254 genes and return 1,254 rows on both
sides; DESeq2, which skips that filter in both tools, tests 2,347. Truecell
wins seven of the eight: `roc` 8.2x, `bimod` 6.3x, `MAST` 4.1x, `DESeq2` 3.9x,
`t` 3.3x, `negbinom` 2.6x, `LR` 1.7x. It loses `wilcox` by 3.6x, which is
wins seven of the eight: `roc` 8.9x, `bimod` 6.4x, `MAST` 3.8x, `t` 3.4x,
`negbinom` 2.7x, `DESeq2` 2.5x, `LR` 1.7x. It loses `wilcox` by 3.7x, which is
presto's C++ Wilcoxon against `scipy.stats.mannwhitneyu`. `negbinom` has been
truecell's own GLM since 2.0.0, where it was statsmodels', and stays 2.6x ahead.

**DESeq2 has a row now, and a memory cost.** In 1.0.0 the two tools meant
different things by the name and truecell's had no row here; since 2.0.0
`test_use="deseq2"` is Seurat's per-cell `DESeq2DETest`. It is 3.9x faster
(2.50s against 9.67s), but it takes the process tree from under 1 GB to 5.2 GB.
That is pydeseq2: it starts one worker process per core, 18 here, and truecell
does not limit it; the workers stay alive after the call returns. Measured
separately, the same call held to one worker takes 4.2s and peaks at 1.2 GB,
with identical output — still 2.3x faster than Seurat, which runs DESeq2 in
one process.
truecell's own GLM since 2.0.0, where it was statsmodels', and stays 2.7x ahead.

**DESeq2 has a row now.** In 1.0.0 the two tools meant different things by the
name and truecell's had no row here; since 2.0.0 `test_use="deseq2"` is Seurat's
per-cell `DESeq2DETest`. It is 2.5x faster (4.00s against 10.01s) on half
Seurat's memory, and it runs in one process, as DESeq2 does in R. Left to its
default, pydeseq2 starts one worker process per core. The first measurement for
this report caught that: with 18 workers the call took 2.50s, but the process
tree went from under 1 GB to 5.2 GB and stayed there after the call returned,
for the same output. 2.0.0 gives pydeseq2 one worker.

**Batch integration.** Harmony goes to truecell at 1.6x (harmonypy against R's
harmony, neither BLAS-bound), and so does RPCA, at 1.9x. Seurat's CCA is
Expand All @@ -506,30 +505,29 @@ this one is a wall, and no BLAS changes it.
|---|---:|---:|---:|---:|---:|
| `pbmc3k` | 13.7s | 7.3s | 1.9x slower | 2693 MB | 1844 MB |
| `sctransform` | 49.0s | 54.0s | **1.1x** | 1669 MB | 3316 MB |
| `de` | 108.7s | 303.7s | **2.8x** | 7158 MB | 5503 MB |
| `de` | 121.0s | 304.4s | **2.5x** | 3063 MB | 5564 MB |
| `dimreduc` | 7.3s | 18.9s | **2.6x** | 1027 MB | 1205 MB |
| `objects` | 1.5s | 3.9s | **2.6x** | 1403 MB | 2011 MB |
| `integration` | 37.6s | 42.9s | **1.1x** | 5474 MB | 6296 MB |
| `cellcycle` | 4.8s | 12.5s | **2.6x** | 3728 MB | 15482 MB |
| `svf` | 0.8s | 5.8s | **7.7x** | 408 MB | 1068 MB |
| `visium` | 2.8s | 5.6s | **2.0x** | 1615 MB | 2055 MB |
| `lazy` | 139.0s | 8.1s | 17.2x slower | 11459 MB | 4064 MB |
| `lazy` | 165.4s | 8.0s | 20.6x slower | 5649 MB | 4118 MB |

These are the tutorial scripts as they ship, not the benches — a different
question, and a noisier one. Each script also prints validation, writes CSVs or
draws figures, and the two sides do not do equal amounts of that. Two rows need
saying out loud:

* **`lazy` is not a 17x loss.** The Python script runs eight DE tests on an
* **`lazy` is not a 21x loss.** The Python script runs eight DE tests on an
out-of-core layer; `FindMarkers` on a BPCells `IterableMatrix` supports
`wilcox` alone, so the R script attempts eight and completes one. That is a
capability difference being reported by a stopwatch — the R script finishes
sooner because there is less it can do. Half its Python peak of 11.5 GB is
pydeseq2's per-core workers (section 5): held to one worker, the same script
peaks at 5.6 GB, measured separately.
sooner because there is less it can do. Its Python peak fell from 11.5 GB to
5.6 GB when DESeq2 moved to one process (section 5).
* **`de`** runs all nine tests on both sides, and Seurat's `MAST`, `DESeq2` and
`negbinom` are most of its 303.7s. Its Python peak of 7.2 GB is mostly
pydeseq2's workers again: held to one worker, the script peaks at 3.1 GB.
`negbinom` are most of its 304.4s. Its Python peak fell from 7.2 GB to 3.1 GB
when DESeq2 moved to one process.

---

Expand All @@ -544,15 +542,15 @@ dense TSV truecell reads 20,729 cells in 0.57s and 1.2 GB against Seurat's
**PCA is truecell's by 1.5–2.2x.** Randomized SVD against irlba, both on the
same Accelerate.

**Truecell wins seven of the eight shared DE tests** by 1.7–8.2x, and Moran's I
**Truecell wins seven of the eight shared DE tests** by 1.7–8.9x, and Moran's I
by 77x with no *n* limit.

**Seurat wins eight operations, and two of them decide the totals.**

| Operation | Gap | Where it comes from |
|---|---|---|
| `umap` (seeded) | 1.7–3.9x | umap-learn drops to one thread under a `random_state` |
| `de_wilcox` | 3.6x | presto's C++ Wilcoxon vs `scipy.stats.mannwhitneyu` |
| `de_wilcox` | 3.7x | presto's C++ Wilcoxon vs `scipy.stats.mannwhitneyu` |
| `integrate_cca` | 3.1x | |
| `find_all_markers` | 1.6–2.6x | presto again, one call per cluster |
| `hvg_vst` | 1.7–2.5x | |
Expand Down Expand Up @@ -587,11 +585,11 @@ what keeps truecell behind on totals it would otherwise win at ≥8k cells (2.3)
Either document it at the call site or expose the choice, so reproducibility is
something a caller opts into knowingly rather than pays for by default.

**3. Limit pydeseq2's worker processes.** One per core bought 1.6s on this DE
bench and cost about 4 GB, which stays resident after the call returns. On a many-core
server the cost grows with the core count. One worker gives the same output.
**3. ~~Limit pydeseq2's worker processes.~~ Done in 2.0.0 — see section 5.**
One worker per core bought 1.5s on the DE bench and cost 4 GB that stayed
resident after the call, more on a machine with more cores.

**4. Consider a fast path for Wilcoxon.** `scipy.stats.mannwhitneyu` is 3.6x
**4. Consider a fast path for Wilcoxon.** `scipy.stats.mannwhitneyu` is 3.7x
off presto on the same 1,254 genes, and Wilcoxon is the default test — so it
lands on `find_all_markers` too, and is the whole of that gap.

Expand Down
Loading
Loading