Repository navigation
feat: keep TreeData trees through write_h5mu/write_zarr/write_h5ad - #216
Merged
Merged
Conversation
A TreeData modality was written as plain AnnData and read back as AnnData, losing obst, vart, label, allow_overlap and alignment. Mark such a modality group with encoding-subtype = "treedata" (encoding-type stays "anndata") and store the extra elements by calling treedata.write_tree_elems; on read, the subtype selects treedata.read_tree_elems to rebuild the TreeData. A private registry maps subtype names to the module, class and write/read functions, so another AnnData subclass is one more entry. If the package is missing or the subtype is unknown, warn and read the modality as AnnData. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #216 +/- ##
==========================================
+ Coverage 91.28% 91.45% +0.17%
==========================================
Files 12 12
Lines 1847 1885 +38
==========================================
+ Hits 1686 1724 +38
Misses 161 161
🚀 New features to boost your workflow:
|
ilia-kats
requested changes
Oct 16, 2026
ilia-kats
left a comment
Collaborator
There was a problem hiding this comment.
I'd prefer having the new tests in a separate file (e.g. test_io_anndata_subtype.py). Then it can be easily removed once AnnData itself supports extension hooks. Otherwise this looks good to me.
Review feedback: keep them in tests/test_io_anndata_subtype.py so they can be removed together once AnnData supports extension hooks itself. test_io.py is unchanged compared to main. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Contributor
Author
|
I've moved the tests. The required changes to treedata will be released shortly in v0.3.2 |
The registry now refers to the writer and reader as "module:function" (treedata.io:write_tree_elems, treedata.io:read_tree_elems) and resolves them with importlib, so they can live in any submodule of the package. The test stand-in package gets an io submodule to match. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Contributor
Author
|
One more small change... I've moved |
ilia-kats
approved these changes
Oct 19, 2026
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.
Refs #210. Implements option 1 from the discussion there (short term). It takes a different approach from #211, following the maintainers' feedback in that thread.
What
A
TreeDatamodality was written as plain AnnData and read back asAnnData, losing its trees. Now the modality group is marked withencoding-subtype = "treedata"and mudata calls into treedata to store and rebuild the extra elements:write_h5mu,write_zarr,write_h5ad): after the AnnData elements, if a registered subclass's module is already insys.modulesand the modality is an instance of it, call its writer (treedata.io.write_tree_elems(group, adata, dataset_kwargs=...)) and setencoding-subtype. An instance can only exist if its module was imported, so mudata never imports it on write.read_h5mu,read_zarr,read_h5ad): if the group hasencoding-subtype, look it up in the registry, import its module and return the reader's result (treedata.io.read_tree_elems(group, adata)). An unknown subtype, a missing package, or a package without the reader warns and returns theAnnData. For h5mu the rebuild happens before the file manager is attached, so backed reads also return the subclass.encoding-type = "anndata"), so older mudata andanndata.io.read_elemstill read them as AnnData. Modalities that are not registered subclasses are unchanged on disk and on read.Design
A private registry
_SUBTYPESmaps the subtype name to the module, class, and"module:function"references to the writer and reader that handle it.treedatais the only entry, and supporting another AnnData subclass is one more entry. It is private on purpose: it is not a public hook, and mudata stays in control of which packages it calls. All handling sits in_write_mod_extras/_read_mod_extras, so it can be replaced by an anndata-level extension mechanism or on-disk spec later (see the long-term discussion in #210); the file layout would not need to change.encoding-subtypefollows the suggestion in that discussion.Tests
tests/test_io_anndata_subtype.pyuses a generic AnnData subclass in a stand-in package registered in_SUBTYPES(no treedata and no new test dependency): h5mu and zarr round trips (including the attributes on the group), backed h5mu,write_h5ad/read_h5ad, package not installed, unknown subtype in the file, and a package without the writer (write still works, stored as plain AnnData).Full suite (Python 3.12, anndata 0.13.4, zarr 3.4.1): 549 passed, 2 xfailed.
ruff check/formatpass. Also checked end to end with the real treedata using the in-progresstreedata.io.write_tree_elems/read_tree_elems(h5mu, zarr, backed,write_h5ad, sliced TreeData).Dependency
Needs a treedata release providing
treedata.io.write_tree_elemsandtreedata.io.read_tree_elems(companion: YosefLab/treedata#103, which adds the functions; they are being moved totreedata.ioin a follow-up). Without it nothing changes for TreeData modalities.🤖 Generated with Claude Code