def binned_posterior_probability(bp_rp, apparent_mag, absolute_mag, ratios, **kwargs): fig = kwargs.pop("fig", None) if fig is None: fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 8))) else: ax = fig.axes[0] latex_labels = kwargs.pop("latex_labels", dict()) band = kwargs.pop("band", None) kwds = dict(function="mean", ax=ax, full_output=True) kwds.update(kwargs) _, im = mpl_utils.plot_binned_statistic(bp_rp, absolute_mag, ratios, **kwds) ax.set_aspect(np.ptp(ax.get_xlim()) / np.ptp(ax.get_ylim())) ax.xaxis.set_major_locator(MaxNLocator(5)) ax.yaxis.set_major_locator(MaxNLocator(5)) ax.set_xlabel(r"{bp - rp}") # TODO: actually use the kwd["band"] entry. ax.set_ylabel(r"{absolute G magnitude}") ax.set_facecolor("#eeeeee") fig.tight_layout() return fig
def log_density_gp_expectation_value(x, y, **kwargs): fig, ax = plt.subplots() _, im = mpl_utils.plot_binned_statistic(x, y, y, function="count", ax=ax, full_output=True, **kwargs) return fig
def density_rv_excess_vs_absolute_magnitude(K_est, absolute_mag, **kwargs): fig, ax = plt.subplots() x, y = (K_est, absolute_mag) bins = (np.logspace(0, 3.1, 201), np.linspace(-10, 10, 201)) _, im = mpl_utils.plot_binned_statistic( x, y, y, function="count", ax=ax, full_output=True, interpolation="none", bins=bins, norm=LogNorm(), cmap="Greys", ) ax.set_ylim(ax.get_ylim()[::-1]) # TODO: create a mirrored axis or something? # TODO: this is all f****d up. # TODO: basically we want to have a log-x scale for the bins (which works) # but then we can't tell the axis that the scaling is log otherwise it blows up # TODO: alter Equation 3 from arxiv:1711.00660 and show in figure. ticks = np.unique(np.log10(bins[0]).astype(int)) ax.set_xticks(10**ticks) ax.set_xticklabels([r"$10^{{{0}}}$".format(_) for _ in ticks]) # TODO, this is all f****d up print("this is not for publication") return fig
def binned_bayes_factor(x, log_bf, **kwargs): fig = kwargs.pop("fig", None) if fig is None: fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 8))) else: ax = fig.axes[0] latex_labels = kwargs.pop("latex_labels", dict()) kwds = dict(function="mean", ax=ax, full_output=True) kwds.update(kwargs) _, im = mpl_utils.plot_binned_statistic(x, log_bf, log_bf, **kwds) ax.set_aspect(np.ptp(ax.get_xlim()) / np.ptp(ax.get_ylim())) ax.xaxis.set_major_locator(MaxNLocator(5)) ax.yaxis.set_major_locator(MaxNLocator(5)) ax.set_xlabel(r"{x}") ax.set_ylabel(r"{BF}") ax.set_facecolor("#eeeeee") fig.tight_layout() return fig
kwds = dict(bins=100, function="mean", interpolation="none", full_output=True) for parameter_name in ("theta", "mu_single", "sigma_single", "sigma_multiple"): p, p_var = results[ f"models/{model_name}/gp_predictions/{parameter_name}"][()].T fig, axes = plt.subplots(1, 2, figsize=(8, 4)) _, im = mpl.plot_binned_statistic(bp_rp, absolute_g_mag, p, ax=axes[0], **kwds) mpl.plot_binned_statistic(bp_rp, phot_g_mean_mag, p, ax=axes[1], **kwds) axes[0].set_ylabel('absolute g mag') axes[1].set_ylabel('phot g mean mag') for ax in axes: ax.set_xlabel('bp - rp') cbar = plt.colorbar(im)
def binned_gp_expectation_values(bp_rp, apparent_mag, absolute_mag, expectation_values, num_bins=100, function="mean", band="g", colorbar_labels=None, **kwargs): if not isinstance(expectation_values, tuple): raise TypeError("expectation_values must be a tuple") K = len(expectation_values) # the number of GP expectation values to show L = 2 # (apparent mag, absolute mag) latex_labels = kwargs.pop("latex_labels", dict()) if latex_labels is None: latex_labels = dict() get_label = lambda _, __=None: latex_labels.get(_, __ or _) figsize = kwargs.pop("figsize", (7.4, 7.6)) if figsize is None: figsize = (L * 3, K * 3) fig = plt.figure(figsize=figsize) grid = AxesGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.75, label_mode="all", share_all=False, cbar_location="top", cbar_mode="edge", cbar_size="5%", cbar_pad=0.05) for i in range(2): ax_abs, ax_app = axes_row = (grid[i], grid[i + 2]) for ax in axes_row: ax.set_facecolor("#eeeeee") _, im = mpl_utils.plot_binned_statistic(bp_rp, apparent_mag, expectation_values[i], function=function, ax=ax_app, full_output=True, **kwargs) _, im = mpl_utils.plot_binned_statistic(bp_rp, absolute_mag, expectation_values[i], function=function, ax=ax_abs, full_output=True, **kwargs) for ax in axes_row: ax.set_aspect(np.ptp(ax.get_xlim()) / np.ptp(ax.get_ylim())) ax.xaxis.set_major_locator(MaxNLocator(3)) ax.yaxis.set_major_locator(MaxNLocator(6)) ax.set_xlabel(get_label("bp_rp", "bp - rp")) ax_abs.set_ylabel( get_label(f"absolute_{band}_mag", f"absolute {band} mag")) ax_app.set_ylabel( get_label(f"apparent_{band}_mag", f"apparent {band} mag")) caxes = (grid.cbar_axes[0], grid.cbar_axes[1]) cax = caxes[i] cax.colorbar(im) cax.toggle_label(True) cax.axis[cax.orientation].set_label( get_label(colorbar_labels[i], colorbar_labels[i])) for cax in grid.cbar_axes: if cax not in caxes: cax.set_visible(False) fig.tight_layout() fig.subplots_adjust(top=0.925) return fig
minimum_points=model_config["kdtree_minimum_points"], maximum_points=model_config["kdtree_maximum_points"]) # Optimize the non-parametric model for those sources. npm_results = np.zeros((M, 5)) done = np.zeros(M, dtype=bool) if plot_mixture_model_figures: kwds = dict(function="count", bins=250, cmap="Greys", norm=LogNorm()) mpl.plot_binned_statistic(X[:, 0], X[:, 1], X[:, 1], ax=axes[0], **kwds) mpl.plot_binned_statistic(X[:, 0], X[:, 2], X[:, 2], ax=axes[1], **kwds) for ax in axes[:2]: ax.set_aspect(np.ptp(ax.get_xlim()) / np.ptp(ax.get_ylim())) mu_multiple_scalar = model_config["mu_multiple_scalar"] # TODO: put scalar to the config file. def optimize_mixture_model(index, inits=None, debug=False):
H_inv = (1.0/H).flatten() keep = np.where(np.isfinite(H_inv))[0] p = H_inv[keep] / np.sum(H_inv[keep]) indices = np.random.choice(keep, M, replace=False, p=p.flatten()) chosen_bin_numbers = np.array(np.unravel_index(indices, H.shape)).T npm_indices = np.zeros(M, dtype=int) for i, bin_number in enumerate(tqdm.tqdm(chosen_bin_numbers)): in_bin = np.all(bin_numbers.T == np.atleast_2d(bin_number), axis=1) npm_indices[i] = np.random.choice(np.where(in_bin)[0], 1) fig1 = mpl.plot_binned_statistic(X[npm_indices, 0], X[npm_indices, 1], X[npm_indices, 1], function="count") fig2 = mpl.plot_binned_statistic(X[npm_indices, 0], X[npm_indices, 1], X[npm_indices, 1], function="count") else: raise NotImplementedError("unrecognised coreset method") for i, label_name in enumerate(all_label_names[1:]): v = X[npm_indices, i] print(f"{label_name} min: {np.min(v)} max: {np.max(v)}") logger.info(f"Building K-D tree with N = {X.shape[0]}, D = {X.shape[1]}...") kdt, scales, offsets = npm.build_kdtree(X, relative_scales=model_config["kdtree_relative_scales"])
bins=num_bins) bins = (xedges, yedges) plot_binned_statistic_kwds = dict(function="median", vmin=0, vmax=1, cmap=DEFAULT_SEQUENTIAL_CMAP, mask=None, subsample=subsample, min_entries_per_bin=3, bins=bins) fig, axes = plt.subplots(1, K, figsize=(3 * K, 3)) for i, (ax, ylabel, zlabel, title) \ in enumerate(zip(axes, ylabels, zlabels, titles)): plot_binned_statistic( velociraptor[xlabel], velociraptor[ylabel], velociraptor[zlabel], ax=ax, ylabel=latex_labels.get(ylabel, ylabel), **plot_binned_statistic_kwds) ax.set_xlabel(latex_labels.get(xlabel, xlabel)) ax.xaxis.set_major_locator(MaxNLocator(5)) ax.yaxis.set_major_locator(MaxNLocator(5)) ax.set_xlim(xlims) ax.set_ylim(ylims) ax.set_title(title) cbar = plt.colorbar(ax.images[0], ax=list(axes))#=axes[-1])