-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlocal_explanations.py
278 lines (241 loc) · 6.39 KB
/
local_explanations.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
import os
from os.path import join
from pathlib import Path
import matplotlib as mpl
import numpy as np
import seaborn as sns
import toml
import torch
from PIL import Image
from matplotlib import pyplot as plt
from matplotlib.gridspec import GridSpec
from data.data_ukb import get_pgs_imaging_data, get_genetics_imaging_data
from feature_explanations import FONT_PROPERTIES, get_pgs_labels, run_explanations
from models.model_loading import LoaderModel
sns.set_style("whitegrid")
# TODO: might need to adapt that
FONT_PROPERTIES = Path(mpl.get_data_path(), "Calibri.ttf")
WEIGHTS_BASE = toml.load("paths.toml")["CHECKPOINTS_BASE_PATH"]
PGS_WEIGHTS = join(WEIGHTS_BASE, "cm_r50_risk_scores_gen_h1/last.ckpt")
RAW_WEIGHTS = join(WEIGHTS_BASE, "cm_r50_raw_snps_h1/last.ckpt")
# TODO: fix iids for which to create local explanations
INDIVIDUALS = [
"2667657",
]
def final_plots():
method = "ig-noise"
od = "local_plots_final"
bs = 1000
h1 = 2048
h2 = None
explain_indivs_pgs(out_dir=od, top=15, bot=15, bs=bs, method=method, h1=h1, h2=h2)
explain_indivs_raw(out_dir=od, top=15, bot=15, bs=bs, method=method, h1=h1, h2=h2)
# convenience 1-liners
def explain_indivs_raw(
iids=INDIVIDUALS,
out_dir="exp_plots",
load_result=None,
imgs=None,
top=6,
mid=0,
bot=6,
method="ig-noise",
bs=10,
h1=2048,
h2=None,
):
if load_result is None or imgs is None:
load_result, imgs = load_raw_indiv(iids, bs=bs, h1=h1, h2=h2)
explain_indivs_base(
load_result=load_result,
imgs=imgs,
out_dir=out_dir,
fn_templ="indiv_raw_{iid}.svg",
top=top,
mid=mid,
bot=bot,
method=method,
)
def explain_indivs_pgs(
iids=INDIVIDUALS,
load_result=None,
imgs=None,
top=6,
mid=0,
bot=6,
method="ig-noise",
bs=10,
out_dir="exp_plots",
h1=2048,
h2=None,
):
if load_result is None or imgs is None:
load_result, imgs = load_pgs_indiv(
indivs=iids,
weights=PGS_WEIGHTS,
bs=bs,
h1=h1,
h2=h2,
)
explain_indivs_base(
load_result=load_result,
imgs=imgs,
out_dir=out_dir,
fn_templ="indiv_pgs_{iid}.svg",
top=top,
mid=mid,
bot=bot,
method=method,
)
def explain_indivs_base(
load_result,
imgs,
out_dir="exp_plots",
fn_templ="{iid}.pdf",
top=10,
mid=10,
bot=10,
method="ig-noise",
):
os.makedirs(out_dir, exist_ok=True)
M, rI, rG, I, G, iid, L = load_result
exps = run_explanations(
img=I,
gen=G,
ref_img=rI,
ref_gen=rG,
model=M,
methods=[method],
)
for i, img in enumerate(imgs):
fn = join(out_dir, fn_templ.format(iid=iid[i]))
plot_local_exp(
img=img,
exp=exps[method][i],
labels=L,
fn=fn,
top=top,
bot=bot,
mid=mid,
)
# plotting
def plot_local_exp(
exp,
labels,
fn="tmp.pdf",
top=10,
bot=10,
figsize=(5, 10),
):
fig = plt.figure(figsize=figsize)
gs = GridSpec(10, 1)
gs.update(wspace=0.01, hspace=0.31)
ax2 = fig.add_subplot(gs[:, 0])
asort = exp.argsort()
cmap1 = sns.cubehelix_palette(
start=-0.2, rot=0.6, reverse=True, light=0.7, dark=0.25, as_cmap=True
)
cmap2 = sns.cubehelix_palette(
start=0.5, rot=-0.5, reverse=False, light=0.7, dark=0.25, as_cmap=True
)
exp_bot = exp[asort[:bot]]
ax2.barh(
np.arange(bot),
exp_bot,
label="bottom",
color=[cmap1(x) for x in np.linspace(0, 1, bot)],
)
ax2.barh([bot], 0)
exp_top = exp[asort[-top:]]
ax2.barh(
np.arange(bot + 1, bot + 1 + top),
exp_top,
label="top",
color=[cmap2(x) for x in np.linspace(0, 1, top)],
)
xticks = np.arange(bot + 1 + top)
xticklabels = (
[l[:25] for l in np.array(labels)[asort[:bot]]]
+ ["..."]
+ [l[:25] for l in np.array(labels)[asort[-top:]]]
)
plt.xticks(fontsize=19, fontproperties=FONT_PROPERTIES)
ax2.set_yticks(xticks)
ax2.set_yticklabels(xticklabels, fontsize=19, fontproperties=FONT_PROPERTIES)
ax2.set_xlabel("Attribution score", fontsize=25, fontproperties=FONT_PROPERTIES)
ax2.set_ylabel("Feature", fontsize=25, fontproperties=FONT_PROPERTIES)
ax2.invert_yaxis()
plt.savefig(fn, bbox_inches="tight", dpi=300)
# data loading
def load_raw_indiv(
indivs,
bs=10,
weights=RAW_WEIGHTS,
dev="cuda:0",
sid_slice=slice(0, None, 100),
h1=2048,
h2=None,
):
(tl, vl, ttl), nf = get_genetics_imaging_data(
rsids=None,
sid_slice=sid_slice,
burdens_zeros=None,
size=448,
batch_size=bs,
return_iid=True,
)
labels = tl.dataset.datasets[0].feature_names
return load_indiv_base(
indivs=indivs,
weights=weights,
tl=tl,
vl=vl,
ttl=ttl,
nf=nf,
dev=dev,
h1=h1,
h2=h2,
labels=labels,
)
def load_pgs_indiv(indivs, bs=10, weights=PGS_WEIGHTS, h1=2048, h2=None, dev="cuda:0"):
(tl, vl, ttl), nf = get_pgs_imaging_data(size=448, batch_size=bs, return_iid=True)
labels = get_pgs_labels()
return load_indiv_base(
indivs=indivs,
weights=weights,
tl=tl,
vl=vl,
ttl=ttl,
nf=nf,
dev=dev,
labels=labels,
h1=h1,
h2=h2,
)
def load_indiv_base(
indivs, weights, tl, vl, ttl, nf, h1=2048, h2=None, dev="cuda:0", labels=None
):
ref_img, _, ref_gen, _ = next(iter(ttl))
all_iids = [dl.dataset.datasets[0].iids for dl in [tl, vl, ttl]]
idxs = dict()
for j, iids in enumerate(all_iids):
for iid in indivs:
if iid in iids:
idxs[iid] = (j, list(iids).index(iid))
imgs = []
timgs = []
gens = []
for iid in indivs:
dset = [tl, vl, ttl][idxs[iid][0]].dataset.datasets[0]
idx = idxs[iid][1]
t, _, gen, _ = dset[idx]
img = Image.open(dset.paths[idx])
timgs.append(t)
imgs.append(img)
gens.append(gen)
timgs = torch.stack(timgs)
gens = torch.stack(gens)
LM = LoaderModel(nf, h1=h1, h2=h2)
LM.init(weights)
ret = (LM.model.eval().to(dev), ref_img, ref_gen, timgs, gens, indivs, labels)
return ret, imgs