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

feat: par_names handles non-scalar modifiers with 1 parameter #1560

Merged
merged 3 commits into from
Aug 25, 2021
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
13 changes: 4 additions & 9 deletions src/pyhf/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,10 @@ def par_slice(self, name):
"""
return self.par_map[name]['slice']

def par_names(self, fstring='{name}[{index}]'):
def par_names(self):
"""
The names of the parameters in the model including binned-parameter indexing.

Args:
fstring (:obj:`str`): Format string for the parameter names using ``name`` and ``index`` variables. Default: ``'{name}[{index}]'``.

Returns:
:obj:`list`: Names of the model parameters.

Expand All @@ -336,18 +333,16 @@ def par_names(self, fstring='{name}[{index}]'):
... )
>>> model.config.par_names()
['mu', 'uncorr_bkguncrt[0]', 'uncorr_bkguncrt[1]']
>>> model.config.par_names(fstring='{name}_{index}')
['mu', 'uncorr_bkguncrt_0', 'uncorr_bkguncrt_1']
"""
_names = []
for name in self.par_order:
_npars = self.param_set(name).n_parameters
if _npars == 1:
param_set = self.param_set(name)
if param_set.is_scalar:
_names.append(name)
continue

_names.extend(
[fstring.format(name=name, index=idx) for idx in range(_npars)]
[f'{name}[{index}]' for index in range(param_set.n_parameters)]
)
return _names

Expand Down
2 changes: 1 addition & 1 deletion tests/test_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def test_minuit_param_names(mocker):
data = [10] + pdf.config.auxdata
_, result = pyhf.infer.mle.fit(data, pdf, return_result_obj=True)
assert 'minuit' in result
assert result.minuit.parameters == ('mu', 'uncorr_bkguncrt')
assert result.minuit.parameters == ('mu', 'uncorr_bkguncrt[0]')

pdf.config.par_names = mocker.Mock(return_value=None)
_, result = pyhf.infer.mle.fit(data, pdf, return_result_obj=True)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,3 +890,34 @@ def test_reproducible_model_spec():
{'bounds': [[0, 5]], 'inits': [1], 'name': 'mu'}
]
assert pyhf.Model(model_from_ws.spec)


def test_par_names_scalar_nonscalar():
"""
Testing to ensure that nonscalar parameters are still indexed, even if
n_parameters==1.
"""
spec = {
'channels': [
{
'name': 'channel',
'samples': [
{
'name': 'goodsample',
'data': [1.0],
'modifiers': [
{'type': 'normfactor', 'name': 'scalar', 'data': None},
{'type': 'shapesys', 'name': 'nonscalar', 'data': [1.0]},
],
},
],
}
]
}

model = pyhf.Model(spec, poi_name="scalar")
assert model.config.par_order == ["scalar", "nonscalar"]
assert model.config.par_names() == [
'scalar',
'nonscalar[0]',
]