def plot_observations(ax, key, range, bins=10, marker="o", color="k", markersize=3, linewidth=0): """ TODO: Move to utils. """ df = load_sample(select=True) values = df[key].values h, e = np.histogram(values, range=range, bins=bins) c = 0.5 * (e[1:] + e[:-1]) err = np.sqrt(h) norm = h.sum() * (e[1] - e[0]) xlim = ax.get_xlim() ylim = ax.get_ylim() ax.errorbar(c, h / norm, yerr=err / norm, elinewidth=1, markersize=markersize, color=color, linewidth=linewidth, marker=marker, label="observed", zorder=10) ax.set_xlim(xlim) ax.set_ylim(ylim) return ax
def make_faux_observations(self, df=None, dfo=None, makeplots=False, **kwargs): """ Get sample of mock observations from the model sample Args: df (pd.DataFrame): Model samples from the best-fitting model. Returns: pd.DataFrame: Faux observations extracted from model samples. """ if df is None: df = self.load_best_sample(**kwargs) if dfo is None: dfo = load_sample(select=True) # Choose a random sample of the same size as observations indices = np.random.randint(0, df.shape[0], dfo.shape[0]) # Map the model keys into observation keys dff = pd.DataFrame() for key, obskey in self.config["obskeys"].items(): dff[obskey] = df[key].values[indices] if makeplots: self.summary_plot(dfo=dff) return dff
def plot_observations(grid, ax_dict, color="k"): """ """ dfo = load_sample(select=True) # Find the good-fitting model with the best ks-statistics dfm = grid.load_confident_metrics() metrics = dfm.iloc[np.argmax(dfm["kstest_min"].values)] for key, ax in ax_dict.items(): if key in OBSKEYS: ax = ax_dict[key] values = dfo[OBSKEYS[key]].values rng = RANGES[key] hist_norm, edges = np.histogram(values, range=rng, bins=BINS_OBS, density=True) centres = 0.5 * (edges[1:] + edges[:-1]) hist, _ = np.histogram(values, range=rng, bins=BINS_OBS, density=False) yerr = np.sqrt(hist) * hist_norm.max() / hist.max() ax.errorbar(centres, hist_norm, yerr=yerr, color=color, linewidth=0, linestyle=None, elinewidth=1.5, marker="o", markersize=3, zorder=10) pval = metrics[KSTEST_KEYS[key]] ax.text(0.22, 0.75, r"$p_{KS}=$" + rf"{pval:.2f}", transform=ax.transAxes, fontsize=FONTSIZE - 1, color="k")
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._dfobs = load_sample(config=self.config, logger=self.logger, select=True)
return result if __name__ == "__main__": model_name = "blue_final" metric = "poisson_likelihood_2d" confs = None config = get_config() image_dir = os.path.join(config["directories"]["data"], "images") image_filename = os.path.join(image_dir, f"model_fit_{model_name}.png") grid = ParameterGrid(model_name) dfm = grid.load_best_sample(metric=metric) dfo = load_sample() keys = "uae_obs_jig", "rec_obs_jig" range = {k: r for k, r in zip(keys, RANGE)} dfs = grid.load_confident_samples(q=Q) dfbest = grid.load_best_sample() confs = get_confidence_intervals(dfs, dfbest, keys, range=range, bins=BINS1D) fig = plt.figure(figsize=FIGSIZE) spec = gridspec.GridSpec(ncols=10, nrows=2, figure=fig, hspace=0.35, wspace=1.6) ax1 = fig.add_subplot(spec[:, :4]) ax2 = fig.add_subplot(spec[0, 4:]) ax3 = fig.add_subplot(spec[1, 4:]) # 2D model histogram
import os from udgsizes.core import get_config, get_logger from udgsizes.obs.sample import load_sample, load_gama_specobj from udgsizes.utils import xmatch if __name__ == "__main__": logger = get_logger() radius = 3. / 3600 df = load_sample(select=False) dfg = load_gama_specobj() dfm = xmatch.match_dataframe(df, dfg, radius=radius) logger.info(f"Matched {dfm.shape[0]} sources.") datadir = get_config()["directories"]["data"] dfm.to_csv(os.path.join(datadir, "input", "lsbgs_gama_xmatch.csv"))
def fit_summary_plot(df, dfo=None, show=True, bins=15, select=True, **kwargs): if dfo is None: dfo = load_sample(select=select, **kwargs) if select: df = df[df["selected_jig"] == 1].reset_index(drop=True) fig = plt.figure(figsize=(6, 6)) ax0 = plt.subplot(3, 1, 1) histkwargs = dict(density=True, histtype="step") rng = (min(dfo['mueff_av'].min(), df['uae_obs_jig'].min()), max(dfo['mueff_av'].max(), df['uae_obs_jig'].max())) ax0.hist(dfo['mueff_av'].values, color="k", range=rng, bins=bins, label="obs", **histkwargs) ax0.hist(df['uae_obs_jig'].values, color="b", range=rng, bins=bins, label="model", **histkwargs) ks = kstest(dfo['mueff_av'].values, df['uae_obs_jig'].values)[1] ax0.legend(loc="best") ax0.set_xlabel(f"uae (KS pval={ks:.2f})") ax1 = plt.subplot(3, 1, 2) rng = (min(dfo['rec_arcsec'].min(), df['rec_obs_jig'].min()), max(dfo['rec_arcsec'].max(), df['rec_obs_jig'].max())) ax1.hist(dfo['rec_arcsec'].values, color="k", range=rng, bins=bins, **histkwargs, label="obs") ax1.hist(df['rec_obs_jig'].values, color="b", range=rng, bins=bins, **histkwargs, label="model") ks = kstest(dfo['rec_arcsec'].values, df['rec_obs_jig'].values)[1] ax1.legend(loc="best") ax1.set_xlabel(f"rec (KS pval={ks:.2f})") ax1 = plt.subplot(3, 1, 3) rng = (min(dfo['g_r'].min(), df['colour_obs'].min()), max(dfo['g_r'].max(), df['colour_obs'].max())) ax1.hist(dfo['g_r'].values, color="k", range=rng, bins=bins, **histkwargs, label="obs") ax1.hist(df['colour_obs'].values, color="b", range=rng, bins=bins, **histkwargs, label="model") ks = kstest(dfo['g_r'].values, df['colour_obs'].values)[1] ax1.legend(loc="best") ax1.set_xlabel(f"g-r (KS pval={ks:.2f})") plt.tight_layout() if show: plt.show(block=False) return fig
OBSKEYS = { "uae_obs_jig": "mueff_av", "rec_obs_jig": "rec_arcsec", "colour_obs": "g_r" } # TODO: Add KS values to graphs # TODO: Make into grid method / plotting utils if __name__ == "__main__": grid = ParameterGrid(MODEL_NAME) df = grid.load_best_sample(apply_prior=True, select=True) df = df[df["selected_jig"].values == 1].reset_index(drop=True) dfo = load_sample(select=True) fig = plt.figure(figsize=(FIGHEIGHT * len(PAR_NAMES), FIGHEIGHT * 1.2)) for i, par_name in enumerate(PAR_NAMES): ax = plt.subplot(1, len(PAR_NAMES), i + 1) values = df[par_name].values ax.hist(values, color="k", alpha=0.4, **HISTKWARGS) if par_name in OBSKEYS: values_obs = dfo[OBSKEYS[par_name]].values rng = values.min(), values.max() ax.hist(values_obs, range=rng, color="b", alpha=0.4, **HISTKWARGS) if i == 0:
import matplotlib.pyplot as plt from udgsizes.obs.sample import load_sample from udgsizes.obs.index_colour import Classifier, get_classifier_filename if __name__ == "__main__": df = load_sample() indices = df["n_sersic"].values colours = df["g_r"].values c = Classifier() filename = get_classifier_filename() c.fit(indices, colours, filename=filename, makeplots=True) plt.show(block=False)