From 781325380b2b932552a51d7eab1ec5e721ea7503 Mon Sep 17 00:00:00 2001 From: Alexander Held Date: Tue, 20 Dec 2022 14:06:37 +0100 Subject: [PATCH 1/2] support models with custom modifiers --- src/cabinetry/model_utils.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/cabinetry/model_utils.py b/src/cabinetry/model_utils.py index ec6533f8..493029be 100644 --- a/src/cabinetry/model_utils.py +++ b/src/cabinetry/model_utils.py @@ -41,7 +41,13 @@ class ModelPrediction(NamedTuple): def model_and_data( - spec: Dict[str, Any], *, asimov: bool = False, include_auxdata: bool = True + spec: Dict[str, Any], + *, + asimov: bool = False, + include_auxdata: bool = True, + validate: bool = True, + poi_name: Optional[str] = None, + modifier_set: Optional[Dict[str, Tuple]] = None, ) -> Tuple[pyhf.pdf.Model, List[float]]: """Returns model and data for a ``pyhf`` workspace specification. @@ -50,18 +56,27 @@ def model_and_data( asimov (bool, optional): whether to return the Asimov dataset, defaults to False include_auxdata (bool, optional): whether to also return auxdata, defaults to True + validate (bool, optional): whether to validate the workspace and model against + the respective JSON schema, defaults to True + poi_name (Optional[str], optional): name of POI to set for model, defaults to + None (then use POI as given in measurement specification) + modifier_set (Optional[Dict[str, Tuple]], optional): additional custom modifiers + to support, defaults to None (no custom modifiers) Returns: Tuple[pyhf.pdf.Model, List[float]]: - a HistFactory-style model in ``pyhf`` format - the data (plus auxdata if requested) for the model """ - workspace = pyhf.Workspace(spec) + workspace = pyhf.Workspace(spec, validate=validate) model = workspace.model( + validate=validate, + poi_name=poi_name, + modifier_set=modifier_set, modifier_settings={ "normsys": {"interpcode": "code4"}, "histosys": {"interpcode": "code4p"}, - } + }, ) # use HistFactory InterpCode=4 (default in pyhf since v0.6.0) if not asimov: data = workspace.data(model, include_auxdata=include_auxdata) From 2decbe8e2a4726fd4554b08fe734031a8dbef665 Mon Sep 17 00:00:00 2001 From: Alexander Held Date: Tue, 20 Dec 2022 14:19:50 +0100 Subject: [PATCH 2/2] handle poi_name=None --- src/cabinetry/model_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cabinetry/model_utils.py b/src/cabinetry/model_utils.py index 493029be..984bc757 100644 --- a/src/cabinetry/model_utils.py +++ b/src/cabinetry/model_utils.py @@ -69,14 +69,15 @@ def model_and_data( - the data (plus auxdata if requested) for the model """ workspace = pyhf.Workspace(spec, validate=validate) + poi_name_kwarg = {"poi_name": poi_name} if poi_name is not None else {} model = workspace.model( validate=validate, - poi_name=poi_name, modifier_set=modifier_set, modifier_settings={ "normsys": {"interpcode": "code4"}, "histosys": {"interpcode": "code4p"}, }, + **poi_name_kwarg, ) # use HistFactory InterpCode=4 (default in pyhf since v0.6.0) if not asimov: data = workspace.data(model, include_auxdata=include_auxdata)