PatchSet#

class pyhf.patchset.PatchSet(spec, **config_kwargs)[source]#

Bases: object

A way to store a collection of patches (Patch).

It contains metadata about the PatchSet itself:

  • a high-level description of what the patches represent or the analysis it is for

  • a list of references where the patchset is sourced from (e.g. hepdata)

  • a list of digests corresponding to the background-only workspace the patchset was made for

  • the labels of the dimensions of the phase-space for what the patches cover

In addition to the above metadata, the PatchSet object behaves like a:

  • smart list allowing you to iterate over all the patches defined

  • smart dictionary allowing you to access a patch by the patch name or the patch values

The below example shows various ways one can interact with a PatchSet object.

Example

>>> import pyhf
>>> patchset = pyhf.PatchSet({
...     "metadata": {
...         "references": { "hepdata": "ins1234567" },
...         "description": "example patchset",
...         "digests": { "md5": "098f6bcd4621d373cade4e832627b4f6" },
...         "labels": ["x", "y"]
...     },
...     "patches": [
...         {
...             "metadata": {
...                 "name": "patch_name_for_2100x_800y",
...                 "values": [2100, 800]
...             },
...             "patch": [
...                 {
...                     "op": "add",
...                     "path": "/foo/0/bar",
...                     "value": {
...                         "foo": [1.0]
...                     }
...                 }
...             ]
...         }
...     ],
...     "version": "1.0.0"
... })
...
>>> patchset.version
'1.0.0'
>>> patchset.references
{'hepdata': 'ins1234567'}
>>> patchset.description
'example patchset'
>>> patchset.digests
{'md5': '098f6bcd4621d373cade4e832627b4f6'}
>>> patchset.labels
['x', 'y']
>>> patchset.patches
[<pyhf.patchset.Patch object 'patch_name_for_2100x_800y(2100, 800)' at 0x...>]
>>> patchset['patch_name_for_2100x_800y']
<pyhf.patchset.Patch object 'patch_name_for_2100x_800y(2100, 800)' at 0x...>
>>> patchset[(2100,800)]
<pyhf.patchset.Patch object 'patch_name_for_2100x_800y(2100, 800)' at 0x...>
>>> patchset[[2100,800]]
<pyhf.patchset.Patch object 'patch_name_for_2100x_800y(2100, 800)' at 0x...>
>>> patchset[2100,800]
<pyhf.patchset.Patch object 'patch_name_for_2100x_800y(2100, 800)' at 0x...>
>>> for patch in patchset:
...     print(patch.name)
...
patch_name_for_2100x_800y
>>> len(patchset)
1
__init__(spec, **config_kwargs)[source]#

Construct a PatchSet.

Parameters:
  • spec (jsonable) – The patchset JSON specification

  • config_kwargs – Possible keyword arguments for the patchset validation

Returns:

The PatchSet instance.

Return type:

patchset (PatchSet)

Attributes

description#

The description in the PatchSet metadata

digests#

The digests in the PatchSet metadata

labels#

The labels in the PatchSet metadata

metadata#

The metadata of the PatchSet

patches#

The patches in the PatchSet

references#

The references in the PatchSet metadata

version#

The version of the PatchSet

Methods

apply(spec, key)[source]#

Apply the patch associated with the key to the background-only workspace specificatiom.

Parameters:
  • spec (Workspace) – The workspace specification to verify the patchset against.

  • key (str or tuple of int/float) – The key to look up the associated patch - either a name or a set of values.

Raises:
Returns:

The background-only workspace with the patch applied.

Return type:

workspace (Workspace)

verify(spec)[source]#

Verify the patchset digests against a background-only workspace specification. Verified if no exception was raised.

Parameters:

spec (Workspace) – The workspace specification to verify the patchset against.

Raises:

PatchSetVerificationError – if the patchset cannot be verified against the workspace specification

Returns:

None