fix: ensure read_10x_mtx returns CSR format#4017
fix: ensure read_10x_mtx returns CSR format#4017ernestprovo23 wants to merge 2 commits intoscverse:mainfrom
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.27% -0.07%
==========================================
Files 117 117
Lines 12782 12807 +25
==========================================
+ Hits 10014 10025 +11
- Misses 2768 2782 +14
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, |
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