Skip to content

Commit

Permalink
feat: Add POI-less specification support (#1638)
Browse files Browse the repository at this point in the history
* Support JSON specifications with empty string for POI by treating as POI-less
* Support poi_name as empty string, in addition to None
* Add test for POI-less workspace
  • Loading branch information
kratsg authored Oct 15, 2021
1 parent ae92c25 commit 48be630
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pyhf/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ def __init__(
modifier_set, self.config, self.spec, self.batch_size
)

poi_name = None if poi_name == "" else poi_name
if poi_name is not None:
self.config.set_poi(poi_name)

Expand Down
33 changes: 33 additions & 0 deletions tests/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,39 @@ def test_poiless_model(backend):
pyhf.infer.hypotest(1.0, data, model)


def test_poiless_model_empty_string(backend):
spec = {
'channels': [
{
'name': 'channel',
'samples': [
{
'name': 'goodsample',
'data': [10.0],
'modifiers': [
{
'type': 'normsys',
'name': 'shape',
'data': {"hi": 0.5, "lo": 1.5},
}
],
},
],
}
]
}
model = pyhf.Model(spec, poi_name="")

data = [12] + model.config.auxdata
pyhf.infer.mle.fit(data, model)

with pytest.raises(pyhf.exceptions.UnspecifiedPOI):
pyhf.infer.mle.fixed_poi_fit(1.0, data, model)

with pytest.raises(pyhf.exceptions.UnspecifiedPOI):
pyhf.infer.hypotest(1.0, data, model)


def test_pdf_integration_shapesys_zeros(backend):
spec = {
"channels": [
Expand Down
12 changes: 12 additions & 0 deletions tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,15 @@ def test_wspace_immutable():
after = model.config.suggested_init()

assert before == after


def test_workspace_poiless(datadir):
"""
Test that a workspace with a measurement with empty POI string is treated as POI-less
"""
spec = json.load(open(datadir.join("poiless.json")))
ws = pyhf.Workspace(spec)
model = ws.model()

assert model.config.poi_name is None
assert model.config.poi_index is None
59 changes: 59 additions & 0 deletions tests/test_workspace/poiless.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"channels": [
{
"name": "singlechannel",
"samples": [
{
"name": "signal",
"data": [
12,
11
],
"modifiers": [
{
"name": "mu",
"type": "normfactor",
"data": null
}
]
},
{
"name": "background",
"data": [
50,
52
],
"modifiers": [
{
"name": "uncorr_bkguncrt",
"type": "shapesys",
"data": [
3,
7
]
}
]
}
]
}
],
"observations": [
{
"name": "singlechannel",
"data": [
51,
48
]
}
],
"measurements": [
{
"name": "Measurement",
"config": {
"poi": "",
"parameters": []
}
}
],
"version": "1.0.0"
}

0 comments on commit 48be630

Please sign in to comment.