Skip to content

Fit the guide RNA mixture model by multi-start EM and fix sparse guide assignment - #1172

Merged
Zethson merged 4 commits into
mainfrom
fix/guide-assignment-correctness
Oct 20, 2026
Merged

Zethson merged 4 commits into
mainfrom
fix/guide-assignment-correctness

Conversation

@Zethson

@Zethson Zethson commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Fits the Poisson-Gaussian guide mixture by multi-start EM on each guide's distinct count values instead of 500 Adam steps from one start, which left thresholds unconverged, swapped the components on high-MOI guides and dropped small guides; the model and threshold rule are unchanged, results reach the same objective value as crispat run to convergence on all 106 shared Papalexi guides (105 identical thresholds), and the fit is 20x faster at a third of the memory. The threshold methods now accept CSC and sparse-array input (the Papalexi dataset ships as CSC and the documented example failed), explicitly stored zeros no longer enter the fit, plot_heatmap saves a reusable cell order, the unused learning_rate argument is deprecated, and the tutorial submodule is bumped to the re-executed notebook. Python 3.13 is now required, as anndata main already does, so the test matrix, the mypy hook and the docs build resolve again.

@Zethson
Zethson force-pushed the fix/guide-assignment-correctness branch 2 times, most recently from f49d123 to 36bdcc0 Compare October 20, 2026 11:47
…e assignment

The Poisson-Gaussian mixture behind `assign_mixture_model` was fit by 500 Adam steps from one prior-median start.
That left 17 of 102 Papalexi thresholds unconverged, swapped the Gaussian and Poisson components on high-MOI guides so every cell was assigned, and lost small guides to a vanishing Gaussian weight.
The model, priors and threshold rule are unchanged.
The MAP is now found by EM on the distinct count values of each guide from prior-median and quantile-split starts, keeping the lowest-loss solution that is neither label-switched nor pinned at the weight or scale floor.
On Papalexi the result equals crispat run to convergence on all 106 commonly fitted guides (same loss on 106, same threshold on 105), while crispat's default of 500 SVI steps is under-converged on 77 of them and its SVI diverges to NaN on 2 guides when run longer.

Also fixed:
- `assign_by_threshold` and `assign_to_max_guide` rejected CSC and sparse-array input, which is how the Papalexi dataset ships; the documented example failed on both the positional threshold and the CSC matrix.
- Explicitly stored zeros in sparse input entered the mixture fit as log2(0) and silently returned the initial parameters.
- `plot_heatmap` saved the permutation instead of the cell positions, so reusing `key_to_save_order` plotted the inverse order.
- The sparse threshold kernel was numba-parallel for an elementwise comparison (20x slower than numpy plus compile time) and aliased the input's index buffers.
- `only_return_results=True` still wrote the fit to `adata.var`.

`learning_rate` is deprecated and ignored.
`n_iter` is now the EM iteration cap.

Benchmarks on CPU, steady state: Papalexi (20,729 cells x 111 guides) 2.9 s -> 0.14 s; 100k cells x 500 guides 12.6 s and 3.5 GB peak RSS -> 1.0 s and 1.1 GB; crispat serial takes 180 s on Papalexi.
@Zethson
Zethson force-pushed the fix/guide-assignment-correctness branch from 36bdcc0 to c384037 Compare October 20, 2026 11:51
Zethson added a commit to scverse/pertpy-tutorials that referenced this pull request Oct 20, 2026
…lexi dataset (#79)

The mixture model section claimed an optax fit that matches crispat's output and subsetted the data to 5,000 cells and 10 guides for speed.
With scverse/pertpy#1172 the fit is a multi-start EM over all guides and takes a few seconds on CPU, so the tutorial now fits all 20,729 cells and 111 guides, describes the model and threshold rule accurately, and compares the result with the maximum assignment.
The notebook was re-executed with that branch.
@codecov-commenter

codecov-commenter commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.05660% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.81%. Comparing base (c246f24) to head (43bad24).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/pertpy/preprocessing/_guide_rna.py 97.93% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1172      +/-   ##
==========================================
+ Coverage   75.68%   75.81%   +0.13%     
==========================================
  Files          55       55              
  Lines        8435     8464      +29     
==========================================
+ Hits         6384     6417      +33     
+ Misses       2051     2047       -4     
Files with missing lines Coverage Δ
src/pertpy/preprocessing/_guide_rna_mixture.py 99.35% <100.00%> (+0.28%) ⬆️
src/pertpy/preprocessing/_guide_rna.py 89.88% <97.93%> (+1.62%) ⬆️

... 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.

scverse/pertpy-tutorials#79 describes the EM fit and runs the mixture model on the full Papalexi dataset.
…nment tests

anndata main requires Python 3.13, so the 3.12 test environment and the mypy hook's fresh resolution cannot be solved anymore.
The docs build fails on the mocked ete4.Tree reference, which is now ignored like the other ete4 classes.
The two regression tests for explicit sparse zeros and the saved heatmap order are dropped and the mixture model docstring is shortened.
Without a pin uv picks the newest interpreter it can download, currently 3.15, where h5py has no wheel and the source build fails on CI.
@Zethson
Zethson force-pushed the fix/guide-assignment-correctness branch from 28e78aa to 43bad24 Compare October 20, 2026 12:20
@Zethson
Zethson merged commit 72eb852 into main Oct 20, 2026
22 checks passed
@Zethson
Zethson deleted the fix/guide-assignment-correctness branch October 20, 2026 12:26
Zethson added a commit that referenced this pull request Nov 3, 2026
Adds the changelog for 1.5.0 and bumps the release drafter templates.
The version itself comes from the git tag via hatch-vcs.
The v1.5.0 section gains the guide assignment rework (#1172, #1173), the Mixscape min_cells fix (#1189), the Python 3.13 requirement and the CI and template PRs (#1165, #1184, #1185, #1190), and its two Maintenance blocks are merged in the 1.4.0 section order.
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.

2 participants