Skip to content

Commit 5bf3242

Browse files
authored
feat: Prune/Rename raise errors if item name not in workspace (#1050)
* prune/rename raise exceptions if a supplied name for channel, sample, measurement, modifier, or modifier type is not in the workspace * Add tests for raised exceptions
1 parent e3f50bb commit 5bf3242

File tree

2 files changed

+143
-144
lines changed

2 files changed

+143
-144
lines changed

src/pyhf/workspace.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ def _prune_and_rename(
473473
Returns:
474474
~pyhf.workspace.Workspace: A new workspace object with the specified components removed or renamed
475475
476+
Raises:
477+
~pyhf.exceptions.InvalidWorkspaceOperation: An item name to prune or rename does not exist in the workspace.
478+
476479
"""
477480
# avoid mutable defaults
478481
prune_modifiers = [] if prune_modifiers is None else prune_modifiers
@@ -487,6 +490,36 @@ def _prune_and_rename(
487490
rename_channels = {} if rename_channels is None else rename_channels
488491
rename_measurements = {} if rename_measurements is None else rename_measurements
489492

493+
for modifier_type in prune_modifier_types:
494+
if modifier_type not in dict(self.modifiers).values():
495+
raise exceptions.InvalidWorkspaceOperation(
496+
f"{modifier_type} is not one of the modifier types in this workspace."
497+
)
498+
499+
for modifier_name in (*prune_modifiers, *rename_modifiers.keys()):
500+
if modifier_name not in dict(self.modifiers):
501+
raise exceptions.InvalidWorkspaceOperation(
502+
f"{modifier_name} is not one of the modifiers in this workspace."
503+
)
504+
505+
for sample_name in (*prune_samples, *rename_samples.keys()):
506+
if sample_name not in self.samples:
507+
raise exceptions.InvalidWorkspaceOperation(
508+
f"{sample_name} is not one of the samples in this workspace."
509+
)
510+
511+
for channel_name in (*prune_channels, *rename_channels.keys()):
512+
if channel_name not in self.channels:
513+
raise exceptions.InvalidWorkspaceOperation(
514+
f"{channel_name} is not one of the channels in this workspace."
515+
)
516+
517+
for measurement_name in (*prune_measurements, *rename_measurements.keys()):
518+
if measurement_name not in self.measurement_names:
519+
raise exceptions.InvalidWorkspaceOperation(
520+
f"{measurement_name} is not one of the measurements in this workspace."
521+
)
522+
490523
newspec = {
491524
'channels': [
492525
{
@@ -573,6 +606,9 @@ def prune(
573606
Returns:
574607
~pyhf.workspace.Workspace: A new workspace object with the specified components removed
575608
609+
Raises:
610+
~pyhf.exceptions.InvalidWorkspaceOperation: An item name to prune does not exist in the workspace.
611+
576612
"""
577613
# avoid mutable defaults
578614
modifiers = [] if modifiers is None else modifiers
@@ -605,6 +641,9 @@ def rename(self, modifiers=None, samples=None, channels=None, measurements=None)
605641
Returns:
606642
~pyhf.workspace.Workspace: A new workspace object with the specified components renamed
607643
644+
Raises:
645+
~pyhf.exceptions.InvalidWorkspaceOperation: An item name to rename does not exist in the workspace.
646+
608647
"""
609648
# avoid mutable defaults
610649
modifiers = {} if modifiers is None else modifiers

0 commit comments

Comments
 (0)