From 4b66cd2cfa0bd4bb53b14c525118d86567be8257 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 5 Aug 2021 07:38:35 -0500 Subject: [PATCH] feat: Sort `_ChannelSummaryMixin.channel_nbins` by keys to match `channels` order (#1546) * Sort _ChannelSummaryMixin.channel_nbins dict by keys to match the order of _ChannelSummaryMixin.channels * Add a test for the order of channel_nbins keys matching the order of channels entries --- src/pyhf/mixins.py | 3 +++ tests/test_mixins.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/pyhf/mixins.py b/src/pyhf/mixins.py index 01f3fc4265..077395fa14 100644 --- a/src/pyhf/mixins.py +++ b/src/pyhf/mixins.py @@ -42,6 +42,9 @@ def __init__(self, *args, **kwargs): self.samples = sorted(list(set(self.samples))) self.parameters = sorted(list(set(self.parameters))) self.modifiers = sorted(list(set(self.modifiers))) + self.channel_nbins = { + channel: self.channel_nbins[channel] for channel in self.channels + } self.channel_slices = {} begin = 0 diff --git a/tests/test_mixins.py b/tests/test_mixins.py index e0213c23ea..7fc4eae690 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -45,3 +45,12 @@ def test_channel_summary_mixin_empty(): assert mixin.modifiers == [] assert mixin.parameters == [] assert mixin.samples == [] + + +def test_channel_nbins_sorted_as_channels(spec): + assert "channels" in spec + spec["channels"].append(spec["channels"][0].copy()) + spec["channels"][-1]["name"] = "a_make_first_in_sort_channel2" + mixin = pyhf.mixins._ChannelSummaryMixin(channels=spec["channels"]) + assert mixin.channels == ["a_make_first_in_sort_channel2", "channel1"] + assert list(mixin.channel_nbins.keys()) == mixin.channels