Skip to content

Take feature_plot's quantile cutoffs as Seurat's SetQuantile does - #151

Merged
shanikawm merged 2 commits into
mainfrom
fix/feature-plot-cutoffs-145
Oct 7, 2026
Merged

shanikawm merged 2 commits into
mainfrom
fix/feature-plot-cutoffs-145

Conversation

@shanikawm

Copy link
Copy Markdown
Contributor

Fixes #145, though the cause is not the one the issue assumed.

The issue read the collapsed colour bar as a documentation gap: the skills recommend min_cutoff="q05", max_cutoff="q95", and for a gene detected in under 5% of cells both quantiles are 0. That is true of feature_plot, but not of Seurat. Seurat:::SetQuantile on 5.5.1 takes the percentile over the cells that express the feature (data <- data[data > 0]), so the same call works in R. feature_plot took it over every cell. This is a port divergence, and I fixed it as one.

What changed

  • Quantiles are Seurat's. _set_quantile mirrors SetQuantile: any q and one or two digits, over the values above zero, R's default percentile definition. The values are pinned in tests/test_feature_plot_cutoffs.py to what Seurat:::SetQuantile returns for the same vectors (q05 0.625, q95 17.5, the sparse example 0.6 and 2.85, and so on). Negatives are left out, as in R. A feature nothing expresses gets 0, where R gives NA.
  • Any qNN is accepted. The code recognised only the literal strings "q05" and "q95". Anything else fell through to vmin = "q99", and "q99" raised a TypeError comparing a string with a float, which includes the "q99" the issue itself suggests as a workaround. Anything that is not a number or qNN is now a ValueError that says what is allowed.
  • The issue's warning, for the cases that remain. Cutoffs that still leave the scale no range (equal, inverted, or every expresser having the same value, as with counts of 1) now warn and draw the feature over its own range, instead of one colour. A feature nothing expresses does not warn, since the cutoffs are not what is wrong there.
  • The skill says a quantile is over the expressing cells.

A committed tutorial figure showed this

tutorials/figures_sctransform/04_sct_featureplots_2.png had XCL1 with a colour bar of 0.0 ... 1.0 ×1e-9: every expressing cell one colour, and FCER2 nearly binary. The caption says "matching the vignette". I regenerated that figure and 03_sct_featureplots_1.png, and looked at both: XCL1 now has a scale of about 0.7 to 1.35 with a visible gradient.

  • The other four figures the same script writes are identical to the pixel, and so are the tracked handoff files, so the diff is exactly those two PNGs. I regenerated them once more after merging main, which includes the group-order change in Draw groups in a categorical column's own order, so reorder_ident has an effect #149, and they are still byte-identical to the committed ones.
  • The CITE-seq ADT panel does not change. Every cell has a positive CLR value there (frac > 0 is 1.00 for all eight proteins), so the cells that express a protein are all of them and both rules give the same scale. R's panel already used these quantiles. I rendered it under both rules and the two images are byte-identical.

This is the one change in the series that alters what an existing call draws, so it is worth your eye. git revert is clean if you disagree.

Verification

  • Seven mutants, each failing its own tests: the percentile over every cell (the original bug, 13 failures), only the two literal strings, zeros out and negatives in, no degenerate-scale handling, warn-without-fallback, warning for an all-zero feature, and a different percentile definition.
  • Full suite locally on the merged base: 1844 passed, 30 skipped. ruff on plotting.py is down two (131), because the new float | str | None replaced two Optional annotations. mypy is at its 4 baseline errors.

No release is needed for this change on its own, though it changes how documented calls draw. The CHANGELOG entry is under [Unreleased] → Fixed.

🤖 Generated with Claude Code

shanikawm and others added 2 commits October 7, 2026 22:01
min_cutoff="q05", max_cutoff="q95", the call the skills recommend for a washed-out
colour scale, collapsed the colour bar of a gene detected in under 5% of cells to
(0, 1e-9): every expressing cell the same colour and no sign anything was wrong.
The committed SCTransform tutorial figure showed it, on XCL1.

The issue read this as a documentation gap. Seurat:::SetQuantile on 5.5.1 shows it
is a divergence: Seurat takes the percentile over the cells that express the feature
(data[data > 0]), where feature_plot took it over every cell, so a sparse gene's 5th
and 95th percentiles were both 0 and the same call works in R. The values are pinned
to SetQuantile's own output, including negatives and an all-zero feature, which R
returns NA for and this maps to 0.

Only the strings "q05" and "q95" were recognised, and "q99" raised a TypeError from
comparing a string with a float. Any q and one or two digits is accepted now, as in
Seurat, and anything else is a ValueError. Cutoffs that still leave the scale no
range (equal, inverted, or every expresser the same value) warn and draw the feature
over its own range, as the issue asked.

03_sct_featureplots_1.png and 04_sct_featureplots_2.png are redrawn; the other four
figures the script writes are identical to the pixel. The CITE-seq ADT panel does not
change, since every cell has a positive CLR value there. Seven mutants each fail
their own tests.

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

feature_plot: the documented min_cutoff='q05', max_cutoff='q95' example collapses the colour scale for sparse genes

1 participant