Skip to content

feat: let AnnData subclass modalities round-trip their extra elements - #211

Closed
pedrocr83 wants to merge 1 commit into
scverse:mainfrom
pedrocr83:fix/modality-subclass-extras
Closed

pedrocr83 wants to merge 1 commit into
scverse:mainfrom
pedrocr83:fix/modality-subclass-extras

Conversation

@pedrocr83

@pedrocr83 pedrocr83 commented Oct 10, 2026 •

Copy link
Copy Markdown

Closes #210.

What

An opt-in, duck-typed hook so that a modality which is an AnnData subclass keeps the elements it adds (and its type) through write_h5mu/write_zarr/write_h5ad and read_h5mu/read_zarr/read_h5ad:

  • After writing a modality's AnnData elements, if the object defines _write_mudata_extras(group, *, dataset_kwargs), mudata calls it and sets the group attribute mudata-modality-class = "module:qualname".
  • On read, if that attribute is present, mudata imports the class and returns cls._read_mudata_extras(group, adata). If the class is not importable, or is not an AnnData subclass with the hook, it warns and returns the AnnData.
  • For h5mu the subclass is rebuilt before the file manager and raw are attached, so backed reads (backed="r"/"r+") also return the subclass.

Why

The writers write a fixed list of AnnData elements and the readers always return AnnData, so e.g. treedata.TreeData modalities lose their trees. anndata's IO registry matches exact types and has no public registration API, so a subclass cannot hook in there. Nothing in this change refers to a specific subclass.

Compatibility

Tests

New tests in tests/test_io.py use a minimal AnnData subclass defined in the test module (no new test dependency):

  • round-trip through h5mu and zarr (assert_equal on the MuData, subclass type and extra element restored, attribute set only on the subclass modality),
  • backed h5mu read,
  • write_h5ad/read_h5ad of a single modality,
  • unavailable class: warns and falls back to AnnData.

All 5 fail on main and pass with the change. Full suite (Python 3.13, anndata 0.13.4, zarr 3.4.1): 547 passed, 2 xfailed (main: 542 passed, 2 xfailed). prek run --all-files passes. I also ran treedata's tests and an end-to-end TreeData round trip (h5mu, zarr, backed, write_h5ad) against this branch with the companion PR below.

Dependency

This PR stands alone: with an unpatched treedata nothing changes for TreeData modalities. The companion treedata PR (YosefLab/treedata#103) implements the two hooks; it is a no-op until a mudata release includes this change.

Found while building biotapy, a microbiome toolkit on TreeData/MuData.

🤖 Generated with Claude Code

MuData writes a fixed list of AnnData elements for each modality and
reads every modality back as AnnData, so an AnnData subclass loses the
elements it adds (and its type) on write_h5mu/write_zarr/write_h5ad and
read.

If a modality defines `_write_mudata_extras(group, *, dataset_kwargs)`,
call it after writing the AnnData elements and record the class as the
`mudata-modality-class` group attribute ("module:qualname"). On read,
import that class and call its `_read_mudata_extras(group, adata)`
classmethod; if the class is not available, warn and keep the AnnData.
Modalities without the hook are written and read exactly as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.40%. Comparing base (26ddfd7) to head (8e265c6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #211      +/-   ##
==========================================
+ Coverage   91.28%   91.40%   +0.11%     
==========================================
  Files          12       12              
  Lines        1848     1873      +25     
==========================================
+ Hits         1687     1712      +25     
  Misses        161      161              
Files with missing lines Coverage Δ
src/mudata/_core/io.py 96.00% <100.00%> (+0.30%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ilia-kats

Copy link
Copy Markdown
Collaborator

superseded by #216, closing

@ilia-kats ilia-kats closed this Oct 19, 2026
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.

AnnData subclass modalities (e.g. TreeData) lose their extra elements and type on write_h5mu/write_zarr + read

2 participants