Skip to content

Commit a3d9023

Browse files
Split Explainer into Base & Explainer (#649)
* split Explainer into Base and Explainer. * update metadata docs * minor correction meta docs
1 parent 41db95e commit a3d9023

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

alibi/api/interfaces.py

+29-22
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):
6464

6565

6666
@attr.s
67-
class Explainer(abc.ABC):
67+
class Base:
6868
"""
69-
Base class for explainer algorithms
69+
Base class for all `alibi` algorithms. Implements a structured approach to handle metadata.
7070
"""
71-
meta: dict = attr.ib(default=attr.Factory(default_meta), repr=alibi_pformat) #: Explainer meta-data.
71+
72+
meta: dict = attr.ib(default=attr.Factory(default_meta), repr=alibi_pformat) #: Object metadata.
7273

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

83+
def _update_metadata(self, data_dict: dict, params: bool = False) -> None:
84+
"""
85+
Updates the metadata of the object using the data from the `data_dict`. If the params option
86+
is specified, then each key-value pair is added to the metadata ``'params'`` dictionary.
87+
88+
Parameters
89+
----------
90+
data_dict
91+
Contains the data to be stored in the metadata.
92+
params
93+
If ``True``, the method updates the ``'params'`` attribute of the metadata.
94+
"""
95+
96+
if params:
97+
for key in data_dict.keys():
98+
self.meta['params'].update([(key, data_dict[key])])
99+
else:
100+
self.meta.update(data_dict)
101+
102+
103+
class Explainer(abc.ABC, Base):
104+
"""
105+
Base class for explainer algorithms from :py:mod:`alibi.explainers`.
106+
"""
107+
82108
@abc.abstractmethod
83109
def explain(self, X: Any) -> "Explanation":
84110
pass
@@ -123,25 +149,6 @@ def save(self, path: Union[str, os.PathLike]) -> None:
123149
"""
124150
save_explainer(self, path)
125151

126-
def _update_metadata(self, data_dict: dict, params: bool = False) -> None:
127-
"""
128-
Updates the metadata of the explainer using the data from the `data_dict`. If the params option
129-
is specified, then each key-value pair is added to the metadata ``'params'`` dictionary.
130-
131-
Parameters
132-
----------
133-
data_dict
134-
Contains the data to be stored in the metadata.
135-
params
136-
If ``True``, the method updates the ``'params'`` attribute of the metadata.
137-
"""
138-
139-
if params:
140-
for key in data_dict.keys():
141-
self.meta['params'].update([(key, data_dict[key])])
142-
else:
143-
self.meta.update(data_dict)
144-
145152

146153
class FitMixin(abc.ABC):
147154
@abc.abstractmethod

0 commit comments

Comments
 (0)