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 withmodifier_settings=hifa_modifier_settings(ws). This reversesfrom_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 intows.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_distmodel_cchannel and observation
c(a name withoutmodel_is kept as is)analysis
mmeasurement
mmodifier on parameter
xmodifier
x: pyhf correlates modifiers by name, HS3 by parameter, so the HS3 modifier name is not usedparameters
x_bin_0, …,one per-bin modifier
x, whose binx_bin_<n-1>iisx[i]A per-bin modifier’s parameter list is one group
xwhen every name in it isxfollowed 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>, asfrom_hifa()writes them;x_bin<i>, as pyhs3 names the parameters of astaterrorthat lists none (staterror_<channel>_bin<i>);x_<i>, as ROOT writes them.
The suffixes are tried in that order, so
x_bin_0is bin 0 ofx, not ofx_bin. One pyhf binx[i]must come from one HS3 parameter whereverxis used. Ashapefactororshapesysmust number its bins from 0. Astaterrormust 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,histosysandshapefactormap to the same pyhf type. Ashapesyswithconstraint: Poissonmaps to pyhfshapesysand one withGaussto a single-sample pyhfstaterror, both withvalsas the absolute uncertainties. A Barlow-Beeston litestaterrormaps to pyhfstaterror, with each sample’sdata.errorsas its uncertainties. Anormfactoronlumiwith agaussian_distonlumiin the likelihood’saux_distributionsmaps to pyhflumi.Measurement config.
poiis the analysis’s one parameter of interest, orpoiwhen given. Initial values andconstflags come from one parameter point:parameter_setwhen given, else the analysis’sinit. An analysis with neither that violates HS3 section 2.7 without a parameter point (ROOT exports, which holdLumiconst only in their parameter points, do) takes the first parameter point in which every parameter outside its domains isconst, asUnlistedParameterErrorsuggests, and logs a warning naming it. Bounds come from the analysis’s domains (intersected when a parameter is in several). The config lists onlyinitsandboundsthat differ from pyhf’s defaults (seefrom_hifa()) andfixed: trueforconstparameters, plus, forlumi, theinits,bounds,auxdataandsigmaspyhf requires (auxdataandsigmasare the constraint’sxandsigma;initsdefaults tox). A parameter missing from the parameter point or the domains takes pyhf’s default. A per-bin parameter isfixedwhen every bin isconst, 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
metadataandmisc,errorson samples without astaterror, 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 andconstflags from, instead of each analysis’sinit.
- Return type:
- Returns:
The pyhf workspace, as a dict.
- Raises:
ValueError – For content pyhf cannot express: a likelihood holding a distribution other than
histfactory_distor data other thanbinned, or data whose bin count differs from its channel’s; an aux distribution other than onegaussian_distonlumiwith numericxandsigma, or a lumi constraint with nonormfactoronlumi; a modifier parameter that is a function; aPoissonorLogNormalconstraint on anormsysorhistosys; aLogNormalshapesys; a non-Gaussor Barlow-Beeston fullstaterror, or litestaterrormodifiers 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 ashapefactorwith different bin counts in different channels; ashapesyswhose 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 partlyconst; or a parameter with an open bound in the analysis’s domains, orlumiwith none. Also for ambiguous input: likelihoods pairing the channels with different data (or differing in their lumi constraint) withoutlikelihood, an unknownlikelihood, two channels with one pyhf name, a likelihood without an analysis, or an analysis with no or several parameters of interest and nopoi(or apoiit does not list). Also for an unknownparameter_set, or an analysis with neitherinitnorparameter_setthat violates HS3 section 2.7 without a parameter point when no parameter point holds every parameter outside its domainsconst.
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