Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/kin_scaling_restructuring' into …
Browse files Browse the repository at this point in the history
…kin_scaling_restructuring

# Conflicts:
#	test/test_Sampling/test_Distributions/test_lens_distribution.py
  • Loading branch information
sibirrer committed Jun 27, 2024
2 parents c0ed4a6 + 4bb09ba commit 3688ea9
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 227 deletions.
10 changes: 7 additions & 3 deletions hierarc/LensPosterior/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def __init__(
ImageModelPosterior.__init__(
self, theta_E, theta_E_error, gamma, gamma_error, r_eff, r_eff_error
)
KinScalingConfig.__init__(self, anisotropy_model, r_eff,
gamma_in_scaling=gamma_in_scaling,
log_m2l_scaling=log_m2l_scaling)
KinScalingConfig.__init__(
self,
anisotropy_model,
r_eff,
gamma_in_scaling=gamma_in_scaling,
log_m2l_scaling=log_m2l_scaling,
)
8 changes: 6 additions & 2 deletions hierarc/LensPosterior/kin_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ def hierarchy_configuration(self, num_sample_model=20):
"kin_scaling_param_list": self.param_name_list,
"j_kin_scaling_param_axes": self.kin_scaling_param_array,
"j_kin_scaling_grid_list": ani_scaling_array_list,

}
return kwargs_likelihood

Expand Down Expand Up @@ -253,7 +252,12 @@ def _anisotropy_scaling_relative(self, j_ani_0):

if self._anisotropy_model == "GOM":
ani_scaling_array_list = [
np.zeros((len(self.kin_scaling_param_array[0]), len(self.kin_scaling_param_array[1])))
np.zeros(
(
len(self.kin_scaling_param_array[0]),
len(self.kin_scaling_param_array[1]),
)
)
for _ in range(num_data)
]
for i, a_ani in enumerate(self.kin_scaling_param_array[0]):
Expand Down
13 changes: 5 additions & 8 deletions hierarc/LensPosterior/kin_constraints_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,18 @@ def hierarchy_configuration(self, num_sample_model=20):
"j_model": j_model_list,
"error_cov_measurement": error_cov_measurement,
"error_cov_j_sqrt": error_cov_j_sqrt,

#"ani_param_array": self.kin_scaling_param_array,
#"gamma_in_array": self.gamma_in_array,
#"log_m2l_array": self.log_m2l_array,
#"param_scaling_grid_list": ani_scaling_grid_list,

# "ani_param_array": self.kin_scaling_param_array,
# "gamma_in_array": self.gamma_in_array,
# "log_m2l_array": self.log_m2l_array,
# "param_scaling_grid_list": ani_scaling_grid_list,
"gamma_in_prior_mean": self._gamma_in_prior_mean,
"gamma_in_prior_std": self._gamma_in_prior_std,

"kin_scaling_param_list": self.param_name_list,
"j_kin_scaling_param_axes": self.kin_scaling_param_array,
"j_kin_scaling_grid_list": ani_scaling_grid_list,
}

#if not self._is_m2l_population_level:
# if not self._is_m2l_population_level:
# kwargs_likelihood["log_m2l_array"] = None
return kwargs_likelihood

Expand Down
13 changes: 8 additions & 5 deletions hierarc/LensPosterior/kin_scaling_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class KinScalingConfig(object):
"""Class to manage the anisotropy model and parameters for the Posterior
processing."""

