From 64ceaa2c1a45376d41a82613c15903153d55e4fb Mon Sep 17 00:00:00 2001 From: Knut Dundas Moraa Date: Thu, 13 Jul 2023 15:11:23 -0400 Subject: [PATCH 1/9] Removes all hash for parameters not used for each source, and for all efficiency parameters. This way, blueice will not cache pdfs for different values of these parameters --- alea/blueice_extended_model.py | 36 ++++++++++++++++++- .../unbinned_wimp_statistical_model.yaml | 22 +++++++++--- alea/statistical_model.py | 3 +- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index d1af0cdd..bcf521d8 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -76,7 +76,13 @@ def get_expectation_values(self, **kwargs) -> dict: """ ret = dict() for ll in self._likelihood.likelihood_list[:-1]: # ancillary likelihood does not contribute - mus = ll(full_output=True, **kwargs)[1] + + ll_pars = list(ll.rate_parameters.keys()) + list(ll.shape_parameters.keys()) + call_args = {k:i for k,i in kwargs.items() if k in ll_pars} + if "livetime_days" in kwargs: + call_args["livetime_days"] = kwargs["livetime_days"] + + mus = ll(full_output=True, **call_args)[1] for n, mu in zip(ll.source_name_list, mus): ret[n] = ret.get(n, 0) + mu return ret @@ -105,6 +111,18 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": config, template_folder_list) blueice_config["livetime_days"] = self.parameters[ blueice_config["livetime_parameter"]].nominal_value + for p in self.parameters: + blueice_config[p.name] = blueice_config.get(p.name, p.nominal_value) + + #add all parameters to extra_dont_hash for each source unless it is used: + for i,source in enumerate(config["sources"]): + parameters_to_ignore = [ p.name for p in self.parameters if (p.type == "shape") & (p.name not in source["parameters"])] + parameters_to_ignore += [ p.name for p in self.parameters if (p.type == "efficiency")] # no efficiency affects PDF + parameters_to_ignore += source.get("extra_dont_hash_settings", []) + + #ignore all shape parameters known to this model not named specifically in the source: + blueice_config["sources"][i]["extra_dont_hash_settings"] = parameters_to_ignore + ll = likelihood_object(blueice_config) for source in config["sources"]: @@ -130,6 +148,22 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": # TODO: Implement setting shape parameters raise NotImplementedError("Shape parameters are not yet supported.") + + # Set efficiency parameters + if source.get("apply_efficiency", False): + assert "efficiency_name" in source, "Unspecified efficiency_name for source {:s}".format(source["name"]) + efficiency_name = source["efficiency_name"] + assert efficiency_name in source["parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) + efficiency_parameter = self.parameters[efficiency_name] + assert efficiency_parameter.type == "efficiency", "The parameter {:s} must be an efficiency".format(efficiency_name) + limits = efficiency_parameter.fit_limits + assert 0<=limits[0], "Efficiency parameters including {:s} must be constrained to be nonngative".format(efficiency_name) + assert np.isfinite(limits[1]), "Efficiency parameters including {:s} must be constrained to be finite".format(efficiency_name) + ll.add_shape_parameter(efficiency_name, anchors=(limits[0],limits[1])) + + + + ll.prepare() lls.append(ll) diff --git a/alea/examples/unbinned_wimp_statistical_model.yaml b/alea/examples/unbinned_wimp_statistical_model.yaml index f482b7d1..ebc0237e 100644 --- a/alea/examples/unbinned_wimp_statistical_model.yaml +++ b/alea/examples/unbinned_wimp_statistical_model.yaml @@ -33,6 +33,18 @@ parameter_definition: - null fit_guess: 1.0 + signal_efficiency: + nominal_value: 1.0 + ptype: efficiency + uncertainty: 0.1 + relative_uncertainty: true + fittable: true + fit_limits: + - 0 + - 10. + fit_guess: 1.0 + description: Parameter to account for the uncertain signal expectation given a certain cross-section + # er_band_shift: # nominal_value: 0 # ptype: shape @@ -76,9 +88,10 @@ likelihood_config: parameters: - wimp_rate_multiplier - wimp_mass + - signal_efficiency template_filename: wimp50gev_template.h5 - apply_efficiency: False - efficiency_name: 'signal_eff' # TODO: Check + apply_efficiency: True + efficiency_name: signal_efficiency # TODO: Check # SR1 - name: sr1 @@ -104,6 +117,7 @@ likelihood_config: parameters: - wimp_rate_multiplier - wimp_mass + - signal_efficiency template_filename: wimp50gev_template.h5 - apply_efficiency: False - efficiency_name: 'wimp_eff' # TODO: Check \ No newline at end of file + apply_efficiency: True + efficiency_name: signal_efficiency # TODO: Check \ No newline at end of file diff --git a/alea/statistical_model.py b/alea/statistical_model.py index ce0542a4..9e7d54bc 100644 --- a/alea/statistical_model.py +++ b/alea/statistical_model.py @@ -163,7 +163,8 @@ def store_data( kw = {'metadata': metadata} if metadata is not None else dict() toydata_to_file(file_name, data_list, data_name_list, **kw) - def get_expectation_values(self): + def get_expectation_values(self, **parameter_values): + mus = self._ll(full_output=True, **parameter_values) return NotImplementedError("get_expectation_values is optional to implement") @property From 0eccaf01105ec93535786d9591836f5d6d72ca34 Mon Sep 17 00:00:00 2001 From: Knut Dundas Moraa Date: Thu, 13 Jul 2023 16:33:31 -0400 Subject: [PATCH 2/9] Accepted some of the linting comments --- alea/blueice_extended_model.py | 35 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index bcf521d8..389fe827 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -1,4 +1,6 @@ from pydoc import locate # to lookup likelihood class +from typing import List + from alea.statistical_model import StatisticalModel from alea.simulators import BlueiceDataGenerator from alea.utils import adapt_likelihood_config_for_blueice @@ -78,7 +80,7 @@ def get_expectation_values(self, **kwargs) -> dict: for ll in self._likelihood.likelihood_list[:-1]: # ancillary likelihood does not contribute ll_pars = list(ll.rate_parameters.keys()) + list(ll.shape_parameters.keys()) - call_args = {k:i for k,i in kwargs.items() if k in ll_pars} + call_args = {k:i for k, i in kwargs.items() if k in ll_pars} if "livetime_days" in kwargs: call_args["livetime_days"] = kwargs["livetime_days"] @@ -114,16 +116,18 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": for p in self.parameters: blueice_config[p.name] = blueice_config.get(p.name, p.nominal_value) - #add all parameters to extra_dont_hash for each source unless it is used: - for i,source in enumerate(config["sources"]): - parameters_to_ignore = [ p.name for p in self.parameters if (p.type == "shape") & (p.name not in source["parameters"])] - parameters_to_ignore += [ p.name for p in self.parameters if (p.type == "efficiency")] # no efficiency affects PDF + # add all parameters to extra_dont_hash for each source unless it is used: + for i, source in enumerate(config["sources"]): + parameters_to_ignore: List[str] = [p.name for p in self.parameters if (p.type == "shape") + & (p.name not in source["parameters"])] + # no efficiency affects PDF: + parameters_to_ignore += [ p.name for p in self.parameters if (p.type == "efficiency")] parameters_to_ignore += source.get("extra_dont_hash_settings", []) - #ignore all shape parameters known to this model not named specifically in the source: - blueice_config["sources"][i]["extra_dont_hash_settings"] = parameters_to_ignore + # ignore all shape parameters known to this model not named specifically in the source: + blueice_config["sources"][i]["extra_dont_hash_settings"] = parameters_to_ignore - ll = likelihood_object(blueice_config) + ll = likelihood_object(blueice_config) # noqa: E127 for source in config["sources"]: @@ -152,16 +156,17 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": # Set efficiency parameters if source.get("apply_efficiency", False): assert "efficiency_name" in source, "Unspecified efficiency_name for source {:s}".format(source["name"]) - efficiency_name = source["efficiency_name"] + efficiency_name = source["efficiency_name"] assert efficiency_name in source["parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) efficiency_parameter = self.parameters[efficiency_name] - assert efficiency_parameter.type == "efficiency", "The parameter {:s} must be an efficiency".format(efficiency_name) + assert efficiency_parameter.type == "efficiency", "The parameter {:s} must" \ + " be an efficiency".format(efficiency_name) limits = efficiency_parameter.fit_limits - assert 0<=limits[0], "Efficiency parameters including {:s} must be constrained to be nonngative".format(efficiency_name) - assert np.isfinite(limits[1]), "Efficiency parameters including {:s} must be constrained to be finite".format(efficiency_name) - ll.add_shape_parameter(efficiency_name, anchors=(limits[0],limits[1])) - - + assert 0 <= limits[0], 'Efficiency parameters including {:s} must be' \ + ' constrained to be nonnegative'.format(efficiency_name) + assert np.isfinite(limits[1]), 'Efficiency parameters including {:s} must be' \ + ' constrained to be finite'.format(efficiency_name) + ll.add_shape_parameter(efficiency_name, anchors=(limits[0], limits[1])) ll.prepare() From 28c5dd6c58ceff99768178282c16d198ae3480f7 Mon Sep 17 00:00:00 2001 From: Robert Hammann Date: Fri, 14 Jul 2023 07:41:56 +0200 Subject: [PATCH 3/9] fix ptype - type missmatch --- alea/examples/unbinned_wimp_statistical_model.yaml | 8 ++++---- alea/parameters.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/alea/examples/unbinned_wimp_statistical_model.yaml b/alea/examples/unbinned_wimp_statistical_model.yaml index ebc0237e..98b6290e 100644 --- a/alea/examples/unbinned_wimp_statistical_model.yaml +++ b/alea/examples/unbinned_wimp_statistical_model.yaml @@ -16,7 +16,7 @@ parameter_definition: wimp_rate_multiplier: nominal_value: 1.0 - ptype: rate + type: rate fittable: true fit_limits: - 0 @@ -24,7 +24,7 @@ parameter_definition: er_rate_multiplier: nominal_value: 1.0 - ptype: rate + type: rate uncertainty: 0.2 relative_uncertainty: true fittable: true @@ -35,7 +35,7 @@ parameter_definition: signal_efficiency: nominal_value: 1.0 - ptype: efficiency + type: efficiency uncertainty: 0.1 relative_uncertainty: true fittable: true @@ -47,7 +47,7 @@ parameter_definition: # er_band_shift: # nominal_value: 0 - # ptype: shape + # type: shape # uncertainty: scipy.stats.uniform(loc=-1, scale=2) # relative_uncertainty: false # fittable: true diff --git a/alea/parameters.py b/alea/parameters.py index 2367c889..de02134f 100644 --- a/alea/parameters.py +++ b/alea/parameters.py @@ -9,7 +9,7 @@ class Parameter: name (str): The name of the parameter. nominal_value (float, optional): The nominal value of the parameter. fittable (bool, optional): Indicates if the parameter is fittable or always fixed. - ptype (str, optional): The type of the parameter. + type (str, optional): The type of the parameter. uncertainty (float or str, optional): The uncertainty of the parameter. If a string, it can be evaluated as a numpy or scipy function to define non-gaussian constraints. @@ -27,7 +27,7 @@ def __init__( name: str, nominal_value: Optional[float] = None, fittable: bool = True, - ptype: Optional[str] = None, + type: Optional[str] = None, uncertainty: Optional[float or str] = None, relative_uncertainty: Optional[bool] = None, blueice_anchors: Optional[List] = None, @@ -39,7 +39,7 @@ def __init__( self.name = name self.nominal_value = nominal_value self.fittable = fittable - self.type = ptype + self.type = type self._uncertainty = uncertainty self.relative_uncertainty = relative_uncertainty self.blueice_anchors = blueice_anchors From 97d3dfe21375b7ecd0d327a0df0ad71f42f2a2ac Mon Sep 17 00:00:00 2001 From: Robert Hammann Date: Fri, 14 Jul 2023 09:00:02 +0200 Subject: [PATCH 4/9] The dog convinced me: Let's go with ptype instead. --- alea/blueice_extended_model.py | 10 +++++----- alea/examples/unbinned_wimp_statistical_model.yaml | 8 ++++---- alea/parameters.py | 6 +++--- alea/statistical_model.py | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index 389fe827..b60a7339 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -118,10 +118,10 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": # add all parameters to extra_dont_hash for each source unless it is used: for i, source in enumerate(config["sources"]): - parameters_to_ignore: List[str] = [p.name for p in self.parameters if (p.type == "shape") + parameters_to_ignore: List[str] = [p.name for p in self.parameters if (p.ptype == "shape") & (p.name not in source["parameters"])] # no efficiency affects PDF: - parameters_to_ignore += [ p.name for p in self.parameters if (p.type == "efficiency")] + parameters_to_ignore += [ p.name for p in self.parameters if (p.ptype == "efficiency")] parameters_to_ignore += source.get("extra_dont_hash_settings", []) # ignore all shape parameters known to this model not named specifically in the source: @@ -133,7 +133,7 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": # Set rate parameters rate_parameters = [ - p for p in source["parameters"] if self.parameters[p].type == "rate"] + p for p in source["parameters"] if self.parameters[p].ptype == "rate"] if len(rate_parameters) != 1: raise ValueError( f"Source {source['name']} must have exactly one rate parameter.") @@ -147,7 +147,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"] + p for p in source["parameters"] if self.parameters[p].ptype == "shape"] if shape_parameters: # TODO: Implement setting shape parameters raise NotImplementedError("Shape parameters are not yet supported.") @@ -159,7 +159,7 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": efficiency_name = source["efficiency_name"] assert efficiency_name in source["parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) efficiency_parameter = self.parameters[efficiency_name] - assert efficiency_parameter.type == "efficiency", "The parameter {:s} must" \ + assert efficiency_parameter.ptype == "efficiency", "The parameter {:s} must" \ " be an efficiency".format(efficiency_name) limits = efficiency_parameter.fit_limits assert 0 <= limits[0], 'Efficiency parameters including {:s} must be' \ diff --git a/alea/examples/unbinned_wimp_statistical_model.yaml b/alea/examples/unbinned_wimp_statistical_model.yaml index 98b6290e..ebc0237e 100644 --- a/alea/examples/unbinned_wimp_statistical_model.yaml +++ b/alea/examples/unbinned_wimp_statistical_model.yaml @@ -16,7 +16,7 @@ parameter_definition: wimp_rate_multiplier: nominal_value: 1.0 - type: rate + ptype: rate fittable: true fit_limits: - 0 @@ -24,7 +24,7 @@ parameter_definition: er_rate_multiplier: nominal_value: 1.0 - type: rate + ptype: rate uncertainty: 0.2 relative_uncertainty: true fittable: true @@ -35,7 +35,7 @@ parameter_definition: signal_efficiency: nominal_value: 1.0 - type: efficiency + ptype: efficiency uncertainty: 0.1 relative_uncertainty: true fittable: true @@ -47,7 +47,7 @@ parameter_definition: # er_band_shift: # nominal_value: 0 - # type: shape + # ptype: shape # uncertainty: scipy.stats.uniform(loc=-1, scale=2) # relative_uncertainty: false # fittable: true diff --git a/alea/parameters.py b/alea/parameters.py index de02134f..17448ffa 100644 --- a/alea/parameters.py +++ b/alea/parameters.py @@ -9,7 +9,7 @@ class Parameter: name (str): The name of the parameter. nominal_value (float, optional): The nominal value of the parameter. fittable (bool, optional): Indicates if the parameter is fittable or always fixed. - type (str, optional): The type of the parameter. + ptype (str, optional): The ptype of the parameter. uncertainty (float or str, optional): The uncertainty of the parameter. If a string, it can be evaluated as a numpy or scipy function to define non-gaussian constraints. @@ -27,7 +27,7 @@ def __init__( name: str, nominal_value: Optional[float] = None, fittable: bool = True, - type: Optional[str] = None, + ptype: Optional[str] = None, uncertainty: Optional[float or str] = None, relative_uncertainty: Optional[bool] = None, blueice_anchors: Optional[List] = None, @@ -39,7 +39,7 @@ def __init__( self.name = name self.nominal_value = nominal_value self.fittable = fittable - self.type = type + self.ptype = ptype self._uncertainty = uncertainty self.relative_uncertainty = relative_uncertainty self.blueice_anchors = blueice_anchors diff --git a/alea/statistical_model.py b/alea/statistical_model.py index 9e7d54bc..60768077 100644 --- a/alea/statistical_model.py +++ b/alea/statistical_model.py @@ -285,9 +285,9 @@ def _confidence_interval_checks( "You must set parameter_interval_bounds in the parameter config" " or when calling confidence_interval") - if parameter_of_interest.type == "rate": + if parameter_of_interest.ptype == "rate": try: - if parameter_of_interest.type == "rate" and poi_name.endswith("_rate_multiplier"): + if parameter_of_interest.ptype == "rate" and poi_name.endswith("_rate_multiplier"): source_name = poi_name.replace("_rate_multiplier", "") else: source_name = poi_name From 2752b0f69d96e1ed0376c5cd518614b75a50a061 Mon Sep 17 00:00:00 2001 From: Robert Hammann Date: Fri, 14 Jul 2023 14:35:11 +0200 Subject: [PATCH 5/9] remove my TODOs --- alea/blueice_extended_model.py | 2 +- alea/examples/unbinned_wimp_statistical_model.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index b60a7339..52d592f7 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -121,7 +121,7 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": parameters_to_ignore: List[str] = [p.name for p in self.parameters if (p.ptype == "shape") & (p.name not in source["parameters"])] # no efficiency affects PDF: - parameters_to_ignore += [ p.name for p in self.parameters if (p.ptype == "efficiency")] + parameters_to_ignore += [p.name for p in self.parameters if (p.ptype == "efficiency")] parameters_to_ignore += source.get("extra_dont_hash_settings", []) # ignore all shape parameters known to this model not named specifically in the source: diff --git a/alea/examples/unbinned_wimp_statistical_model.yaml b/alea/examples/unbinned_wimp_statistical_model.yaml index ebc0237e..58b38c2b 100644 --- a/alea/examples/unbinned_wimp_statistical_model.yaml +++ b/alea/examples/unbinned_wimp_statistical_model.yaml @@ -91,7 +91,7 @@ likelihood_config: - signal_efficiency template_filename: wimp50gev_template.h5 apply_efficiency: True - efficiency_name: signal_efficiency # TODO: Check + efficiency_name: signal_efficiency # SR1 - name: sr1 @@ -120,4 +120,4 @@ likelihood_config: - signal_efficiency template_filename: wimp50gev_template.h5 apply_efficiency: True - efficiency_name: signal_efficiency # TODO: Check \ No newline at end of file + efficiency_name: signal_efficiency \ No newline at end of file From 5fadf962de4be74644a5ffd5baec9b1bcf10cb20 Mon Sep 17 00:00:00 2001 From: Knut Dundas Moraa Date: Fri, 14 Jul 2023 10:15:50 -0400 Subject: [PATCH 6/9] Separate out efficiency set fcn --- alea/blueice_extended_model.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index 389fe827..7141560d 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -155,18 +155,7 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": # Set efficiency parameters if source.get("apply_efficiency", False): - assert "efficiency_name" in source, "Unspecified efficiency_name for source {:s}".format(source["name"]) - efficiency_name = source["efficiency_name"] - assert efficiency_name in source["parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) - efficiency_parameter = self.parameters[efficiency_name] - assert efficiency_parameter.type == "efficiency", "The parameter {:s} must" \ - " be an efficiency".format(efficiency_name) - limits = efficiency_parameter.fit_limits - assert 0 <= limits[0], 'Efficiency parameters including {:s} must be' \ - ' constrained to be nonnegative'.format(efficiency_name) - assert np.isfinite(limits[1]), 'Efficiency parameters including {:s} must be' \ - ' constrained to be finite'.format(efficiency_name) - ll.add_shape_parameter(efficiency_name, anchors=(limits[0], limits[1])) + self._set_efficiency(source, ll) ll.prepare() @@ -295,3 +284,18 @@ def _get_constraint_functions(self, **generate_values) -> dict: "Only float uncertainties are supported at the moment.") constraint_functions[name] = func return constraint_functions + + def _set_efficiency(self, source, ll): + assert "efficiency_name" in source, "Unspecified efficiency_name for source {:s}".format(source["name"]) + efficiency_name = source["efficiency_name"] + assert efficiency_name in source[ + "parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) + efficiency_parameter = self.parameters[efficiency_name] + assert efficiency_parameter.type == "efficiency", "The parameter {:s} must" \ + " be an efficiency".format(efficiency_name) + limits = efficiency_parameter.fit_limits + assert 0 <= limits[0], 'Efficiency parameters including {:s} must be' \ + ' constrained to be nonnegative'.format(efficiency_name) + assert np.isfinite(limits[1]), 'Efficiency parameters including {:s} must be' \ + ' constrained to be finite'.format(efficiency_name) + ll.add_shape_parameter(efficiency_name, anchors=(limits[0], limits[1])) From ac602d355da44b285290ff080af1123656f9d4fe Mon Sep 17 00:00:00 2001 From: Knut Dundas Moraa Date: Fri, 14 Jul 2023 11:00:27 -0400 Subject: [PATCH 7/9] Remove errant code in get_expectation_values in base class. --- alea/blueice_extended_model.py | 3 +-- alea/statistical_model.py | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index 7141560d..52745a20 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -80,9 +80,8 @@ def get_expectation_values(self, **kwargs) -> dict: for ll in self._likelihood.likelihood_list[:-1]: # ancillary likelihood does not contribute ll_pars = list(ll.rate_parameters.keys()) + list(ll.shape_parameters.keys()) + ll_pars += ["livetime_days"] call_args = {k:i for k, i in kwargs.items() if k in ll_pars} - if "livetime_days" in kwargs: - call_args["livetime_days"] = kwargs["livetime_days"] mus = ll(full_output=True, **call_args)[1] for n, mu in zip(ll.source_name_list, mus): diff --git a/alea/statistical_model.py b/alea/statistical_model.py index 9e7d54bc..4f13799e 100644 --- a/alea/statistical_model.py +++ b/alea/statistical_model.py @@ -164,7 +164,6 @@ def store_data( toydata_to_file(file_name, data_list, data_name_list, **kw) def get_expectation_values(self, **parameter_values): - mus = self._ll(full_output=True, **parameter_values) return NotImplementedError("get_expectation_values is optional to implement") @property From 0c41d99fbf7b6eb4d0b8ed8d635e07f2b2b4002a Mon Sep 17 00:00:00 2001 From: Knut Dundas Moraa Date: Fri, 14 Jul 2023 11:14:38 -0400 Subject: [PATCH 8/9] linter mess --- alea/blueice_extended_model.py | 37 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index 15716a82..0b22c12b 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -81,7 +81,7 @@ def get_expectation_values(self, **kwargs) -> dict: ll_pars = list(ll.rate_parameters.keys()) + list(ll.shape_parameters.keys()) ll_pars += ["livetime_days"] - call_args = {k:i for k, i in kwargs.items() if k in ll_pars} + call_args = {k: i for k, i in kwargs.items() if k in ll_pars} mus = ll(full_output=True, **call_args)[1] for n, mu in zip(ll.source_name_list, mus): @@ -120,13 +120,13 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": parameters_to_ignore: List[str] = [p.name for p in self.parameters if (p.type == "shape") & (p.name not in source["parameters"])] # no efficiency affects PDF: - parameters_to_ignore += [ p.name for p in self.parameters if (p.type == "efficiency")] + parameters_to_ignore += [p.name for p in self.parameters if (p.type == "efficiency")] parameters_to_ignore += source.get("extra_dont_hash_settings", []) # ignore all shape parameters known to this model not named specifically in the source: blueice_config["sources"][i]["extra_dont_hash_settings"] = parameters_to_ignore - ll = likelihood_object(blueice_config) # noqa: E127 + ll = likelihood_object(blueice_config) for source in config["sources"]: @@ -151,12 +151,10 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": # TODO: Implement setting shape parameters raise NotImplementedError("Shape parameters are not yet supported.") - # Set efficiency parameters if source.get("apply_efficiency", False): self._set_efficiency(source, ll) - ll.prepare() lls.append(ll) @@ -206,6 +204,21 @@ def _generate_ancillary_measurements(self, **generate_values) -> dict: return ancillary_measurements + def _set_efficiency(self, source, ll): + assert "efficiency_name" in source, "Unspecified efficiency_name for source {:s}".format(source["name"]) + efficiency_name = source["efficiency_name"] + assert efficiency_name in source[ + "parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) + efficiency_parameter = self.parameters[efficiency_name] + assert efficiency_parameter.type == "efficiency", "The parameter {:s} must" \ + " be an efficiency".format(efficiency_name) + limits = efficiency_parameter.fit_limits + assert 0 <= limits[0], 'Efficiency parameters including {:s} must be' \ + ' constrained to be nonnegative'.format(efficiency_name) + assert np.isfinite(limits[1]), 'Efficiency parameters including {:s} must be' \ + ' constrained to be finite'.format(efficiency_name) + ll.add_shape_parameter(efficiency_name, anchors=(limits[0], limits[1])) + class CustomAncillaryLikelihood(LogAncillaryLikelihood): """ @@ -284,17 +297,3 @@ def _get_constraint_functions(self, **generate_values) -> dict: constraint_functions[name] = func return constraint_functions - def _set_efficiency(self, source, ll): - assert "efficiency_name" in source, "Unspecified efficiency_name for source {:s}".format(source["name"]) - efficiency_name = source["efficiency_name"] - assert efficiency_name in source[ - "parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) - efficiency_parameter = self.parameters[efficiency_name] - assert efficiency_parameter.type == "efficiency", "The parameter {:s} must" \ - " be an efficiency".format(efficiency_name) - limits = efficiency_parameter.fit_limits - assert 0 <= limits[0], 'Efficiency parameters including {:s} must be' \ - ' constrained to be nonnegative'.format(efficiency_name) - assert np.isfinite(limits[1]), 'Efficiency parameters including {:s} must be' \ - ' constrained to be finite'.format(efficiency_name) - ll.add_shape_parameter(efficiency_name, anchors=(limits[0], limits[1])) From 90c2ca10e930e227b44192e25a3b275921f4bc32 Mon Sep 17 00:00:00 2001 From: Robert Hammann Date: Mon, 17 Jul 2023 09:09:52 +0200 Subject: [PATCH 9/9] .type -> .ptype --- alea/blueice_extended_model.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/alea/blueice_extended_model.py b/alea/blueice_extended_model.py index 0b22c12b..03bf7413 100644 --- a/alea/blueice_extended_model.py +++ b/alea/blueice_extended_model.py @@ -117,10 +117,10 @@ def _build_ll_from_config(self, likelihood_config: dict) -> "LogLikelihoodSum": # add all parameters to extra_dont_hash for each source unless it is used: for i, source in enumerate(config["sources"]): - parameters_to_ignore: List[str] = [p.name for p in self.parameters if (p.type == "shape") + parameters_to_ignore: List[str] = [p.name for p in self.parameters if (p.ptype == "shape") & (p.name not in source["parameters"])] # no efficiency affects PDF: - parameters_to_ignore += [p.name for p in self.parameters if (p.type == "efficiency")] + parameters_to_ignore += [p.name for p in self.parameters if (p.ptype == "efficiency")] parameters_to_ignore += source.get("extra_dont_hash_settings", []) # ignore all shape parameters known to this model not named specifically in the source: @@ -210,8 +210,8 @@ def _set_efficiency(self, source, ll): assert efficiency_name in source[ "parameters"], "The efficiency_name for source {:s} is not in its parameter list".format(source["name"]) efficiency_parameter = self.parameters[efficiency_name] - assert efficiency_parameter.type == "efficiency", "The parameter {:s} must" \ - " be an efficiency".format(efficiency_name) + assert efficiency_parameter.ptype == "efficiency", "The parameter {:s} must" \ + " be an efficiency".format(efficiency_name) limits = efficiency_parameter.fit_limits assert 0 <= limits[0], 'Efficiency parameters including {:s} must be' \ ' constrained to be nonnegative'.format(efficiency_name) @@ -296,4 +296,3 @@ def _get_constraint_functions(self, **generate_values) -> dict: "Only float uncertainties are supported at the moment.") constraint_functions[name] = func return constraint_functions -