Skip to content

fix: keep trees of a TreeData modality through MuData IO - #103

Merged
colganwi merged 4 commits into
YosefLab:mainfrom
pedrocr83:fix/mudata-modality-trees
Oct 16, 2026
Merged

colganwi merged 4 commits into
YosefLab:mainfrom
pedrocr83:fix/mudata-modality-trees

Conversation

@pedrocr83

Copy link
Copy Markdown
Contributor

Closes #102.

What

  • _write_tree_elems(group, tdata, kwargs) / _read_tree_elems(group): the writing and reading of label, allow_overlap, alignment, obst and vart, factored out of _write_tdata/_read_tdata and made group-relative. _write_tdata/_read_tdata call them, so .h5td and zarr output is unchanged.
  • TreeData._write_mudata_extras(group, *, dataset_kwargs) and the classmethod TreeData._read_mudata_extras(group, adata): the hooks proposed in AnnData subclass modalities (e.g. TreeData) lose their extra elements and type on write_h5mu/write_zarr + read scverse/mudata#210, so a TreeData modality of a MuData keeps its trees, label, allow_overlap and alignment through mudata.write_h5mu/write_zarr/write_h5ad and read (in memory and backed).
  • Changelog entry under Unreleased / Fixed.

Why

mudata writes only AnnData elements per modality and reads modalities back as AnnData, so the trees are lost (#102). treedata's writer only works on a file root, so there was nothing mudata could call.

Tests

In tests/test_readwrite.py:

  • test_mudata_hooks[h5|zarr]: writes a TreeData (non-default label, allow_overlap=False, alignment="subset") as an AnnData group, calls the two hooks, and checks the restored TreeData. No mudata needed.
  • test_mudata_readwrite[h5mu|zarr]: end-to-end MuData round trip. It is skipped when mudata is not installed (it is not a test dependency) or when the installed mudata does not call the hooks.

Full suite (Python 3.13, anndata 0.13.4, zarr 3.4.1):

mudata installed result
none 60 passed, 2 skipped
0.4.1 (released) 60 passed, 2 skipped
scverse/mudata#211 branch 62 passed

main: 58 passed. prek run --all-files passes.

Dependency

This PR is safe to merge on its own: with released mudata the hooks are never called and behavior is unchanged. The fix only takes effect once scverse/mudata#211 (or whatever form of the hook the mudata maintainers accept) is released; if they choose a different protocol I will adapt this PR. You may want to add mudata to the test dependency group so the end-to-end test runs in CI once that release exists.

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

🤖 Generated with Claude Code

mudata writes only the AnnData elements of each modality and reads it
back as AnnData, so a TreeData modality loses obst, vart, label,
allow_overlap and alignment on write_h5mu/write_zarr/write_h5ad + read.

Factor the writing and reading of those elements out of _write_tdata and
_read_tdata into group-relative _write_tree_elems/_read_tree_elems, and
expose them as the _write_mudata_extras/_read_mudata_extras hooks that a
mudata with AnnData subclass modality support calls. With a mudata that
does not call the hooks nothing changes.

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

Copy link
Copy Markdown
Collaborator

@pedrocr83 thanks for the PR. Resolving this will depend on scverse/mudata#210

colganwi and others added 2 commits October 15, 2026 12:37
…ooks

Replace the private _write_mudata_extras/_read_mudata_extras methods with
public functions that store and restore the tree elements (obst, vart,
label, allow_overlap, alignment) in an existing AnnData group. mudata calls
them for TreeData modalities and marks the group with
encoding-subtype = "treedata". On-disk output of .h5td and zarr is unchanged.

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 93.05%. Comparing base (0544cd9) to head (adf401e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #103      +/-   ##
==========================================
+ Coverage   92.91%   93.05%   +0.13%     
==========================================
  Files           7        7              
  Lines         762      777      +15     
==========================================
+ Hits          708      723      +15     
  Misses         54       54              
Files with missing lines Coverage Δ
src/treedata/__init__.py 100.00% <100.00%> (ø)
src/treedata/_core/read.py 89.43% <100.00%> (+0.83%) ⬆️
src/treedata/_core/write.py 96.29% <100.00%> (+0.14%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@colganwi

Copy link
Copy Markdown
Collaborator

Thanks for the PR and the report. I pushed two commits on top of this branch that change the approach, so the description above no longer matches the code.

Why

In the discussion on scverse/mudata#210, the mudata maintainers did not want a duck-typed hook that any AnnData subclass can plug into (private methods, and a class imported by name from a file attribute). The direction agreed there for now is that mudata calls treedata directly for TreeData modalities, and an anndata-level extension mechanism can replace that later.

What changed

  • Removed the private TreeData._write_mudata_extras / _read_mudata_extras methods.
  • write_tree_elems(group, tdata, *, dataset_kwargs=None) and read_tree_elems(group, adata) are now public and exported from treedata. They are the existing group-relative writer and reader factored out of _write_tdata/_read_tdata. read_tree_elems returns a TreeData built from the AnnData mudata has read plus the tree elements in the group, so mudata does not depend on the constructor arguments.
  • They are deliberately not listed in docs/api.md.
  • The changelog entry moved to Added and describes the new functions.
  • test_mudata_hooks became test_tree_elems and uses the public functions. The new tests are now above the if __name__ == "__main__" block. .h5td and zarr output is unchanged.

How mudata uses it

The companion mudata change (still in my fork for review: colganwi/mudata#1) writes encoding-subtype = "treedata" on the modality group and calls these two functions. The group keeps encoding-type = "anndata", so it stays readable as plain AnnData by older readers. If treedata is missing on read, mudata warns and returns the AnnData.

Testing

With that mudata branch installed, the suite gives 62 passed, including test_mudata_readwrite (h5mu and zarr). I also checked backed h5mu, write_h5ad and a sliced TreeData by hand. pre-commit passes. With released mudata nothing changes, since the functions are never called.

The fix only takes effect once mudata ships the matching change.

@colganwi
colganwi merged commit 9271226 into YosefLab:main Oct 16, 2026
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.

TreeData modality of a MuData loses obst/vart/label/allow_overlap/alignment on mudata write + read

2 participants