Skip to content

Commit

Permalink
feat: Flag to set absolute path for explainerfile implemented (#222)
Browse files Browse the repository at this point in the history
* feat: Flag to set absolute path for explainerfile implemented

* fix: os.path.join used instead plain joining

* give specific absolute explainer path

* adds absolute explainerfile path to release notes

Co-authored-by: Oege Dijk <oege.dijk@fourkind.com>
  • Loading branch information
jenoOvchi and Oege Dijk committed Jun 15, 2022
1 parent c247f47 commit f2e550c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Upgrades the dashboard to bootstrap5 and dash-bootstrap-components v1 (which is also based on bootstrap5)
- Both sklearn and imblearn Pipelines are now supported, as long as the transformers have a `.get_feature_names_out()` method
- Adds `shap_kwargs` parameter to the explainers that allow you to pass additional kwargs to the shap values generating call, e.g. `shap_kwargs=dict(check_addivity=False)`
- Can now specify absolute path for `explainerfile` when dumping `dashboard.yaml`

### Bug Fixes
- Suppresses warnings when extracting final model from pipeline that was not fitted on a dataframe.
Expand Down
28 changes: 22 additions & 6 deletions explainerdashboard/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,15 @@ def from_config(cls, arg1, arg2=None, **update_params):
else:
return cls(explainer, **dashboard_params, **kwargs)

def to_yaml(self, filepath=None, return_dict=False,
explainerfile="explainer.joblib", dump_explainer=False):
def to_yaml(self,
filepath:Union[str, Path]=None,
return_dict:bool=False,
explainerfile:str="explainer.joblib",
dump_explainer:bool=False,
explainerfile_absolute_path:Union[str, Path, bool]=None):
"""Returns a yaml configuration of the current ExplainerDashboard
that can be used by the explainerdashboard CLI. Recommended filename
that can be used by the explainerdashboard CLI or to reinstate an identical
dashboard from the (dumped) explainer and saved configuration. Recommended filename
is `dashboard.yaml`.
Args:
Expand All @@ -731,9 +736,13 @@ def to_yaml(self, filepath=None, return_dict=False,
return_dict (bool, optional): instead of yaml return dict with
config.
explainerfile (str, optional): filename of explainer dump. Defaults
to `explainer.joblib`.
to `explainer.joblib`. Should en in either .joblib .dill or .pkl
dump_explainer (bool, optional): dump the explainer along with the yaml.
You must pass explainerfile parameter for the filename. Defaults to False.
explainerfile_absolute_path (str, Path, bool, optional): absolute path to save explainerfile if
not in the same directory as the yaml file. You can also pass True which still
saves the explainerfile to the filepath directory, but adds an absolute path
to the yaml file.
"""
import oyaml as yaml
Expand All @@ -749,13 +758,20 @@ def to_yaml(self, filepath=None, return_dict=False,
if filepath is not None:
dashboard_path = Path(filepath).absolute().parent
dashboard_path.mkdir(parents=True, exist_ok=True)

if explainerfile_absolute_path:
if isinstance(explainerfile_absolute_path, bool):
explainerfile_absolute_path = dashboard_path / explainerfile
dashboard_config["dashboard"]["explainerfile"] = str(Path(explainerfile_absolute_path).absolute())
else:
explainerfile_absolute_path = dashboard_path / explainerfile

print(f"Dumping configuration .yaml to {Path(filepath).absolute()}...", flush=True)
yaml.dump(dashboard_config, open(filepath, "w"))

if dump_explainer:
print(f"Dumping explainer to {dashboard_path / explainerfile}...", flush=True)
self.explainer.dump(dashboard_path / explainerfile)
print(f"Dumping explainer to {explainerfile_absolute_path}...", flush=True)
self.explainer.dump(explainerfile_absolute_path)
return
return yaml.dump(dashboard_config)

Expand Down

0 comments on commit f2e550c

Please sign in to comment.