Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #479 +/- ##
==========================================
- Coverage 86.05% 86.00% -0.05%
==========================================
Files 15 15
Lines 1276 1279 +3
==========================================
+ Hits 1098 1100 +2
- Misses 178 179 +1
🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
Reference issues or PRs
Relates to #453.
What does your PR implement? Be specific.
Diagnosis of #453
When every gene contains at least one zero,
fit_size_factorsuses theiterativemethod. If size factors have not been computed,fit_genewise_dispersions()callsfit_size_factors, making size factor estimation the main source of runtime in this case. Adding 1 to all counts removes the zeros, allowing the median-of-ratios method to be used instead, which explains the workaround described in the issue.In a simulated dataset with 12 samples, 300 genes, and 60% zeros, size factor estimation took 5.1 seconds, whereas genewise dispersion fitting took less than 0.5 seconds.
Profiling
_fit_iterate_size_factorsshowed that a substantial portion of the runtime comes from repeated operations within the objective function passed to Powell's optimizer. At each evaluation, the function slices the AnnData object twice (self[:, self.non_zero_genes]) and indexesself.varby gene names. These inputs remain unchanged throughout the optimization, so the repeated operations are unnecessary. Over a full fit, Powell evaluates the objective several hundred to a few thousand times.Implementation
The counts, unscaled means, and dispersions for non-zero genes are now extracted once per iteration and passed to the objective function using
minimize(..., args=...). The objective's arithmetic and operation order remain unchanged, preserving bit-identical results.Verification
main(np.array_equal) on simulated datasets with 50–90% zeros, including a design with a continuous covariate.The test suite passes, including
test_iterative_size_factors, which compares this implementation with the output of DESeq2'sestimateSizeFactorsIterate. No additional test was added because the numerical results are unchanged.Potential follow-ups (not included)
For larger datasets, a substantial part of the remaining runtime is spent recomputing
gammalnterms innb_nllthat do not depend on the size factors. Precomputing these terms once per iteration reduces runtime for the 60-sample, 2,000-gene dataset to 15.4 seconds (a 2.3× speedup relative tomain). However, this changes the numerical results slightly, with relative differences in size factors of up to 6 × 10⁻⁸, and would partially duplicate the likelihood calculation innb_nll. This optimization could be considered separately.The warning emitted when switching to the iterative method could mention
size_factors_fit_type="poscounts"as an alternative for datasets in which many genes contain zeros.