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

Refactoring Exceptions #2580

Merged
merged 11 commits into from Jun 15, 2022

Conversation

alexisdrakopoulos
Copy link
Contributor

@alexisdrakopoulos alexisdrakopoulos commented Jun 6, 2022

SHAP currently calls Exception a lot throughout the code base. This makes it tough to handle exceptions internally as one has to match to messages to infer why an exception was raised.

For example; I want to try to build an explainer object and raise an error in cases where a tree explainer (or similar) won't work:

try:
    explainer = shap.Explainer(mod)
except Exception as e:
    if "The passed model is not callable and cannot be analyzed directly with the given masker!" in str(e):
        # do something
    else:
        raise e

Since I want to avoid catching all the other Exceptions that might be raised I have to match against the message. This is brittle and ugly in my opinion, I would much rather have something like:

try:
    explainer = shap.Explainer(mod)
except InvalidMaskerError:
    # do something

I am opening this draft PR which replaces some Exception calls with "more" appropriate ones including custom ones. As to not waste time in case this is not desired I wrote this very quickly and hence I'm sure some exceptions aren't the ideal ones to raise.

What do we think of doing it this way? If there's a positive reception to the idea I'll do it properly and open this as a PR.

@slundberg
Copy link
Collaborator

Thanks @alexisdrakopoulos, I like the idea! I agree that handling exceptions via string matching is not ideal, and there is clear room to improvement here. What you have looks like the right direction, so go for it. Once you have a PR ready I expect it will be a simple merge.

@alexisdrakopoulos alexisdrakopoulos marked this pull request as ready for review June 15, 2022 07:48
@alexisdrakopoulos
Copy link
Contributor Author

There are three tests for which I need to update the exception catching.

@alexisdrakopoulos
Copy link
Contributor Author

@slundberg can we try running tests?

@codecov
Copy link

codecov bot commented Jun 15, 2022

Codecov Report

Merging #2580 (84ddd09) into master (854426a) will increase coverage by 0.09%.
The diff coverage is 34.48%.

@@            Coverage Diff             @@
##           master    #2580      +/-   ##
==========================================
+ Coverage   51.51%   51.60%   +0.09%     
==========================================
  Files          90       91       +1     
  Lines       13116    13145      +29     
==========================================
+ Hits         6757     6784      +27     
- Misses       6359     6361       +2     
Impacted Files Coverage Δ
shap/benchmark/_explanation_error.py 0.00% <0.00%> (ø)
shap/benchmark/_sequential.py 0.00% <0.00%> (ø)
shap/benchmark/experiments.py 0.00% <0.00%> (ø)
shap/explainers/_additive.py 33.33% <0.00%> (ø)
shap/explainers/_exact.py 71.50% <0.00%> (ø)
shap/explainers/_partition.py 53.30% <0.00%> (ø)
shap/explainers/_permutation.py 80.00% <0.00%> (ø)
shap/explainers/other/_maple.py 12.12% <0.00%> (ø)
shap/explainers/other/_random.py 28.57% <0.00%> (ø)
shap/explainers/other/_treegain.py 23.80% <0.00%> (ø)
... and 13 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@slundberg
Copy link
Collaborator

Just looked it over and it all looks good to me, except for one line I commented on. After you get a chance to look at that I think this is ready to merge. Thanks!

@slundberg slundberg merged commit b6e90c8 into shap:master Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants