Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: writexml handles missing parameter configs for normfactor #1819

Merged
merged 5 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pyhf/writexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def build_modifier(spec, modifierspec, channelname, samplename, sampledata):
high = 10
for p in spec['measurements'][0]['config']['parameters']:
if p['name'] == modifierspec['name']:
val = p['inits'][0]
low, high = p['bounds'][0]
val = p.get('inits', [val])[0]
low, high = p.get('bounds', [[low, high]])[0]
attrs['Val'] = str(val)
attrs['Low'] = str(low)
attrs['High'] = str(high)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,30 @@ def test_integer_data(datadir, mocker):

channel = pyhf.writexml.build_channel(spec, channel_spec, {})
assert channel


@pytest.mark.parametrize(
"fname,val,low,high",
[
('workspace_no_parameter_inits.json', '1', '-5', '5'),
('workspace_no_parameter_bounds.json', '5', '0', '10'),
],
ids=['no_inits', 'no_bounds'],
)
def test_issue1814(datadir, mocker, fname, val, low, high):
with open(datadir / fname) as spec_file:
spec = json.load(spec_file)

modifierspec = {'data': None, 'name': 'mu_sig', 'type': 'normfactor'}
channelname = None
samplename = None
sampledata = None

modifier = pyhf.writexml.build_modifier(
spec, modifierspec, channelname, samplename, sampledata
)
assert modifier is not None
assert sorted(modifier.keys()) == ['High', 'Low', 'Name', 'Val']
assert modifier.get('Val') == val
assert modifier.get('Low') == low
assert modifier.get('High') == high
37 changes: 37 additions & 0 deletions tests/test_export/workspace_no_parameter_bounds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"channels": [
{
"name": "ch",
"samples": [
{
"data": [1000.0],
"modifiers": [
{"data": null, "name": "mu_sig", "type": "normfactor"},
{
"data": {"hi": 1.5, "lo": 0.5},
"name": "unc",
"type": "normsys"
}
],
"name": "signal"
}
]
}
],
"measurements": [
{
"config": {
"parameters": [
{
"name": "mu_sig",
"inits": [5]
}
],
"poi": "mu_sig"
},
"name": "meas"
}
],
"observations": [{"data": [1000], "name": "ch"}],
"version": "1.0.0"
}
37 changes: 37 additions & 0 deletions tests/test_export/workspace_no_parameter_inits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"channels": [
{
"name": "ch",
"samples": [
{
"data": [1000.0],
"modifiers": [
{"data": null, "name": "mu_sig", "type": "normfactor"},
{
"data": {"hi": 1.5, "lo": 0.5},
"name": "unc",
"type": "normsys"
}
],
"name": "signal"
}
]
}
],
"measurements": [
{
"config": {
"parameters": [
{
"name": "mu_sig",
"bounds": [[-5, 5]]
}
],
"poi": "mu_sig"
},
"name": "meas"
}
],
"observations": [{"data": [1000], "name": "ch"}],
"version": "1.0.0"
}