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

1d Confidence Regions #145

Merged
merged 33 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8a0235a
1D confidence regions function added to utils and optional plot added…
htjb Feb 3, 2021
5c83e3d
generalisation of iso_probability_1d function to handle different con…
htjb Feb 8, 2021
357496c
testing of iso_probability_1d via plot functions
htjb Feb 8, 2021
60155cf
double checking and editing docs
htjb Feb 8, 2021
db12715
flake8 compliant and increased coverage
htjb Feb 8, 2021
91840d2
added explicit tests to for utils 1d propability contour function
htjb Feb 8, 2021
379fea8
flake8 on test_utils.py
htjb Feb 8, 2021
0e7d08a
changed color kwargs to use pre-existing, fixed docs and moved fill_b…
htjb Feb 8, 2021
3868a0c
flake8 fix
htjb Feb 8, 2021
478f31f
docstring fix in fill_plot_1d
htjb Feb 8, 2021
dd36aac
docstring fix in fill_plot_1d
htjb Feb 8, 2021
01d94e4
adapting 1d contour method to properly plot intervals using existing …
htjb Feb 9, 2021
4eeea25
removed redundant iso_probability_1d function
htjb Feb 9, 2021
701af35
updated test_utils.py to meet changes with the code
htjb Feb 9, 2021
29e50dc
minor change to fill colors
htjb Feb 9, 2021
c1b38f7
changed color scheme to match up with 2D posterior plotting
htjb Feb 9, 2021
a98033c
updating tests to fit with change in code
htjb Feb 9, 2021
23c5429
added back in cmap kwarg, seems to crash tests without it
htjb Feb 9, 2021
1e6250e
flake8 fix
htjb Feb 9, 2021
49c2105
added filledkde and filledfaskde plot types
htjb Feb 16, 2021
b56558d
fixed tests
htjb Feb 16, 2021
ade1a71
added new samples tests for the additional code
htjb Feb 16, 2021
b13c563
removing filledfastkde test in samples as fastkde doesnt appear to be…
htjb Feb 16, 2021
c6dab50
trying to get codecov/path to pass
htjb Feb 16, 2021
7e57295
fix for fastkde 1d filled plots
htjb Feb 17, 2021
2cdf69a
adding in weights for iso_probability_contours_from_samples call
htjb Feb 17, 2021
db055c8
fixed issue when changing colormaps
htjb Feb 23, 2021
f4b98fc
fixing flake issue
htjb Feb 23, 2021
469db4c
triggering fill with facecolor for 1d plots and removing filled1d kwarg
htjb Mar 6, 2021
f4fc6a7
reorganised kwargs for filled 1d plots/color scheme
htjb Mar 8, 2021
cb57977
edits to color scheme
htjb Mar 9, 2021
c256b75
few edits to plot.py and additional tests added
htjb Mar 9, 2021
3206407
bumping beta version number and fixing docstring error
htjb Mar 10, 2021
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
18 changes: 9 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ anesthetic: nested sampling visualisation
=========================================
:anesthetic: nested sampling visualisation
:Author: Will Handley and Lukas Hergt
:Version: 2.0.0-beta.6
:Version: 2.0.0-beta.7
:Homepage: https://github.com/williamjameshandley/anesthetic
:Documentation: http://anesthetic.readthedocs.io/

Expand Down Expand Up @@ -64,9 +64,9 @@ Features
- Both samples and plotting axes are stored as a ``pandas.DataFrame``, with parameter names as indices, which makes for easy access and modification.
- Sensible color scheme for plotting nearly flat distributions.
- For easy extension/modification, uses the standard python libraries:
`numpy <https://www.numpy.org/>`__,
`scipy <https://www.scipy.org/>`__,
`matplotlib <https://matplotlib.org/>`__
`numpy <https://www.numpy.org/>`__,
`scipy <https://www.scipy.org/>`__,
`matplotlib <https://matplotlib.org/>`__
and `pandas <https://pandas.pydata.org/>`__.

Installation
Expand All @@ -82,7 +82,7 @@ or via the setup.py

.. code:: bash

git clone https://github.com/williamjameshandley/anesthetic
git clone https://github.com/williamjameshandley/anesthetic
cd anesthetic
python setup.py install --user

