Skip to content

Leave a layer narrower than the assay out of as_anndata, with a warning - #147

Merged
shanikawm merged 2 commits into
mainfrom
fix/anndata-narrow-layers-141
Oct 7, 2026
Merged

shanikawm merged 2 commits into
mainfrom
fix/anndata-narrow-layers-141

Conversation

@shanikawm

Copy link
Copy Markdown
Contributor

Fixes #141.

scale_data() scales only the variable features unless it is given others, so scale.data has one row per variable feature. as_anndata wrote every non-default layer into layers unchanged, and AnnData rejects a layer that is not as wide as var, so the conversion raised a ValueError on any object that had been through normalize_data, find_variable_features and scale_data.

The change. A layer with fewer features than the assay is left out, with a UserWarning that names it, says how many features it holds against the assay's, and says how to keep it: scale_data(obj, features=obj.feature_names()) before converting. This is the issue's first suggestion. I did not pad, because from_anndata turns every layer back into an assay layer, so padded rows would read back as scaled values that never were. A layer that covers every feature is exported exactly as before.

Both assay classes. The issue only tried Assay5. The legacy Assay branch writes its scale layer as scale_data and fails the same way, so the guard sits where the two branches meet, and the tests run both.

Not changed, but found while checking. An object whose layers have been split (split_layers) fails earlier, in X itself: the default layer becomes counts.A, which has only some of the cells, so AnnData reports that obs has more rows than X. That is a different problem from this issue and I left it alone.

Verification

  • The issue's reproduction: no exception, and the all-features workaround still gives (1500, 2863).
  • tests/test_anndata_compat.py gains five tests. They run the standard workflow through as_anndata for both assay classes, check that a full-width scale.data is still exported with the right values and no warning, and check that the result goes back through from_anndata. No existing test had converted an object that had been scaled.
  • Mutation-tested both ways: with the guard removed, three tests fail with the issue's own ValueError (both classes and the round trip); with it over-broad, so that every extra layer is dropped, four fail.
  • Full suite locally: 1739 passed, 30 skipped, including the mkdocs --strict build with the new paragraph in docs/interop.md. ruff findings in compat/anndata.py are unchanged (5), and mypy still reports its 4 baseline errors in 2 files.

No release is needed for this change on its own. The CHANGELOG entry is under [Unreleased] → Fixed.

🤖 Generated with Claude Code

shanikawm and others added 2 commits October 7, 2026 21:25
…ng (#141)

scale_data() scales only the variable features unless it is given others, so
scale.data has one row per variable feature, and AnnData rejects a layer that is
not as wide as var. as_anndata raised a ValueError from inside AnnData on any
object that had been through normalize_data, find_variable_features and
scale_data, in either assay class.

A layer with fewer features than the assay is now left out, and the warning names
it and says how to keep it: scale_data(obj, features=obj.feature_names()) before
converting. It is not padded, because from_anndata turns every layer back into an
assay layer and padded rows would read back as scaled values that never were.

No test had converted an object that had been scaled. The new ones do, for both
assay classes, and were mutation-tested both ways: with the guard removed they
fail with the issue's ValueError, and with it over-broad the layers that fit go
missing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@shanikawm
shanikawm merged commit 94d42a3 into main Oct 7, 2026
8 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.

as_anndata raises ValueError on objects from the standard workflow (scale.data layer is narrower than var)

1 participant