forked from abacusai/xai-bench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
270 lines (239 loc) · 10.2 KB
/
main.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
# pylint: disable=R0801
import argparse
from datetime import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib as mpl
import pathlib
import sys
try:
# try importing as if xai_bench is a module
from .src import datasets, explainer, metric, experiments, model
except ImportError as e:
if "relative import" in str(e):
# try importing as if called from this file
sys.path.append('../')
from src import datasets, explainer, metric, experiments, model
else:
raise e
current_dir = pathlib.Path(__file__).parent
plt.style.use('https://raw.githubusercontent.com/RobGeada/stylelibs/main/material_rh.mplstyle')
"""
Define test configs
- rhos: rho determines feature independence of generated datasets, part of metric grid
- bench_datasets: datasets to bench, part of metric grid
- bench_explainers: the explainers to benchmark per grid entry
- each explainer has a name and set of kwargs to pass to explainer.__init__
- bench_models: models to bench, part of metric grid
- bench_metrics: metrics to include in grid
- num_features: features per generated dataset
- benchmark runs in a grid of rhos x datasets x explainers x models x metrics
"""
LEVEL_0_CONFIG = {
"rhos": [.5],
"bench_datasets": ["gaussianPiecewiseConstant"],
"bench_explainers": {
"shap_trustyai": {},
"lime_trustyai": {"samples": 100, "normalise_weights": False, "separable_dataset_ratio": .9},
},
"bench_models": ["mlp"],
"bench_metrics": ["roar_faithfulness", "roar_monotonicity", "faithfulness", "monotonicity",
"shapley", "shapley_corr", "infidelity"],
"num_features": 3
}
LEVEL_1_CONFIG = {
"rhos": np.linspace(0, 1, 5),
"bench_datasets": ["gaussianLinear", "gaussianNonLinearAdditive", "gaussianPiecewiseConstant"],
"bench_explainers": {
"shap_trustyai": {},
"shap": {},
"lime_trustyai": {"samples": 100, "normalise_weights": False, "separable_dataset_ratio": .9},
"lime": {"samples": 100},
"random": {}
},
"bench_models": ["lr", "dtree", "mlp"],
"bench_metrics": ["roar_faithfulness", "roar_monotonicity", "faithfulness", "monotonicity",
"shapley", "shapley_corr", "infidelity"],
"num_features": 5
}
LEVEL_LIME_CONFIG = {
"rhos": np.linspace(0, 1, 5),
"bench_datasets": ["gaussianLinear", "gaussianNonLinearAdditive", "gaussianPiecewiseConstant"],
"bench_explainers": {
"lime_trustyai": {"samples": 100, "normalise_weights": False, "separable_dataset_ratio": .9},
"lime": {"samples": 100},
"random": {}
},
"bench_models": ["lr", "dtree", "mlp"],
"bench_metrics": ["roar_faithfulness", "roar_monotonicity", "faithfulness", "monotonicity",
"shapley", "shapley_corr", "infidelity"],
"num_features": 5
}
LEVEL_SHAP_CONFIG = {
"rhos": np.linspace(0, 1, 5),
"bench_datasets": ["gaussianLinear", "gaussianNonLinearAdditive", "gaussianPiecewiseConstant"],
"bench_explainers": {
"shap_trustyai": {},
"shap": {},
"random": {}
},
"bench_models": ["lr", "dtree", "mlp"],
"bench_metrics": ["roar_faithfulness", "roar_monotonicity", "faithfulness", "monotonicity",
"shapley", "shapley_corr", "infidelity"],
"num_features": 5
}
LEVEL_2_CONFIG = {
"rhos": np.linspace(0, 1, 5),
"bench_datasets": ["gaussianLinear", "gaussianNonLinearAdditive", "gaussianPiecewiseConstant",
"mixtureLinear", "mixtureNonLinearAdditive", "mixturePiecewiseConstant"],
"bench_explainers": {
"shap_trustyai": {},
"shap": {},
"lime_trustyai": {"samples": 100, "normalise_weights": False, "separable_dataset_ratio": .9},
"lime": {"samples": 100, },
"random": {}
},
"bench_models": ["lr", "dtree", "mlp"],
"bench_metrics": ["roar_faithfulness", "roar_monotonicity", "faithfulness", "monotonicity",
"shapley", "shapley_corr", "infidelity"],
"num_features": 7
}
configs = [LEVEL_0_CONFIG, LEVEL_1_CONFIG, LEVEL_2_CONFIG]
config_dict = {
"0": LEVEL_0_CONFIG,
"1": LEVEL_1_CONFIG,
"2": LEVEL_2_CONFIG,
"lime": LEVEL_LIME_CONFIG,
"shap": LEVEL_SHAP_CONFIG
}
def run_test_config(test_config):
"""Run a particular test config"""
data = []
for dataset in test_config['bench_datasets']:
for rho in test_config['rhos']:
if "gaussian" in dataset:
exp_dataset = datasets.Data(
name=dataset,
mode="regression",
mu=np.zeros(test_config['num_features']),
rho=rho,
dim=test_config['num_features'],
noise=0.01,
weight=np.arange(test_config['num_features'] - 1, -1, -1),
num_train_samples=1000,
num_val_samples=100)
else:
exp_dataset = datasets.Data(
name=dataset,
mode="regression",
mus=[np.ones(test_config['num_features']), (-1 * np.ones(test_config['num_features']))],
rho=rho,
dim=test_config['num_features'],
noise=0.01,
weight=np.arange(test_config['num_features'] - 1, -1, -1),
num_train_samples=1000,
num_val_samples=100)
exp_models = [model.Model(name=m, mode="regression") for m in test_config['bench_models']]
exp_explainers = [explainer.Explainer(name=e, **k) for e, k in test_config['bench_explainers'].items()]
exp_metrics = [metric.Metric(name=m, conditional="observational")
for m in test_config['bench_metrics']]
experiment_results = experiments.Experiment(
exp_dataset,
exp_models,
exp_explainers,
exp_metrics
).get_results()
data += experiment_results
return pd.DataFrame(data)
def plot_test_results(df, suffix):
"""Show the results of a benchmark as violin plots"""
# get data information from df
metrics = [x for x in list(df) if "metric" in x or x == 'runtime']
metric_down = ["metric_infidelity", "metric_runtime", "metric_shapley", "runtime"]
metric_labels = {m: m + (" ↓" if m in metric_down else " ↑") for m in metrics}
explainers = df['explainer'].unique()
n_exp = len(explainers)
# setup plots
cmap = mpl.colormaps['viridis']
fig = plt.figure(figsize=(16, 9))
labels = []
# iterate through metrics
for metric_idx, metric_name in enumerate(metrics):
# iterate over explainers
for exp_idx, explainer_name in enumerate(explainers):
# slice dataframe over explainer
exp_df = df[df['explainer'] == explainer_name]
# grab metric data for this explainer
color = cmap(exp_idx / n_exp)
plt.subplot(2, 4, metric_idx + 1)
xs = []
ys = []
for idx, (_, row) in enumerate(exp_df.iterrows()):
xs.append(exp_idx)
ys.append(row[metric_name])
# plot metric data of this explainer
plt.scatter(xs + ((np.random.rand(len(xs)) - .5) / 5) + .5, ys, c=[color] * len(xs), s=1)
violin_parts = plt.violinplot(ys, positions=[exp_idx + .5], showmeans=True)
# color violins per-explainer
for part_name in ('cbars', 'cmins', 'cmaxes', 'cmeans'):
violin_parts[part_name].set_color(color)
for v_idx, pc in enumerate(violin_parts['bodies']):
pc.set_facecolor(color)
pc.set_edgecolor(color)
# create legend labels
if metric_idx == 0:
labels.append([mpatches.Patch(color=color), explainer_name])
# format plot
ax = plt.gca()
locs = np.arange(n_exp)
ax.xaxis.set_ticks(locs, minor=True)
if "runtime" not in metric_name:
ax.set_ylim(0, 1)
ax.yaxis.set_ticks(np.linspace(0, 1, 5), minor=True)
else:
ax.yaxis.set_ticks(np.array(plt.yticks()[0]), minor=True)
ax.xaxis.set(ticks=locs + .5, ticklabels=explainers)
ax.grid(True, which='minor', axis='x')
ax.grid(False, which='major', axis='x')
plt.xticks(rotation=45, ha='right')
plt.title(
metric_labels[metric_name]
.replace("metric_", "")
.replace("_", " ")
.title()
.replace("Roar", "ROAR")
)
# title and save
plt.suptitle("TrustyAI XAIBench L{} @ {}".format(
suffix,
datetime.strftime(datetime.now(), "%H:%M %Y-%m-%d")
), color='k', fontsize=16)
plt.tight_layout()
fig.legend(*zip(*labels), loc='lower center', bbox_to_anchor=(.5, -.05))
plt.savefig(
current_dir / "results" / "plots" / "xai_bench_{}.pdf".format(suffix),
bbox_inches='tight'
)
plt.show()
def run_benchmark_config(config, save=False, plot=False):
"""Run a benchmark config, optionally plot and save the results to file"""
results_df = run_test_config(configs[config])
if plot:
plot_test_results(results_df, config)
if save:
results_df.to_pickle(current_dir / "results" / "level_{}_results.pkl".format(config))
return results_df
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Benchmark explainers with xai-bench.')
parser.add_argument("-c", "--config", type=str, required=True,
help='The benchmark config to run. See the readme for the details of each')
parser.add_argument("-l", "--label", type=str,
help="A shorthand name for this benchmark run to remember it")
args = parser.parse_args()
suffix = args.config + "_" + args.label if args.label else args.config
"""Run a benchmark config, then plot and save the results to file"""
results_df = run_test_config(config_dict[args.config.lower()])
plot_test_results(results_df, suffix)
results_df.to_pickle(current_dir / "results" / "level_{}_results.pkl".format(suffix))