Skip to content

Fix pre-release test failures for AnnData and read-only sparse matrices#118

Open
marcovarrone wants to merge 2 commits into
mainfrom
fix-tests-v037
Open

Fix pre-release test failures for AnnData and read-only sparse matrices#118
marcovarrone wants to merge 2 commits into
mainfrom
fix-tests-v037

Conversation

@marcovarrone

@marcovarrone marcovarrone commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes
    • Prevented unintended in-place changes to adjacency matrices during intra-cluster link removal.
    • Improved robustness of sparse matrix handling to avoid side effects and ensure consistent zero-elimination behavior.
    • Refined diagonal masking for enrichment calculations to use a safer helper approach.
  • Tests
    • Updated the non_visium_adata fixture to cast coordinates to integers explicitly for more reliable AnnData construction.

Update the non_visium_adata fixture to cast X before AnnData construction,
since newer AnnData no longer accepts a dtype keyword argument.
Avoid in-place modification of read-only obsp views from h5ad-backed data by
copying adjacency matrices before removing intra-cluster links in nhood
enrichment, and handle non-writable sparse .data arrays in
remove_intra_cluster_links by copying and writing results back to adata.obsp.
@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2a0b9b9f-3d5b-492d-b53f-d6d996b42d73

📥 Commits

Reviewing files that changed from the base of the PR and between fa37f70 and 44cc65c.

📒 Files selected for processing (1)
  • src/cellcharter/gr/_nhood.py

📝 Walkthrough

Walkthrough

Three bug fixes address unsafe in-place mutation of sparse adjacency matrices: _remove_intra_cluster_links now copies non-writeable matrices before mutating, remove_intra_cluster_links uses explicit separate assignments for conns and dists, and _nhood_enrichment passes a copy of adj. A new _with_diagonal helper replaces direct diagonal masking operations. The test fixture adds an explicit .astype(int) cast.

Changes

Sparse adjacency matrix mutation safety

Layer / File(s) Summary
Writeable check and explicit conns/dists assignment
src/cellcharter/gr/_build.py
_remove_intra_cluster_links detects non-writeable sparse matrices and copies them before mutating data. remove_intra_cluster_links replaces a generator-based combined computation with two separate _remove_intra_cluster_links calls and individual adata.obsp key assignments.
Diagonal masking helper and caller-side copy
src/cellcharter/gr/_nhood.py
Introduces _with_diagonal(df, value) helper to safely set diagonal values in DataFrames. _nhood_enrichment passes adj.copy() to avoid side effects on the original matrix and uses the new helper for diagonal-nan masking of observed, expected, and enrichment.
Fixture dtype correction
tests/conftest.py
The non_visium_adata fixture casts coordinates using .astype(int) instead of passing dtype=int to the AnnData constructor.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A matrix once frozen, too rigid to write,
Now gets a fresh copy before we take flight.
The diagonal masking, no longer in-place,
A helper ensures data stays safe and sound.
adj.copy() called — side effects are gone!
And fixtures cast cleanly with .astype(int). ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: fixes for test failures related to AnnData and read-only sparse matrices, which aligns with the modifications to handle sparse matrix writeability and AnnData construction changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-tests-v037

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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