From 463b38afb73d6b8adfd61139c791727fe29d61df Mon Sep 17 00:00:00 2001 From: Zihao Xu Date: Mon, 27 May 2024 09:57:42 -0500 Subject: [PATCH 1/7] filter out inf --- appletree/plot.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/appletree/plot.py b/appletree/plot.py index e587c475..3cefd05e 100644 --- a/appletree/plot.py +++ b/appletree/plot.py @@ -27,11 +27,22 @@ 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 posterior + mask = np.all(~np.isinf(self.posterior), 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 posterior + mask = ~np.isinf(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"] From c0fa7e2ec82b7a8d008ee29d497d442d89876577 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 27 May 2024 15:00:39 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- appletree/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appletree/plot.py b/appletree/plot.py index 3cefd05e..2c609eda 100644 --- a/appletree/plot.py +++ b/appletree/plot.py @@ -34,7 +34,7 @@ def __init__(self, backend_file_name, discard=0, thin=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) From 61e2f6d639b1f9363a4e43727cad4006d7a16fe8 Mon Sep 17 00:00:00 2001 From: Zihao Xu Date: Mon, 27 May 2024 10:37:26 -0500 Subject: [PATCH 3/7] nan filter --- appletree/plot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/appletree/plot.py b/appletree/plot.py index 2c609eda..6cee087b 100644 --- a/appletree/plot.py +++ b/appletree/plot.py @@ -30,7 +30,8 @@ def __init__(self, backend_file_name, discard=0, thin=1): self.posterior = backend.get_log_prob(discard=discard, thin=thin) self.prior = backend.get_blobs(discard=discard, thin=thin) # We drop iterations with inf posterior - mask = np.all(~np.isinf(self.posterior), axis=1) + mask = ~np.isinf(self.posterior) & ~np.isnan(self.posterior) + mask = np.all(mask, axis=1) self.chain = self.chain[mask] self.posterior = self.posterior[mask] self.prior = self.prior[mask] @@ -39,7 +40,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): 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 posterior - mask = ~np.isinf(self.flat_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] From b88cf2def48785d4b582fadec9aafb1e2cc29779 Mon Sep 17 00:00:00 2001 From: Zihao Xu Date: Mon, 27 May 2024 10:40:39 -0500 Subject: [PATCH 4/7] fix comment --- appletree/plot.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/appletree/plot.py b/appletree/plot.py index 6cee087b..33caa49d 100644 --- a/appletree/plot.py +++ b/appletree/plot.py @@ -29,7 +29,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): self.chain = backend.get_chain(discard=discard, thin=thin) self.posterior = backend.get_log_prob(discard=discard, thin=thin) self.prior = backend.get_blobs(discard=discard, thin=thin) - # We drop iterations with inf posterior + # We drop iterations with inf and nan posterior mask = ~np.isinf(self.posterior) & ~np.isnan(self.posterior) mask = np.all(mask, axis=1) self.chain = self.chain[mask] @@ -37,9 +37,11 @@ def __init__(self, backend_file_name, discard=0, thin=1): 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_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 posterior + # 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] @@ -54,7 +56,9 @@ def __init__(self, backend_file_name, discard=0, thin=1): self.n_iter, self.n_walker, self.n_param = self.chain.shape - def make_all_plots(self, save=False, save_path=".", fmt=["png", "pdf"], **save_kwargs): + def make_all_plots( + self, save=False, save_path=".", fmt=["png", "pdf"], **save_kwargs + ): """Make all plots and save them if save is True. The plot styles are default. save_kwargs will be passed to fig.savefig(). @@ -173,13 +177,17 @@ def plot_marginal_posterior(self, fig=None, **hist_kwargs): axes = [] for i in range(self.n_param): ax = fig.add_subplot(n_rows, n_cols, i + 1) - ax.hist(self.flat_chain[:, i], density=True, label="Posterior", **hist_kwargs) + ax.hist( + self.flat_chain[:, i], density=True, label="Posterior", **hist_kwargs + ) prior = self.param_prior[self.param_names[i]] prior_type = prior["prior_type"] args = prior["prior_args"] if prior_type != "free": x = np.linspace(*ax.get_xlim(), 100) - ax.plot(x, pdf[prior_type](x, **args), color="grey", ls="--", label="Prior") + ax.plot( + x, pdf[prior_type](x, **args), color="grey", ls="--", label="Prior" + ) ax.set_xlabel(self.param_names[i]) ax.set_ylabel("PDF") ax.set_ylim(0, None) @@ -211,7 +219,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"])) From 5e17a50ef73f113d11a0591ea33e39867b71bda4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 27 May 2024 15:40:53 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- appletree/plot.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/appletree/plot.py b/appletree/plot.py index 33caa49d..2b8421ec 100644 --- a/appletree/plot.py +++ b/appletree/plot.py @@ -37,9 +37,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): 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_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) @@ -56,9 +54,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): self.n_iter, self.n_walker, self.n_param = self.chain.shape - def make_all_plots( - self, save=False, save_path=".", fmt=["png", "pdf"], **save_kwargs - ): + def make_all_plots(self, save=False, save_path=".", fmt=["png", "pdf"], **save_kwargs): """Make all plots and save them if save is True. The plot styles are default. save_kwargs will be passed to fig.savefig(). @@ -177,17 +173,13 @@ def plot_marginal_posterior(self, fig=None, **hist_kwargs): axes = [] for i in range(self.n_param): ax = fig.add_subplot(n_rows, n_cols, i + 1) - ax.hist( - self.flat_chain[:, i], density=True, label="Posterior", **hist_kwargs - ) + ax.hist(self.flat_chain[:, i], density=True, label="Posterior", **hist_kwargs) prior = self.param_prior[self.param_names[i]] prior_type = prior["prior_type"] args = prior["prior_args"] if prior_type != "free": x = np.linspace(*ax.get_xlim(), 100) - ax.plot( - x, pdf[prior_type](x, **args), color="grey", ls="--", label="Prior" - ) + ax.plot(x, pdf[prior_type](x, **args), color="grey", ls="--", label="Prior") ax.set_xlabel(self.param_names[i]) ax.set_ylabel("PDF") ax.set_ylim(0, None) From 957c81dacfb1073691f4b3a068e248f65cb6e2d8 Mon Sep 17 00:00:00 2001 From: Zihao Xu Date: Mon, 27 May 2024 11:11:53 -0500 Subject: [PATCH 6/7] change to isfinite --- appletree/plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appletree/plot.py b/appletree/plot.py index 2b8421ec..2f5a2897 100644 --- a/appletree/plot.py +++ b/appletree/plot.py @@ -30,7 +30,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): self.posterior = backend.get_log_prob(discard=discard, thin=thin) 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) + mask = ~np.isfinite(self.posterior) mask = np.all(mask, axis=1) self.chain = self.chain[mask] self.posterior = self.posterior[mask] @@ -40,7 +40,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): 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) + mask = ~np.isfinite(self.flat_posterior) self.flat_chain = self.flat_chain[mask] self.flat_posterior = self.flat_posterior[mask] self.flat_prior = self.flat_prior[mask] From 0ead13ac5e1974f06838f496f690852eeca38991 Mon Sep 17 00:00:00 2001 From: Zihao Xu Date: Mon, 27 May 2024 14:17:33 -0500 Subject: [PATCH 7/7] fix a bug --- appletree/plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appletree/plot.py b/appletree/plot.py index 2f5a2897..b59bfd9d 100644 --- a/appletree/plot.py +++ b/appletree/plot.py @@ -30,7 +30,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): self.posterior = backend.get_log_prob(discard=discard, thin=thin) self.prior = backend.get_blobs(discard=discard, thin=thin) # We drop iterations with inf and nan posterior - mask = ~np.isfinite(self.posterior) + mask = np.isfinite(self.posterior) mask = np.all(mask, axis=1) self.chain = self.chain[mask] self.posterior = self.posterior[mask] @@ -40,7 +40,7 @@ def __init__(self, backend_file_name, discard=0, thin=1): 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.isfinite(self.flat_posterior) + mask = np.isfinite(self.flat_posterior) self.flat_chain = self.flat_chain[mask] self.flat_posterior = self.flat_posterior[mask] self.flat_prior = self.flat_prior[mask]