Skip to content

Commit bc5cf85

Browse files
feat: Make par_names a _ModelConfig property attribute (#2027)
* Make pyhf.pdf._ModelConfig.par_names() property attribute pyhf.pdf._ModelConfig.par_names, like the the pyhf.pdf._ModelConfig.par_order API. * Update all usage of `.par_names()` to `par_names`. * Use unittest.mock.PropertyMock to properly mock par_names return.
1 parent cccbda1 commit bc5cf85

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

src/pyhf/optimize/mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def minimize(
181181

182182
# handle non-pyhf ModelConfigs
183183
try:
184-
par_names = pdf.config.par_names()
184+
par_names = pdf.config.par_names
185185
except AttributeError:
186186
par_names = None
187187

src/pyhf/pdf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ def par_slice(self, name):
358358
"""
359359
return self.par_map[name]['slice']
360360

361+
@property
361362
def par_names(self):
362363
"""
363364
The names of the parameters in the model including binned-parameter indexing.
@@ -370,8 +371,10 @@ def par_names(self):
370371
>>> model = pyhf.simplemodels.uncorrelated_background(
371372
... signal=[12.0, 11.0], bkg=[50.0, 52.0], bkg_uncertainty=[3.0, 7.0]
372373
... )
373-
>>> model.config.par_names()
374+
>>> model.config.par_names
374375
['mu', 'uncorr_bkguncrt[0]', 'uncorr_bkguncrt[1]']
376+
377+
.. versionchanged:: 0.7.0 Changed from method to property attribute.
375378
"""
376379
_names = []
377380
for name in self.par_order:

tests/test_optim.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest.mock import patch, PropertyMock
12
import pyhf
23
from pyhf.optimize.mixins import OptimizerMixin
34
from pyhf.optimize.common import _get_tensor_shim, _make_stitch_pars
@@ -576,7 +577,10 @@ def test_minuit_param_names(mocker):
576577
assert 'minuit' in result
577578
assert result.minuit.parameters == ('mu', 'uncorr_bkguncrt[0]')
578579

579-
pdf.config.par_names = mocker.Mock(return_value=None)
580-
_, result = pyhf.infer.mle.fit(data, pdf, return_result_obj=True)
581-
assert 'minuit' in result
582-
assert result.minuit.parameters == ('x0', 'x1')
580+
with patch(
581+
"pyhf.pdf._ModelConfig.par_names", new_callable=PropertyMock
582+
) as mock_par_names:
583+
mock_par_names.return_value = None
584+
_, result = pyhf.infer.mle.fit(data, pdf, return_result_obj=True)
585+
assert "minuit" in result
586+
assert result.minuit.parameters == ("x0", "x1")

tests/test_pdf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ def test_par_names_scalar_nonscalar():
957957

958958
model = pyhf.Model(spec, poi_name="scalar")
959959
assert model.config.par_order == ["scalar", "nonscalar"]
960-
assert model.config.par_names() == [
960+
assert model.config.par_names == [
961961
'scalar',
962962
'nonscalar[0]',
963963
]
@@ -1159,7 +1159,7 @@ def test_pdf_clipping(backend):
11591159
model = ws.model()
11601160
data = tensorlib.astensor([100.0, 100.0, 10.0, 0.0, 0.0])
11611161

1162-
for par_name in model.config.par_names():
1162+
for par_name in model.config.par_names:
11631163
if "np" in par_name:
11641164
par_values.append(-0.6) # np_1 / np_2
11651165
else:

tests/test_simplemodels.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_correlated_background(backend):
1818
assert model.config.channels == ["single_channel"]
1919
assert model.config.samples == ["background", "signal"]
2020
assert model.config.par_order == ["correlated_bkg_uncertainty", "mu"]
21-
assert model.config.par_names() == ['correlated_bkg_uncertainty', "mu"]
21+
assert model.config.par_names == ["correlated_bkg_uncertainty", "mu"]
2222
assert model.config.suggested_init() == [0.0, 1.0]
2323

2424

@@ -29,7 +29,7 @@ def test_uncorrelated_background(backend):
2929
assert model.config.channels == ["singlechannel"]
3030
assert model.config.samples == ["background", "signal"]
3131
assert model.config.par_order == ["mu", "uncorr_bkguncrt"]
32-
assert model.config.par_names() == [
32+
assert model.config.par_names == [
3333
'mu',
3434
'uncorr_bkguncrt[0]',
3535
'uncorr_bkguncrt[1]',
@@ -52,7 +52,7 @@ def test_correlated_background_default_backend(default_backend):
5252
assert model.config.channels == ["single_channel"]
5353
assert model.config.samples == ["background", "signal"]
5454
assert model.config.par_order == ["correlated_bkg_uncertainty", "mu"]
55-
assert model.config.par_names() == ['correlated_bkg_uncertainty', "mu"]
55+
assert model.config.par_names == ["correlated_bkg_uncertainty", "mu"]
5656
assert model.config.suggested_init() == [0.0, 1.0]
5757

5858

@@ -68,7 +68,7 @@ def test_uncorrelated_background_default_backend(default_backend):
6868
assert model.config.channels == ["singlechannel"]
6969
assert model.config.samples == ["background", "signal"]
7070
assert model.config.par_order == ["mu", "uncorr_bkguncrt"]
71-
assert model.config.par_names() == [
71+
assert model.config.par_names == [
7272
'mu',
7373
'uncorr_bkguncrt[0]',
7474
'uncorr_bkguncrt[1]',

0 commit comments

Comments
 (0)