Skip to content

significant speed up of imputation - #3

Merged
philinscience merged 1 commit into
mainfrom
feat/impute_speed_up
Jul 29, 2026
Merged

significant speed up of imputation#3
philinscience merged 1 commit into
mainfrom
feat/impute_speed_up

Conversation

@philinscience

Copy link
Copy Markdown
Collaborator

GPU 20X faster

@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/cellpin/models/utils/nb_sampling.py 92.00% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new Monte‑Carlo negative binomial normalization helper to accelerate (including on GPU) the computation of E[log1p(norm(X))] used by CellPin.impute(..., return_norm=True), and wires it into the imputation path with improved reproducibility controls.

Changes:

  • Add mc_log1p_norm with a threaded NumPy backend (CPU) and a Torch backend (GPU) in a new nb_sampling utility module.
  • Update CellPin.impute to use the new helper and add nb_seed for reproducible imputed_norm generation.
  • Add targeted unit tests covering correctness vs a reference implementation, chunking behavior, and reproducibility.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/models/utils/test_nb_sampling.py Adds unit tests for the new Monte‑Carlo NB normalization helper (reference matching, chunking, reproducibility).
tests/models/test_cellpin.py Adds integration tests for impute(..., return_norm=True) and nb_seed reproducibility + area_key behavior.
src/cellpin/models/utils/nb_sampling.py Implements chunked MC estimator with NumPy (threaded) and Torch (GPU) backends.
src/cellpin/models/utils/init.py Exports mc_log1p_norm from cellpin.models.utils.
src/cellpin/models/cellpin_model.py Switches imputed_norm computation to mc_log1p_norm and adds nb_seed parameter/docs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +144 to +147
if n_samples < 1:
raise ValueError(f"n_samples must be >= 1, got {n_samples}")

n_cells, n_genes = mu.shape
Comment on lines +99 to +102
acc = torch.zeros_like(mu_t)
for _ in range(n_samples):
lam = torch._standard_gamma(concentration, generator) / rate
draw = torch.poisson(lam, generator=generator)
Comment on lines +148 to +151
theta = np.asarray(theta, dtype=np.float64)
if theta.shape != (n_genes,):
raise ValueError(f"theta must have shape ({n_genes},), got {theta.shape}")
if scale is not None:
@philinscience
philinscience merged commit 5b0464b into main Jul 29, 2026
8 checks passed
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