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: skip histosys modifier creation for single-bin regions #398

Merged
merged 3 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions src/cabinetry/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def normplusshape_modifiers(
provides the `histosys` and `normsys` modifiers for ``pyhf`` (in `HistFactory`
language, this corresponds to a `HistoSys` and an `OverallSys`). Symmetrization
could happen either at this stage (this is the case currently), or somewhere
earlier, such as during template postprocessing.
earlier, such as during template postprocessing. A `histosys` modifier is not
created for single-bin channels, as it has no effect in this case (everything is
handled by the `normsys` modifier already).

Args:
region (Dict[str, Any]): region the systematic variation acts in
Expand Down Expand Up @@ -200,13 +202,15 @@ def normplusshape_modifiers(
modifiers.append(norm_modifier)

# add the shape part in a histosys
shape_modifier = {}
shape_modifier.update({"name": modifier_name})
shape_modifier.update({"type": "histosys"})
shape_modifier.update(
{"data": {"hi_data": histo_yield_up, "lo_data": histo_yield_down}}
)
modifiers.append(shape_modifier)
if len(histogram_nominal.yields) > 1:
# only relevant if there is more than one bin, otherwise there is no "shape"
shape_modifier = {}
shape_modifier.update({"name": modifier_name})
shape_modifier.update({"type": "histosys"})
shape_modifier.update(
{"data": {"hi_data": histo_yield_up, "lo_data": histo_yield_down}}
)
modifiers.append(shape_modifier)
return modifiers

def sys_modifiers(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ def test_WorkspaceBuilder_normalization_modifier():
# for test of symmetrization: up and nominal
histo.Histogram.from_arrays([0, 1, 2], [26.0, 24.0], [0.1, 0.1]),
histo.Histogram.from_arrays([0, 1, 2], [20.0, 20.0], [0.1, 0.1]),
# single bin for test of histosys being skipped (up and nominal)
histo.Histogram.from_arrays([0, 1], [26.0], [0.1]),
histo.Histogram.from_arrays([0, 1], [20.0], [0.1]),
],
)
def test_WorkspaceBuilder_normplusshape_modifiers(mock_histogram):
Expand Down Expand Up @@ -194,6 +197,12 @@ def test_WorkspaceBuilder_normplusshape_modifiers(mock_histogram):
((pathlib.Path("path"), region, sample, {}), {"modified": True}),
]

# single bin, causing histosys being skipped
modifiers = ws_builder.normplusshape_modifiers(region, sample, systematic)
assert modifiers == [
{"name": "mod_name", "type": "normsys", "data": {"hi": 1.3, "lo": 0.7}},
]


@mock.patch(
"cabinetry.workspace.WorkspaceBuilder.normplusshape_modifiers",
Expand Down