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

Fix a bug of plotter which contains inf #165

Merged
merged 7 commits into from
May 27, 2024
Merged
Changes from 5 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
19 changes: 16 additions & 3 deletions appletree/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ def __init__(self, backend_file_name, discard=0, thin=1):
backend = emcee.backends.HDFBackend(self.backend_file_name, read_only=True)

self.chain = backend.get_chain(discard=discard, thin=thin)
self.flat_chain = backend.get_chain(discard=discard, thin=thin, flat=True)
self.posterior = backend.get_log_prob(discard=discard, thin=thin)
self.flat_posterior = backend.get_log_prob(discard=discard, thin=thin, flat=True)
self.prior = backend.get_blobs(discard=discard, thin=thin)
# We drop iterations with inf and nan posterior
mask = ~np.isinf(self.posterior) & ~np.isnan(self.posterior)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mask = np.all(mask, axis=1)
self.chain = self.chain[mask]
self.posterior = self.posterior[mask]
self.prior = self.prior[mask]

self.flat_chain = backend.get_chain(discard=discard, thin=thin, flat=True)
self.flat_posterior = backend.get_log_prob(discard=discard, thin=thin, flat=True)
self.flat_prior = backend.get_blobs(discard=discard, thin=thin, flat=True)
# We drop samples with inf and nan posterior
mask = ~np.isinf(self.flat_posterior) & ~np.isnan(self.flat_posterior)
self.flat_chain = self.flat_chain[mask]
self.flat_posterior = self.flat_posterior[mask]
self.flat_prior = self.flat_prior[mask]

with h5py.File(self.backend_file_name, "r") as f:
self.param_names = f["mcmc"].attrs["parameter_fit"]
Expand Down Expand Up @@ -199,7 +211,8 @@ def plot_corner(self, fig=None):
if fig is None:
fig = plt.figure(figsize=(2 * (self.n_param + 2), 2 * (self.n_param + 2)))
samples = np.concatenate(
(self.flat_chain, self.flat_posterior[:, None], self.flat_prior[:, None]), axis=1
(self.flat_chain, self.flat_posterior[:, None], self.flat_prior[:, None]),
axis=1,
)
labels = np.concatenate((self.param_names, ["log posterior", "log prior"]))

Expand Down
Loading