jkanner / streamlit-pe-demo

Demo of streamlit pe stuff
0 stars 0 forks source link

Gaussian kdes and triangle plots #2

Open pesummary opened 3 years ago

pesummary commented 3 years ago

As with #1, if I generate a triangle plot with a_1 and a_2, I see some unwanted smoothing from the gaussian_kde. This is replicated below,

from pesummary.utils.samples_dict import MultiAnalysisSamplesDict
import numpy as np

data = MultiAnalysisSamplesDict({"label1": {"a_1": np.random.uniform(0, 1, 10000), "a_2": np.random.uniform(0, 1, 10000)}})
fig, _, _, _ = data.plot(["a_1", "a_2"], type="reverse_triangle")
fig.savefig("gaussian.png")
fig.close()

gaussian

To overcome this, you can pass the kde=bounded_1d_kde, kde_kwargs={"xlow": 0., "xhigh": 1.}, kde_2d=Bounded_2d_kde, kde_2d_kwargs={"xlow": 0., "xhigh": 1., "ylow": 0., "yhigh": 1.} kwargs. These kwargs are passed to the pesummary.core.plots.bounded_1d_kde.bounded_1d_kde and pesummary.core.plots.bounded_2d_kde.Bounded_2d_kde functions. You can see an example below,

from pesummary.core.plots.bounded_1d_kde import bounded_1d_kde
from pesummary.core.plots.bounded_2d_kde import Bounded_2d_kde

fig, _, _, _ = data.plot(["a_1", "a_2"], type="reverse_triangle", kde=bounded_1d_kde, kde_2d=Bounded_2d_kde, kde_kwargs={"xlow": 0., "xhigh": 1.}, kde_2d_kwargs={"xlow": 0., "xhigh": 1., "ylow": 0., "yhigh": 1.})
fig.savefig("bounded.png")
fig.close()

bounded

It can be expensive to compute the bounded 2d KDE and therefore is probably not practical for this app. I would advocate for using the bounded_1d_kde though.

jkanner commented 3 years ago

This sounds like a good suggestion, but it might be tricky to pass the bounds of the prior into the plot method. In this example, you've hard-coded 0 and 1 as bounds, which would not apply for every parameter. Currently, the user selects which parameter to plot. I would need some way of obtaining the bounds automatically for each parameter. Is there ... a dictionary of priors ... or similar inside the PE sample files? I could access this to read out prior bounds if I can figure out how.

pesummary commented 3 years ago

Yes, we store a list of bounds for all parameters in pesummary. You can see the dictionary here: https://git.ligo.org/lscsoft/pesummary/-/blob/master/pesummary/gw/plots/bounds.py#L19. This is probably the easiest way to get bounds for a given parameter :)

jkanner commented 3 years ago

Got it! Thank you.