-
Notifications
You must be signed in to change notification settings - Fork 28
/
local_methods.py
297 lines (240 loc) · 12.2 KB
/
local_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
# 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.plot import plot_local_report
from timeshap.explainer import local_pruning, local_event, local_feat, local_cell_level
from timeshap.utils import validate_input
def validate_local_input(f: Callable[[np.ndarray], np.ndarray],
data: np.array,
pruning_dict: dict,
event_dict: dict,
feature_dict: dict,
cell_dict: dict = None,
baseline: Union[pd.DataFrame, np.ndarray]=None,
model_features: List[Union[int, str]] = None,
entity_col: Union[str, int] = None,
time_col: Union[str, int] = None,
entity_uuid: str = None,
):
"""Verifies for local inputs if inputs are according
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: 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
model_features: List[str]
Features to be used by the model. Requires same order as training dataset
entity_col: str
Entity column to identify sequences
time_col: str
Data column that represents the time feature in order to sort sequences
temporally
"""
def check_dict(dict_to_check, key, types, message):
if dict_to_check.get(key):
assert isinstance(dict_to_check.get(key), types), message
validate_input(f, data, baseline, model_features, None, entity_col, time_col)
if isinstance(data, pd.DataFrame):
data_cols = set(data.columns)
if model_features:
assert set(model_features).issubset(data_cols), "When providing model features, these should be on the given DataFrame"
else:
print("Assuming all features are model features")
assert entity_col is None, "Entity col provided but no model features provided"
assert time_col is None, "Time col provided but no model features provided"
if entity_col is not None:
assert entity_col in data_cols, "When providing entity feature, these should be on the given DataFrame"
assert len(np.unique(data[entity_col].values)) == 1, "For local report, provided data must contain one instance only"
if time_col is not None:
assert time_col in data_cols, "When providing time feature, these should be on the given DataFrame"
else:
assert len(data.shape) == 3, "Provided data must be an numpy array with 3 dimensions"
assert data.shape[0] == 1, "For local report, provided data must contain one instance only"
assert baseline is None or isinstance(baseline, (pd.DataFrame, np.ndarray)), "Baseline must be a pd.DataFrame or np.ndarrays"
assert pruning_dict is None or pruning_dict.get("tol") is not None, "Prunning dict must have tolerance attribute"
if pruning_dict is not None:
assert isinstance(pruning_dict.get("tol"), (int, float)), "Provided tolerance must be a int or float"
if isinstance(pruning_dict.get("tol"), int):
assert pruning_dict.get("tol") == 0, "Provided tolerance must be a float or 0"
check_dict(event_dict, 'rs', int, "Provided random seed must be a int")
check_dict(event_dict, 'nsamples', int, "Provided nsamples must be a int")
check_dict(feature_dict, 'rs', int, "Provided random seed must be a int")
check_dict(feature_dict, 'nsamples', int, "Provided nsamples must be a int")
check_dict(feature_dict, 'top_feats', int, "Provided top_feats must be a int")
check_dict(feature_dict, 'plot_features', dict, "Provided plot_features must be a dict, mapping model features, to plot features")
if cell_dict is not None:
check_dict(cell_dict, 'rs', int, "Provided random seed must be a int")
check_dict(cell_dict, 'nsamples', int, "Provided nsamples must be a int")
# assert we have
if 'threshold' in cell_dict or 'top_x' in cell_dict:
provided = 'threshold' if 'threshold' in cell_dict else 'top_x'
assert 'feat_threshold' not in cell_dict, f"Provided both feat_threshold and {provided}. Please only provide one"
assert 'top_x_feats' not in cell_dict, f"Provided both top_x_feats and {provided}. Please only provide one"
assert 'event_threshold' not in cell_dict, f"Provided both event_threshold and {provided}. Please only provide one"
assert 'top_x_events' not in cell_dict, f"Provided both top_x_events and {provided}. Please only provide one"
else:
if not('feat_threshold' in cell_dict or 'top_x_feats' in cell_dict):
raise ValueError("No way to determine relevant features for cell level")
if not ('event_threshold' in cell_dict or 'top_x_events' in cell_dict):
raise ValueError( "No way to determine relevant events for cell level")
def calc_local_report(f: Callable[[np.ndarray], np.ndarray],
data: Union[pd.DataFrame, np.array],
pruning_dict: dict,
event_dict: dict,
feature_dict: dict,
cell_dict: dict = None,
baseline: Union[pd.DataFrame, np.ndarray] = None,
model_features: List[Union[int, str]] = None,
entity_col=None,
entity_uuid=None,
time_col=None,
verbose=False,
) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
"""Calculates local 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[pd.DataFrame, np.array]
Sequence to be explained.
pruning_dict: dict
Information required for pruning algorithm
event_dict: dict
Information required for the event level explanation calculation
feature_dict: dict
Information required for the feature level explanation calculation
cell_dict: dict
Information required for the cell level explanation calculation
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
time_col: str
Data column that represents the time feature in order to sort sequences
temporally
model_features: List[str]
Features to be used by the model. Requires same order as training dataset
baseline: Union[pd.DataFrame, np.array]
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`.
verbose: bool
If process is verbose
Returns
-------
pd.DataFrame
Local pruning algorithm data
pd.DataFrame
Local event explanations
pd.DataFrame
Local feature explanations
pd.DataFrame
Local cell explanations
"""
validate_local_input(f, data, pruning_dict, event_dict, feature_dict, cell_dict,
baseline, model_features, entity_col, time_col, entity_uuid)
# deals with given date being a DataFrame
if isinstance(data, pd.DataFrame):
if time_col is not None:
data[time_col] = data[[time_col]].apply(pd.to_numeric)
data = data.sort_values(time_col)
if model_features is not None:
data = data[model_features]
else:
data = data.values
data = np.expand_dims(data.to_numpy().copy(), axis=0).astype(float)
if pruning_dict is None:
print("No pruning dict passed. Skipping pruning procedures")
pruning_idx = 0
coal_plot_data = None
else:
coal_plot_data, coal_prun_idx = local_pruning(f, data, pruning_dict, baseline, entity_uuid, entity_col, verbose)
pruning_idx = data.shape[1] + coal_prun_idx
event_data = local_event(f, data, event_dict, entity_uuid, entity_col, baseline, pruning_idx)
feature_data = local_feat(f, data, feature_dict, entity_uuid, entity_col, baseline, pruning_idx)
if cell_dict:
cell_data = local_cell_level(f, data, cell_dict, event_data, feature_data, entity_uuid, entity_col, baseline, pruning_idx)
else:
cell_data = None
return coal_plot_data, event_data, feature_data, cell_data
def local_report(f: Callable[[np.ndarray], np.ndarray],
data: Union[pd.DataFrame, np.array],
pruning_dict: dict,
event_dict: dict,
feature_dict: dict,
cell_dict: dict = None,
baseline: Union[pd.DataFrame, np.array] = None,
model_features: List[Union[str, int]] = None,
entity_col: str = None,
entity_uuid: str = None,
time_col: str = None,
verbose=False,
):
"""Calculates local report and plots it.
`None` on the pruning_dict argument makes TimeSHAP skip the pruning step.
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[pd.DataFrame, np.array]
Sequence to be explained.
pruning_dict: dict
Information required for pruning algorithm
event_dict: dict
Information required for the event level explanation calculation
feature_dict: dict
Information required for the feature level explanation calculation
cell_dict: dict
Information required for the cell level explanation calculation
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
time_col: str
Data column that represents the time feature in order to sort sequences
temporally
model_features: List[str]
Features to be used by the model. Requires same order as training dataset
baseline: Union[pd.DataFrame, np.array]
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`.
verbose: bool
If process is verbose
"""
pruning_data, event_data, feature_data, cell_level = \
calc_local_report(f, data, pruning_dict, event_dict, feature_dict,
cell_dict, baseline, model_features, entity_col,
entity_uuid, time_col, verbose
)
plot = plot_local_report(pruning_dict, event_dict, feature_dict, cell_dict,
pruning_data, event_data, feature_data, cell_level
)
return plot