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

feat: support models with custom modifiers #385

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
22 changes: 19 additions & 3 deletions src/cabinetry/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -50,18 +56,28 @@ 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)
poi_name_kwarg = {"poi_name": poi_name} if poi_name is not None else {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poi_name=None in pyhf.Model means "no POI", while in model_and_data it is now used as the default saying "use the workspace default". Should perhaps use an object() sentinel instead of None.

model = workspace.model(
validate=validate,
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)
Expand Down
Loading