Skip to content

Commit

Permalink
properly treat optional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzennio committed May 30, 2024
1 parent 02dce09 commit 60c75da
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/cabinetry/fit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ def _goodness_of_fit(
model: pyhf.pdf.Model,
data: List[float],
best_twice_nll: float,
*,
fix_pars: Optional[List[bool]] = None,
) -> float:
"""Calculates goodness-of-fit p-value with a saturated model.
Expand Down Expand Up @@ -421,7 +422,7 @@ def _goodness_of_fit(
# of bins minus the number of unconstrained parameters
n_dof = sum(
model.config.channel_nbins.values()
) - model_utils.unconstrained_parameter_count(model, fix_pars)
) - model_utils.unconstrained_parameter_count(model, fix_pars=fix_pars)
log.debug(f"number of degrees of freedom: {n_dof}")

if n_dof <= 0:
Expand Down Expand Up @@ -507,7 +508,9 @@ def fit(

if goodness_of_fit:
# calculate goodness-of-fit with saturated model
p_val = _goodness_of_fit(model, data, fit_results.best_twice_nll, fix_pars)
p_val = _goodness_of_fit(
model, data, fit_results.best_twice_nll, fix_pars=fix_pars
)
fit_results = fit_results._replace(goodness_of_fit=p_val)

return fit_results
Expand Down
1 change: 1 addition & 0 deletions src/cabinetry/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ def prediction(

def unconstrained_parameter_count(
model: pyhf.pdf.Model,
*,
fix_pars: Optional[List[bool]] = None,
) -> int:
"""Returns the number of unconstrained, non-constant parameters in a model.
Expand Down
7 changes: 4 additions & 3 deletions tests/fit/test_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test__goodness_of_fit(
assert mock_pars.call_args[1] == {}
assert mock_count.call_count == 1
assert mock_count.call_args[0][0].spec == model.spec
assert mock_count.call_args[1] == {}
assert mock_count.call_args[1] == {"fix_pars": None}
assert "Delta NLL = 0.084185" in [rec.message for rec in caplog.records]
assert np.allclose(p_val, 0.91926079)
caplog.clear()
Expand All @@ -372,7 +372,7 @@ def test__goodness_of_fit(
assert mock_pars.call_count == 2 # no new call (no auxdata)
assert mock_count.call_count == 3
assert mock_count.call_args[0][0].spec == model.spec
assert mock_count.call_args[1] == {}
assert mock_count.call_args[1] == {"fix_pars": None}
assert (
"cannot calculate p-value: 0 degrees of freedom and Delta NLL = 0.000000"
in [rec.message for rec in caplog.records]
Expand Down Expand Up @@ -480,7 +480,8 @@ def test_fit(mock_fit, mock_print, mock_gof):

# goodness-of-fit test
fit_results_gof = fit.fit(model, data, goodness_of_fit=True)
assert mock_gof.call_args_list == [((model, data, 2.0, None), {})]
assert mock_gof.call_args[0] == (model, data, 2.0)
assert mock_gof.call_args[1] == {"fix_pars": None}
assert fit_results_gof.goodness_of_fit == 0.1


Expand Down
2 changes: 1 addition & 1 deletion tests/test_model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def test_unconstrained_parameter_count(example_spec, example_spec_shapefactor):
model = pyhf.Workspace(example_spec_shapefactor).model()
fix_pars = model.config.suggested_fixed()
fix_pars[0] = True
assert model_utils.unconstrained_parameter_count(model, fix_pars) == 2
assert model_utils.unconstrained_parameter_count(model, fix_pars=fix_pars) == 2

# fixed parameters are skipped in counting
example_spec_shapefactor["measurements"][0]["config"]["parameters"].append(
Expand Down

0 comments on commit 60c75da

Please sign in to comment.