From a73eb8445d934fd39c9cd487d7378bd0b6c891c8 Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Mon, 16 Sep 2024 17:59:46 -0400 Subject: [PATCH] bug fix for scaling of gamma_pl pre-processing --- hierarc/Diagnostics/goodness_of_fit.py | 12 +++++++++--- hierarc/LensPosterior/imaging_constraints.py | 4 +++- hierarc/Likelihood/cosmo_likelihood.py | 2 +- hierarc/Likelihood/kin_scaling.py | 3 +-- test/test_Diagnostics/test_goodness_of_fit.py | 4 ++-- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/hierarc/Diagnostics/goodness_of_fit.py b/hierarc/Diagnostics/goodness_of_fit.py index 79c0b01..6be1af9 100644 --- a/hierarc/Diagnostics/goodness_of_fit.py +++ b/hierarc/Diagnostics/goodness_of_fit.py @@ -1,25 +1,31 @@ import matplotlib.pyplot as plt import numpy as np from hierarc.Likelihood.lens_sample_likelihood import LensSampleLikelihood +from hierarc.Likelihood.cosmo_likelihood import CosmoLikelihood class GoodnessOfFit(object): """Class to manage goodness of fit diagnostics.""" - def __init__(self, kwargs_likelihood_list): + def __init__(self, kwargs_likelihood_list, kwargs_model): """ :param kwargs_likelihood_list: list of likelihood kwargs of individual lenses consistent with the LensLikelihood module + :param kwargs_model: model settings for ParamManager() class + :type kwargs_model: dict """ self._kwargs_likelihood_list = kwargs_likelihood_list self._sample_likelihood = LensSampleLikelihood( - kwargs_likelihood_list, normalized=False + kwargs_likelihood_list, + normalized=False, + kwargs_global_model=kwargs_model, ) def reduced_chi2(self, cosmo, kwargs_lens, kwargs_kin): """Reduced chi^2 of fit.""" - logL = self._sample_likelihood.log_likelihood(cosmo, kwargs_lens, kwargs_kin) + logL = self._sample_likelihood.log_likelihood(cosmo=cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin, + verbose=False) num_data = self._sample_likelihood.num_data() return -logL * 2 / num_data diff --git a/hierarc/LensPosterior/imaging_constraints.py b/hierarc/LensPosterior/imaging_constraints.py index 68d96cc..83d5a5c 100644 --- a/hierarc/LensPosterior/imaging_constraints.py +++ b/hierarc/LensPosterior/imaging_constraints.py @@ -27,7 +27,9 @@ def draw_lens(self, gamma_pl=None, no_error=False): :return: theta_E, gamma, r_eff, delta_r_eff """ if no_error is True: - return self._theta_E, self._gamma, self._r_eff, 1 + if gamma_pl is None: + gamma_pl = self._gamma + return self._theta_E, gamma_pl, self._r_eff, 1 theta_E_draw = np.maximum( np.random.normal(loc=self._theta_E, scale=self._theta_E_error), 0 ) diff --git a/hierarc/Likelihood/cosmo_likelihood.py b/hierarc/Likelihood/cosmo_likelihood.py index 5435f80..623b609 100644 --- a/hierarc/Likelihood/cosmo_likelihood.py +++ b/hierarc/Likelihood/cosmo_likelihood.py @@ -47,7 +47,7 @@ def __init__( log likelihood value (e.g. prior) :param interpolate_cosmo: bool, if True, uses interpolated comoving distance in the calculation for speed-up :param num_redshift_interp: int, number of redshift interpolation steps - :param cosmo_fixed: astropy.cosmology instance to be used and held fixed throughout the sampling + :param cosmo_fixed: ~astropy.cosmology instance to be used and held fixed throughout the sampling :param normalized: bool, if True, returns the normalized likelihood, if False, separates the constant prefactor (in case of a Gaussian 1/(sigma sqrt(2 pi)) ) to compute the reduced chi2 statistics """ diff --git a/hierarc/Likelihood/kin_scaling.py b/hierarc/Likelihood/kin_scaling.py index 44b1022..c8bfd1c 100644 --- a/hierarc/Likelihood/kin_scaling.py +++ b/hierarc/Likelihood/kin_scaling.py @@ -84,7 +84,7 @@ def __init__(self, param_grid_axes, j_kin_scaling_grid): if self._dim_scaling == 1: self._f_ani = interp1d( - param_grid_axes[0], j_kin_scaling_grid, kind="linear" + param_grid_axes[0], j_kin_scaling_grid, kind="linear", fill_value="extrapolate" ) elif self._dim_scaling == 2: self._f_ani = interp2d( @@ -103,7 +103,6 @@ def j_scaling(self, param_array): :param param_array: sorted list of parameters for the interpolation function :return: scaling J(a_ani) for single slit """ - if self._evalute_scaling is not True or len(param_array) == 0: return 1 if self._dim_scaling == 1: diff --git a/test/test_Diagnostics/test_goodness_of_fit.py b/test/test_Diagnostics/test_goodness_of_fit.py index 714b009..174c0c7 100644 --- a/test/test_Diagnostics/test_goodness_of_fit.py +++ b/test/test_Diagnostics/test_goodness_of_fit.py @@ -100,7 +100,7 @@ def setup_method(self): self.kwargs_likelihood_list[i]["likelihood_type"] = likelihood_type self.goodnessofFit = GoodnessOfFit( - kwargs_likelihood_list=self.kwargs_likelihood_list + kwargs_likelihood_list=self.kwargs_likelihood_list, kwargs_model={} ) def test_plot_ddt_fit(self): @@ -153,7 +153,7 @@ def test_raise(self): } ] goodness_of_fit = GoodnessOfFit( - kwargs_likelihood_list=kwargs_likelihood_list + kwargs_likelihood_list=kwargs_likelihood_list, kwargs_model={} ) f, ax = plt.subplots(1, 1, figsize=(4, 4)) kwargs_lens = {"lambda_mst": 1}