@@ -64,11 +64,12 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):
64
64
65
65
66
66
@attr .s
67
- class Explainer ( abc . ABC ) :
67
+ class Base :
68
68
"""
69
- Base class for explainer algorithms
69
+ Base class for all `alibi` algorithms. Implements a structured approach to handle metadata.
70
70
"""
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.
72
73
73
74
def __attrs_post_init__ (self ):
74
75
# add a name and version to the metadata dictionary
@@ -79,6 +80,31 @@ def __attrs_post_init__(self):
79
80
for key , value in self .meta .items ():
80
81
setattr (self , key , value )
81
82
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
+
82
108
@abc .abstractmethod
83
109
def explain (self , X : Any ) -> "Explanation" :
84
110
pass
@@ -123,25 +149,6 @@ def save(self, path: Union[str, os.PathLike]) -> None:
123
149
"""
124
150
save_explainer (self , path )
125
151
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
-
145
152
146
153
class FitMixin (abc .ABC ):
147
154
@abc .abstractmethod
0 commit comments