Skip to content

refactor: draw cdf and cdf-diff SBC plots with one helper, remove unused colour helpers - #2161

Merged
janfb merged 3 commits into
sbi-dev:mainfrom
dgedon:refactor/sbc-cdf-helpers
Nov 12, 2026
Merged

janfb merged 3 commits into
sbi-dev:mainfrom
dgedon:refactor/sbc-cdf-helpers

Conversation

@dgedon

@dgedon dgedon commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two cleanups in the plotting code, with no change to what is drawn.

1. One helper for the cdf and cdf-diff SBC plots. #1951 added the cdf-diff plot type as copies of the cdf curve and band helpers (_plot_ranks_as_cdf_diff, _plot_cdf_diff_region_expected_under_uniformity), plus a second set of branches in _sbc_rank_plot, in both the subplot and the single-axis layout. The copies differ only in subtracting the uniform CDF, the y range and label, and a dashed zero line. #2119 already shares the band and the x positions (_uniform_cdf_band, _cdf_x).

This PR gives _plot_ranks_as_cdf and _plot_cdf_region_expected_under_uniformity a diff flag and removes the copies. _sbc_rank_plot now has one branch for both plot types. plot_type is validated before plotting, so the two else: raise ValueError branches could not be reached and are removed too. #1951's review noted that the "general refactoring" part of #1734 was still open, because the docstrings of the CDF and histogram helpers didn't match their arguments. Those docstrings already match on main; this PR removes the duplication #1951 left in the CDF helpers.

2. Remove hex2rgb and rgb2hex from sbi/utils/plotting_helpers.py. They came over with the plotting code in 2020, nothing in sbi calls them, sbi.utils doesn't re-export them, and a GitHub code search finds no users.

Evidence

The plots don't change. I rendered sbc_rank_plot for every combination of:

  • plot type (cdf, cdf-diff, hist),
  • one or two sets of ranks,
  • params_in_subplots on and off,
  • num_bins (default, 1, 7, 30),
  • num_posterior_samples (10, 1000),
  • show_uniform_region on and off,
  • three rank distributions (uniform, too wide, shifted).

All 576 images are pixel-identical to main. As a control, scaling the uniform reference by 0.99 changes 384 of them, all the cdf and cdf-diff cases.

tests/plot_test.py and tests/sbc_test.py::test_sbc_plotting pass (564 passed, 9 expected failures), including #2119's tests of the drawn SBC curves and bands. pyright sbi and pre-commit on the changed files pass. I did not run the full test suite locally.

Merge Danger

Door: two-way.

Blast Radius:

  • Private SBC helpers only. The public sbc_rank_plot keeps its signature, and what it draws doesn't change.
  • hex2rgb and rgb2hex can no longer be imported from sbi.utils.plotting_helpers.

Related: independent of #2160, which edits other parts of plot.py; the two merge without conflicts. #2154 edits other lines of sbi/utils/plotting_helpers.py.

I used Claude Code for the analysis, the implementation and the checks.

🤖 Generated with Claude Code

dgedon and others added 2 commits November 10, 2026 09:09
sbi-dev#1951 added the cdf-diff plot type as copies of the cdf curve and band
helpers, and as a second set of branches in `_sbc_rank_plot`. The copies
differ only in subtracting the uniform CDF, the y range and label, and a
zero line. Give `_plot_ranks_as_cdf` and
`_plot_cdf_region_expected_under_uniformity` a `diff` flag and drop the
copies. `plot_type` is validated before plotting, so the `else: raise`
branches could not be reached.

The plots don't change: 576 renders over plot types, rank sets, subplot
layouts, bin counts, numbers of posterior samples and rank distributions
are pixel-identical to main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They came over with the plotting code in 2020 and nothing calls them, in
sbi or, according to a GitHub code search, elsewhere. `sbi.utils` does
not re-export them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The SBC plotting code now uses shared helpers for CDF and CDF-difference plots. The plotting helpers module also removes the hex2rgb and rgb2hex functions.

Changes: SBC CDF plotting

Layer / File(s) Summary
CDF and uniformity-band difference behavior
sbi/analysis/plot.py
The CDF helper subtracts the uniform CDF in difference mode, uses a [-0.5, 0.5] range, and draws a zero reference line. The expected-uniformity band is also offset by the uniform CDF.
CDF plot routing
sbi/analysis/plot.py
Both subplot layouts route "cdf" and "cdf-diff" through the shared CDF and uniformity-band helpers. Other plot types retain the histogram path.

Color conversion helper removal

Layer / File(s) Summary
Remove color conversion helpers
sbi/utils/plotting_helpers.py
The module removes hex2rgb and rgb2hex.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related PRs

  • sbi-dev/sbi#2119: Changes rank binning and CDF curve and band scaling in sbc_rank_plot, which is also affected by this PR’s CDF plotting changes.

Suggested reviewers: janfb, abelaba

Merge Risk: Minimal · up to 245ea

No actionable issue remains that should block merging. The removed color helpers will no longer be importable, as intended.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly summarizes both the shared CDF plotting helper and removal of unused color helpers.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description clearly explains both changes, their purpose, and supporting test evidence. It also discloses AI use. The checklist is omitted, and the issue-closure section does not state whether thi…
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 1 files.

Full details: Description check

Explanation

The description clearly explains both changes, their purpose, and supporting test evidence. It also discloses AI use. The checklist is omitted, and the issue-closure section does not state whether this PR closes an issue, but the description is otherwise complete.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9c89581c-6339-4e5c-bb04-e7daf9f537b9
Commits

Reviewing files that changed from the base of the PR and between cd8f131 and 245ea05.

Files selected for processing: 2

  • sbi/analysis/plot.py
  • sbi/utils/plotting_helpers.py
Files with no reviewable changes: 1

  • sbi/utils/plotting_helpers.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.72%. Comparing base (cd8f131) to head (592eba7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2161      +/-   ##
==========================================
+ Coverage   90.69%   90.72%   +0.03%     
==========================================
  Files         142      142              
  Lines       14855    14830      -25     
==========================================
- Hits        13472    13454      -18     
+ Misses       1383     1376       -7     
Flag Coverage Δ
fast 86.89% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sbi/analysis/plot.py 80.76% <100.00%> (-0.21%) ⬇️
sbi/utils/plotting_helpers.py 96.49% <ø> (+4.55%) ⬆️

... and 1 file with indirect coverage changes

It sat inside the parameter loop, at the end of the histogram branch,
but describes the code that runs after the loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dgedon
dgedon requested a review from janfb November 10, 2026 10:15
@janfb

janfb commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Thanks @dgedon, nice cleanup, and the pixel comparison against main makes it easy to trust.

While reviewing I found two problems in the SBC plots that were in main already and that this PR keeps as they are:

  • cdf-diff fixes the y-axis at ±0.5, but the difference can reach ±1, so a badly calibrated posterior gets clipped at 0.5.
  • With more than one set of ranks, the cdf and cdf-diff subplots draw no legend, so you can't tell the methods apart.

I'll fix both in a follow-up PR and ask you for the review.

@janfb
janfb merged commit 5394dea into sbi-dev:main Nov 12, 2026
18 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.

2 participants