feat: add sparse_format='csr' parameter to read_10x_mtx#4017
Conversation
Transposing a CSR matrix in scipy produces CSC format. Add explicit tocsr() conversion after the .T transpose in _read_10x_mtx() and add a format assertion to the test suite. Fixes scverse#4016
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4017 +/- ##
==========================================
+ Coverage 78.34% 78.51% +0.17%
==========================================
Files 117 117
Lines 12782 12753 -29
==========================================
- Hits 10014 10013 -1
+ Misses 2768 2740 -28
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
Hi, thank you! So currently Anndata’s
|
Per reviewer feedback, replace the read() + tocsr() approach with a new _read_mtx() helper that converts COO→CSC directly (one conversion). Transposing CSC then yields CSR with no extra work. - Add _read_mtx() with sparse_format parameter (Literal['csr','csc','coo']) - Inline cache handling in _read_10x_mtx (preserves existing cache behavior) - Use CSRBase from _compat for type assertions per project conventions - Pre-commit clean (ruff, formatting) Fixes scverse#4016
|
Thanks for the detailed feedback @flying-sheep — updated to follow your suggested approach:
Let me know if the cache handling approach looks right or if you'd prefer it structured differently. |
|
Hi, please don’t repeat all the caching code. As I said, |
|
Understood — replaced read_mtx in _read() instead, cache stays in one place. Removed the duplication. |
|
Great, thank you. Next time please don’t delete the PR template, but fill it out instead. |
sparse_format='csr' parameter to read_10x_mtx
Summary
Ensure
read_10x_mtx()returns an AnnData object with a CSR sparse matrix instead of CSC.Motivation
Fixes #4016
When
_read_10x_mtx()transposes the matrix read byanndata.read_mtx(), scipy automatically converts the CSR matrix to CSC format (this is how scipy sparse transpose works — CSR.T → CSC). Most downstream scanpy operations expect CSR format.Changes
adata.X = adata.X.tocsr()after the.Ttranspose in_read_10x_mtx()(src/scanpy/readwrite.py)test_read_10x(tests/test_read_10x.py)Testing
test_read_10xparametrized cases pass locally (v1.2.0 + v3.0.0, with and without prefix)Notes for reviewers