Repository navigation
Conversation
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]>
This was referenced Oct 10, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
Collaborator
|
superseded by #216, closing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #210.
What
An opt-in, duck-typed hook so that a modality which is an
AnnDatasubclass keeps the elements it adds (and its type) throughwrite_h5mu/write_zarr/write_h5adandread_h5mu/read_zarr/read_h5ad:_write_mudata_extras(group, *, dataset_kwargs), mudata calls it and sets the group attributemudata-modality-class = "module:qualname".cls._read_mudata_extras(group, adata). If the class is not importable, or is not anAnnDatasubclass with the hook, it warns and returns the AnnData.raware 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.TreeDatamodalities 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
anndata.io.read_elemread them as AnnData (checked).docs/io/spec.md.Tests
New tests in
tests/test_io.pyuse a minimalAnnDatasubclass defined in the test module (no new test dependency):assert_equalon the MuData, subclass type and extra element restored, attribute set only on the subclass modality),write_h5ad/read_h5adof a single modality,All 5 fail on
mainand 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-filespasses. 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