pyhs3.to_hifa

pyhs3.to_hifa(ws, *, likelihood=None, poi=None, parameter_set=None)[source]

Convert an HS3 workspace of HistFactory channels to a pyhf workspace.

The result is a dict valid against pyhf’s schemas/1.0.0/workspace.json; pyhf.Workspace(result) loads it. pyhf’s JSON holds no interpolation codes, so build the model with modifier_settings=hifa_modifier_settings(ws). This reverses from_hifa(): converting a workspace it produced gives back the pyhf workspace, up to measurement-config entries equal to pyhf’s defaults.

What is converted. Without likelihood, every likelihood is converted, which requires them to pair the same channels with the same data; likelihood (a name or an index into ws.likelihoods) picks one. Each channel of the likelihood, in order, becomes a pyhf channel with the likelihood’s data for it as the observation. Each analysis on a converted likelihood becomes a pyhf measurement.

Names

HS3

pyhf

histfactory_dist model_c

channel and observation c (a name without model_ is kept as is)

analysis m

measurement m

modifier on parameter x

modifier x: pyhf correlates modifiers by name, HS3 by parameter, so the HS3 modifier name is not used

parameters x_bin_0, …,

one per-bin modifier x, whose bin

x_bin_<n-1>

i is x[i]

A per-bin modifier’s parameter list is one group x when every name in it is x followed by one of these bin suffixes, the same one for the whole list, with indices that count up by one in list order:

  • x_bin_<i>, as from_hifa() writes them;

  • x_bin<i>, as pyhs3 names the parameters of a staterror that lists none (staterror_<channel>_bin<i>);

  • x_<i>, as ROOT writes them.

The suffixes are tried in that order, so x_bin_0 is bin 0 of x, not of x_bin. One pyhf bin x[i] must come from one HS3 parameter wherever x is used. A shapefactor or shapesys must number its bins from 0. A staterror must number them as pyhf does: over the channels using its name, sorted by channel name, so a channel’s first bin is the number of bins in the channels before it.

Modifiers. normfactor, normsys, histosys and shapefactor map to the same pyhf type. A shapesys with constraint: Poisson maps to pyhf shapesys and one with Gauss to a single-sample pyhf staterror, both with vals as the absolute uncertainties. A Barlow-Beeston lite staterror maps to pyhf staterror, with each sample’s data.errors as its uncertainties. A normfactor on lumi with a gaussian_dist on lumi in the likelihood’s aux_distributions maps to pyhf lumi.

Measurement config. poi is the analysis’s one parameter of interest, or poi when given. Initial values and const flags come from one parameter point: parameter_set when given, else the analysis’s init. An analysis with neither that violates HS3 section 2.7 without a parameter point (ROOT exports, which hold Lumi const only in their parameter points, do) takes the first parameter point in which every parameter outside its domains is const, as UnlistedParameterError suggests, and logs a warning naming it. Bounds come from the analysis’s domains (intersected when a parameter is in several). The config lists only inits and bounds that differ from pyhf’s defaults (see from_hifa()) and fixed: true for const parameters, plus, for lumi, the inits, bounds, auxdata and sigmas pyhf requires (auxdata and sigmas are the constraint’s x and sigma; inits defaults to x). A parameter missing from the parameter point or the domains takes pyhf’s default. A per-bin parameter is fixed when every bin is const, leaving out bins pyhf fixes on its own (no positive yield or no uncertainty).

Multi-dimensional channels are flattened: HS3 already stores a channel’s bins as one flat list, in the same order for the samples and the data, and the HistFactory likelihood is a product over bins, so pyhf’s likelihood equals pyhs3’s. Only the binning is lost, as for one axis.

Dropped, with a debug log message: each channel’s binning, the workspace’s metadata and misc, errors on samples without a staterror, and parameter points other than the ones used.

Parameters:
  • ws (Workspace) – The HS3 workspace.

  • likelihood (str | int | None) – Name or index of the likelihood to convert. Required when the workspace’s likelihoods pair the channels with different data.

  • poi (str | None) – The parameter of interest, for an analysis listing several or none.

  • parameter_set (str | None) – Name of the parameter point to take initial values and const flags from, instead of each analysis’s init.

Return type:

dict[str, Any]

Returns:

The pyhf workspace, as a dict.

Raises:

ValueError – For content pyhf cannot express: a likelihood holding a distribution other than histfactory_dist or data other than binned, or data whose bin count differs from its channel’s; an aux distribution other than one gaussian_dist on lumi with numeric x and sigma, or a lumi constraint with no normfactor on lumi; a modifier parameter that is a function; a Poisson or LogNormal constraint on a normsys or histosys; a LogNormal shapesys; a non-Gauss or Barlow-Beeston full staterror, or lite staterror modifiers on different parameters in one channel; per-bin parameters that are not one group (see above), not numbered as pyhf numbers them, or naming one pyhf bin with two HS3 parameters; per-bin parameter lists that only partly overlap, including a shapefactor with different bin counts in different channels; a shapesys whose parameters sit on more than one sample or channel; one HS3 parameter that would be two pyhf parameters; one pyhf name used by modifier types with different pyhf defaults; one modifier type with mixed interpolation codes; a per-bin parameter only partly const; or a parameter with an open bound in the analysis’s domains, or lumi with none. Also for ambiguous input: likelihoods pairing the channels with different data (or differing in their lumi constraint) without likelihood, an unknown likelihood, two channels with one pyhf name, a likelihood without an analysis, or an analysis with no or several parameters of interest and no poi (or a poi it does not list). Also for an unknown parameter_set, or an analysis with neither init nor parameter_set that violates HS3 section 2.7 without a parameter point when no parameter point holds every parameter outside its domains const.

Examples

>>> import pyhs3
>>> spec = {
...     "channels": [
...         {
...             "name": "SR",
...             "samples": [
...                 {
...                     "name": "signal",
...                     "data": [5.0, 10.0],
...                     "modifiers": [
...                         {"name": "mu", "type": "normfactor", "data": None}
...                     ],
...                 }
...             ],
...         }
...     ],
...     "observations": [{"name": "SR", "data": [6.0, 9.0]}],
...     "measurements": [
...         {
...             "name": "meas",
...             "config": {
...                 "poi": "mu",
...                 "parameters": [{"name": "mu", "bounds": [[0.0, 5.0]]}],
...             },
...         }
...     ],
...     "version": "1.0.0",
... }
>>> pyhs3.to_hifa(pyhs3.from_hifa(spec)) == spec
True