Skip to content

Fix plots and add_module_score reading the wrong rows of scale.data - #146

Merged
shanikawm merged 1 commit into
mainfrom
fix/layer-feature-alignment-140
Oct 7, 2026
Merged

shanikawm merged 1 commit into
mainfrom
fix/layer-feature-alignment-140

Conversation

@shanikawm

Copy link
Copy Markdown
Contributor

Fixes #140.

scale_data() scales only the variable features unless it is given others, so row i of scale.data is not feature i of the assay. Two places still found a gene by its place in the assay's full list and used that to index scale.data:

  • dim_heatmap raised IndexError when the place fell past the last scaled row, and otherwise drew another gene's row under the right label.
  • _get_expression, the helper behind dot_plot, feature_plot, feature_scatter, image_feature_plot, ridge_plot, spatial_feature_plot and vln_plot, did the same whenever layer="scale.data" was passed.

Both now use _resolve_layer, which do_heatmap already used, so a gene is found in the layer's own feature list. A gene that is in the assay but not in the layer raises a KeyError naming the layer and how many of the assay's features it holds, instead of "not found in assay".

One addition beyond the issue. I swept for other places that pair the assay's full feature list with a layer's matrix. add_module_score(layer="scale.data") was the only other one (markers.py and reduction.py were fixed earlier, under #66). It raised IndexError on the standard workflow; it is fixed here by the same rule, and the default layer="data" path returns the same list as before.

Verification

  • The issue's reproduction, rerun: case A no longer raises, case B draws 20 of 20 rows from the gene they are labelled with, and case C's axes equal each gene's own scale.data row.
  • tests/test_layer_feature_alignment.py, six tests, on the standard workflow with the genes shuffled. The fixture asserts its own premise: the scaled genes are not the assay's first rows, since otherwise a lookup by position would be right by accident. That is also why the existing plotting tests missed this: every one of them scales all features.
  • Mutation-tested one site at a time. Putting the old lookup back into _get_expression fails three tests, into dim_heatmap one, and into add_module_score one. The test that the default layer still reads every gene stays green throughout.
  • Full suite locally: 1740 passed, 30 skipped. ruff findings are unchanged for both package files (133 and 12) and mypy still reports its 4 baseline errors in 2 files.
  • No tutorial calls dim_heatmap or passes layer="scale.data", so no committed figure can move.

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

🤖 Generated with Claude Code

…140)

scale_data() scales only the variable features unless it is given others, so
row i of scale.data is not feature i of the assay. dim_heatmap and the helper
behind dot_plot, feature_plot, feature_scatter, ridge_plot, vln_plot and the
spatial plots found a gene by its place in the assay's full list and used that
to index scale.data: an IndexError when the place fell past the last scaled
row, and another gene's row under the right label when it did not.
add_module_score(layer="scale.data") had the same lookup.

All three now go through the layer's own feature list. A gene that is in the
assay but not in the layer raises a KeyError that names the layer and how many
of the assay's features it holds.

The plotting tests all scaled every feature, which makes a gene's place in the
assay equal its row in the layer, so none could see it. The new tests use the
standard workflow, check that their fixture can tell the two lookups apart, and
were mutation-tested one site at a time.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@shanikawm
shanikawm merged commit 7e1f3b7 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.

dim_heatmap and every plot taking layer= read the wrong rows when scale.data covers only the variable features

1 participant