Expand All @@ -97,7 +97,7 @@ You can check that things are working by running the test suite:


Dependencies
~~~~~~~~~~~~
~~~~~~~~~~~~

Basic requirements:

Expand Down Expand Up @@ -173,10 +173,10 @@ Another posterior plotting tool?

There are several excellent tools for plotting marginalised posteriors:

- `getdist <http://getdist.readthedocs.io/en/latest/intro.html>`__
- `getdist <http://getdist.readthedocs.io/en/latest/intro.html>`__
- `corner <https://corner.readthedocs.io>`__
- `pygtc <https://pygtc.readthedocs.io>`__
- `dynesty <https://dynesty.readthedocs.io>`__
- `dynesty <https://dynesty.readthedocs.io>`__
- `MontePython <http://baudren.github.io/montepython.html>`__

Why create another one? In general, any dedicated user of software will find that there is some functionality that in their use case is lacking, and the designs of previous codes make such extensions challenging. In my case this was:
Expand All @@ -192,7 +192,7 @@ Why create another one? In general, any dedicated user of software will find tha
samples['omegab'] = samples.omegabh2/(samples.H0/100)**2 # Define omegab
samples.tex['omegab'] = '$\Omega_b$' # Label omegab
samples.plot_1d('omegab') # Simple 1D plot

3. Many KDE plotting tools have conventions that don't play well with uniformly distributed parameters, which presents a problem if you are trying to plot priors along with your posteriors. ``anesthetic`` has a sensible mechanism, by defining the contours by the amount of iso-probability mass they contain, but colouring the fill in relation to the probability density of the contour.

What's in a name?
Expand Down
79 changes: 77 additions & 2 deletions anesthetic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,50 @@ def fastkde_plot_1d(ax, data, *args, **kwargs):
lower/upper prior bound
optional, default None

levels: list
values at which to draw iso-probability lines.
optional, default [0.68, 0.95]

williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
facecolor: bool or string
If set to True then the 1d plot will be shaded with the value of the
``color`` kwarg. Set to a string such as 'blue', 'k', 'r', 'C1' ect.
to define the color of the shading directly.
optional, default False

Returns
-------
lines: matplotlib.lines.Line2D
A list of line objects representing the plotted data (same as
matplotlib matplotlib.axes.Axes.plot command)

