From cc74664181f49595fa8d503738fd13c9cf05450d Mon Sep 17 00:00:00 2001 From: martinkilbinger Date: Fri, 6 Jun 2025 08:44:04 +0200 Subject: [PATCH] computing and plotting binned quantities --- .gitignore | 1 + notebooks/leakage_minimal.py | 37 +++---------- notebooks/plot_binned_quantities.py | 85 +++++++++++++++++++++++------ sp_validation/calibration.py | 4 +- sp_validation/plots.py | 57 +++++++++++++++++-- 5 files changed, 132 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index bd605b2..d313ac4 100644 --- a/.gitignore +++ b/.gitignore @@ -170,3 +170,4 @@ notebooks/demo_check_footprint.ipynb notebooks/demo_calibrate_minimal_cat.ipynb notebooks/leakage_minimal.ipynb notebooks/demo_add_bands.ipynb +notebooks/demo_add_bands_to_empty.ipynb diff --git a/notebooks/leakage_minimal.py b/notebooks/leakage_minimal.py index 075ff34..8cf487a 100644 --- a/notebooks/leakage_minimal.py +++ b/notebooks/leakage_minimal.py @@ -1,5 +1,5 @@ # %% -# Plot binned quantites, see leakage_minima.py +# Compute binned quantities, see plot_binned_quantities.py # %% from IPython import get_ipython @@ -12,6 +12,7 @@ ipython.run_line_magic("autoreload", "2") # %% +import os import numpy as np import pandas as pd @@ -26,10 +27,8 @@ from sp_validation import io from sp_validation import plots as sp_plots -if ipython is not None: - ipython.run_line_magic('matplotlib', 'inline') - - +# %% +print("pwd:", os.getcwd()) # %% # Initialize calibration class instance @@ -118,10 +117,6 @@ # Get metacal outputs; here mask is needed g_corr_mc, g_uncorr, w, mask_metacal, c, c_err = calibration.get_calibrated_m_c(gal_metacal) -# %% -# Apply masks to data -#datm = dat[mask_combined._mask][mask_metacal] - # %% cat_gal = {} @@ -130,28 +125,14 @@ df = calibration.build_df(cat_gal) -# % - -num_bins_x = 12 -num_bins_y = 12 +# %% +num_bins_x = 15 +num_bins_y = 15 -quantities, bin_edges = calibration.get_quantities_binned(cat_gal, num_bins_x, num_bins_y) +s +quantities, bin_edges = calibration.get_quantities_binned(cat_gal, num_bins_x, num_bins_y, which=which) # Save binned matrices to files for key in quantities: io.write_binned_quantity(quantities[key], key, bin_edges) - -# %% -vmin = {"diag": -0.2, "offdiag": -0.2} -vmax = {"diag": 1.2, "offdiag": 0.2} - -sp_plots.plot_binned(quantities, "response", bin_edges["snr"], bin_edges["size_ratio"], "R", vmin=vmin, vmax=vmax, xlabel="SNR", ylabel=r"$r / r_{\rm psf}$") - # %% -sp_plots.plot_binned(quantities, "number", bin_edges["snr"], bin_edges["size_ratio"], "R", vmin=1, vmax=np.nanmax(quantities["number"]), xlabel="SNR", ylabel=r"$r / r_{\rm psf}$") - -# %% -vmin = {"diag": -0.2, "offdiag": -0.2} -vmax = {"diag": 0.2, "offdiag": 0.2} - -sp_plots.plot_binned(quantities, "leakage", bin_edges["snr"], bin_edges["size_ratio"], r"\alpha", vmin=vmin, vmax=vmax, xlabel="SNR", ylabel=r"$r / r_{\rm psf}$") diff --git a/notebooks/plot_binned_quantities.py b/notebooks/plot_binned_quantities.py index 5d22d63..69ada73 100644 --- a/notebooks/plot_binned_quantities.py +++ b/notebooks/plot_binned_quantities.py @@ -1,5 +1,5 @@ # %% -# Plot binned quantites, which are the outputs of leakage_minima.py +# Plot binned quantities, which are the outputs of leakage_minimal.py # %% from IPython import get_ipython @@ -11,7 +11,7 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") -# %% +# %% import matplotlib.pyplot as plt import numpy as np import numpy as np @@ -28,19 +28,21 @@ from sp_validation import io from sp_validation import plots - -# %% -# enable inline plotting for interactive sessions -# (must be done *after* importing package that sets agg backend) -if ipython is not None: - ipython.run_line_magic("matplotlib", "inline") + +## %% +## enable inline plotting for interactive sessions +## (must be done *after* importing package that sets agg backend) +#if ipython is not None: +# print("matplotlib inline") +# ipython.run_line_magic("matplotlib", "inline") # %% bin_edges = {} quantities = {} # %% -keys = ["number", "response", "leakage"] +keys = ["number", "response", "leakage", "w_iv", "mag", "NGMIX_Tpsf_NOSHEAR", "N_EPOCH", "e1_PSF", "e2_PSF", "fwhm_PSF"] + for key in keys: fname = f"{key}_binned.npz" @@ -49,21 +51,68 @@ if xy != "quantity": bin_edges[xy] = result[xy] quantities[key] = result["quantity"] + +xlabel = "SNR" +ylabel = r"$r / r_{\rm psf}$" -# %% -vmin = {"diag": -0.2, "offdiag": -0.03} -vmax = {"diag": 1.2, "offdiag": 0.03} +lines = { + "x": [10, 500], + "y": [0.5, 3], +} -plots.plot_binned(quantities, "response", bin_edges["snr"], bin_edges["size_ratio"], "R", vmin=vmin, vmax=vmax, xlabel="SNR", ylabel=r"$r / r_{\rm psf}$") -# %% -plots.plot_binned(quantities, "number", bin_edges["snr"], bin_edges["size_ratio"], "R", vmin=1, vmax=np.nanmax(quantities["number"]), xlabel="SNR", ylabel=r"$r / r_{\rm psf}$") # %% -vmin = {"diag": -0.2, "offdiag": -0.2} -vmax = {"diag": 0.1, "offdiag": 0.1} +vmin = {"diag": -0.2, "offdiag": -0.1} +vmax = {"diag": 1.2, "offdiag": 0.1} + +plots.plot_binned( + quantities, + "response", + bin_edges["snr"], + bin_edges["size_ratio"], + title="R", + vmin=vmin, + vmax=vmax, + xlabel=xlabel, + ylabel=ylabel, + lines=lines, + close_fig=False, +) -plots.plot_binned(quantities, "leakage", bin_edges["snr"], bin_edges["size_ratio"], r"\alpha", vmin=vmin, vmax=vmax, xlabel="SNR", ylabel=r"$r / r_{\rm psf}$") +# %% +plots.plot_binned( + quantities, + "number", + bin_edges["snr"], + bin_edges["size_ratio"], + title="n", + vmin=1, + vmax=np.nanmax(quantities["number"]), + xlabel=xlabel, + ylabel=ylabel, + lines=lines, + close_fig=False, +) +# %% +vmin = {"diag": -0.2, "offdiag": -0.2} +vmax = {"diag": 0.2, "offdiag": 0.2} + +plots.plot_binned( + quantities, + "leakage", + bin_edges["snr"], + bin_edges["size_ratio"], + title=r"\alpha", + vmin=vmin, + vmax=vmax, + xlabel=xlabel, + ylabel=ylabel, + lines=lines, + close_fig=False, +) # %% +bin_edges +# %% diff --git a/sp_validation/calibration.py b/sp_validation/calibration.py index e450b36..0e6a08d 100644 --- a/sp_validation/calibration.py +++ b/sp_validation/calibration.py @@ -470,8 +470,8 @@ def get_quantities_binned(cat_gal, num_bins_x, num_bins_y=None, which=["response # Create logarithmic bins in size and SNR bin_edges = {} - bin_edges["snr"] = cut_to_bins(df_gal, "snr", num_bins_x, type="log", x_min=3, x_max=300) - bin_edges["size_ratio"] = cut_to_bins(df_gal, "size_ratio", num_bins_y, type="log", x_min=0.3, x_max=20) + bin_edges["snr"] = cut_to_bins(df_gal, "snr", num_bins_x, type="log", x_min=3, x_max=700) + bin_edges["size_ratio"] = cut_to_bins(df_gal, "size_ratio", num_bins_y, type="log", x_min=0.3, x_max=10) # Initialize output dict quantities = {} diff --git a/sp_validation/plots.py b/sp_validation/plots.py index 30ac89c..5404243 100644 --- a/sp_validation/plots.py +++ b/sp_validation/plots.py @@ -314,6 +314,21 @@ def plot_map_stacked(kappa, title, radius, output_path, vlim=None): return vlim +def plot_lines(lines): + + if lines is None: + return + + for key in lines: + for value in lines[key]: + if key == "x": + plt.axvline(x=value, color="red") + elif key == "y": + plt.axhline(y=value, color="red") + else: + raise ValueError(f"line of type {key} invalid") + + def plot_binned_one(ax, quantity, bin_edges_x, bin_edges_y, vmin=None, vmax=None, title=None, xlabel=None, ylabel=None): # Note: transpose R slice to match (y, x) shape required by pcolormesh @@ -329,7 +344,19 @@ def plot_binned_one(ax, quantity, bin_edges_x, bin_edges_y, vmin=None, vmax=None plt.colorbar(pcm, ax=ax) -def plot_binned(quantities, key, bin_edges_x, bin_edges_y, title_base, vmin=None, vmax=None, xlabel=None, ylabel=None): +def plot_binned( + quantities, + key, + bin_edges_x, + bin_edges_y, + title=None, + vmin=None, + vmax=None, + xlabel=None, + ylabel=None, + lines=None, + close_fig=True, +): len_shape = len(quantities[key].shape) @@ -339,7 +366,18 @@ def plot_binned(quantities, key, bin_edges_x, bin_edges_y, title_base, vmin=None if len_shape == 2: ax = plt.subplot2grid((1, 1), (0, 0)) - plot_binned_one(ax, quantities[key].T, bin_edges_x, bin_edges_y, vmin=vmin, vmax=vmax, title=title_base, xlabel=xlabel, ylabel=ylabel) + plot_binned_one( + ax, + quantities[key].T, + bin_edges_x, + bin_edges_y, + vmin=vmin, + vmax=vmax, + title=f"${title}$", + xlabel=xlabel, + ylabel=ylabel + ) + plot_lines(lines) elif len_shape == 4: for idx in (0, 1): @@ -357,8 +395,19 @@ def plot_binned(quantities, key, bin_edges_x, bin_edges_y, title_base, vmin=None my_vmin = None my_vmax = None - plot_binned_one(ax, quantities[key][:,:, idx, jdx].T, bin_edges_x, bin_edges_y, vmin=my_vmin, vmax=my_vmax, title=f"${title_base}_{{{idx+1}{jdx+1}}}$", xlabel=xlabel, ylabel=ylabel) + plot_binned_one( + ax, + quantities[key][:,:, idx, jdx].T, + bin_edges_x, + bin_edges_y, + vmin=my_vmin, + vmax=my_vmax, + title=f"${title}_{{{idx+1}{jdx+1}}}$", + xlabel=xlabel, + ylabel=ylabel + ) + plot_lines(lines) plt.tight_layout() - plots.savefig(f"{key}_binned.png") \ No newline at end of file + plots.savefig(f"{key}_binned.png", close_fig=close_fig) \ No newline at end of file