Skip to content

Commit

Permalink
this reviewdog is really nitpicky...
Browse files Browse the repository at this point in the history
  • Loading branch information
hammannr committed Jul 13, 2023
1 parent 1d5137e commit 536d290
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
37 changes: 30 additions & 7 deletions alea/blueice_extended_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class BlueiceExtendedModel(StatisticalModel):
"""

def __init__(self, parameter_definition: dict, likelihood_config: dict):
"""Initializes the statistical model.
Args:
parameter_definition (dict): A dictionary defining the model parameters.
likelihood_config (dict): A dictionary defining the likelihood.
"""
super().__init__(parameter_definition=parameter_definition)
self._likelihood = self._build_ll_from_config(likelihood_config)
self.likelihood_names = [t["name"] for t in likelihood_config["likelihood_terms"]]
Expand All @@ -33,15 +39,21 @@ def __init__(self, parameter_definition: dict, likelihood_config: dict):

@classmethod
def from_config(cls, config_file_path: str) -> "BlueiceExtendedModel":
"""Initializes the statistical model from a yaml config file.
Args:
config_file_path (str): Path to the yaml config file.
Returns:
BlueiceExtendedModel: Statistical model.
"""
with open(config_file_path, "r") as f:
config = yaml.safe_load(f)
return cls(**config)

@property
def data(self) -> list:
"""
Returns the data of the statistical model.
"""
"""Returns the data of the statistical model."""
return super().data

@data.setter
Expand Down Expand Up @@ -72,6 +84,7 @@ def get_expectation_values(self, **kwargs) -> dict:
def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum":
"""
Iterate through all likelihood terms and build blueice ll
Args:
likelihood_config (dict): A dictionary defining the likelihood.
"""
Expand Down Expand Up @@ -105,7 +118,7 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum":
# Set shape parameters
shape_parameters = [
p for p in source["parameters"] if self.parameters[p].type == "shape"]
if len(shape_parameters) > 0:
if shape_parameters:
# TODO: Implement setting shape parameters
raise NotImplementedError("Shape parameters are not yet supported.")

Expand Down Expand Up @@ -161,13 +174,21 @@ def _generate_ancillary_measurements(self, **generate_values) -> dict:

class CustomAncillaryLikelihood(LogAncillaryLikelihood):
"""
Custom ancillary likelihood that can be used to add constraint terms for parameters of the likelihood.
Custom ancillary likelihood that can be used to add constraint terms
for parameters of the likelihood.
Args:
parameters (Parameters): Parameters object containing the parameters to be constrained.
parameters (Parameters): Parameters object containing the
parameters to be constrained.
"""

def __init__(self, parameters: Parameters):
"""Initialize the CustomAncillaryLikelihood.
Args:
parameters (Parameters): Parameters object containing the
parameters to be constrained.
"""
self.parameters = parameters
# check that there are no None values in the uncertainties dict
assert set(self.parameters.uncertainties.keys()) == set(self.parameters.names)
Expand All @@ -181,13 +202,15 @@ def __init__(self, parameters: Parameters):
@property
def constraint_terms(self) -> dict:
"""
Dict of all constraint terms (logpdf of constraint functions) of the ancillary likelihood.
Dict of all constraint terms (logpdf of constraint functions)
of the ancillary likelihood.
"""
return {name: func.logpdf for name, func in self.constraint_functions.items()}

def set_data(self, d: dict):
"""
Set the data of the ancillary likelihood (ancillary measurements).
Args:
d (dict): Data in this case is a dict of ancillary measurements.
"""
Expand Down
7 changes: 5 additions & 2 deletions alea/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import scipy.stats as sps
import logging
import blueice
logging.basicConfig( level=logging.INFO)
logging.basicConfig(level=logging.INFO)


class BlueiceDataGenerator:
"""
A class for generating data from a blueice likelihood term.
Args:
ll_term (blueice.likelihood.BinnedLogLikelihood or blueice.likelihood.UnbinnedLogLikelihood): A blueice likelihood term.
ll_term (blueice.likelihood.BinnedLogLikelihood
or blueice.likelihood.UnbinnedLogLikelihood):
A blueice likelihood term.
"""

def __init__(self, ll_term):
if isinstance(ll_term, blueice.likelihood.BinnedLogLikelihood):
binned = True
Expand Down
11 changes: 11 additions & 0 deletions alea/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ def adapt_inference_object_config(config_data, wimp_mass, cache_dir=None):

def adapt_likelihood_config_for_blueice(likelihood_config: dict,
template_folder: str) -> dict:
"""
Adapt likelihood config to be compatible with blueice.
Args:
likelihood_config (dict): likelihood config dict
template_folder (str): base folder where templates are located.
If the folder starts with alea/, the alea folder is used as base.
Returns:
dict: adapted likelihood config
"""
# if template folder starts with alea: get location of alea
if template_folder.startswith("alea/"):
import alea
Expand Down

0 comments on commit 536d290

Please sign in to comment.