Fix dens(): honour p.rope and fail loudly on degenerate posteriors#3
Merged
Conversation
Two bugs found while diagnosing a user-reported plotting glitch with
LOOCV.
1. dens() drew the orange ROPE axvlines at hardcoded +/-0.01 regardless
of the posterior's `rope` parameter. Users passing rope=0.05 or
rope=0.001 saw lines that did not match the rope used in the test —
silently misleading the eye while p.probs() was based on the real
value. Now the axvlines use p.rope.
2. _add_posterior() blew up with `ValueError: Axis limits cannot be NaN
or Inf` from matplotlib when the posterior was degenerate (var=0,
typical when the two input score arrays are identical). Replaced
with an explicit ValueError up front explaining the cause, instead
of leaking the numpy/matplotlib stack trace.
Tests
-----
* Parametrised test that the two ROPE axvlines fall at +/-rope for
rope in {0.001, 0.01, 0.05}.
* Test that dens() raises ValueError on a degenerate (var=0) posterior.
Bump 1.2.0 -> 1.2.1.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two bugs found while helping a user diagnose a confusing
dens()plot with LOOCV:dens()ignoredp.ropefor the orange ROPE axvlines. They were hardcoded at±0.01. If the user passedrope=0.05orrope=0.001toCorrelatedTTest, the plot showed lines at±0.01whileposterior.probs()was correctly computed against the real rope — silently misleading.dens()crashed withValueError: Axis limits cannot be NaN or Inffrom inside matplotlib when the posterior was degenerate (var=0, typical when the two score arrays are identical). The user got a numpy/matplotlib stack trace with no hint of the cause.Fixes
axvlinehonoursp.rope```diff
```
Explicit error on degenerate posterior
```python
if not (p.var > 0):
raise ValueError(
f"Cannot draw density: posterior variance is {p.var!r}. "
"The two classifiers likely produced identical scores; "
"the posterior is degenerate and has no density to plot."
)
```
The check covers
var=0,var<0(shouldn't happen but defensive), andvar=NaN. Users get an actionable message instead of a leaked traceback from matplotlib.Tests
TestRopeAxvline— parametrised overrope ∈ {0.001, 0.01, 0.05}, asserts the two ROPE axvlines sit at exactly±rope.TestDegeneratePosterior— assertsValueError(with\"degenerate\"in the message) when the posterior hasvar=0.Compatibility
dens()still accepts the same arguments and returns the same figure object.rope=0.01, which is what was hardcoded — the fix is a no-op for that case.Bumped to 1.2.1.
Test plan
pytest --mpl— 23/23 passing locally.python -m build— builds wheel + sdist.twine check dist/*— PASSED.v1.2.1→ publish to PyPI via the existing release workflow.🤖 Generated with Claude Code