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

anesthetic style plotting functions #35

Open
wants to merge 125 commits into
base: master
Choose a base branch
from
Open

anesthetic style plotting functions #35

wants to merge 125 commits into from

Conversation

williamjameshandley
Copy link
Contributor

@williamjameshandley williamjameshandley commented Feb 28, 2024

Description

This PR implements anesthetic-style plotting functionality.

There is a new set of functions in:

lsbi.plot.pdf_plot_1d(ax, dist, index=0, *args, **kwargs):
lsbi.plot.pdf_plot_2d(ax, dist, index=[0, 1], *args, **kwargs):
lsbi.plot.scatter_plot_2d(ax, dist, index=[0, 1], *args, **kwargs):
lsbi.plot.plot_1d(dist, axes=None, *args, **kwargs):
lsbi.plot.plot_2d(dist, axes=None, *args, **kwargs):

These mirror e.g. anesthetic.plot.contour_plot_2d, but instead plot exact marginal pdfs.

This is then linked into lsbi.dist functionality via lsbi.multivariate_normal.plot_1d (and the same for 2d and mixture_normal).

As a fun example, it allows you to plot things like this:

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

from lsbi.model import LinearModel, MixtureModel
from lsbi.plot import make_2d_axes, plot_2d

root = "penguin"
data = np.array(Image.open(f"{root}.png"))
x = np.linspace(-1, 1, data.shape[1])
y = np.linspace(1, -1, data.shape[0])
X, Y = np.meshgrid(x, y)
x = np.stack([X[data < 255], Y[data < 255]]).T

i = np.random.choice(np.arange(x.shape[0]), 10000)


model = MixtureModel(
    M=0, μ=X[data < 255][i, None], m=Y[data < 255][i, None], C=0.03**2, Σ=0.03**2
)

fig, axes = make_2d_axes([r"$\theta$", "$D$"])
axes = plot_2d(model.joint(), axes, color="C4")
plot_2d(model.prior(), axes.iloc[:1, :1], color="C1")
plot_2d(model.evidence(), axes.iloc[1:, 1:], color="C3")

D = [0]
axes.axlines(dict(zip(axes.index[model.d :], D)), color="C0")
plot_2d(model.posterior(D), axes.iloc[: model.d, : model.d], color="C0")

θ = [0]

axes.axlines(dict(zip(axes.index[: model.d], θ)), color="C2")
plot_2d(model.likelihood(θ), axes.iloc[model.d :, model.d :], color="C2")
# axes.iloc[1,0].plot(model.μ.T, model.m.T, '.', color='C4')

axes.iloc[0, 0].set_xlim(-1, 1)
axes.iloc[1, 1].set_xlim(-1, 1)
axes.iloc[0, 0].set_yticks([])
axes.iloc[1, 0].set_yticks([])
axes.iloc[1, 0].set_xticks([])
axes.iloc[1, 1].set_xticks([])
fig.set_size_inches(6, 6)
fig.tight_layout()
fig.savefig(f"{root}_posterior.png")

penguin_posterior
cat_posterior

Checklist:

  • I have performed a self-review of my own code
  • My code is black compliant (black . --check)
  • My code is isort compliant (isort . --profile black --filter-files)
  • My code contains compliant docstrings (pydocstyle --convention=numpy lsbi)
  • New and existing unit tests pass locally with my changes (python -m pytest)
  • I have added tests that prove my fix is effective or that my feature works
  • I have appropriately incremented the semantic version number in both README.rst and lsbi/_version.py

Copy link

codecov bot commented Mar 2, 2024

Codecov Report

Attention: Patch coverage is 85.90604% with 21 lines in your changes are missing coverage. Please review.

Project coverage is 96.94%. Comparing base (65e7ec2) to head (ea8e6e0).

❗ Current head ea8e6e0 differs from pull request most recent head f766620. Consider uploading reports for the commit f766620 to get more accurate results

Files Patch % Lines
lsbi/stats.py 48.78% 21 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##            master      #35      +/-   ##
===========================================
- Coverage   100.00%   96.94%   -3.06%     
===========================================
  Files            6        7       +1     
  Lines          546      688     +142     
===========================================
+ Hits           546      667     +121     
- Misses           0       21      +21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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