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

fix: Catch use of multi-component parameters as POI with error message #2197

Merged
merged 8 commits into from
May 17, 2023

Conversation

alexander-held
Copy link
Member

@alexander-held alexander-held commented May 11, 2023

Description

Resolves #2200.

This addresses #2194. When a parameter with multiple components (shapefactor / shapesys / staterror) is used as POI, the model construction currently fails at an assert (assuming Python is not run with -O). This PR turns the failure into an exception with an error message to provide more context.

For simple MLEs, bypassing the assert actually makes things work (so one could argue to not catch this early and force the exception). However, this would cause even more confusing issues down the line once the POI starts mattering. As an example, a hypotest could result in RuntimeWarning: invalid value encountered in subtract within scipy. I do not see any benefit in allowing to bypass the exception to allow running things that do not depend on the POI definition though.

Checklist Before Requesting Reviewer

  • Tests are passing
  • "WIP" removed from the title of the pull request
  • Selected an Assignee for the PR to be responsible for the log summary

Before Merging

For the PR Assignees:

  • Summarize commit messages into a comprehensive review of the PR

(summary provided by @matthewfeickert)

* Raise exceptions.InvalidModel for multiple component parameter of interest.
  This guards against modifiers like 'shapefactor', 'shapesys', and 'staterror'
  from being used as POIs.
   - Remove use of 'assert' to check the same information.
* Add test to test_pdf.py to validate.

@matthewfeickert matthewfeickert added fix A bug fix need-to-backport tmp label until can be backported to patch release branch labels May 11, 2023
@alexander-held
Copy link
Member Author

alexander-held commented May 11, 2023

Not sure how to best handle custom modifiers: the model config contains the desired POI in parameters but not in modifiers (should it?). Working around this now by using a get, but unclear to me whether there may be a better approach.

Switched approach to be more generic.

@codecov
Copy link

codecov bot commented May 11, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (22c1699) 98.30% compared to head (87a5f5d) 98.30%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2197   +/-   ##
=======================================
  Coverage   98.30%   98.30%           
=======================================
  Files          69       69           
  Lines        4533     4533           
  Branches      801      802    +1     
=======================================
  Hits         4456     4456           
  Misses         45       45           
  Partials       32       32           
Flag Coverage Δ
contrib 97.88% <100.00%> (ø)
doctest 61.08% <33.33%> (-0.05%) ⬇️
unittests-3.10 96.31% <100.00%> (ø)
unittests-3.11 96.31% <100.00%> (ø)
unittests-3.8 96.33% <100.00%> (ø)
unittests-3.9 96.36% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/pyhf/pdf.py 98.03% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

src/pyhf/pdf.py Outdated Show resolved Hide resolved
@alexander-held alexander-held force-pushed the fix/catch-shapefactor-poi branch from 922438e to a34efec Compare May 11, 2023 20:07
@@ -464,6 +464,11 @@ def set_poi(self, name):
raise exceptions.InvalidModel(
f"The parameter of interest '{name:s}' cannot be fit as it is not declared in the model specification."
)
if self.param_set(name).n_parameters > 1:
Copy link
Member Author

@alexander-held alexander-held May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now handles shapefactor / shapesys / staterror if they act on channels with multiple bins. All of these cases would fail the assert below. Setups like a histosys as POI seem to technically work, so even if they might not be useful in practice I guess there is no need to catch those here.

@alexander-held alexander-held changed the title fix: Catch use of shapefactor modifiers as POI with error message fix: Catch use of multi-component parameters as POI with error message May 12, 2023
@matthewfeickert matthewfeickert force-pushed the fix/catch-shapefactor-poi branch from c82deac to 2ed7648 Compare May 16, 2023 03:43
Copy link
Member

@matthewfeickert matthewfeickert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @alexander-held and LGTM. 👍 I just have one question on if we couldn't go further.

src/pyhf/pdf.py Show resolved Hide resolved
Copy link
Member

@matthewfeickert matthewfeickert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the assert removal this all LGTM @alexander-held. 👍 If @kratsg also approves then once this is merged and I finish the other backports I'll release pyhf v0.7.2 with this in it today.

@matthewfeickert matthewfeickert merged commit 30dc756 into main May 17, 2023
@matthewfeickert matthewfeickert deleted the fix/catch-shapefactor-poi branch May 17, 2023 23:11
@matthewfeickert matthewfeickert removed the need-to-backport tmp label until can be backported to patch release branch label May 18, 2023
matthewfeickert added a commit that referenced this pull request May 18, 2023
* Backport PR #2197

Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix A bug fix tests pytest
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

shapefactor as parameter of interest
3 participants