Skip to content

feat: keep TreeData trees through write_h5mu/write_zarr/write_h5ad - #216

Merged
ilia-kats merged 3 commits into
scverse:mainfrom
colganwi:treedata-modality-io
Oct 19, 2026
Merged

ilia-kats merged 3 commits into
scverse:mainfrom
colganwi:treedata-modality-io

Conversation

@colganwi

@colganwi colganwi commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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 TreeData modality was written as plain AnnData and read back as AnnData, losing its trees. Now the modality group is marked with encoding-subtype = "treedata" and mudata calls into treedata to store and rebuild the extra elements:

  • Write (write_h5mu, write_zarr, write_h5ad): after the AnnData elements, if a registered subclass's module is already in sys.modules and the modality is an instance of it, call its writer (treedata.io.write_tree_elems(group, adata, dataset_kwargs=...)) and set encoding-subtype. An instance can only exist if its module was imported, so mudata never imports it on write.
  • Read (read_h5mu, read_zarr, read_h5ad): if the group has encoding-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 the AnnData. For h5mu the rebuild happens before the file manager is attached, so backed reads also return the subclass.
  • Groups stay valid AnnData groups (encoding-type = "anndata"), so older mudata and anndata.io.read_elem still read them as AnnData. Modalities that are not registered subclasses are unchanged on disk and on read.

Design

A private registry _SUBTYPES maps the subtype name to the module, class, and "module:function" references to the writer and reader that handle it. treedata is 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-subtype follows the suggestion in that discussion.

Tests

tests/test_io_anndata_subtype.py uses 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/format pass. Also checked end to end with the real treedata using the in-progress treedata.io.write_tree_elems/read_tree_elems (h5mu, zarr, backed, write_h5ad, sliced TreeData).

Dependency

Needs a treedata release providing treedata.io.write_tree_elems and treedata.io.read_tree_elems (companion: YosefLab/treedata#103, which adds the functions; they are being moved to treedata.io in a follow-up). Without it nothing changes for TreeData modalities.

🤖 Generated with Claude Code

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

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.45%. Comparing base (35c60bf) to head (8b47370).
⚠️ Report is 1 commits behind head on main.

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              
Files with missing lines Coverage Δ
src/mudata/_core/io.py 96.14% <100.00%> (+0.45%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ilia-kats ilia-kats left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@colganwi

Copy link
Copy Markdown
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]>
@colganwi

Copy link
Copy Markdown
Contributor Author

One more small change... I've moved read/write_tree_elems into a new io module and made the mudata function lookup robust to this configuration

@ilia-kats
ilia-kats merged commit dde6b50 into scverse:main Oct 19, 2026
8 of 9 checks passed
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.

2 participants