def __init__(self, anisotropy_model, r_eff, gamma_in_scaling=None, log_m2l_scaling=None):
def __init__(
self, anisotropy_model, r_eff, gamma_in_scaling=None, log_m2l_scaling=None
):
"""
:param anisotropy_model: type of stellar anisotropy model. Supported are 'OM' and 'GOM' or 'const', see details in lenstronomy.Galkin module
Expand All @@ -28,10 +30,12 @@ def __init__(self, anisotropy_model, r_eff, gamma_in_scaling=None, log_m2l_scali
]
self._param_name_list = ["a_ani", "beta_inf"]
elif self._anisotropy_model == "const":
self._ani_param_array = [np.linspace(-0.49, 1, 7)] # used for constant anisotropy description
self._ani_param_array = [
np.linspace(-0.49, 1, 7)
] # used for constant anisotropy description
self._param_name_list = ["a_ani"]
elif self._anisotropy_model == "NONE":
self._param_name_list =[]
self._param_name_list = []
else:
raise ValueError(
"anisotropy model %s not supported." % self._anisotropy_model
Expand Down Expand Up @@ -77,8 +81,7 @@ def kin_scaling_param_array(self):

@property
def param_name_list(self):
"""
list of parameters in same order as interpolated
"""List of parameters in same order as interpolated.
:return:
"""
Expand Down
6 changes: 1 addition & 5 deletions hierarc/Likelihood/cosmo_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ def __init__(
normalized=normalized,
kwargs_global_model=kwargs_model,
)
self.param = ParamManager(
cosmology,
**kwargs_model,
**kwargs_bounds
)
self.param = ParamManager(cosmology, **kwargs_model, **kwargs_bounds)
self._lower_limit, self._upper_limit = self.param.param_bounds
self._prior_add = False
if custom_prior is not None:
Expand Down
84 changes: 50 additions & 34 deletions hierarc/Likelihood/hierarchy_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from hierarc.Likelihood.LensLikelihood.base_lens_likelihood import LensLikelihoodBase
from hierarc.Likelihood.kin_scaling import KinScaling
from hierarc.Sampling.Distributions.los_distributions import LOSDistribution
from hierarc.Sampling.Distributions.anisotropy_distributions import AnisotropyDistribution
from hierarc.Sampling.Distributions.anisotropy_distributions import (
AnisotropyDistribution,
)
from hierarc.Sampling.Distributions.lens_distribution import LensDistribution
import numpy as np
import copy
Expand Down Expand Up @@ -94,11 +96,12 @@ def __init__(
"""
TransformedCosmography.__init__(self, z_lens=z_lens, z_source=z_source)

KinScaling.__init__(self,
j_kin_scaling_param_axes=j_kin_scaling_param_axes,
j_kin_scaling_grid_list=j_kin_scaling_grid_list,
j_kin_scaling_param_name_list=kin_scaling_param_list
)
KinScaling.__init__(
self,
j_kin_scaling_param_axes=j_kin_scaling_param_axes,
j_kin_scaling_grid_list=j_kin_scaling_grid_list,
j_kin_scaling_param_name_list=kin_scaling_param_list,
)

LensLikelihoodBase.__init__(
self,
Expand All @@ -119,28 +122,32 @@ def __init__(
los_distributions=los_distributions,
)
kwargs_min, kwargs_max = self.param_bounds_interpol()
self._lens_distribution = LensDistribution(lambda_mst_sampling=False,
lambda_mst_distribution=lambda_mst_distribution,
gamma_in_sampling=gamma_in_sampling,
gamma_in_distribution=gamma_in_distribution,
log_m2l_sampling=log_m2l_sampling,
log_m2l_distribution=log_m2l_distribution,
alpha_lambda_sampling=alpha_lambda_sampling,
beta_lambda_sampling=beta_lambda_sampling,
alpha_gamma_in_sampling=alpha_gamma_in_sampling,
alpha_log_m2l_sampling=alpha_log_m2l_sampling,
log_scatter=log_scatter,
mst_ifu=mst_ifu,
lambda_scaling_property=lambda_scaling_property,
lambda_scaling_property_beta=lambda_scaling_property_beta,
kwargs_min=kwargs_min,
kwargs_max=kwargs_max,)

self._aniso_distribution = AnisotropyDistribution(anisotropy_model=anisotropy_model,
anisotropy_sampling=anisotropy_sampling,
distribution_function=anisotroy_distribution_function,
kwargs_anisotropy_min=kwargs_min,
kwargs_anisotropy_max=kwargs_max)
self._lens_distribution = LensDistribution(
lambda_mst_sampling=False,
lambda_mst_distribution=lambda_mst_distribution,
gamma_in_sampling=gamma_in_sampling,
gamma_in_distribution=gamma_in_distribution,
log_m2l_sampling=log_m2l_sampling,
log_m2l_distribution=log_m2l_distribution,
alpha_lambda_sampling=alpha_lambda_sampling,
beta_lambda_sampling=beta_lambda_sampling,
alpha_gamma_in_sampling=alpha_gamma_in_sampling,
alpha_log_m2l_sampling=alpha_log_m2l_sampling,
log_scatter=log_scatter,
mst_ifu=mst_ifu,
lambda_scaling_property=lambda_scaling_property,
lambda_scaling_property_beta=lambda_scaling_property_beta,
kwargs_min=kwargs_min,
kwargs_max=kwargs_max,
)

self._aniso_distribution = AnisotropyDistribution(
anisotropy_model=anisotropy_model,
anisotropy_sampling=anisotropy_sampling,
distribution_function=anisotroy_distribution_function,
kwargs_anisotropy_min=kwargs_min,
kwargs_anisotropy_max=kwargs_max,
)

self._gamma_in_prior_mean = gamma_in_prior_mean
self._gamma_in_prior_std = gamma_in_prior_std
Expand Down Expand Up @@ -278,7 +285,10 @@ def log_likelihood_single(
realization of the hyperparameter distribution
"""
kwargs_lens_draw = self._lens_distribution.draw_lens(**kwargs_lens)
lambda_mst, gamma_ppn = kwargs_lens_draw["lambda_mst"], kwargs_lens_draw["gamma_ppn"]
lambda_mst, gamma_ppn = (
kwargs_lens_draw["lambda_mst"],
kwargs_lens_draw["gamma_ppn"],
)
kappa_ext = self._los.draw_los(kwargs_los)

# draw intrinsic source magnitude
Expand Down Expand Up @@ -309,9 +319,9 @@ def log_likelihood_single(
and "gamma_in" in kwargs_lens_draw
):
gamma_in = kwargs_lens_draw["gamma_in"]
lnlikelihood -= (
self._gamma_in_prior_mean - gamma_in
) ** 2 / (2 * self._gamma_in_prior_std**2)
lnlikelihood -= (self._gamma_in_prior_mean - gamma_in) ** 2 / (
2 * self._gamma_in_prior_std**2
)

return np.nan_to_num(lnlikelihood)

Expand Down Expand Up @@ -433,7 +443,10 @@ def sigma_v_measured_vs_predict(
cov_error_predict = np.zeros_like(cov_error_measurement)
for i in range(self._num_distribution_draws):
kwargs_lens_draw = self._lens_distribution.draw_lens(**kwargs_lens)
lambda_mst, gamma_ppn = kwargs_lens_draw["lambda_mst"], kwargs_lens_draw["gamma_ppn"]
lambda_mst, gamma_ppn = (
kwargs_lens_draw["lambda_mst"],
kwargs_lens_draw["gamma_ppn"],
)
kappa_ext = self._los.draw_los(kwargs_los)
ddt_, dd_, _ = self.displace_prediction(
ddt, dd, gamma_ppn=gamma_ppn, lambda_mst=lambda_mst, kappa_ext=kappa_ext
Expand Down Expand Up @@ -478,7 +491,10 @@ def ddt_dd_model_prediction(self, cosmo, kwargs_lens=None, kwargs_los=None):
dd_draws = []
for i in range(self._num_distribution_draws):
kwargs_lens_draw = self._lens_distribution.draw_lens(**kwargs_lens)
lambda_mst, gamma_ppn = kwargs_lens_draw["lambda_mst"], kwargs_lens_draw["gamma_ppn"]
lambda_mst, gamma_ppn = (
kwargs_lens_draw["lambda_mst"],
kwargs_lens_draw["gamma_ppn"],
)
kappa_ext = self._los.draw_los(kwargs_los)
ddt_, dd_, _ = self.displace_prediction(
ddt, dd, gamma_ppn=gamma_ppn, lambda_mst=lambda_mst, kappa_ext=kappa_ext
Expand Down
37 changes: 26 additions & 11 deletions hierarc/Likelihood/kin_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def __init__(self, param_grid_axes, j_kin_scaling_grid):
param_grid_axes = [param_grid_axes]

if self._dim_scaling == 1:
self._f_ani = interp1d(param_grid_axes[0], j_kin_scaling_grid, kind="linear")
self._f_ani = interp1d(
param_grid_axes[0], j_kin_scaling_grid, kind="linear"
)
elif self._dim_scaling == 2:
self._f_ani = interp2d(param_grid_axes[0], param_grid_axes[1], j_kin_scaling_grid.T)
self._f_ani = interp2d(
param_grid_axes[0], param_grid_axes[1], j_kin_scaling_grid.T
)
else:
self._f_ani = RegularGridInterpolator(
tuple(param_grid_axes),
Expand Down Expand Up @@ -57,7 +61,10 @@ class KinScaling(object):
"""Class to manage model parameter and anisotropy scalings for IFU data."""

def __init__(
self, j_kin_scaling_param_axes=None, j_kin_scaling_grid_list=None, j_kin_scaling_param_name_list=None
self,
j_kin_scaling_param_axes=None,
j_kin_scaling_grid_list=None,
j_kin_scaling_param_name_list=None,
):
"""
Expand All @@ -71,7 +78,10 @@ def __init__(
else:
self._param_list = j_kin_scaling_param_name_list
self._param_arrays = j_kin_scaling_param_axes
if not isinstance(j_kin_scaling_param_axes, list) and j_kin_scaling_param_name_list is not None:
if (
not isinstance(j_kin_scaling_param_axes, list)
and j_kin_scaling_param_name_list is not None
):
self._param_arrays = [j_kin_scaling_param_axes]
self._evaluate_scaling = False
self._is_log_m2l_population_level = False
Expand All @@ -85,7 +95,9 @@ def __init__(
self._f_ani_list = []
for scaling_grid in j_kin_scaling_grid_list:
self._j_scaling_ifu.append(
ParameterScalingSingleMeasurement(j_kin_scaling_param_axes, scaling_grid)
ParameterScalingSingleMeasurement(
j_kin_scaling_param_axes, scaling_grid
)
)

if isinstance(j_kin_scaling_param_axes, list):
Expand All @@ -94,22 +106,25 @@ def __init__(
self._dim_scaling = 1

def _kwargs2param_array(self, kwargs):
"""
converts dictionary to sorted array in same order as interpolation grid
"""Converts dictionary to sorted array in same order as interpolation grid.
:param kwargs: dictionary of all model components, must include the one that are interpolated
:param kwargs: dictionary of all model components, must include the one that are
interpolated
:return: sorted list of parameters to interpolate
"""
param_array = []
for param in self._param_list:
if param not in kwargs:
raise ValueError("key %s not in parameters and hence kinematic scaling not possible" % param)
raise ValueError(
"key %s not in parameters and hence kinematic scaling not possible"
% param
)
param_array.append(kwargs.get(param))
return param_array

def param_bounds_interpol(self):
"""
minimum and maximum bounds of parameters that are being used to call interpolation function
"""Minimum and maximum bounds of parameters that are being used to call
interpolation function.
:return: dictionaries of minimum and maximum bounds
"""
Expand Down
38 changes: 19 additions & 19 deletions hierarc/Likelihood/lens_sample_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ def __init__(self, kwargs_lens_list, normalized=False, kwargs_global_model=None)
DSPLikelihood(normalized=normalized, **_kwargs_lens)
)
else:
kwargs_lens_ = self._merge_global2local_settings(kwargs_global_model=kwargs_global_model,
kwargs_lens=kwargs_lens)
self._lens_list.append(
LensLikelihood(**kwargs_lens_)
kwargs_lens_ = self._merge_global2local_settings(
kwargs_global_model=kwargs_global_model, kwargs_lens=kwargs_lens
)
self._lens_list.append(LensLikelihood(**kwargs_lens_))

def log_likelihood(
self,
Expand Down Expand Up @@ -91,18 +90,19 @@ def _merge_global2local_settings(kwargs_global_model, kwargs_lens):
return {**kwargs_global_model_subset, **kwargs_lens}


_input_param_list = ["anisotropy_model",
"anisotropy_sampling",
"anisotroy_distribution_function",
"los_distributions",
"lambda_mst_distribution",
"gamma_in_sampling",
"gamma_in_distribution",
"log_m2l_sampling",
"log_m2l_distribution",
"alpha_lambda_sampling",
"beta_lambda_sampling",
"alpha_gamma_in_sampling",
"alpha_log_m2l_sampling",
"log_scatter"
]
_input_param_list = [
"anisotropy_model",
"anisotropy_sampling",
"anisotroy_distribution_function",
"los_distributions",
"lambda_mst_distribution",
"gamma_in_sampling",
"gamma_in_distribution",
"log_m2l_sampling",
"log_m2l_distribution",
"alpha_lambda_sampling",
"beta_lambda_sampling",
"alpha_gamma_in_sampling",
"alpha_log_m2l_sampling",
"log_scatter",
]
Loading

0 comments on commit 3688ea9

Please sign in to comment.