Skip to content

Commit 54ca58c

Browse files
jhmatthewspre-commit-ci[bot]dfm
authored
Proposed fix for title errorbars/quantiles bug (#193)
* initial attempt to fix hardwired title quantiles * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating tests * adding baseline images Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dan F-M <[email protected]>
1 parent c05c15e commit 54ca58c

File tree

7 files changed

+61
-5
lines changed

7 files changed

+61
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ dist
77
build
88
test_figures
99
.coverage
10-
!corner/tests/baseline_images/*/*.png
10+
!tests/baseline_images/*/*.png
1111
.pytest_cache
1212
corner_version.py

src/corner/core.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def corner_impl(
4141
truth_color="#4682b4",
4242
scale_hist=False,
4343
quantiles=None,
44+
title_quantiles=None,
4445
verbose=False,
4546
fig=None,
4647
max_n_ticks=5,
@@ -62,6 +63,20 @@ def corner_impl(
6263
if titles is None:
6364
titles = labels
6465

66+
# deal with title quantiles so they much quantiles unless desired otherwise
67+
if title_quantiles is None:
68+
if len(quantiles) > 0:
69+
title_quantiles = quantiles
70+
else:
71+
# a default for when quantiles not supplied.
72+
title_quantiles = [0.16, 0.5, 0.84]
73+
74+
if show_titles and len(title_quantiles) != 3:
75+
raise ValueError(
76+
"'title_quantiles' must contain exactly three values; "
77+
"pass a length-3 list or array using the 'title_quantiles' argument"
78+
)
79+
6580
# Deal with 1D sample lists.
6681
xs = _parse_input(xs)
6782
assert xs.shape[0] <= xs.shape[1], (
@@ -212,15 +227,15 @@ def corner_impl(
212227
if title_fmt is not None:
213228
# Compute the quantiles for the title. This might redo
214229
# unneeded computation but who cares.
215-
q_16, q_50, q_84 = quantile(
216-
x, [0.16, 0.5, 0.84], weights=weights
230+
q_lo, q_mid, q_hi = quantile(
231+
x, title_quantiles, weights=weights
217232
)
218-
q_m, q_p = q_50 - q_16, q_84 - q_50
233+
q_m, q_p = q_mid - q_lo, q_hi - q_mid
219234

220235
# Format the quantile display.
221236
fmt = "{{0:{0}}}".format(title_fmt).format
222237
title = r"${{{0}}}_{{-{1}}}^{{+{2}}}$"
223-
title = title.format(fmt(q_50), fmt(q_m), fmt(q_p))
238+
title = title.format(fmt(q_mid), fmt(q_m), fmt(q_p))
224239

225240
# Add in the column name if it's given.
226241
if titles is not None:

src/corner/corner.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def corner(
2929
label_kwargs=None,
3030
titles=None,
3131
show_titles=False,
32+
title_quantiles=None,
3233
title_fmt=".2f",
3334
title_kwargs=None,
3435
truths=None,
@@ -135,6 +136,11 @@ def corner(
135136
Displays a title above each 1-D histogram showing the 0.5 quantile
136137
with the upper and lower errors supplied by the quantiles argument.
137138
139+
title_quantiles : iterable
140+
A list of 3 fractional quantiles to show as the the upper and lower
141+
errors. If `None` (default), inherit the values from quantiles, unless
142+
quantiles is `None`, in which case it defaults to [0.16,0.5,0.84]
143+
138144
title_fmt : string
139145
The format string for the quantiles given in titles. If you explicitly
140146
set ``show_titles=True`` and ``title_fmt=None``, the labels will be
@@ -241,6 +247,7 @@ def corner(
241247
label_kwargs=label_kwargs,
242248
titles=titles,
243249
show_titles=show_titles,
250+
title_quantiles=title_quantiles,
244251
title_fmt=title_fmt,
245252
title_kwargs=title_kwargs,
246253
truths=truths,
@@ -271,6 +278,7 @@ def corner(
271278
label_kwargs=label_kwargs,
272279
titles=titles,
273280
show_titles=show_titles,
281+
title_quantiles=title_quantiles,
274282
title_fmt=title_fmt,
275283
title_kwargs=title_kwargs,
276284
truths=truths,
98.1 KB
Loading
97.1 KB
Loading
92.6 KB
Loading

tests/test_corner.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,39 @@ def test_quantiles():
8080
_run_corner(quantiles=[0.16, 0.5, 0.84])
8181

8282

83+
@image_comparison(
84+
baseline_images=["title_quantiles"], remove_text=False, extensions=["png"]
85+
)
86+
def test_title_quantiles():
87+
_run_corner(
88+
quantiles=[0.16, 0.5, 0.84],
89+
title_quantiles=[0.05, 0.5, 0.95],
90+
show_titles=True,
91+
)
92+
93+
94+
@image_comparison(
95+
baseline_images=["title_quantiles_default"],
96+
remove_text=False,
97+
extensions=["png"],
98+
)
99+
def test_title_quantiles_default():
100+
_run_corner(quantiles=[0.16, 0.5, 0.84], show_titles=True)
101+
102+
103+
@image_comparison(
104+
baseline_images=["title_quantiles_raises"],
105+
remove_text=False,
106+
extensions=["png"],
107+
)
108+
def test_title_quantiles_raises():
109+
with pytest.raises(ValueError):
110+
_run_corner(quantiles=[0.05, 0.16, 0.5, 0.84, 0.95], show_titles=True)
111+
112+
# This one shouldn't raise since show_titles isn't provided
113+
_run_corner(quantiles=[0.05, 0.16, 0.5, 0.84, 0.95])
114+
115+
83116
@image_comparison(
84117
baseline_images=["color"], remove_text=True, extensions=["png"]
85118
)

0 commit comments

Comments
 (0)