Skip to content

Commit

Permalink
Split Explainer into Base & Explainer (#649)
Browse files Browse the repository at this point in the history
* split Explainer into Base and Explainer.

* update metadata docs

* minor correction meta docs
  • Loading branch information
RobertSamoilescu authored May 4, 2022
1 parent 41db95e commit a3d9023
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions alibi/api/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):


@attr.s
class Explainer(abc.ABC):
class Base:
"""
Base class for explainer algorithms
Base class for all `alibi` algorithms. Implements a structured approach to handle metadata.
"""
meta: dict = attr.ib(default=attr.Factory(default_meta), repr=alibi_pformat) #: Explainer meta-data.

meta: dict = attr.ib(default=attr.Factory(default_meta), repr=alibi_pformat) #: Object metadata.

def __attrs_post_init__(self):
# add a name and version to the metadata dictionary
Expand All @@ -79,6 +80,31 @@ def __attrs_post_init__(self):
for key, value in self.meta.items():
setattr(self, key, value)

def _update_metadata(self, data_dict: dict, params: bool = False) -> None:
"""
Updates the metadata of the object using the data from the `data_dict`. If the params option
is specified, then each key-value pair is added to the metadata ``'params'`` dictionary.
Parameters
----------
data_dict
Contains the data to be stored in the metadata.
params
If ``True``, the method updates the ``'params'`` attribute of the metadata.
"""

if params:
for key in data_dict.keys():
self.meta['params'].update([(key, data_dict[key])])
else:
self.meta.update(data_dict)


class Explainer(abc.ABC, Base):
"""
Base class for explainer algorithms from :py:mod:`alibi.explainers`.
"""

@abc.abstractmethod
def explain(self, X: Any) -> "Explanation":
pass
Expand Down Expand Up @@ -123,25 +149,6 @@ def save(self, path: Union[str, os.PathLike]) -> None:
"""
save_explainer(self, path)

def _update_metadata(self, data_dict: dict, params: bool = False) -> None:
"""
Updates the metadata of the explainer using the data from the `data_dict`. If the params option
is specified, then each key-value pair is added to the metadata ``'params'`` dictionary.
Parameters
----------
data_dict
Contains the data to be stored in the metadata.
params
If ``True``, the method updates the ``'params'`` attribute of the metadata.
"""

if params:
for key in data_dict.keys():
self.meta['params'].update([(key, data_dict[key])])
else:
self.meta.update(data_dict)


class FitMixin(abc.ABC):
@abc.abstractmethod
Expand Down

0 comments on commit a3d9023

Please sign in to comment.