Skip to content

Commit

Permalink
Allow default parameter values (#194)
Browse files Browse the repository at this point in the history
* Allow default values for parameters

* Default alphag to 1

* Draft of the implementation for the organization of Internal and Sampler parameters in updatable objects.

* Updated all Updatable objects to use the new Sampler/Internal parameter classes.

* Added support for lower case only parameters (CosmoSIS requirement) to ParamsMap. Added missing sacc tracer do the SNIa parameters.

* Running black.

* Removed print.

* Update updatable.py

* Fixed line too long

Co-authored-by: Sandro Dias Pinto Vitenti <vitenti@uel.br>
Co-authored-by: Marc Paterno <paterno@fnal.gov>
  • Loading branch information
3 people authored Nov 3, 2022
1 parent 11435ad commit 2c6855f
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 175 deletions.
2 changes: 1 addition & 1 deletion examples/des_y1_3x2pt/des_y1_3x2pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Sources


lai_systematic = wl.LinearAlignmentSystematic(sacc_tracer="")
lai_systematic = wl.LinearAlignmentSystematic(sacc_tracer="", alphag=None)

"""
Creating sources, each one maps to a specific section of a SACC file. In
Expand Down
2 changes: 1 addition & 1 deletion examples/srd_sn/sn_srd_values.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ w = -1.0
wa = 0.0

[firecrown_supernova_parameters]
M=-20 -19.3 -19
sn_ddf_sample_m=-20 -19.3 -19
; need to add in M to the likelihood

2 changes: 1 addition & 1 deletion examples/srd_sn/sn_srd_values_planck_bao.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ log1e10As = 2.9 3.0448 3.1
sigma8_input = 0.801

[firecrown_supernova_parameters]
M=-20 -19.3 -19
sn_ddf_sample_m=-20 -19.3 -19
2 changes: 1 addition & 1 deletion examples/srd_sn/snonly_values.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ log1e10As = 3.0448
sigma8_input = 0.801

[firecrown_supernova_parameters]
M=-20 -19.3 -19
sn_ddf_sample_m=-20 -19.3 -19

1 change: 1 addition & 0 deletions firecrown/connector/cosmosis/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def calculate_firecrown_params(self, sample: cosmosis.datablock) -> ParamsMap:
if "firecrown" in section:
sec_dict = extract_section(sample, section)
firecrown_params = ParamsMap({**firecrown_params, **sec_dict})
firecrown_params.use_lower_case_keys(True)
return firecrown_params


Expand Down
6 changes: 3 additions & 3 deletions firecrown/likelihood/gauss_family/gauss_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _reset_gaussian_family(self) -> None:
subclasses."""

@final
def required_parameters(self) -> RequiredParameters:
def _required_parameters(self) -> RequiredParameters:
"""Return a RequiredParameters object containing the information for
this Updatable.
Expand All @@ -125,12 +125,12 @@ def required_parameters(self) -> RequiredParameters:
Derived classes must implement required_parameters_gaussian_family."""
stats_rp = self.statistics.required_parameters()
stats_rp = self.required_parameters_gaussian_family() + stats_rp
stats_rp = self._required_parameters_gaussian_family() + stats_rp

return stats_rp

@abstractmethod
def required_parameters_gaussian_family(self):
def _required_parameters_gaussian_family(self):
"""Required parameters for GaussFamily subclasses."""

@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion firecrown/likelihood/gauss_family/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _reset_gaussian_family(self):
pass

@final
def required_parameters_gaussian_family(self):
def _required_parameters_gaussian_family(self):
return RequiredParameters([])

@final
Expand Down
110 changes: 37 additions & 73 deletions firecrown/likelihood/gauss_family/statistic/source/number_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

from .source import Source
from .source import Systematic
from ..... import parameters
from .....parameters import (
ParamsMap,
RequiredParameters,
parameter_get_full_name,
DerivedParameterScalar,
DerivedParameterCollection,
)
Expand Down Expand Up @@ -63,23 +63,19 @@ class LinearBiasSystematic(NumberCountsSystematic):
"""

params_names = ["alphaz", "alphag", "z_piv"]
alphaz: float
alphag: float
z_piv: float

def __init__(self, sacc_tracer: str):
super().__init__()

self.alphaz = parameters.create()
self.alphag = parameters.create()
self.z_piv = parameters.create()
self.sacc_tracer = sacc_tracer

@final
def _update(self, params: ParamsMap):
"""Read the corresponding named tracer from the given collection of
parameters."""
self.alphaz = params.get_from_prefix_param(self.sacc_tracer, "alphaz")
self.alphag = params.get_from_prefix_param(self.sacc_tracer, "alphag")
self.z_piv = params.get_from_prefix_param(self.sacc_tracer, "z_piv")
"""Perform any updates necessary after the parameters have being updated.
This implementation has nothing to do."""

@final
def _reset(self) -> None:
Expand All @@ -88,10 +84,8 @@ def _reset(self) -> None:
This implementation has nothing to do."""

@final
def required_parameters(self) -> RequiredParameters:
return RequiredParameters(
[parameter_get_full_name(self.sacc_tracer, pn) for pn in self.params_names]
)
def _required_parameters(self) -> RequiredParameters:
return RequiredParameters([])

@final
def _get_derived_parameters(self) -> DerivedParameterCollection:
Expand Down Expand Up @@ -130,9 +124,9 @@ class MagnificationBiasSystematic(NumberCountsSystematic):
Parameters
----------
r_lim : str
r_lim : float
The name of the limiting magnitude in r band filter.
sig_c, eta, z_c, z_m : str
sig_c, eta, z_c, z_m : float
The name of the fitting parameters in Joachimi & Bridle (2010) equation
(C.1).
Expand All @@ -141,27 +135,22 @@ class MagnificationBiasSystematic(NumberCountsSystematic):
apply : apply the systematic to a source
"""

params_names = ["r_lim", "sig_c", "eta", "z_c", "z_m"]
r_lim: float
sig_c: float
eta: float
z_c: float
z_m: float

def __init__(self, sacc_tracer: str):
super().__init__()

self.r_lim = parameters.create()
self.sig_c = parameters.create()
self.eta = parameters.create()
self.z_c = parameters.create()
self.z_m = parameters.create()

self.sacc_tracer = sacc_tracer

@final
def _update(self, params: ParamsMap):
"""Read the corresponding named tracer from the given collection of
parameters."""
self.r_lim = params.get_from_prefix_param(self.sacc_tracer, "r_lim")
self.sig_c = params.get_from_prefix_param(self.sacc_tracer, "sig_c")
self.eta = params.get_from_prefix_param(self.sacc_tracer, "eta")
self.z_c = params.get_from_prefix_param(self.sacc_tracer, "z_c")
self.z_m = params.get_from_prefix_param(self.sacc_tracer, "z_m")
"""Perform any updates necessary after the parameters have being updated.
This implementation has nothing to do."""

@final
def _reset(self) -> None:
Expand All @@ -170,10 +159,8 @@ def _reset(self) -> None:
This implementation has nothing to do."""

@final
def required_parameters(self) -> RequiredParameters:
return RequiredParameters(
[parameter_get_full_name(self.sacc_tracer, pn) for pn in self.params_names]
)
def _required_parameters(self) -> RequiredParameters:
return RequiredParameters([])

@final
def _get_derived_parameters(self) -> DerivedParameterCollection:
Expand Down Expand Up @@ -217,17 +204,17 @@ class PhotoZShift(NumberCountsSystematic):
This systematic shifts the photo-z distribution by some ammount `delta_z`.
"""

params_names = ["delta_z"]
delta_z: float

def __init__(self, sacc_tracer: str):
super().__init__()

self.delta_z = parameters.create()
self.sacc_tracer = sacc_tracer

@final
def _update(self, params: ParamsMap):
self.delta_z = params.get_from_prefix_param(self.sacc_tracer, "delta_z")
"""Perform any updates necessary after the parameters have being updated.
This implementation has nothing to do."""

@final
def _reset(self) -> None:
Expand All @@ -236,10 +223,8 @@ def _reset(self) -> None:
This implementation has nothing to do."""

@final
def required_parameters(self) -> RequiredParameters:
return RequiredParameters(
[parameter_get_full_name(self.sacc_tracer, pn) for pn in self.params_names]
)
def _required_parameters(self) -> RequiredParameters:
return RequiredParameters([])

@final
def _get_derived_parameters(self) -> DerivedParameterCollection:
Expand All @@ -265,10 +250,6 @@ def apply(self, cosmo: pyccl.Cosmology, tracer_arg: NumberCountsArgs):
class NumberCounts(Source):
"""Source class for number counts."""

params_names = ["bias", "mag_bias"]
bias: float
mag_bias: Optional[float]

systematics: UpdatableCollection
tracer_arg: NumberCountsArgs

Expand All @@ -286,9 +267,11 @@ def __init__(

self.sacc_tracer = sacc_tracer
self.has_rsd = has_rsd
self.has_mag_bias = has_mag_bias
self.derived_scale = derived_scale

self.bias = parameters.create()
self.mag_bias = parameters.create(None if has_mag_bias else 0.0)

self.systematics = UpdatableCollection([])
if systematics:
for systematic in systematics:
Expand All @@ -301,37 +284,18 @@ def __init__(

@final
def _update_source(self, params: ParamsMap):
self.bias = params.get_from_prefix_param(self.sacc_tracer, "bias")

if self.has_mag_bias:
self.mag_bias = params.get_from_prefix_param(self.sacc_tracer, "mag_bias")
else:
self.mag_bias = None
"""Perform any updates necessary after the parameters have being updated.
This implementation must update all contained Updatable instances."""
self.systematics.update(params)

@final
def _reset_source(self) -> None:
self.systematics.reset()

@final
def required_parameters(self) -> RequiredParameters:
if self.has_mag_bias:
rp = RequiredParameters(
[
parameter_get_full_name(self.sacc_tracer, pn)
for pn in self.params_names
]
)
else:
rp = RequiredParameters(
[
parameter_get_full_name(self.sacc_tracer, pn)
for pn in self.params_names
if pn != "mag_bias"
]
)
return rp + self.systematics.required_parameters()
def _required_parameters(self) -> RequiredParameters:
return self.systematics.required_parameters()

@final
def _get_derived_parameters(self) -> DerivedParameterCollection:
Expand Down Expand Up @@ -382,7 +346,7 @@ def create_tracer(self, cosmo: pyccl.Cosmology):
mag_bias=tracer_args.mag_bias,
)

if self.mag_bias is not None:
if self.mag_bias != 0.0:
mag_bias = np.ones_like(tracer_args.z) * self.mag_bias
tracer_args = NumberCountsArgs(
scale=tracer_args.scale,
Expand All @@ -395,7 +359,7 @@ def create_tracer(self, cosmo: pyccl.Cosmology):
for systematic in self.systematics:
tracer_args = systematic.apply(cosmo, tracer_args)

if self.has_mag_bias:
if self.mag_bias != 0.0:
tracer = pyccl.NumberCountsTracer(
cosmo,
has_rsd=self.has_rsd,
Expand Down
2 changes: 0 additions & 2 deletions firecrown/likelihood/gauss_family/statistic/source/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class Source(Updatable):
Parameters
----------
scale : 1.0, optional
The default scale for this source.
systematics : list of str, optional
A list of the source-level systematics to apply to the source. The
default of `None` implies no systematics.
Expand Down
Loading

0 comments on commit 2c6855f

Please sign in to comment.