-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_heatmap.py
More file actions
103 lines (76 loc) · 3.17 KB
/
Copy pathplot_heatmap.py
File metadata and controls
103 lines (76 loc) · 3.17 KB
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
import os
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
path_fig = os.path.join(
'figs_heatmap',
'dataset_id={}-heatmap.pdf'
)
labels = ['1.5-norm', '2.5-norm', '5-norm', '10-norm', r'$\infty$-norm', 'ELC',
'Banzhaf']
for key in [(16,1), (8,1), (4,1), (2,1), (1,1), (1,2), (1,4), (1,8), (1,16), (1,32)]:
labels.append(str(key))
keys = [1.5, 2.5, 5, 10, np.inf, 'ELC', 0.5]
def plot_heatmap(curves, dataset_id):
path_components = path_fig.split(os.sep)
os.makedirs(os.sep.join(path_components[:-1]), exist_ok=True)
candidates = []
for key in keys:
curve = curves[key]
if curve[0,0,0] == np.inf:
candidates.append(curve[:, -1, :].sum(axis=1))
else:
candidates.append(curve[:, :-1, :].sum(axis=-1).mean(axis=-1))
for key in [(16,1), (8,1), (4,1), (2,1), (1,1), (1,2), (1,4), (1,8), (1,16), (1,32)]:
curve = curves[key]
candidates.append(curve[:, -1, :].sum(axis=1))
A = np.empty((17, 17), dtype=np.float64)
for i, row in enumerate(candidates):
for j, column in enumerate(candidates):
A[i, j] = ((row > column).sum() - (row < column).sum()) / 100
fig, ax = plt.subplots(figsize=(32, 24))
sns.heatmap(
A,
annot=True,
fmt='.2f',
vmin=0, vmax=1,
xticklabels=labels,
yticklabels=labels,
linewidths=0.5,
linecolor='white',
ax=ax,
annot_kws={'size': 20, 'weight': 'bold'},
cbar_kws={'pad': 0.01}
)
ax.axhline(y=6, color='blue', linewidth=5)
ax.axvline(x=6, color='blue', linewidth=5)
cbar = ax.collections[0].colorbar
cbar.ax.tick_params(labelsize=20)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90, ha='right', fontsize=20)
ax.set_yticklabels(ax.get_yticklabels(), rotation=0, fontsize=20)
plt.savefig(path_fig.format(dataset_id), bbox_inches='tight')
plt.close(fig)
if __name__ == '__main__':
from main import arg_dict
from args import process_arg_dict
from collections import defaultdict
args = process_arg_dict(arg_dict)
args_dataset = defaultdict(list)
for arg in args:
args_dataset[arg['dataset_id']].append(arg)
for dataset_id, args_2nd in args_dataset.items():
print(dataset_id)
data = np.load(args_2nd[0]['path_data'])
n_features = len(data['attribution'])
curves = defaultdict(lambda : np.full((100, 6, n_features+1), np.inf, dtype=np.float64))
for arg in args_2nd:
data = np.load(arg['path_data'])
auc = data['auc']
if arg['method'] == 'semivalue':
curves[arg['weight']][arg['instance_id'], -1] = auc
else:
if arg['random_seed_sampling'] == 'exact':
curves[arg['p']][arg['instance_id'], -1] = auc
else:
curves[arg['p']][arg['instance_id'], arg['random_seed_sampling']] = auc
plot_heatmap(curves, dataset_id)