-
Notifications
You must be signed in to change notification settings - Fork 28
/
global_methods.py
307 lines (242 loc) · 11.8 KB
/
global_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright 2022 Feedzai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Callable, Union, List, Tuple
import numpy as np
import pandas as pd
from timeshap.explainer import prune_all, event_explain_all, feat_explain_all
from timeshap.explainer.pruning import verify_pruning_dict
from timeshap.explainer.event_level import verify_event_dict
from timeshap.explainer.feature_level import verify_feature_dict
from timeshap.plot import plot_global_report
import os
from timeshap.utils import convert_to_indexes, convert_data_to_3d, validate_input
def validate_global_input(f: Callable[[np.ndarray], np.ndarray],
data: Union[pd.DataFrame, np.array],
pruning_dict: dict,
event_dict: dict,
feature_dict: dict,
baseline: Union[pd.DataFrame, np.array] = None,
model_features: List[Union[int, str]] = None,
schema: List[str] = None,
entity_col: Union[int, str] = None,
time_col: Union[int, str] = None,
append_to_files: bool = False,
verbose: bool = False,
):
""" Validates the inputs for global reports
Parameters
----------
f: Callable[[np.ndarray], np.ndarray]
Point of entry for model being explained.
This method receives a 3-D np.ndarray (#samples, #seq_len, #features).
This method returns a 2-D np.ndarray (#samples, 1).
data: Union[List[np.ndarray], pd.DataFrame, np.array]
Sequences to be explained.
Must contain columns with names disclosed on `model_features`.
pruning_dict: dict
Information required for the pruning algorithm
event_dict: dict
Information required for the event level explanation calculation
feature_dict: dict
Information required for the feature level explanation calculation
baseline: Union[np.ndarray, pd.DataFrame],
Dataset baseline. Median/Mean of numerical features and mode of categorical.
In case of np.array feature are assumed to be in order with `model_features`.
The baseline can be an average event or an average sequence
model_features: List[str]
Features to be used by the model. Requires same order as training dataset
schema: List[str]
Schema of provided data
entity_col: str
Entity column to identify sequences
time_col: str
Data column that represents the time feature in order to sort sequences
temporally
append_to_files: bool
Append explanations to files if file already exists
verbose: bool
If process is verbose
"""
validate_input(f, data, baseline, model_features, schema, entity_col, time_col)
if isinstance(data, np.ndarray):
if len(data.shape) == 2:
assert entity_col is not None, "Entity column must be provided when using 2D numpy arrays as data"
if pruning_dict is not None:
verify_pruning_dict(pruning_dict)
if pruning_dict.get("path"):
if os.path.exists(pruning_dict.get("path")) and not append_to_files:
print(
"The defined path for pruning data already exists and the append option is turned off. TimeSHAP will only read from this file and will not create new explanation data")
else:
print("No path to persist pruning data provided.")
verify_event_dict(event_dict)
verify_feature_dict(feature_dict)
if event_dict.get("path"):
if os.path.exists(event_dict.get("path")) and not append_to_files:
print("The defined path for event explanations already exists and the append option is turned off. TimeSHAP will only read from this file and will not create new explanation data")
else:
print("No path to persist event explanations provided.")
if feature_dict.get("path"):
if os.path.exists(feature_dict.get("path")) and not append_to_files:
print("The defined path for feature explanations already exists and the append option is turned off. TimeSHAP will only read from this file and will not create new explanation data")
else:
print("No path to persist feature explanations provided.")
def calc_global_explanations(f: Callable[[np.ndarray], np.ndarray],
data: Union[pd.DataFrame, np.array],
pruning_dict: dict,
event_dict: dict,
feature_dict: dict,
baseline: Union[pd.DataFrame, np.array] = None,
model_features: List[Union[int, str]] = None,
schema: List[str] = None,
entity_col: Union[int, str] = None,
time_col: Union[int, str] = None,
append_to_files: bool = False,
max_instances: int = 10000,
verbose: bool = False,
) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
""" Calculates global report explanations
Parameters
----------
f: Callable[[np.ndarray], np.ndarray]
Point of entry for model being explained.
This method receives a 3-D np.ndarray (#samples, #seq_len, #features).
This method returns a 2-D np.ndarray (#samples, 1).
data: Union[List[np.ndarray], pd.DataFrame, np.array]
Sequences to be explained.
Must contain columns with names disclosed on `model_features`.
pruning_dict: dict
Information required for the pruning algorithm
event_dict: dict
Information required for the event level explanation calculation
feature_dict: dict
Information required for the feature level explanation calculation
baseline: Union[np.ndarray, pd.DataFrame],
Dataset baseline. Median/Mean of numerical features and mode of categorical.
In case of np.array feature are assumed to be in order with `model_features`.
The baseline can be an average event or an average sequence
model_features: List[str]
Features to be used by the model. Requires same order as training dataset
schema: List[str]
Schema of provided data
entity_col: str
Entity column to identify sequences
time_col: str
Data column that represents the time feature in order to sort sequences
temporally
append_to_files: bool
Append explanations to files if file already exists
max_instances: int
Max number of instances to explain
verbose: bool
If process is verbose
Returns
-------
pd.DataFrame
Global pruning algorithm information
pd.DataFrame
Global event explanations
pd.DataFrame
Global feature explanations
"""
if schema is None and isinstance(data, pd.DataFrame):
#schema = list(data.columns)
schema = list(data.columns.map(str))
validate_global_input(
f, data, pruning_dict, event_dict, feature_dict, baseline, model_features,
schema, entity_col, time_col, append_to_files, verbose)
model_features_index, entity_col_index, time_col_index = convert_to_indexes(model_features, schema, entity_col, time_col)
data = convert_data_to_3d(data, entity_col_index, time_col_index)
if len(data) > max_instances:
selected_sequences = np.random.choice(np.arange(len(data)), max_instances, False)
data = [data[idx] for idx in selected_sequences]
if pruning_dict is None:
prun_indexes = None
else:
print("Calculating pruning algorithm")
prun_indexes = prune_all(f, data, pruning_dict, baseline,
model_features_index, schema, entity_col_index,
time_col_index, append_to_files, verbose)
print("Calculating event data")
event_data = event_explain_all(f, data, event_dict, prun_indexes, baseline, model_features_index, schema, entity_col_index, time_col_index, append_to_files, verbose)
print("Calculating feat data")
feat_data = feat_explain_all(f, data, feature_dict, prun_indexes, baseline, model_features_index, schema, entity_col_index, time_col_index, append_to_files, verbose)
return prun_indexes, event_data, feat_data
def global_report(f: Callable[[np.ndarray], np.ndarray],
data: Union[pd.DataFrame, np.array],
pruning_dict: dict,
event_dict: dict,
feature_dict: dict,
baseline: Union[pd.DataFrame, np.array] = None,
model_features: List[Union[int, str]] = None,
schema: List[str] = None,
entity_col: Union[int, str] = None,
time_col: Union[int, str] = None,
append_to_files: bool = False,
max_instances: int = 10000,
verbose: bool = False,
):
""" Calculates the global report and plots it.\
Parameters
----------
f: Callable[[np.ndarray], np.ndarray]
Point of entry for model being explained.
This method receives a 3-D np.ndarray (#samples, #seq_len, #features).
This method returns a 2-D np.ndarray (#samples, 1).
data: Union[List[np.ndarray], pd.DataFrame, np.array]
Sequences to be explained.
Must contain columns with names disclosed on `model_features`.
pruning_dict: dict
Information required for the pruning algorithm
event_dict: dict
Information required for the event level explanation calculation
feature_dict: dict
Information required for the feature level explanation calculation
baseline: Union[np.ndarray, pd.DataFrame],
Dataset baseline. Median/Mean of numerical features and mode of categorical.
In case of np.array feature are assumed to be in order with `model_features`.
The baseline can be an average event or an average sequence
model_features: List[str]
Features to be used by the model. Requires same order as training dataset
schema: List[str]
Schema of provided data
entity_col: str
Entity column to identify sequences
time_col: str
Data column that represents the time feature in order to sort sequences
temporally
append_to_files: bool
Append explanations to files if file already exists
max_instances: int
Max instances to use for global plots and explanations.
Used to limit explanation dump file sizes and allow for feasable
plot time
verbose: bool
If process is verbose
Returns
-------
pd.DataFrame
altair.plot
"""
prun_indexes, event_data, feat_data = \
calc_global_explanations(f, data, pruning_dict, event_dict,
feature_dict, baseline, model_features,
schema, entity_col, time_col, append_to_files,
max_instances, verbose
)
prun_stats, global_plot = \
plot_global_report(pruning_dict, event_dict, feature_dict, prun_indexes,
event_data, feat_data
)
return prun_stats, global_plot