Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hierarc fork #7

Merged
merged 87 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
6f59c3b
new line of sight distribution handling for multiple distributions an…
sibirrer Apr 28, 2024
f1cf577
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2024
077e42f
improved unit testing
sibirrer Apr 29, 2024
28e53a3
Merge remote-tracking branch 'origin/los_gev' into los_gev
sibirrer Apr 29, 2024
93d44e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 29, 2024
b1624b2
Merge pull request #33 from sibirrer/los_gev
sibirrer Apr 29, 2024
93d931b
[pre-commit.ci] pre-commit autoupdate
pre-commit-ci[bot] May 6, 2024
6f1dbf1
Merge pull request #34 from sibirrer/pre-commit-ci-update-config
sibirrer May 6, 2024
cec874c
Fix error message
ajshajib May 15, 2024
549246d
Merge pull request #35 from ajshajib/main
sibirrer May 15, 2024
5fdae6b
Refactor MCMCSampler class to separate the EMCEE sampling and sampler…
ajshajib May 16, 2024
581c521
Merge pull request #36 from ajshajib/feature/function_restructuring
ajshajib May 16, 2024
aec7509
added codecov token
sibirrer May 16, 2024
a81a094
Merge remote-tracking branch 'origin/main'
sibirrer May 16, 2024
dc5de27
added codecov token
sibirrer May 29, 2024
905f027
fix test
sibirrer May 29, 2024
a38dd3b
major restructuring of parameter distribution and design of the inter…
sibirrer Jun 27, 2024
4bb09ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2024
be03297
Update setup.py
sibirrer Jun 27, 2024
e55fa33
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2024
87f81f3
Update __init__.py
sibirrer Jun 27, 2024
b07cf43
Update HISTORY.rst
sibirrer Jun 27, 2024
8c2058c
Merge pull request #39 from sibirrer/sibirrer-patch-1
sibirrer Jun 27, 2024
58d9bd4
minor change in plotting if there is only a single lens
sibirrer Jun 27, 2024
c0ed4a6
improved tests in lens distribution draws
sibirrer Jun 27, 2024
3688ea9
Merge remote-tracking branch 'origin/kin_scaling_restructuring' into …
sibirrer Jun 27, 2024
567b0d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2024
caa13de
improve testing and stability
sibirrer Jun 28, 2024
f5c5b13
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2024
4758eab
re-definition of distribution functions for OM and GOM model for a_an…
sibirrer Jun 28, 2024
2cd0960
Merge remote-tracking branch 'origin/kin_scaling_restructuring' into …
sibirrer Jun 28, 2024
73a3464
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2024
506df69
minor testing changes
sibirrer Jun 28, 2024
4fd5fae
Merge remote-tracking branch 'origin/kin_scaling_restructuring' into …
sibirrer Jun 28, 2024
7ce952a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2024
36c98e4
minor testing changes
sibirrer Jun 28, 2024
db60e47
Merge remote-tracking branch 'origin/kin_scaling_restructuring' into …
sibirrer Jun 28, 2024
3e6a1e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2024
a68ea3e
minor testing changes
sibirrer Jun 29, 2024
3be0ca7
Merge remote-tracking branch 'origin/kin_scaling_restructuring' into …
sibirrer Jun 29, 2024
d093fb0
Merge pull request #38 from sibirrer/kin_scaling_restructuring
sibirrer Jun 29, 2024
0611f3f
first simplification of code to implement gamma_pl in kinematics
sibirrer Jul 1, 2024
f94945b
re-structuring of code, before major refactoring
sibirrer Jul 2, 2024
5ad80b2
generalized treatment of different dimensions in the kinematic pre-pr…
sibirrer Jul 2, 2024
2261583
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2024
f01f942
added tests
sibirrer Jul 2, 2024
7a9d2dd
priors reformulated
sibirrer Jul 3, 2024
e6df2f2
Merge remote-tracking branch 'origin/gamma_pl_sampling' into gamma_pl…
sibirrer Jul 3, 2024
721e13b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 3, 2024
65f971b
first implementation of sampling individual power-law density profiles
sibirrer Jul 5, 2024
c89fa56
Merge remote-tracking branch 'origin/gamma_pl_sampling' into gamma_pl…
sibirrer Jul 5, 2024
f637d52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2024
d5f7faf
fully integrated with sampling
sibirrer Jul 5, 2024
9b7f176
Merge remote-tracking branch 'origin/gamma_pl_sampling' into gamma_pl…
sibirrer Jul 5, 2024
314b6fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2024
d36c598
tests improvements and base model for gamma_pl adapted
sibirrer Jul 6, 2024
474d7e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2024
ee1af42
changed test settings
sibirrer Jul 6, 2024
88f47ab
Merge remote-tracking branch 'origin/gamma_pl_sampling' into gamma_pl…
sibirrer Jul 6, 2024
31acffe
added testing
sibirrer Jul 6, 2024
78a994f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2024
ec09916
Merge pull request #40 from sibirrer/gamma_pl_sampling
sibirrer Jul 6, 2024
bee9c59
refactored LOSDistribution
sibirrer Jul 29, 2024
dcf3d3b
update to latest version of lenstronomy
sibirrer Jul 30, 2024
18ac255
[pre-commit.ci] pre-commit autoupdate
pre-commit-ci[bot] Aug 5, 2024
84bc01c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 5, 2024
0563aea
Merge pull request #41 from sibirrer/pre-commit-ci-update-config
sibirrer Aug 5, 2024
25cd5de
double source plane likelihood with power-law and MST transform in place
sibirrer Aug 6, 2024
252f5c8
double source plane sampling with gamma_pl globally in place
sibirrer Aug 7, 2024
e1a42d2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 7, 2024
bbaace3
tests updated
sibirrer Aug 7, 2024
3a88c87
Merge remote-tracking branch 'origin/dsp_msd' into dsp_msd
sibirrer Aug 7, 2024
8c7d314
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 7, 2024
cbf807f
improved testing and slight re-working of interpolated cosmology
sibirrer Aug 7, 2024
5c762b7
Merge remote-tracking branch 'origin/dsp_msd' into dsp_msd
sibirrer Aug 7, 2024
318e8c7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 7, 2024
df999a3
improve testing
sibirrer Aug 8, 2024
e0a0c21
Merge remote-tracking branch 'origin/dsp_msd' into dsp_msd
sibirrer Aug 8, 2024
40832a8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 8, 2024
cba5c9c
Merge pull request #42 from sibirrer/dsp_msd
sibirrer Aug 8, 2024
b0c3508
verbose feature for better diagnostics for the likelihood
sibirrer Aug 13, 2024
11bdde7
Merge remote-tracking branch 'origin/dsp_msd' into dsp_msd
sibirrer Aug 13, 2024
232c5ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2024
da20372
testing improved with verbose
sibirrer Aug 13, 2024
d5bfb53
Merge pull request #43 from sibirrer/dsp_msd
sibirrer Aug 13, 2024
3a5a45b
added GAUSSIAN_SCALED in parameter handling
sibirrer Aug 13, 2024
f482350
backend ereased before initializing new EMCEE object
sibirrer Sep 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ci:

repos:
- repo: https://github.com/psf/black
rev: 24.3.0
rev: 24.8.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
Expand All @@ -19,7 +19,7 @@ repos:
# https://pre-commit.com/#top_level-default_language_version
language_version: python3
- repo: https://github.com/psf/black
rev: 24.3.0
rev: 24.8.0
hooks:
- id: black-jupyter
language_version: python3
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ History
* double source plane likelihood
* Pantheon+ likelihood
* improved API

1.1.3 (2024-06-27)
------------------

* composite model likelihood and fitting
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
codecov:
token: 399bc344-9039-4f2b-9ade-fee2aac10b78
comment: # this is a top-level key
layout: " diff, flags, files"
behavior: default
Expand Down
27 changes: 20 additions & 7 deletions hierarc/Diagnostics/goodness_of_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def plot_ddt_fit(
cosmo,
kwargs_lens,
kwargs_kin,
kwargs_los,
color_measurement=None,
color_prediction=None,
redshift_trend=False,
Expand Down Expand Up @@ -64,7 +65,9 @@ def plot_ddt_fit(
ddt_model_sigma,
dd_model_mean,
dd_model_sigma,
) = likelihood.ddt_dd_model_prediction(cosmo, kwargs_lens=kwargs_lens)
) = likelihood.ddt_dd_model_prediction(
cosmo, kwargs_lens=kwargs_lens, kwargs_los=kwargs_los
)

ddt_name_list.append(name)
ddt_model_mean_list.append(ddt_model_mean)
Expand Down Expand Up @@ -134,13 +137,14 @@ def plot_ddt_fit(
ax.legend()
return f, ax

def kin_fit(self, cosmo, kwargs_lens, kwargs_kin):
def kin_fit(self, cosmo, kwargs_lens, kwargs_kin, kwargs_los):
"""Plots the prediction and the uncorrelated error bars on the individual lenses
currently works for likelihood classes 'TDKinGaussian', 'KinGaussian'.

:param cosmo: astropy.cosmology instance
:param kwargs_lens: lens model parameter keyword arguments
:param kwargs_kin: kinematics model keyword arguments
:param kwargs_los: line of sight list of dictionaries
:return: list of name, measurement, measurement errors, model prediction, model
prediction error
"""
Expand All @@ -160,7 +164,10 @@ def kin_fit(self, cosmo, kwargs_lens, kwargs_kin):
sigma_v_predict_mean,
cov_error_predict,
) = likelihood.sigma_v_measured_vs_predict(
cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin
cosmo,
kwargs_lens=kwargs_lens,
kwargs_kin=kwargs_kin,
kwargs_los=kwargs_los,
)

if sigma_v_measurement is not None:
Expand Down Expand Up @@ -188,6 +195,7 @@ def plot_kin_fit(
cosmo,
kwargs_lens,
kwargs_kin,
kwargs_los,
color_measurement=None,
color_prediction=None,
):
Expand All @@ -197,21 +205,24 @@ def plot_kin_fit(
:param cosmo: astropy.cosmology instance
:param kwargs_lens: lens model parameter keyword arguments
:param kwargs_kin: kinematics model keyword arguments
:param kwargs_los: line of sight list of dictionaries
:param color_measurement: color of measurement
:param color_prediction: color of model prediction
:return: fig, axes of matplotlib instance
"""
logL = self._sample_likelihood.log_likelihood(cosmo, kwargs_lens, kwargs_kin)
logL = self._sample_likelihood.log_likelihood(
cosmo, kwargs_lens, kwargs_kin, kwargs_los=kwargs_los
)
print(logL, "log likelihood")
(
sigma_v_name_list,
sigma_v_measurement_list,
sigma_v_measurement_error_list,
sigma_v_model_list,
sigma_v_model_error_list,
) = self.kin_fit(cosmo, kwargs_lens, kwargs_kin)
) = self.kin_fit(cosmo, kwargs_lens, kwargs_kin, kwargs_los)

f, ax = plt.subplots(1, 1, figsize=(int(len(sigma_v_name_list) / 2), 4))
f, ax = plt.subplots(1, 1, figsize=(max(int(len(sigma_v_name_list) / 2), 1), 4))
ax.errorbar(
np.arange(len(sigma_v_name_list)),
sigma_v_measurement_list,
Expand Down Expand Up @@ -256,6 +267,7 @@ def plot_ifu_fit(
cosmo,
kwargs_lens,
kwargs_kin,
kwargs_los,
lens_index,
bin_edges,
show_legend=True,
Expand All @@ -268,6 +280,7 @@ def plot_ifu_fit(
:param cosmo: astropy.cosmology instance
:param kwargs_lens: lens model parameter keyword arguments
:param kwargs_kin: kinematics model keyword arguments
:param kwargs_los: line of sight list of dictionaries
:param lens_index: int, index in kwargs_lens to be plotted (needs to be of type
'IFUKinCov')
:param bin_edges: radial bin edges in arc seconds. If number, then uniform
Expand All @@ -293,7 +306,7 @@ def plot_ifu_fit(
sigma_v_predict_mean,
cov_error_predict,
) = likelihood.sigma_v_measured_vs_predict(
cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin
cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin, kwargs_los=kwargs_los
)

if len(np.atleast_1d(bin_edges)) < 2:
Expand Down
87 changes: 0 additions & 87 deletions hierarc/LensPosterior/anisotropy_config.py

This file was deleted.

20 changes: 17 additions & 3 deletions hierarc/LensPosterior/base_config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from lenstronomy.Analysis.td_cosmography import TDCosmography
from hierarc.LensPosterior.imaging_constraints import ImageModelPosterior
from hierarc.LensPosterior.anisotropy_config import AnisotropyConfig
from hierarc.LensPosterior.kin_scaling_config import KinScalingConfig


class BaseLensConfig(TDCosmography, ImageModelPosterior, AnisotropyConfig):
class BaseLensConfig(TDCosmography, ImageModelPosterior, KinScalingConfig):
"""This class contains and manages the base configurations of the lens posteriors
and makes sure that they are universally applied consistently through the different
likelihood definitions."""
Expand Down Expand Up @@ -33,6 +33,9 @@ def __init__(
num_kin_sampling=1000,
multi_observations=False,
cosmo_fiducial=None,
gamma_in_scaling=None,
log_m2l_scaling=None,
gamma_pl_scaling=None,
):
"""

Expand Down Expand Up @@ -62,6 +65,9 @@ def __init__(
light profile
:param cosmo_fiducial: astropy.cosmology instance, if None,
uses astropy's default cosmology
:param gamma_in_scaling: array of gamma_in parameter to be interpolated (optional, otherwise None)
:param log_m2l_scaling: array of log_m2l parameter to be interpolated (optional, otherwise None)
:param gamma_pl_scaling: array of power-law density profile slopes to be interpolated (optional, otherwise None)
"""
self._z_lens, self._z_source = z_lens, z_source

Expand Down Expand Up @@ -105,4 +111,12 @@ def __init__(
ImageModelPosterior.__init__(
self, theta_E, theta_E_error, gamma, gamma_error, r_eff, r_eff_error
)
AnisotropyConfig.__init__(self, anisotropy_model, r_eff)
KinScalingConfig.__init__(
self,
anisotropy_model,
r_eff,
gamma_in_scaling=gamma_in_scaling,
log_m2l_scaling=log_m2l_scaling,
gamma_pl_scaling=gamma_pl_scaling,
gamma_pl_mean=gamma,
)
19 changes: 17 additions & 2 deletions hierarc/LensPosterior/ddt_kin_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(
num_psf_sampling=100,
num_kin_sampling=1000,
multi_observations=False,
gamma_pl_scaling=None,
):
"""

Expand Down Expand Up @@ -67,6 +68,7 @@ def __init__(
:param kappa_ext_sigma: 1-sigma distribution uncertainty from which the ddt constraints are coming from
:param multi_observations: bool, if True, interprets kwargs_aperture and kwargs_seeing as lists of multiple
observations
:param gamma_pl_scaling: array of mass density profile power-law slope values (optional, otherwise None)
"""
self._ddt_sample, self._ddt_weights = ddt_samples, ddt_weights
self._kappa_ext_mean, self._kappa_ext_sigma = kappa_ext, kappa_ext_sigma
Expand Down Expand Up @@ -96,6 +98,7 @@ def __init__(
num_psf_sampling=num_psf_sampling,
num_kin_sampling=num_kin_sampling,
multi_observations=multi_observations,
gamma_pl_scaling=gamma_pl_scaling,
)

def hierarchy_configuration(self, num_sample_model=20):
Expand Down Expand Up @@ -124,7 +127,19 @@ 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.ani_param_array,
"ani_scaling_array_list": ani_scaling_array_list,
"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,
}

prior_list = []
if "gamma_pl" in self._param_name_list:
prior_list.append(["gamma_pl", self._gamma, self._gamma_error])
# TODO: make sure to add other priors if needed or available
# if "gamma_in" in self._param_name_list:
# prior_list.append(["gamma_in"])
kwargs_likelihood["prior_list"] = prior_list
# if "gamma_pl" in self._param_name_list:
# kwargs_likelihood["gamma_pl_sampling"] = True

return kwargs_likelihood
5 changes: 3 additions & 2 deletions hierarc/LensPosterior/ddt_kin_gauss_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ 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.ani_param_array,
"ani_scaling_array_list": ani_scaling_array_list,
"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
19 changes: 12 additions & 7 deletions hierarc/LensPosterior/imaging_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ def __init__(self, theta_E, theta_E_error, gamma, gamma_error, r_eff, r_eff_erro
self._gamma, self._gamma_error = gamma, gamma_error
self._r_eff, self._r_eff_error = r_eff, r_eff_error

def draw_lens(self, no_error=False):
def draw_lens(self, gamma_pl=None, no_error=False):
"""

:param no_error: bool, if True, does not render from the uncertainty but uses the mean values instead
:param gamma_pl: power law slope, if None, draws from measurement uncertainty, otherwise takes at fixed value
:type gamma_pl: float or None
:return: theta_E, gamma, r_eff, delta_r_eff
"""
if no_error is True:
return self._theta_E, self._gamma, self._r_eff, 1
theta_E_draw = np.maximum(
np.random.normal(loc=self._theta_E, scale=self._theta_E_error), 0
)
gamma_draw = np.random.normal(loc=self._gamma, scale=self._gamma_error)
# distributions are drawn in the range [1, 3)
# the power-law slope gamma=3 is divergent in mass in the center and values close close to =3 may be unstable
# to compute the kinematics for.
gamma_draw = np.maximum(gamma_draw, 1.0)
gamma_draw = np.minimum(gamma_draw, 2.999)
if gamma_pl is None:
gamma_draw = np.random.normal(loc=self._gamma, scale=self._gamma_error)
# distributions are drawn in the range [1, 3)
# the power-law slope gamma=3 is divergent in mass in the center and values close to =3 may be unstable
# to compute the kinematics for.
gamma_draw = np.maximum(gamma_draw, 1.0)
gamma_draw = np.minimum(gamma_draw, 2.999)
else:
gamma_draw = gamma_pl
# we make sure no negative r_eff are being sampled
delta_r_eff = np.maximum(
np.random.normal(loc=1, scale=self._r_eff_error / self._r_eff), 0.001
Expand Down
Loading