"""
kwargs = normalize_kwargs(
kwargs,
dict(linewidth=['lw'], linestyle=['ls'], color=['c'],
facecolor=['fc'], edgecolor=['ec']))

if len(data) == 0:
return np.zeros(0), np.zeros(0)

if data.max()-data.min() <= 0:
return

levels = kwargs.pop('levels', [0.68, 0.95])

xmin = kwargs.pop('xmin', None)
xmax = kwargs.pop('xmax', None)
cmap = kwargs.pop('cmap', None)
color = kwargs.pop('color', (next(ax._get_lines.prop_cycler)['color']
if cmap is None else cmap(0.68)))
facecolor = kwargs.pop('facecolor', False)

if 'edgecolor' in kwargs:
edgecolor = kwargs.pop('edgecolor')
if edgecolor:
color = edgecolor
else:
edgecolor = color

q = kwargs.pop('q', '5sigma')
q = quantile_plot_interval(q=q)

Expand All @@ -281,6 +307,19 @@ def fastkde_plot_1d(ax, data, *args, **kwargs):

ans = ax.plot(x[i], p[i], color=color, *args, **kwargs)
ax.set_xlim(*check_bounds(x[i], xmin, xmax), auto=True)

if facecolor and facecolor not in [None, 'None', 'none']:
if facecolor is True:
facecolor = color
c = iso_probability_contours(p[i], contours=levels)
cmap = basic_cmap(facecolor)
fill = []
for j in range(len(c)-1):
fill.append(ax.fill_between(x[i], p[i], where=p[i] >= c[j],
color=cmap(c[j]), edgecolor=edgecolor))

return ans, fill

return ans


Expand Down Expand Up @@ -309,6 +348,16 @@ def kde_plot_1d(ax, data, *args, **kwargs):
lower/upper prior bound.
optional, default None

levels: list
values at which to draw iso-probability lines.
optional, default [0.68, 0.95]

facecolor: bool or string
If set to True then the 1d plot will be shaded with the value of the
``color`` kwarg. Set to a string such as 'blue', 'k', 'r', 'C1' ect.
to define the color of the shading directly.
optional, default False

Returns
-------
lines: matplotlib.lines.Line2D
Expand All @@ -324,15 +373,27 @@ def kde_plot_1d(ax, data, *args, **kwargs):

kwargs = normalize_kwargs(
kwargs,
dict(linewidth=['lw'], linestyle=['ls'], color=['c']),
drop=['fc', 'ec'])
dict(linewidth=['lw'], linestyle=['ls'], color=['c'],
facecolor=['fc'], edgecolor=['ec']))

levels = kwargs.pop('levels', [0.68, 0.95])

xmin = kwargs.pop('xmin', None)
xmax = kwargs.pop('xmax', None)
weights = kwargs.pop('weights', None)
ncompress = kwargs.pop('ncompress', 1000)
cmap = kwargs.pop('cmap', None)
color = kwargs.pop('color', (next(ax._get_lines.prop_cycler)['color']
if cmap is None else cmap(0.68)))
facecolor = kwargs.pop('facecolor', False)

if 'edgecolor' in kwargs:
edgecolor = kwargs.pop('edgecolor')
if edgecolor:
color = edgecolor
else:
edgecolor = color

q = kwargs.pop('q', '5sigma')
q = quantile_plot_interval(q=q)

Expand All @@ -354,6 +415,20 @@ def kde_plot_1d(ax, data, *args, **kwargs):
pp /= pp.max()
ans = ax.plot(x[i], pp, color=color, *args, **kwargs)
ax.set_xlim(*check_bounds(x[i], xmin, xmax), auto=True)

if facecolor and facecolor not in [None, 'None', 'none']:
if facecolor is True:
facecolor = color
c = iso_probability_contours_from_samples(pp, contours=levels,
weights=w)
cmap = basic_cmap(facecolor)
fill = []
for j in range(len(c)-1):
fill.append(ax.fill_between(x[i], pp, where=pp >= c[j],
color=cmap(c[j]), edgecolor=edgecolor))

return ans, fill

return ans


Expand Down
12 changes: 11 additions & 1 deletion tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from matplotlib.tri import TriContourSet
from matplotlib.lines import Line2D
from matplotlib.patches import Patch, Polygon
from matplotlib.colors import ColorConverter
from matplotlib.colors import ColorConverter, to_rgba
from matplotlib.figure import Figure
from pandas.core.series import Series
from pandas.core.frame import DataFrame
Expand Down Expand Up @@ -230,6 +230,16 @@ def test_kde_plot_1d(plot_1d):
plot_1d(ax, data, q=0.9)
plot_1d(ax, data, q=(0.1, 0.9))

# Check iso-probability code
line, fill = plot_1d(ax, data, facecolor=True)
plot_1d(ax, data, facecolor=True, levels=[0.2, 0.6, 0.8])
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
line, fill = plot_1d(ax, data, fc='blue', color='k', ec='r')
assert(np.all(fill[0].get_edgecolor()[0] == to_rgba('r')))
assert (to_rgba(line[0].get_color()) == to_rgba('r'))
line, fill = plot_1d(ax, data, fc=True, color='k', ec=None)
assert(len(fill[0].get_edgecolor()) == 0)
assert (to_rgba(line[0].get_color()) == to_rgba('k'))

except ImportError:
if 'fastkde' not in sys.modules:
pass
Expand Down
3 changes: 3 additions & 0 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ def test_plot_2d_types():
fig, axes = ns.plot_2d(params, types={'lower': 'kde', 'diagonal': 'kde'})
assert((~axes.isnull()).sum().sum() == 6)

fig, axes = ns.plot_2d(params, types={'lower': 'kde', 'diagonal': 'kde'})
assert((~axes.isnull()).sum().sum() == 6)

fig, axes = ns.plot_2d(params, types={'lower': 'kde', 'diagonal': 'kde',
'upper': 'scatter'})
assert((~axes.isnull()).sum().sum() == 12)
Expand Down