-
-
Notifications
You must be signed in to change notification settings - Fork 380
GSK-1533 #1307
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
Merged
Merged
GSK-1533 #1307
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
7789fa9
Initial commit with the implementation of the SHAP explanation graphs…
AbSsEnT e31798e
Changed logic of obtaining feature names and types.
AbSsEnT e9de16d
Removed redundant 'model.prepare_dataframe'. Small refactoring.
AbSsEnT 4f92418
Added sorting of logged dataset, test suite result and scan result to…
AbSsEnT 1a59240
Moved 'explain' function below shap-related functions.
AbSsEnT cc15925
Code refactoring.
AbSsEnT 5fbb14c
Changed naming for variables inside functions.
AbSsEnT c1c075e
Removed explainer return, as it is not needed.
AbSsEnT 9dbf847
Moved 'prepare_df' to the separate utils.py file to avoid code duplic…
AbSsEnT 167c629
Added docstring to the '_get_cls_prediction_explanation'
AbSsEnT ff6ed6f
Created dataclass ShapResult to store shap explanations there and enc…
AbSsEnT 6366064
Refactoring of the 'background_example' function.
AbSsEnT 75a013f
Refactoring.
AbSsEnT 4935cd8
Refactoring.
AbSsEnT 8ad73b1
Changed enum class declaration.
AbSsEnT 6bf77d7
Refactored model_explanation.py to be able to perform testing of expl…
AbSsEnT d2c4739
Small fix in comments.
AbSsEnT c4ee3c1
Uncommented fixture.
AbSsEnT 8b11843
Refactored "_get_highest_prob_shap" function. Made it more compact an…
AbSsEnT 660fd66
Removed #noqa options from the shap imports. Optimized imports.
AbSsEnT 99f82b8
Refactored _prepare_for_explanation function. Changed naming of the f…
AbSsEnT 0746ff3
Renamed explain_full(one) to "_calculate_dataset(sample)_shap_values"
AbSsEnT 72bd4d4
Refactored _get_background_example function.
AbSsEnT 0a5928f
Refactored 'explain_with_shap' function and 'ShapResult' dataclass fo…
AbSsEnT d80aae5
Fixed bugs with unit-tests for wandb.
AbSsEnT d98b943
Transferred '_compare_explain_functions' to the 'test_model_explanati…
AbSsEnT feffd84
Refactoring. Renaming and functions replacement.
AbSsEnT 868c33f
Renaming.
AbSsEnT a2e9833
Merge branch 'GSK-1505-wandb' into GSK-1533-wandb-shap
rabah-khalek ce40cf3
Merge branch 'GSK-1505-wandb' into GSK-1533-wandb-shap
rabah-khalek 309a64d
Transferred plotting functions from the shap_result.py to the wandb_u…
AbSsEnT 428e919
Merge remote-tracking branch 'origin/GSK-1533-wandb-shap' into GSK-15…
AbSsEnT 1b2302f
small update to error msg
rabah-khalek e0c20bb
updated unit test
rabah-khalek 0083f30
small update
rabah-khalek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from enum import Enum | ||
| from dataclasses import dataclass | ||
|
|
||
| from shap import Explanation | ||
|
|
||
| from giskard.core.core import ModelType, SupportedModelTypes | ||
|
|
||
|
|
||
| class PanelNames(str, Enum): | ||
| CATEGORICAL = "Feature importance for categorical features" | ||
| NUMERICAL = "Feature importance for numerical features" | ||
| GENERAL = "Global feature importance" | ||
|
|
||
|
|
||
| @dataclass | ||
| class ShapResult: | ||
| explanations: Explanation = None | ||
| feature_types: dict = None | ||
| feature_names: list = None | ||
| model_type: ModelType = None | ||
| only_highest_proba: bool = True | ||
|
|
||
| def _validate_wandb_config(self): | ||
| if not self.only_highest_proba and self.model_type == SupportedModelTypes.CLASSIFICATION: | ||
| raise ValueError( | ||
| "We currently support 'ShapResult.to_wandb()' only with 'only_highest_proba == True' for " | ||
| "classification models." | ||
| ) | ||
|
|
||
| def to_wandb(self, **kwargs) -> None: | ||
| """Create and log to the WandB run SHAP charts.""" | ||
| from giskard.integrations.wandb.wandb_utils import wandb_run | ||
| from giskard.integrations.wandb.wandb_utils import _wandb_bar_plot, _wandb_general_bar_plot, _wandb_scatter_plot | ||
|
|
||
| self._validate_wandb_config() | ||
|
|
||
| with wandb_run(**kwargs) as run: | ||
| charts = dict() | ||
|
|
||
| # Create general SHAP feature importance plot. | ||
| general_bar_plot = _wandb_general_bar_plot(self.explanations, self.feature_names) | ||
| charts.update({f"{PanelNames.GENERAL}/general_shap_bar_plot": general_bar_plot}) | ||
|
|
||
| # Create per-feature SHAP plots. | ||
| for feature_name, feature_type in self.feature_types.items(): | ||
| if feature_type == "category": | ||
| bar_plot = _wandb_bar_plot(self.explanations, feature_name) | ||
| charts.update({f"{PanelNames.CATEGORICAL}/{feature_name}_shap_bar_plot": bar_plot}) | ||
| elif feature_type == "numeric": | ||
| scatter_plot = _wandb_scatter_plot(self.explanations, feature_name) | ||
| charts.update({f"{PanelNames.NUMERICAL}/{feature_name}_shap_scatter_plot": scatter_plot}) | ||
| else: | ||
| raise NotImplementedError( | ||
| "We do not support the wandb logging of ShapResult for text features yet." | ||
| ) | ||
|
|
||
| # Log created plots. | ||
| run.log(charts) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.