Skip to content

Commit

Permalink
update missing channel handle
Browse files Browse the repository at this point in the history
  • Loading branch information
jackaraz committed Jul 15, 2024
1 parent 33dae30 commit e588335
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/spey_pyhf/simplify.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Interface to convert pyhf likelihoods to simplified likelihood framework"""
import copy
import logging
import warnings
from typing import Callable, List, Optional, Text, Union, Literal
from typing import Callable, List, Literal, Optional, Text, Union

import numpy as np
import spey
Expand All @@ -18,6 +19,9 @@ def __dir__():
return []


log = logging.getLogger("Spey")


class ConversionError(Exception):
"""Conversion error class"""

Expand Down Expand Up @@ -175,6 +179,7 @@ def __call__(
}[fittype]

interpreter = WorkspaceInterpreter(bkgonly_model)
bin_map = interpreter.bin_map

# configure signal patch map with respect to channel names
signal_patch_map = interpreter.patch_to_map(signal_patch)
Expand All @@ -190,13 +195,14 @@ def __call__(
)

for channel in interpreter.get_channels(control_region_indices):
interpreter.inject_signal(
channel,
[0.0] * len(signal_patch_map[channel]["data"]),
signal_patch_map[channel]["modifiers"]
if include_modifiers_in_control_model
else None,
)
if channel in signal_patch_map:
interpreter.inject_signal(
channel,
[0.0] * bin_map[channel],
signal_patch_map[channel]["modifiers"]
if include_modifiers_in_control_model
else None,
)

pdf_wrapper = spey.get_backend("pyhf")
control_model = pdf_wrapper(
Expand Down Expand Up @@ -325,7 +331,15 @@ def __call__(
# yields needs to be reordered properly before constructing the simplified likelihood
signal_yields = []
for channel_name in stat_model_pyhf.config.channels:
signal_yields += signal_patch_map[channel_name]["data"]
try:
signal_yields += signal_patch_map[channel_name]["data"]
except KeyError:
log.warning(
f"Channel `{channel_name}` does not exist in the signal patch,"
" yields will be set to zero."
)
signal_yields += [0.0] * bin_map[channel_name]

# NOTE background yields are first moments in simplified framework not the yield values
# in the full statistical model!
background_yields = np.mean(samples, axis=0)
Expand Down

0 comments on commit e588335

Please sign in to comment.