-
Notifications
You must be signed in to change notification settings - Fork 28
/
cell_level.py
323 lines (266 loc) · 11.3 KB
/
cell_level.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# 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, Tuple, List
import numpy as np
import pandas as pd
from timeshap.explainer.kernel import TimeShapKernel
import os
from pathlib import Path
def cell_top_events(event_data: pd.DataFrame,
event_threshold: float = None,
top_x_events: int = None,
**kwargs,
) -> Tuple[list, list]:
"""Calculates the indexes and names of events to participate in the cell level
given the conditions
Parameters
----------
event_data: pd.DataFrame
Event level explanations.
event_threshold: float
The threshold to consider an event relevant.
top_x_events: float
Number of events to include as relevant
Returns
-------
Tuple[list, list]
Tuple containing two lists.
The first list contains the event indexes ordered ascendingly.
The second list contains the correspondent event names.
"""
# order explanations by absolute contribution
ordered_exp = event_data.iloc[(-event_data['Shapley Value'].abs()).argsort()].reset_index()
top_events_idx = []
for _, row in ordered_exp.iterrows():
if row['Feature'] == 'Pruned Events':
# we want to skip previous events
continue
if event_threshold is not None and abs(row['Shapley Value']) < event_threshold:
# we have reached the sopping point
break
top_events_idx += [[-row['index'] -1, row['Feature']]]
if top_x_events is not None and len(top_events_idx) == top_x_events:
# we have added enough events
break
df = pd.DataFrame(top_events_idx, columns=['idx', 'name']).sort_values('idx')
return list(df['idx'].values), list(df['name'].values)
def cell_top_feats(feat_data: pd.DataFrame,
feat_threshold: float = None,
top_x_feats: int = None,
**kwargs,
) -> Tuple[list, list]:
"""Calculates the indexes and names of events to participate in the cell level
given the conditions
Parameters
----------
feat_data: pd.DataFrame
Feature level explanations.
feat_threshold: float
The threshold to consider a feature relevant.
top_x_feats: float
Number of features to include as relevant
Returns
-------
Tuple[list, list]
Tuple containing two lists.
The first list contains the feature indexes ordered ascendingly.
The second list contains the correspondent feature names.
"""
ordered_feats = feat_data.iloc[(-feat_data['Shapley Value'].abs()).argsort()].reset_index()
top_feats_idx = []
for _, row in ordered_feats.iterrows():
if row['Feature'] == 'Pruned Events':
# we want to skip previous events
continue
if feat_threshold is not None and abs(row['Shapley Value']) < feat_threshold:
# we have reached the sopping point
break
top_feats_idx += [[row['index'], row['Feature']]]
if top_x_feats is not None and len(top_feats_idx) == top_x_feats:
# we have added enough events
break
df = pd.DataFrame(top_feats_idx, columns=['feat', 'name']).sort_values('feat')
return list(df['feat'].values), list(df['name'].values)
def considered_cells(event_data: pd.DataFrame,
feat_data: pd.DataFrame,
**kwargs,
) -> Tuple[Tuple[List, List], Tuple[List, List]]:
"""Calculates the indexes and names of events to participate in the cell level
given the conditions
Parameters
----------
event_data: pd.DataFrame
Event level explanations.
feat_data: pd.DataFrame
Feature level explanations.
Returns
-------
Tuple[Tuple[List, List], Tuple[List, List]]
Events and features to include in cell level computations
and their respective name
"""
top_events_idx, top_events_names = cell_top_events(event_data, **kwargs)
top_feats_idx, top_feats_names = cell_top_feats(feat_data, **kwargs)
return (top_events_idx, top_feats_idx), (top_events_names,top_feats_names)
def cell_level(f: Callable,
data: np.ndarray,
baseline: Union[pd.DataFrame, np.ndarray],
event_data: pd.DataFrame,
feat_data: pd.DataFrame,
random_seed: int,
nsamples: int,
cell_dict: dict,
pruned_idx: int,
model_feats=None,
) -> pd.DataFrame:
"""Cell level given relevant events and features
Parameters
----------
f : Callable
Prediction method of model being explained.
Will be called with a 3-D input
data: numpy.ndarray
Input matrix to use. First element of the first dimension is explained,
using the rest of the elements as context/hidden state.
baseline: numpy.ndarray
Baseline event to use. Median of numerical and mode for categorical.
event_data: pd.DataFrame
Event level explanations.
feat_data: pd.DataFrame
Feature level explanations.
random_seed: int
Used random seed for the sampling process.
nsamples: int
The number of coalitions for TimeSHAP to sample.
cell_dict: dict
Information required for the cell level explanation calculation
pruned_idx: int
Index to prune the sequence. All events up to this point are grouped\
model_feats: List
The list of feature names.
If none is provided, "Feature 1" format is used
Returns
-------
pd.DataFrame
"""
kwargs = {}
if cell_dict.get('threshold', False):
# single threshold for everything
kwargs['event_threshold'] = cell_dict.get('threshold')
kwargs['feat_threshold'] = cell_dict.get('threshold')
elif cell_dict.get('event_threshold', False) and cell_dict.get('feat_threshold', False):
kwargs['event_threshold'] = cell_dict.get('event_threshold')
kwargs['feat_threshold'] = cell_dict.get('feat_threshold')
elif cell_dict.get('top_x', False):
kwargs['top_x_events'] = cell_dict.get('top_x')
kwargs['top_x_feats'] = cell_dict.get('top_x')
elif cell_dict.get('top_x_events', False) and cell_dict.get('top_x_feats', False):
kwargs['top_x_events'] = cell_dict.get('top_x_events')
kwargs['top_x_feats'] = cell_dict.get('top_x_feats')
else:
raise ValueError("No threshold condition provided for cell level")
varying_cells, names = considered_cells(event_data, feat_data, **kwargs)
negative_indexes = np.array([False if x >= 0 else True for x in varying_cells[0]])
if any(negative_indexes):
if not all(negative_indexes):
raise ValueError("All indexes must be positive or negative. Not both")
varying_cells = ([data.shape[1] + x for x in varying_cells[0]], varying_cells[1])
explainer = TimeShapKernel(f, baseline, random_seed, "cell", varying=varying_cells)
explanation = explainer.shap_values(data, pruning_idx=pruned_idx, nsamples=nsamples)
ret_df_data = []
i = 0
for event in names[0]:
for feat in names[1]:
row = [event, feat, explanation[i]]
i += 1
ret_df_data += [row]
if explainer.special_cells[0]:
for event in names[0]:
row = [event, 'Other Features', explanation[i]]
i += 1
ret_df_data += [row]
if explainer.special_cells[1]:
for feat in names[1]:
row = ['Other Events', feat, explanation[i]]
i += 1
ret_df_data += [row]
if explainer.special_cells[2]:
ret_df_data += [["Other Events", "Other Features", explanation[i]]]
i += 1
if explainer.special_cells[3]:
ret_df_data += [["Pruned Events", "Pruned Events", explanation[i]]]
return pd.DataFrame(ret_df_data, columns=['Event', 'Feature',
'Shapley Value']).sort_values(
'Shapley Value', ascending=False)
def local_cell_level(f: Callable[[np.ndarray], np.ndarray],
data: Union[pd.DataFrame, np.array],
cell_dict: dict,
event_data: pd.DataFrame,
feat_data: pd.DataFrame,
entity_uuid,
entity_col,
baseline,
pruned_idx,
) -> pd.DataFrame:
"""Method to calculate event level explanations or load them if path is provided
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: pd.DataFrame
Sequence to explain.
cell_dict: dict
Information required for the cell level explanation calculation
event_data: pd.DataFrame
Event level explanations.
feat_data: pd.DataFrame
Feature level explanations.
entity_uuid: Union[str, int, float]
The indentifier of the sequence that is being pruned.
Used when fetching information from a csv of explanations
entity_col: str
Entity column to identify sequences
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`.
pruned_idx: int
Index to prune the sequence. All events up to this point are grouped
Returns
-------
pd.DataFrame
"""
if cell_dict.get("path") is None or not os.path.exists(cell_dict.get("path")):
#print("No path to cell data provided. Calculating data")
cell_data = cell_level(f, data, baseline, event_data, feat_data, cell_dict.get("rs"), cell_dict.get("nsamples"), cell_dict, pruned_idx)
if cell_dict.get("path") is not None:
# create directory
if '/' in cell_dict.get("path"):
Path(cell_dict.get("path").rsplit("/", 1)[0]).mkdir(parents=True, exist_ok=True)
cell_data.to_csv(cell_dict.get("path"), index=False)
elif cell_dict.get("path") is not None and os.path.exists(cell_dict.get("path")):
cell_data = pd.read_csv(cell_dict.get("path"))
if len(cell_data.columns) == 5 and entity_col is not None:
cell_data = cell_data[cell_data[entity_col] == entity_uuid]
elif len(cell_data.columns) == 4:
pass
else:
# TODO
# the provided csv should be generated by timeshap, by either
# explaining the whole dataset with TODO or just the instance in question
raise ValueError
else:
raise ValueError
return cell_data