From 38ac6d0832c7a1074693e10690f8bc864631617d Mon Sep 17 00:00:00 2001 From: Lorenz Gaertner Date: Thu, 30 Nov 2023 10:59:41 +0100 Subject: [PATCH 1/5] Allow skipping of validation when combining workspaces --- src/pyhf/workspace.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pyhf/workspace.py b/src/pyhf/workspace.py index 00abcf77f4..67a3ecc680 100644 --- a/src/pyhf/workspace.py +++ b/src/pyhf/workspace.py @@ -706,7 +706,9 @@ def rename(self, modifiers=None, samples=None, channels=None, measurements=None) ) @classmethod - def combine(cls, left, right, join='none', merge_channels=False): + def combine( + cls, left, right, join='none', merge_channels=False, validate: bool = True + ): """ Return a new workspace specification that is the combination of the two workspaces. @@ -733,6 +735,7 @@ def combine(cls, left, right, join='none', merge_channels=False): right (~pyhf.workspace.Workspace): Another workspace join (:obj:`str`): How to join the two workspaces. Pick from "none", "outer", "left outer", or "right outer". merge_channels (:obj:`bool`): Whether or not to merge channels when performing the combine. This is only done with "outer", "left outer", and "right outer" options. + validate (:obj:`bool`): Whether to validate against a JSON schema Returns: ~pyhf.workspace.Workspace: A new combined workspace object @@ -770,7 +773,7 @@ def combine(cls, left, right, join='none', merge_channels=False): 'observations': new_observations, 'version': new_version, } - return cls(newspec) + return cls(newspec, validate=validate) @classmethod def sorted(cls, workspace): From 58c63ad8e12455d29cba6a3b49971b103ce87b6c Mon Sep 17 00:00:00 2001 From: Lorenz Gaertner Date: Mon, 4 Dec 2023 10:00:58 +0100 Subject: [PATCH 2/5] add to contributors --- docs/contributors.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/contributors.rst b/docs/contributors.rst index c2d0b0a698..d29fd0974b 100644 --- a/docs/contributors.rst +++ b/docs/contributors.rst @@ -33,3 +33,4 @@ Contributors include: - Beojan Stanislaus - Daniel Werner - Jonas Rembser +- Lorenz Gaertner From 28b4152705ce81699ac9cc12397bb2a3d9101a58 Mon Sep 17 00:00:00 2001 From: Lorenz Gaertner Date: Thu, 7 Dec 2023 13:56:14 +0100 Subject: [PATCH 3/5] add test --- tests/test_workspace.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 55ee0df046..303a7ea735 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -791,6 +791,28 @@ def test_combine_workspace(workspace_factory, join): ) +@pytest.mark.parametrize("join", pyhf.Workspace.valid_joins) +def test_combine_workspace_without_validation(mocker, workspace_factory, join): + ws = workspace_factory() + new_ws = ws.rename( + channels={channel: f'renamed_{channel}' for channel in ws.channels}, + samples={sample: f'renamed_{sample}' for sample in ws.samples}, + modifiers={ + modifier: f'renamed_{modifier}' + for modifier, _ in ws.modifiers + if not modifier == 'lumi' + }, + measurements={ + measurement: f'renamed_{measurement}' + for measurement in ws.measurement_names + }, + ) + + mocker.patch('pyhf.schema.validate') + pyhf.Workspace.combine(ws, new_ws, join=join, validate=False) + assert pyhf.schema.validate.called is False + + def test_workspace_equality(workspace_factory): ws = workspace_factory() ws_other = workspace_factory() From 4cf639c22fdc82811def665bf6fef49c2755fcc0 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 7 Dec 2023 14:35:13 +0100 Subject: [PATCH 4/5] Apply style changes --- src/pyhf/workspace.py | 2 +- tests/test_workspace.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pyhf/workspace.py b/src/pyhf/workspace.py index 67a3ecc680..7ce0bc486d 100644 --- a/src/pyhf/workspace.py +++ b/src/pyhf/workspace.py @@ -735,7 +735,7 @@ def combine( right (~pyhf.workspace.Workspace): Another workspace join (:obj:`str`): How to join the two workspaces. Pick from "none", "outer", "left outer", or "right outer". merge_channels (:obj:`bool`): Whether or not to merge channels when performing the combine. This is only done with "outer", "left outer", and "right outer" options. - validate (:obj:`bool`): Whether to validate against a JSON schema + validate (:obj:`bool`): Whether to validate against a JSON schema. Returns: ~pyhf.workspace.Workspace: A new combined workspace object diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 303a7ea735..c6c51a1b72 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -795,20 +795,20 @@ def test_combine_workspace(workspace_factory, join): def test_combine_workspace_without_validation(mocker, workspace_factory, join): ws = workspace_factory() new_ws = ws.rename( - channels={channel: f'renamed_{channel}' for channel in ws.channels}, - samples={sample: f'renamed_{sample}' for sample in ws.samples}, + channels={channel: f"renamed_{channel}" for channel in ws.channels}, + samples={sample: f"renamed_{sample}" for sample in ws.samples}, modifiers={ - modifier: f'renamed_{modifier}' + modifier: f"renamed_{modifier}" for modifier, _ in ws.modifiers - if not modifier == 'lumi' + if not modifier == "lumi" }, measurements={ - measurement: f'renamed_{measurement}' + measurement: f"renamed_{measurement}" for measurement in ws.measurement_names }, ) - mocker.patch('pyhf.schema.validate') + mocker.patch("pyhf.schema.validate") pyhf.Workspace.combine(ws, new_ws, join=join, validate=False) assert pyhf.schema.validate.called is False From 3cafa606e4a00e436e0856010475e0b400e1e095 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 7 Dec 2023 14:35:52 +0100 Subject: [PATCH 5/5] apply logic simplification as recommended by sourcery plugin --- tests/test_workspace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_workspace.py b/tests/test_workspace.py index c6c51a1b72..e966d6c3f1 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -800,7 +800,7 @@ def test_combine_workspace_without_validation(mocker, workspace_factory, join): modifiers={ modifier: f"renamed_{modifier}" for modifier, _ in ws.modifiers - if not modifier == "lumi" + if modifier != "lumi" }, measurements={ measurement: f"renamed_{measurement}"