Skip to content

Optimized the Decontx-Python package - #1

Open
jjia1 wants to merge 7 commits into
NiRuff:masterfrom
jjia1:master
Open

Optimized the Decontx-Python package#1
jjia1 wants to merge 7 commits into
NiRuff:masterfrom
jjia1:master

Conversation

@jjia1

@jjia1 jjia1 commented Jul 1, 2026

Copy link
Copy Markdown

Core algorithmic rewrite: dense → sparse EM (6315838)

The most significant change rewrites the four core numba-accelerated EM functions in
fast_ops.py from dense matrix operations to sparse CSR arithmetic:

  • Before: the EM loop called X.toarray(), allocating full n_cells × n_genes dense
    matrices on every iteration regardless of how sparse the data was. Memory use
    scaled with the full matrix shape.
  • After: all four numba kernels operate directly on CSR indptr/indices/data arrays,
    touching only nonzero entries. Complexity drops from O(n_cells × n_genes) to O(nnz)
    per iteration — typically 10–100× fewer operations for sparse single-cell data.
    The decontaminated output is also returned as a csr_matrix, keeping the sparsity
    pattern intact.

model.py was updated to pass CSR arrays directly instead of densifying, and core.py
now handles multi-batch sparse stacking and keeps layers sparse throughout.

johnathan and others added 7 commits June 25, 2026 10:12
…n_genes)

Rewrites the four core numba functions in fast_ops.py to operate on CSR
indptr/indices/data arrays, never allocating dense n_cells×n_genes matrices.
model.py drops X.toarray() and passes CSR arrays directly; the final
decontaminated counts are returned as a csr_matrix sharing the input sparsity
pattern. core.py keeps layers sparse, adds flush=True to all prints for SLURM
log visibility, and handles multi-batch sparse stacking. Tests updated and
extended with sparse-specific assertions (sparsity pattern preservation,
nonnegativity, per-cell totals, contamination range).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
anndata's h5ad writer has no serializer for Python tuples;
_store_metadata was storing delta as-is (a Tuple[float,float] from
the decontx() signature), causing IORegistryError on write_h5ad.
Coercing to list at the storage site fixes serialization without
changing the public API or EM behavior.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Fix batch_matrices key from tuple(batch_indices.tolist()) to batch_name
- Remove dead row_parts computation (4 lines)
- Store fitted delta (post-estimation) instead of input delta in uns metadata
- Remove unused import pytest from test_core.py
- Remove unused local import alias _csr

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant