Skip to content

Commit

Permalink
set as @Property to show up in docs and type it up
Browse files Browse the repository at this point in the history
  • Loading branch information
kratsg authored and matthewfeickert committed Sep 3, 2022
1 parent 3326029 commit 51bd4c4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ module = [
'pyhf.compat',
'pyhf.events',
'pyhf.utils',
'pyhf.mixins',
'pyhf.constraints',
'pyhf.pdf',
'pyhf.simplemodels',
Expand Down
74 changes: 56 additions & 18 deletions src/pyhf/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations
import logging
from typing import Sequence, Any
from pyhf.typing import Channel

log = logging.getLogger(__name__)

Expand All @@ -13,39 +16,74 @@ class _ChannelSummaryMixin:
**channels: A list of channels to provide summary information about. Follows the `defs.json#/definitions/channel` schema.
"""

def __init__(self, *args, **kwargs):
def __init__(self, *args: Any, **kwargs: Sequence[Channel]):
channels = kwargs.pop('channels')
super().__init__(*args, **kwargs)
self.channels = []
self.samples = []
self.modifiers = []
self._channels: list[str] = []
self._samples: list[str] = []
self._modifiers: list[tuple[str, str]] = []
# keep track of the width of each channel (how many bins)
self.channel_nbins = {}
self._channel_nbins: dict[str, int] = {}
# need to keep track in which order we added the constraints
# so that we can generate correctly-ordered data
for channel in channels:
self.channels.append(channel['name'])
self.channel_nbins[channel['name']] = len(channel['samples'][0]['data'])
self._channels.append(channel['name'])
self._channel_nbins[channel['name']] = len(channel['samples'][0]['data'])
for sample in channel['samples']:
self.samples.append(sample['name'])
self._samples.append(sample['name'])
for modifier_def in sample['modifiers']:
self.modifiers.append(
self._modifiers.append(
(
modifier_def['name'], # mod name
modifier_def['type'], # mod type
)
)

self.channels = sorted(list(set(self.channels)))
self.samples = sorted(list(set(self.samples)))
self.modifiers = sorted(list(set(self.modifiers)))
self.channel_nbins = {
channel: self.channel_nbins[channel] for channel in self.channels
self._channels = sorted(list(set(self._channels)))
self._samples = sorted(list(set(self._samples)))
self._modifiers = sorted(list(set(self._modifiers)))
self._channel_nbins = {
channel: self._channel_nbins[channel] for channel in self._channels
}

self.channel_slices = {}
self._channel_slices = {}
begin = 0
for c in self.channels:
end = begin + self.channel_nbins[c]
self.channel_slices[c] = slice(begin, end)
for c in self._channels:
end = begin + self._channel_nbins[c]
self._channel_slices[c] = slice(begin, end)
begin = end

@property
def channels(self) -> list[str]:
"""
Ordered list of channel names in the model.
"""
return self._channels

@property
def samples(self) -> list[str]:
"""
Ordered list of sample names in the model.
"""
return self._samples

@property
def modifiers(self) -> list[tuple[str, str]]:
"""
Ordered list of pairs of modifier name/type in the model.
"""
return self._modifiers

@property
def channel_nbins(self) -> dict[str, int]:
"""
Dictionary mapping channel name to number of bins in the channel.
"""
return self._channel_nbins

@property
def channel_slices(self) -> dict[str, slice]:
"""
Dictionary mapping channel name to the bin slices in the model.
"""
return self._channel_slices

0 comments on commit 51bd4c4

Please sign in to comment.