pyhs3.from_hifa

pyhs3.from_hifa(spec, *, modifier_settings=None)[source]

Convert a pyhf workspace (HistFactory JSON) to an HS3 workspace.

The input is a workspace following pyhf’s schemas/1.0.0/workspace.json, with any patches already applied (a pyhf.Workspace works, since it is a dict). The result goes through all of Workspace’s validation.

Every pyhf modifier type is supported: normfactor, normsys, histosys, shapefactor, shapesys, staterror and lumi.

Names of the generated HS3 objects

pyhf

HS3

channel c with n bins

histfactory_dist model_c with axis obs_x_c (min=0, max=n, nbins=n)

observation of channel c

binned data obsData_c on axis obs_x_c

measurement m

likelihood, analysis, product_domain and parameter point, all named m

scalar modifier x

parameter x (no alpha_ prefix)

per-bin modifier x

parameters x_bin_0, x_bin_1, … (pyhf’s x[i], which ROOT rejects as a name)

lumi in measurement m

gaussian_dist lumi_constraint_m, in likelihood m’s aux_distributions

A shapefactor or shapesys numbers the bins of its own channel. A staterror numbers them as pyhf does: over the channels using its name, sorted by channel name, so x_bin_i is pyhf’s x[i]. Its parameters stay per channel.

Each likelihood pairs every channel’s distribution with its data, in the input’s channel order. An observation whose name matches no channel is ignored, as pyhf ignores it, and logged at debug level: a patch that removes a channel can leave its observation behind. Each analysis has init and its one domain set to m, and parameters_of_interest set to the measurement’s poi, copied as given even when no modifier uses it. Parameter point m and domain m list every parameter of the model, sorted by pyhf name, with per-bin parameters in bin order.

Parameter defaults (pyhf’s), which the measurement config’s inits, bounds and fixed override; a fixed parameter is const:

modifier

init

bounds

constraint

normfactor

1

[0, 10]

none

normsys, histosys

0

[-5, 5]

Gauss (aux 0, width 1)

shapefactor

1

[0, 10]

none

shapesys

1

[1e-10, 10]

Poisson, tau = (nu/delta)^2

staterror

1

[1e-10, 10]

Gauss, width sqrt(sum delta^2) / sum nu

lumi

none

none

Gauss, config auxdata and sigmas

For a per-bin parameter, inits and bounds hold one entry per bin and fixed applies to every bin. Config entries naming a parameter the model does not use are ignored, as pyhf ignores them.

Per-bin modifiers. A shapefactor maps to HS3 shapefactor, and bin i is shared wherever its name appears, as in pyhf. A shapesys maps to HS3 shapesys with constraint: Poisson and its absolute uncertainties as vals. A staterror maps to Barlow-Beeston lite: an HS3 staterror with constraint: Gauss on each sample carrying it, whose uncertainties go in that sample’s data.errors; samples without it get no errors. pyhs3’s lite mode holds one staterror group per channel: the group on more than one sample if there is one, else the first to appear. Any other group in the channel sits on a single sample and maps to a shapesys with constraint: Gauss and vals = abs(delta), which gives the same width delta / nu.

A shapesys bin with no positive yield or no positive uncertainty, and a staterror bin with no positive summed yield or a combined uncertainty of 0, has no constraint term in pyhs3. pyhf fixes such a bin, so its parameter is const at its initial value (1 unless inits says otherwise).

lumi. pyhf requires inits, bounds, auxdata and sigmas (one value each) for lumi in every measurement. lumi maps to a normfactor on parameter lumi plus, per measurement m, a gaussian_dist with x = auxdata, mean = lumi and sigma = sigmas. A fixed lumi is const and keeps its constraint, which pyhf still evaluates at the fixed value.

Interpolation. Each normsys and histosys modifier records its code in data.interpolation. The defaults are pyhs3’s (and ROOT’s): code4 for normsys and code4p for histosys. modifier_settings overrides them per modifier type, in pyhf’s shape, e.g. {"normsys": {"interpcode": "code1"}}; a type it leaves out keeps pyhs3’s default. Accepted codes are code1 and code4 for normsys, and code0, code2 and code4p for histosys.

Parameters:
  • spec (Mapping[str, Any]) – The pyhf workspace.

  • modifier_settings (Mapping[str, Mapping[str, str]] | None) – Interpolation code per modifier type, as passed to pyhf.Workspace.model(modifier_settings=...).

Return type:

Workspace

Returns:

The HS3 workspace.

Raises:
  • pydantic_core.ValidationError – If spec does not follow pyhf’s schema, or modifier_settings names an unknown modifier type, key or code.

  • ValueError – For duplicate channel, observation, measurement or config-parameter names; a channel without an observation; bin counts that disagree within a channel, including a histosys, shapesys or staterror’s data; one name used by modifier types with different pyhf defaults, such as normfactor with normsys or shapefactor, shapesys with staterror, and lumi with any other type; a shapefactor on channels with different bin counts; a shapesys name used on more than one sample or channel, whose constraint pyhs3 would emit once per use; two staterror groups in one channel that each sit on more than one sample; a sample carrying a staterror in only some of the channels using its name, which pyhf rejects; config inits or bounds whose length is not the parameter’s number of bins (1 for a scalar); config fixed: false on a per-bin parameter with a bin that has no constraint term, which pyhf would free under a dummy constraint; config auxdata, sigmas or factors a modifier type does not use (all three on normfactor and shapefactor, factors on normsys, histosys and staterror, sigmas on shapesys); and config auxdata, sigmas or factors other than the values pyhf derives, within a relative tolerance of 1e-9, which HS3 v0.2 cannot express: [0] and [1] for normsys/histosys, tau (1 for a bin with no constraint term) for shapesys, and auxdata 1 and the width (1 for a bin with no constraint term) for staterror; a lumi whose config lacks inits, bounds, auxdata or sigmas, sets more than one value for any of them, sets factors, or sets a non-positive sigmas.

Examples

>>> import pyhs3
>>> spec = {
...     "channels": [
...         {
...             "name": "SR",
...             "samples": [
...                 {
...                     "name": "signal",
...                     "data": [5.0, 10.0],
...                     "modifiers": [
...                         {"name": "mu", "type": "normfactor", "data": None}
...                     ],
...                 },
...                 {
...                     "name": "background",
...                     "data": [50.0, 60.0],
...                     "modifiers": [
...                         {
...                             "name": "bkg_norm",
...                             "type": "normsys",
...                             "data": {"hi": 1.1, "lo": 0.9},
...                         }
...                     ],
...                 },
...             ],
...         }
...     ],
...     "observations": [{"name": "SR", "data": [53.0, 65.0]}],
...     "measurements": [
...         {"name": "meas", "config": {"poi": "mu", "parameters": []}}
...     ],
...     "version": "1.0.0",
... }
>>> ws = pyhs3.from_hifa(spec)
>>> [p.name for p in ws.parameter_points["meas"]]
['bkg_norm', 'mu']
>>> ws.domains["meas"]["mu"]
(0.0, 10.0)