Skip to content

implement 'use' for writing methods - #222

Open
ilia-kats wants to merge 11 commits into
mainfrom
write_use
Open

ilia-kats wants to merge 11 commits into
mainfrom
write_use

Conversation

@ilia-kats

@ilia-kats ilia-kats commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Same as scverse/anndata#2707, but for MuData.

There are several hacks to bypass the sanity checks in anndata's implementation:

  • overriding the attrs with something returning anndata's encoding type and version. This is necessary to be able to pass in a MuData object to anndata's write_use, which happens when writing e.g. MuData.obs, MuData.var etc.
  • Creating AnnData accessors from MuData accessors. This is necessary to bypass the dimensionality check: The check reads obs_names / var_names of the passed in object, which would be a MuData object if using a MuData accessor, so the names don't match what's on disk for the individual modality.

Removing the hacks would require making anndata's write_use more flexible: Adding a boolean argument to disable encoding type/version checking and adding arguments to pass in obs_names and var_names to check against (or, even better, pass in an AdAcc instance and do something like adata[getattr(A, dim)].index).

The CI is failing due to warnings related to Pandas 3.1. The warnings will disappear with anndata 0.13.5. Everything else is passing (though the new code is not tested in CI yet becase there is no pre-release of anndata 0.14, but the tests pass locally).

@ilia-kats
ilia-kats marked this pull request as ready for review October 30, 2026 12:17
@ilia-kats
ilia-kats requested review from gtca and ilan-gold October 30, 2026 12:17

@ilan-gold ilan-gold left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Removing the hacks would require making anndata's write_use more flexible: Adding a boolean argument to disable encoding type/version checking and adding arguments to pass in obs_names and var_names to check against (or, even better, pass in an AdAcc instance and do something like adata[getattr(A, dim)].index).

If we do this, I'd want to think through EHRData and TreeData as well as potential consumers

Comment thread src/mudata/_core/io.py Resolved
Comment thread src/mudata/_core/io.py Resolved
Comment thread src/mudata/_core/io.py Resolved
Comment thread src/mudata/_core/io.py
have_mods and mdata.mod.keys() != set(disk_mods.keys()) or any(mod not in disk_mods for mod in have_mod)
):
warn(
"Modalities given in 'use' do not correspond to those already on disk and no global metadata is being written. "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not write the global metadata?

@ilia-kats ilia-kats Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Because it may not work.

If the MuData object has axis=-1, then it is possible that obs_names and var_names do match what's on disk after an update(), but it gets really complicated for axis=0 and axis=1. Suppose you have a MuData with axis=0 on disk with 2 modalities, and you add a third. Then there are two possibilities:

  1. You haven't run mdata.update() yet, in which case the global metadata has not changed and writing it won't do anything.
  2. You have run mdata.update(). Then the var_names (and potentially the obs_names) no longer correspond to what's on disk, and the entire write will fail (at least according to current logic).

mudata runs update() in its constructor, which is called when reading from disk, so the read itself should succeed, but the ordering of modalities or global obs_names may or may not change. Hence suprising results.

Running update() in the _write_use() function is not a solution either: The user may have added multiple modalities, but is only writing one of those to disk. After update() the global metadata will reflect all the modalities.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or global obs_names may or may not change.

Ok, so the disk storage of global obs_names is more of a suggestion than a ground truth? Why store it then? Just curious since it sounds like it's reconstructable from the constituent mods...

How could the obs_names "change" also? Maybe be more specific - it seems more of a mismatch issue than a change issue.

@ilia-kats ilia-kats Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ok, so the disk storage of global obs_names is more of a suggestion than a ground truth? Why store it then? Just curious since it sounds like it's reconstructable from the constituent mods...

Suppose you have three modalities, let's call them mod1, mod2, and mod3, where each has a different ordering of observations. When the MuData object is first constructed, update() goes through the modalities in order and determines the global obs_names as a union of all modality obs_names, which means that the global obs_names start with the obs_names of the mod1 in the order that they are in mod1, followed by the obs_names from mod2 that are not in mod1 in the order that they are present in mod2, and so on. Now say you delete mod1 and run update() again. If we were reconstructing obs_names from scratch, their ordering will change. So update() looks at the existing obs_names and obsmap and tries to preserve their order. This can happen here if someone is writing the entire A.mod, for example.

How could the obs_names "change" also? Maybe be more specific - it seems more of a mismatch issue than a change issue.

Again, depends on the exact usecase. The worst case that I can think of right now is: The user renames one or multiple modalities (mdata.mod["mod1"].obs_names = ...) and does not run update(). He then does write(use=A.mod). Since there was no update(), the global names still match with what's on disk, so that check will succeed. But when reading back the file, update() will run, and so the global names will actually change.

I suppose I can add another check when I write an entire modality to verify that also that modality's names match what's on disk, but I'm not sure if that's too restrictive, and it won't help with the scenario above (deleted modality).

Comment thread src/mudata/_core/io.py Resolved
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.12121% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.58%. Comparing base (7cf3108) to head (bea5e07).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/mudata/_core/io.py 92.12% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #222      +/-   ##
==========================================
- Coverage   90.40%   81.58%   -8.83%     
==========================================
  Files          12       14       +2     
  Lines        1939     2118     +179     
==========================================
- Hits         1753     1728      -25     
- Misses        186      390     +204     
Files with missing lines Coverage Δ
src/mudata/_core/io.py 93.82% <92.12%> (-0.46%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ilan-gold ilan-gold left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice! Just a few q's for my own learning

Comment thread src/mudata/_core/io.py Resolved
Comment thread src/mudata/_core/io.py
have_mods and mdata.mod.keys() != set(disk_mods.keys()) or any(mod not in disk_mods for mod in have_mod)
):
warn(
"Modalities given in 'use' do not correspond to those already on disk and no global metadata is being written. "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or global obs_names may or may not change.

Ok, so the disk storage of global obs_names is more of a suggestion than a ground truth? Why store it then? Just curious since it sounds like it's reconstructable from the constituent mods...

How could the obs_names "change" also? Maybe be more specific - it seems more of a mismatch issue than a change issue.

Comment thread src/mudata/_core/io.py Resolved

This branch has not been deployed

No deployments
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