Skip to content

Commit 4ef9bfc

Browse files
authored
feat: Bookkeep fixed parameters (#989)
* Add functionality into _ModelConfig to bookkeep fixed systematics (all parameters for a given systematic) * Add fixed into tests for paramsets * Add tests for fixed parameters
1 parent 5fcf95b commit 4ef9bfc

15 files changed

+249
-27
lines changed

src/pyhf/modifiers/histosys.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def required_parset(cls, sample_data, modifier_data):
2020
'is_shared': True,
2121
'inits': (0.0,),
2222
'bounds': ((-5.0, 5.0),),
23+
'fixed': False,
2324
'auxdata': (0.0,),
2425
}
2526

src/pyhf/modifiers/lumi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def required_parset(cls, sample_data, modifier_data):
2020
'op_code': cls.op_code,
2121
'inits': None, # lumi
2222
'bounds': None, # (0, 10*lumi)
23+
'fixed': False,
2324
'auxdata': None, # lumi
2425
'sigmas': None, # lumi * lumirelerror
2526
}

src/pyhf/modifiers/normfactor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def required_parset(cls, sample_data, modifier_data):
1919
'is_shared': True,
2020
'inits': (1.0,),
2121
'bounds': ((0, 10),),
22+
'fixed': False,
2223
}
2324

2425

src/pyhf/modifiers/normsys.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def required_parset(cls, sample_data, modifier_data):
2020
'is_shared': True,
2121
'inits': (0.0,),
2222
'bounds': ((-5.0, 5.0),),
23+
'fixed': False,
2324
'auxdata': (0.0,),
2425
}
2526

src/pyhf/modifiers/shapefactor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def required_parset(cls, sample_data, modifier_data):
1919
'is_shared': True,
2020
'inits': (1.0,) * len(sample_data),
2121
'bounds': ((0.0, 10.0),) * len(sample_data),
22+
'fixed': False,
2223
}
2324

2425

src/pyhf/modifiers/shapesys.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def required_parset(cls, sample_data, modifier_data):
2727
'is_shared': False,
2828
'inits': (1.0,) * n_parameters,
2929
'bounds': ((1e-10, 10.0),) * n_parameters,
30+
'fixed': False,
3031
# nb: auxdata/factors set by finalize. Set to non-numeric to crash
3132
# if we fail to set auxdata/factors correctly
3233
'auxdata': (None,) * n_parameters,

src/pyhf/modifiers/staterror.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def required_parset(cls, sample_data, modifier_data):
1919
'is_shared': True,
2020
'inits': (1.0,) * len(sample_data),
2121
'bounds': ((1e-10, 10.0),) * len(sample_data),
22+
'fixed': False,
2223
'auxdata': (1.0,) * len(sample_data),
2324
}
2425

src/pyhf/parameters/paramsets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def __init__(self, **kwargs):
66
self.n_parameters = kwargs.pop('n_parameters')
77
self.suggested_init = kwargs.pop('inits')
88
self.suggested_bounds = kwargs.pop('bounds')
9+
self.fixed = kwargs.pop('fixed')
910

1011

1112
class unconstrained(paramset):

src/pyhf/parameters/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def reduce_paramsets_requirements(paramsets_requirements, paramsets_user_configs
1212
'auxdata',
1313
'factors',
1414
'sigmas',
15+
'fixed',
1516
]
1617

1718
# - process all defined paramsets
@@ -27,6 +28,7 @@ def reduce_paramsets_requirements(paramsets_requirements, paramsets_user_configs
2728
combined_paramset = {}
2829
for k in paramset_keys:
2930
for paramset_requirement in paramset_requirements:
31+
# undefined: the modifier does not support configuring that property
3032
v = paramset_requirement.get(k, 'undefined')
3133
combined_paramset.setdefault(k, set([])).add(v)
3234

src/pyhf/pdf.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def _paramset_requirements_from_modelspec(spec, channel_nbins):
9393
paramset_type = paramset_requirements.get('paramset_type')
9494
paramset = paramset_type(**paramset_requirements)
9595
_sets[param_name] = paramset
96+
9697
return _sets
9798

9899

@@ -229,8 +230,8 @@ def __init__(self, spec, **config_kwargs):
229230
)
230231

231232
if config_kwargs:
232-
raise KeyError(
233-
f"""Unexpected keyword argument(s): '{"', '".join(config_kwargs.keys())}'"""
233+
raise exceptions.Unsupported(
234+
f"Unsupported options were passed in: {list(config_kwargs.keys())}."
234235
)
235236

236237
self.par_map = {}
@@ -265,6 +266,31 @@ def par_slice(self, name):
265266
def param_set(self, name):
266267
return self.par_map[name]['paramset']
267268

269+
def suggested_fixed(self):
270+
"""
271+
Identify the fixed parameters in the model.
272+
273+
Returns:
274+
List: A list of booleans, ``True`` for fixed and ``False`` for not fixed.
275+
276+
Something like the following to build fixed_vals appropriately:
277+
278+
.. code:: python
279+
280+
fixed_pars = pdf.config.suggested_fixed()
281+
inits = pdf.config.suggested_init()
282+
fixed_vals = [
283+
(index, init)
284+
for index, (init, is_fixed) in enumerate(zip(inits, fixed_pars))
285+
if is_fixed
286+
]
287+
"""
288+
fixed = []
289+
for name in self.par_order:
290+
paramset = self.par_map[name]['paramset']
291+
fixed = fixed + [paramset.fixed] * paramset.n_parameters
292+
return fixed
293+
268294
def set_poi(self, name):
269295
if name not in [x for x, _ in self.modifiers]:
270296
raise exceptions.InvalidModel(

0 commit comments

Comments
 (0)