Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed fix for title errorbars/quantiles bug #193

Merged
merged 4 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ dist
build
test_figures
.coverage
!corner/tests/baseline_images/*/*.png
!tests/baseline_images/*/*.png
.pytest_cache
corner_version.py
23 changes: 19 additions & 4 deletions src/corner/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def corner_impl(
truth_color="#4682b4",
scale_hist=False,
quantiles=None,
title_quantiles=None,
verbose=False,
fig=None,
max_n_ticks=5,
Expand All @@ -62,6 +63,20 @@ def corner_impl(
if titles is None:
titles = labels

# deal with title quantiles so they much quantiles unless desired otherwise
if title_quantiles is None:
if len(quantiles) > 0:
title_quantiles = quantiles
else:
# a default for when quantiles not supplied.
title_quantiles = [0.16, 0.5, 0.84]

if show_titles and len(title_quantiles) != 3:
raise ValueError(
"'title_quantiles' must contain exactly three values; "
"pass a length-3 list or array using the 'title_quantiles' argument"
)

# Deal with 1D sample lists.
xs = _parse_input(xs)
assert xs.shape[0] <= xs.shape[1], (
Expand Down Expand Up @@ -212,15 +227,15 @@ def corner_impl(
if title_fmt is not None:
# Compute the quantiles for the title. This might redo
# unneeded computation but who cares.
q_16, q_50, q_84 = quantile(
x, [0.16, 0.5, 0.84], weights=weights
q_lo, q_mid, q_hi = quantile(
x, title_quantiles, weights=weights
)
q_m, q_p = q_50 - q_16, q_84 - q_50
q_m, q_p = q_mid - q_lo, q_hi - q_mid

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

# Add in the column name if it's given.
if titles is not None:
Expand Down
8 changes: 8 additions & 0 deletions src/corner/corner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def corner(
label_kwargs=None,
titles=None,
show_titles=False,
title_quantiles=None,
title_fmt=".2f",
title_kwargs=None,
truths=None,
Expand Down Expand Up @@ -135,6 +136,11 @@ def corner(
Displays a title above each 1-D histogram showing the 0.5 quantile
with the upper and lower errors supplied by the quantiles argument.

title_quantiles : iterable
A list of 3 fractional quantiles to show as the the upper and lower
errors. If `None` (default), inherit the values from quantiles, unless
quantiles is `None`, in which case it defaults to [0.16,0.5,0.84]

title_fmt : string
The format string for the quantiles given in titles. If you explicitly
set ``show_titles=True`` and ``title_fmt=None``, the labels will be
Expand Down Expand Up @@ -241,6 +247,7 @@ def corner(
label_kwargs=label_kwargs,
titles=titles,
show_titles=show_titles,
title_quantiles=title_quantiles,
title_fmt=title_fmt,
title_kwargs=title_kwargs,
truths=truths,
Expand Down Expand Up @@ -271,6 +278,7 @@ def corner(
label_kwargs=label_kwargs,
titles=titles,
show_titles=show_titles,
title_quantiles=title_quantiles,
title_fmt=title_fmt,
title_kwargs=title_kwargs,
truths=truths,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions tests/test_corner.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,39 @@ def test_quantiles():
_run_corner(quantiles=[0.16, 0.5, 0.84])


@image_comparison(
baseline_images=["title_quantiles"], remove_text=False, extensions=["png"]
)
def test_title_quantiles():
_run_corner(
quantiles=[0.16, 0.5, 0.84],
title_quantiles=[0.05, 0.5, 0.95],
show_titles=True,
)


@image_comparison(
baseline_images=["title_quantiles_default"],
remove_text=False,
extensions=["png"],
)
def test_title_quantiles_default():
_run_corner(quantiles=[0.16, 0.5, 0.84], show_titles=True)


@image_comparison(
baseline_images=["title_quantiles_raises"],
remove_text=False,
extensions=["png"],
)
def test_title_quantiles_raises():
with pytest.raises(ValueError):
_run_corner(quantiles=[0.05, 0.16, 0.5, 0.84, 0.95], show_titles=True)

# This one shouldn't raise since show_titles isn't provided
_run_corner(quantiles=[0.05, 0.16, 0.5, 0.84, 0.95])


@image_comparison(
baseline_images=["color"], remove_text=True, extensions=["png"]
)
Expand Down