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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
import matplotlib.pylab as plt
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
from vae import HVAE
|
8 |
+
from datasets import morphomnist, ukbb, mimic, get_attr_max_min
|
9 |
+
from pgm.flow_pgm import MorphoMNISTPGM, FlowPGM, ChestPGM
|
10 |
+
from app_utils import (
|
11 |
+
mnist_graph,
|
12 |
+
brain_graph,
|
13 |
+
chest_graph,
|
14 |
+
vae_preprocess,
|
15 |
+
normalize,
|
16 |
+
preprocess_brain,
|
17 |
+
get_fig_arr,
|
18 |
+
postprocess,
|
19 |
+
MidpointNormalize,
|
20 |
+
)
|
21 |
+
|
22 |
+
DATA, MODELS = {}, {}
|
23 |
+
for k in ["Morpho-MNIST", "Brain MRI", "Chest X-ray"]:
|
24 |
+
DATA[k], MODELS[k] = {}, {}
|
25 |
+
|
26 |
+
# mnist
|
27 |
+
DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
28 |
+
# brain
|
29 |
+
MRISEQ_CAT = ["T1", "T2-FLAIR"] # 0,1
|
30 |
+
SEX_CAT = ["female", "male"] # 0,1
|
31 |
+
HEIGHT, WIDTH = 270, 270
|
32 |
+
# chest
|
33 |
+
SEX_CAT_CHEST = ["male", "female"] # 0,1
|
34 |
+
RACE_CAT = ["white", "asian", "black"] # 0,1,2
|
35 |
+
FIND_CAT = ["no disease", "pleural effusion"]
|
36 |
+
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
37 |
+
|
38 |
+
|
39 |
+
class Hparams:
|
40 |
+
def update(self, dict):
|
41 |
+
for k, v in dict.items():
|
42 |
+
setattr(self, k, v)
|
43 |
+
|
44 |
+
|
45 |
+
def get_paths(dataset_id):
|
46 |
+
if "MNIST" in dataset_id:
|
47 |
+
data_path = "./data/morphomnist"
|
48 |
+
pgm_path = "./checkpoints/t_i_d/sup_pgm/checkpoint.pt"
|
49 |
+
vae_path = "./checkpoints/t_i_d/dgauss_cond_big_beta1_dropexo/checkpoint.pt"
|
50 |
+
elif "Brain" in dataset_id:
|
51 |
+
data_path = "./data/ukbb_subset"
|
52 |
+
pgm_path = "./checkpoints/m_b_v_s/sup_pgm/checkpoint.pt"
|
53 |
+
vae_path = "./checkpoints/m_b_v_s/ukbb192_beta5_dgauss_b33/checkpoint.pt"
|
54 |
+
elif "Chest" in dataset_id:
|
55 |
+
data_path = "./data/mimic_subset"
|
56 |
+
pgm_path = "./checkpoints/a_r_s_f/sup_pgm_mimic/checkpoint.pt"
|
57 |
+
vae_path = [
|
58 |
+
"./checkpoints/a_r_s_f/mimic_beta9_gelu_dgauss_1_lr3/checkpoint.pt", # base vae
|
59 |
+
"./checkpoints/a_r_s_f/mimic_dscm_lr_1e5_lagrange_lr_1_damping_10/6500_checkpoint.pt", # cf trained DSCM
|
60 |
+
]
|
61 |
+
return data_path, vae_path, pgm_path
|
62 |
+
|
63 |
+
|
64 |
+
def load_pgm(dataset_id, pgm_path):
|
65 |
+
checkpoint = torch.load(pgm_path, map_location=DEVICE)
|
66 |
+
args = Hparams()
|
67 |
+
args.update(checkpoint["hparams"])
|
68 |
+
args.device = DEVICE
|
69 |
+
if "MNIST" in dataset_id:
|
70 |
+
pgm = MorphoMNISTPGM(args).to(args.device)
|
71 |
+
elif "Brain" in dataset_id:
|
72 |
+
pgm = FlowPGM(args).to(args.device)
|
73 |
+
elif "Chest" in dataset_id:
|
74 |
+
pgm = ChestPGM(args).to(args.device)
|
75 |
+
pgm.load_state_dict(checkpoint["ema_model_state_dict"])
|
76 |
+
MODELS[dataset_id]["pgm"] = pgm
|
77 |
+
MODELS[dataset_id]["pgm_args"] = args
|
78 |
+
|
79 |
+
|
80 |
+
def load_vae(dataset_id, vae_path):
|
81 |
+
if "Chest" in dataset_id:
|
82 |
+
vae_path, dscm_path = vae_path[0], vae_path[1]
|
83 |
+
checkpoint = torch.load(vae_path, map_location=DEVICE)
|
84 |
+
args = Hparams()
|
85 |
+
args.update(checkpoint["hparams"])
|
86 |
+
# backwards compatibility hack
|
87 |
+
if not hasattr(args, "vae"):
|
88 |
+
args.vae = "hierarchical"
|
89 |
+
if not hasattr(args, "cond_prior"):
|
90 |
+
args.cond_prior = False
|
91 |
+
if hasattr(args, "free_bits"):
|
92 |
+
args.kl_free_bits = args.free_bits
|
93 |
+
args.device = DEVICE
|
94 |
+
vae = HVAE(args).to(args.device)
|
95 |
+
|
96 |
+
if "Chest" in dataset_id:
|
97 |
+
dscm_ckpt = torch.load(dscm_path, map_location=DEVICE)
|
98 |
+
vae.load_state_dict(
|
99 |
+
{
|
100 |
+
k[4:]: v
|
101 |
+
for k, v in dscm_ckpt["ema_model_state_dict"].items()
|
102 |
+
if "vae." in k
|
103 |
+
}
|
104 |
+
)
|
105 |
+
else:
|
106 |
+
vae.load_state_dict(checkpoint["ema_model_state_dict"])
|
107 |
+
MODELS[dataset_id]["vae"] = vae
|
108 |
+
MODELS[dataset_id]["vae_args"] = args
|
109 |
+
|
110 |
+
|
111 |
+
def get_dataloader(dataset_id, data_path):
|
112 |
+
MODELS[dataset_id]["pgm_args"].data_dir = data_path
|
113 |
+
args = MODELS[dataset_id]["pgm_args"]
|
114 |
+
if "MNIST" in dataset_id:
|
115 |
+
datasets = morphomnist(args)
|
116 |
+
elif "Brain" in dataset_id:
|
117 |
+
datasets = ukbb(args)
|
118 |
+
elif "Chest" in dataset_id:
|
119 |
+
datasets = mimic(args)
|
120 |
+
DATA[dataset_id]["test"] = torch.utils.data.DataLoader(
|
121 |
+
datasets["test"], shuffle=False, batch_size=args.bs, num_workers=4
|
122 |
+
)
|
123 |
+
|
124 |
+
|
125 |
+
def load_dataset(dataset_id):
|
126 |
+
data_path, _, pgm_path = get_paths(dataset_id)
|
127 |
+
checkpoint = torch.load(pgm_path, map_location=DEVICE)
|
128 |
+
args = Hparams()
|
129 |
+
args.update(checkpoint["hparams"])
|
130 |
+
args.device = DEVICE
|
131 |
+
MODELS[dataset_id]["pgm_args"] = args
|
132 |
+
get_dataloader(dataset_id, data_path)
|
133 |
+
|
134 |
+
|
135 |
+
def load_model(dataset_id):
|
136 |
+
_, vae_path, pgm_path = get_paths(dataset_id)
|
137 |
+
load_pgm(dataset_id, pgm_path)
|
138 |
+
load_vae(dataset_id, vae_path)
|
139 |
+
|
140 |
+
|
141 |
+
@torch.no_grad()
|
142 |
+
def counterfactual_inference(dataset_id, obs, do_pa):
|
143 |
+
pa = {k: v.clone() for k, v in obs.items() if k != "x"}
|
144 |
+
cf_pa = MODELS[dataset_id]["pgm"].counterfactual(
|
145 |
+
obs=pa, intervention=do_pa, num_particles=1
|
146 |
+
)
|
147 |
+
args, vae = MODELS[dataset_id]["vae_args"], MODELS[dataset_id]["vae"]
|
148 |
+
_pa = vae_preprocess(args, {k: v.clone() for k, v in pa.items()})
|
149 |
+
_cf_pa = vae_preprocess(args, {k: v.clone() for k, v in cf_pa.items()})
|
150 |
+
z_t = 0.1 if "mnist" in args.hps else 1.0
|
151 |
+
z = vae.abduct(x=obs["x"], parents=_pa, t=z_t)
|
152 |
+
if vae.cond_prior:
|
153 |
+
z = [z[j]["z"] for j in range(len(z))]
|
154 |
+
px_loc, px_scale = vae.forward_latents(latents=z, parents=_pa)
|
155 |
+
cf_loc, cf_scale = vae.forward_latents(latents=z, parents=_cf_pa)
|
156 |
+
u = (obs["x"] - px_loc) / px_scale.clamp(min=1e-12)
|
157 |
+
u_t = 0.1 if "mnist" in args.hps else 1.0 # cf sampling temp
|
158 |
+
cf_scale = cf_scale * u_t
|
159 |
+
cf_x = torch.clamp(cf_loc + cf_scale * u, min=-1, max=1)
|
160 |
+
return {"cf_x": cf_x, "rec_x": px_loc, "cf_pa": cf_pa}
|
161 |
+
|
162 |
+
|
163 |
+
def get_obs_item(dataset_id, idx=None):
|
164 |
+
if idx is None:
|
165 |
+
n_test = len(DATA[dataset_id]["test"].dataset)
|
166 |
+
idx = torch.randperm(n_test)[0]
|
167 |
+
idx = int(idx)
|
168 |
+
return idx, DATA[dataset_id]["test"].dataset.__getitem__(idx)
|
169 |
+
|
170 |
+
|
171 |
+
def get_mnist_obs(idx=None):
|
172 |
+
dataset_id = "Morpho-MNIST"
|
173 |
+
if not DATA[dataset_id]:
|
174 |
+
load_dataset(dataset_id)
|
175 |
+
idx, obs = get_obs_item(dataset_id, idx)
|
176 |
+
x = get_fig_arr(obs["x"].clone().squeeze().numpy())
|
177 |
+
t = (obs["thickness"].clone() + 1) / 2 * (6.255515 - 0.87598526) + 0.87598526
|
178 |
+
i = (obs["intensity"].clone() + 1) / 2 * (254.90317 - 66.601204) + 66.601204
|
179 |
+
y = DIGITS[obs["digit"].clone().argmax(-1)]
|
180 |
+
return (idx, x, float(np.round(t, 2)), float(np.round(i, 2)), y)
|
181 |
+
|
182 |
+
|
183 |
+
def get_brain_obs(idx=None):
|
184 |
+
dataset_id = "Brain MRI"
|
185 |
+
if not DATA[dataset_id]:
|
186 |
+
load_dataset(dataset_id)
|
187 |
+
idx, obs = get_obs_item(dataset_id, idx)
|
188 |
+
x = get_fig_arr(obs["x"].clone().squeeze().numpy())
|
189 |
+
m = MRISEQ_CAT[int(obs["mri_seq"].clone().item())]
|
190 |
+
s = SEX_CAT[int(obs["sex"].clone().item())]
|
191 |
+
a = obs["age"].clone().item()
|
192 |
+
b = obs["brain_volume"].clone().item() / 1000 # in ml
|
193 |
+
v = obs["ventricle_volume"].clone().item() / 1000 # in ml
|
194 |
+
return (idx, x, m, s, a, float(np.round(b, 2)), float(np.round(v, 2)))
|
195 |
+
|
196 |
+
|
197 |
+
def get_chest_obs(idx=None):
|
198 |
+
dataset_id = "Chest X-ray"
|
199 |
+
if not DATA[dataset_id]:
|
200 |
+
load_dataset(dataset_id)
|
201 |
+
idx, obs = get_obs_item(dataset_id, idx)
|
202 |
+
x = get_fig_arr(postprocess(obs["x"].clone()))
|
203 |
+
s = SEX_CAT_CHEST[int(obs["sex"].clone().squeeze().numpy())]
|
204 |
+
f = FIND_CAT[int(obs["finding"].clone().squeeze().numpy())]
|
205 |
+
r = RACE_CAT[obs["race"].clone().squeeze().numpy().argmax(-1)]
|
206 |
+
a = (obs["age"].clone().squeeze().numpy() + 1) * 50
|
207 |
+
return (idx, x, r, s, f, float(np.round(a, 1)))
|
208 |
+
|
209 |
+
|
210 |
+
def infer_mnist_cf(*args):
|
211 |
+
dataset_id = "Morpho-MNIST"
|
212 |
+
idx, _, t, i, y, do_t, do_i, do_y = args
|
213 |
+
n_particles = 32
|
214 |
+
# preprocess
|
215 |
+
obs = DATA[dataset_id]["test"].dataset.__getitem__(int(idx))
|
216 |
+
obs["x"] = (obs["x"] - 127.5) / 127.5
|
217 |
+
for k, v in obs.items():
|
218 |
+
obs[k] = v.view(1, 1) if len(v.shape) < 1 else v.unsqueeze(0)
|
219 |
+
obs[k] = obs[k].to(MODELS[dataset_id]["vae_args"].device).float()
|
220 |
+
if n_particles > 1:
|
221 |
+
ndims = (1,) * 3 if k == "x" else (1,)
|
222 |
+
obs[k] = obs[k].repeat(n_particles, *ndims)
|
223 |
+
# intervention(s)
|
224 |
+
do_pa = {}
|
225 |
+
if do_t:
|
226 |
+
do_pa["thickness"] = torch.tensor(
|
227 |
+
normalize(t, x_max=6.255515, x_min=0.87598526)
|
228 |
+
).view(1, 1)
|
229 |
+
if do_i:
|
230 |
+
do_pa["intensity"] = torch.tensor(
|
231 |
+
normalize(i, x_max=254.90317, x_min=66.601204)
|
232 |
+
).view(1, 1)
|
233 |
+
if do_y:
|
234 |
+
do_pa["digit"] = F.one_hot(torch.tensor(DIGITS.index(y)), num_classes=10).view(
|
235 |
+
1, 10
|
236 |
+
)
|
237 |
+
|
238 |
+
for k, v in do_pa.items():
|
239 |
+
do_pa[k] = (
|
240 |
+
v.to(MODELS[dataset_id]["vae_args"].device).float().repeat(n_particles, 1)
|
241 |
+
)
|
242 |
+
# infer counterfactual
|
243 |
+
out = counterfactual_inference(dataset_id, obs, do_pa)
|
244 |
+
# avg cf particles
|
245 |
+
cf_x = out["cf_x"].mean(0)
|
246 |
+
cf_x_std = out["cf_x"].std(0)
|
247 |
+
rec_x = out["rec_x"].mean(0)
|
248 |
+
cf_t = out["cf_pa"]["thickness"].mean(0)
|
249 |
+
cf_i = out["cf_pa"]["intensity"].mean(0)
|
250 |
+
cf_y = out["cf_pa"]["digit"].mean(0)
|
251 |
+
# post process
|
252 |
+
cf_x = postprocess(cf_x)
|
253 |
+
cf_x_std = cf_x_std.squeeze().detach().cpu().numpy()
|
254 |
+
rec_x = postprocess(rec_x)
|
255 |
+
cf_t = np.round((cf_t.item() + 1) / 2 * (6.255515 - 0.87598526) + 0.87598526, 2)
|
256 |
+
cf_i = np.round((cf_i.item() + 1) / 2 * (254.90317 - 66.601204) + 66.601204, 2)
|
257 |
+
cf_y = DIGITS[cf_y.argmax(-1)]
|
258 |
+
# plots
|
259 |
+
# plt.close('all')
|
260 |
+
effect = cf_x - rec_x
|
261 |
+
effect = get_fig_arr(
|
262 |
+
effect, cmap="RdBu_r", norm=MidpointNormalize(vmin=-255, midpoint=0, vmax=255)
|
263 |
+
)
|
264 |
+
cf_x = get_fig_arr(cf_x)
|
265 |
+
cf_x_std = get_fig_arr(cf_x_std, cmap="jet")
|
266 |
+
return (cf_x, cf_x_std, effect, cf_t, cf_i, cf_y)
|
267 |
+
|
268 |
+
|
269 |
+
def infer_brain_cf(*args):
|
270 |
+
dataset_id = "Brain MRI"
|
271 |
+
idx, _, m, s, a, b, v = args[:7]
|
272 |
+
do_m, do_s, do_a, do_b, do_v = args[7:]
|
273 |
+
n_particles = 16
|
274 |
+
# preprocessing
|
275 |
+
obs = DATA[dataset_id]["test"].dataset.__getitem__(int(idx))
|
276 |
+
obs = preprocess_brain(MODELS[dataset_id]["vae_args"], obs)
|
277 |
+
for k, _v in obs.items():
|
278 |
+
if n_particles > 1:
|
279 |
+
ndims = (1,) * 3 if k == "x" else (1,)
|
280 |
+
obs[k] = _v.repeat(n_particles, *ndims)
|
281 |
+
# interventions(s)
|
282 |
+
do_pa = {}
|
283 |
+
if do_m:
|
284 |
+
do_pa["mri_seq"] = torch.tensor(MRISEQ_CAT.index(m)).view(1, 1)
|
285 |
+
if do_s:
|
286 |
+
do_pa["sex"] = torch.tensor(SEX_CAT.index(s)).view(1, 1)
|
287 |
+
if do_a:
|
288 |
+
do_pa["age"] = torch.tensor(a).view(1, 1)
|
289 |
+
if do_b:
|
290 |
+
do_pa["brain_volume"] = torch.tensor(b * 1000).view(1, 1)
|
291 |
+
if do_v:
|
292 |
+
do_pa["ventricle_volume"] = torch.tensor(v * 1000).view(1, 1)
|
293 |
+
# normalize continuous attributes
|
294 |
+
for k in ["age", "brain_volume", "ventricle_volume"]:
|
295 |
+
if k in do_pa.keys():
|
296 |
+
k_max, k_min = get_attr_max_min(k)
|
297 |
+
do_pa[k] = (do_pa[k] - k_min) / (k_max - k_min) # [0,1]
|
298 |
+
do_pa[k] = 2 * do_pa[k] - 1 # [-1,1]
|
299 |
+
|
300 |
+
for k, _v in do_pa.items():
|
301 |
+
do_pa[k] = (
|
302 |
+
_v.to(MODELS[dataset_id]["vae_args"].device).float().repeat(n_particles, 1)
|
303 |
+
)
|
304 |
+
# infer counterfactual
|
305 |
+
out = counterfactual_inference(dataset_id, obs, do_pa)
|
306 |
+
# avg cf particles
|
307 |
+
cf_x = out["cf_x"].mean(0)
|
308 |
+
cf_x_std = out["cf_x"].std(0)
|
309 |
+
rec_x = out["rec_x"].mean(0)
|
310 |
+
cf_m = out["cf_pa"]["mri_seq"].mean(0)
|
311 |
+
cf_s = out["cf_pa"]["sex"].mean(0)
|
312 |
+
# post process
|
313 |
+
cf_x = postprocess(cf_x)
|
314 |
+
cf_x_std = cf_x_std.squeeze().detach().cpu().numpy()
|
315 |
+
rec_x = postprocess(rec_x)
|
316 |
+
cf_m = MRISEQ_CAT[int(cf_m.item())]
|
317 |
+
cf_s = SEX_CAT[int(cf_s.item())]
|
318 |
+
cf_ = {}
|
319 |
+
for k in ["age", "brain_volume", "ventricle_volume"]: # unnormalize
|
320 |
+
k_max, k_min = get_attr_max_min(k)
|
321 |
+
cf_[k] = (out["cf_pa"][k].mean(0).item() + 1) / 2 * (k_max - k_min) + k_min
|
322 |
+
# plots
|
323 |
+
# plt.close('all')
|
324 |
+
effect = cf_x - rec_x
|
325 |
+
effect = get_fig_arr(
|
326 |
+
effect,
|
327 |
+
cmap="RdBu_r",
|
328 |
+
norm=MidpointNormalize(vmin=effect.min(), midpoint=0, vmax=effect.max()),
|
329 |
+
)
|
330 |
+
cf_x = get_fig_arr(cf_x)
|
331 |
+
cf_x_std = get_fig_arr(cf_x_std, cmap="jet")
|
332 |
+
return (
|
333 |
+
cf_x,
|
334 |
+
cf_x_std,
|
335 |
+
effect,
|
336 |
+
cf_m,
|
337 |
+
cf_s,
|
338 |
+
np.round(cf_["age"], 1),
|
339 |
+
np.round(cf_["brain_volume"] / 1000, 2),
|
340 |
+
np.round(cf_["ventricle_volume"] / 1000, 2),
|
341 |
+
)
|
342 |
+
|
343 |
+
|
344 |
+
def infer_chest_cf(*args):
|
345 |
+
dataset_id = "Chest X-ray"
|
346 |
+
idx, _, r, s, f, a = args[:6]
|
347 |
+
do_r, do_s, do_f, do_a = args[6:]
|
348 |
+
n_particles = 16
|
349 |
+
# preprocessing
|
350 |
+
obs = DATA[dataset_id]["test"].dataset.__getitem__(int(idx))
|
351 |
+
for k, v in obs.items():
|
352 |
+
obs[k] = v.to(MODELS[dataset_id]["vae_args"].device).float()
|
353 |
+
if n_particles > 1:
|
354 |
+
ndims = (1,) * 3 if k == "x" else (1,)
|
355 |
+
obs[k] = obs[k].repeat(n_particles, *ndims)
|
356 |
+
# intervention(s)
|
357 |
+
do_pa = {}
|
358 |
+
with torch.no_grad():
|
359 |
+
if do_s:
|
360 |
+
do_pa["sex"] = torch.tensor(SEX_CAT_CHEST.index(s)).view(1, 1)
|
361 |
+
if do_f:
|
362 |
+
do_pa["finding"] = torch.tensor(FIND_CAT.index(f)).view(1, 1)
|
363 |
+
if do_r:
|
364 |
+
do_pa["race"] = F.one_hot(
|
365 |
+
torch.tensor(RACE_CAT.index(r)), num_classes=3
|
366 |
+
).view(1, 3)
|
367 |
+
if do_a:
|
368 |
+
do_pa["age"] = torch.tensor(a / 100 * 2 - 1).view(1, 1)
|
369 |
+
for k, v in do_pa.items():
|
370 |
+
do_pa[k] = (
|
371 |
+
v.to(MODELS[dataset_id]["vae_args"].device).float().repeat(n_particles, 1)
|
372 |
+
)
|
373 |
+
# infer counterfactual
|
374 |
+
out = counterfactual_inference(dataset_id, obs, do_pa)
|
375 |
+
# avg cf particles
|
376 |
+
cf_x = out["cf_x"].mean(0)
|
377 |
+
cf_x_std = out["cf_x"].std(0)
|
378 |
+
rec_x = out["rec_x"].mean(0)
|
379 |
+
cf_r = out["cf_pa"]["race"].mean(0)
|
380 |
+
cf_s = out["cf_pa"]["sex"].mean(0)
|
381 |
+
cf_f = out["cf_pa"]["finding"].mean(0)
|
382 |
+
cf_a = out["cf_pa"]["age"].mean(0)
|
383 |
+
# post process
|
384 |
+
cf_x = postprocess(cf_x)
|
385 |
+
cf_x_std = cf_x_std.squeeze().detach().cpu().numpy()
|
386 |
+
rec_x = postprocess(rec_x)
|
387 |
+
cf_r = RACE_CAT[cf_r.argmax(-1)]
|
388 |
+
cf_s = SEX_CAT_CHEST[int(cf_s.item())]
|
389 |
+
cf_f = FIND_CAT[int(cf_f.item())]
|
390 |
+
cf_a = (cf_a.item() + 1) * 50
|
391 |
+
# plots
|
392 |
+
# plt.close('all')
|
393 |
+
effect = cf_x - rec_x
|
394 |
+
effect = get_fig_arr(
|
395 |
+
effect,
|
396 |
+
cmap="RdBu_r",
|
397 |
+
norm=MidpointNormalize(vmin=effect.min(), midpoint=0, vmax=effect.max()),
|
398 |
+
)
|
399 |
+
cf_x = get_fig_arr(cf_x)
|
400 |
+
cf_x_std = get_fig_arr(cf_x_std, cmap="jet")
|
401 |
+
return (cf_x, cf_x_std, effect, cf_r, cf_s, cf_f, np.round(cf_a, 1))
|
402 |
+
|
403 |
+
|
404 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
405 |
+
with gr.Tabs():
|
406 |
+
|
407 |
+
with gr.TabItem("Brain MRI") as brain_tab:
|
408 |
+
brain_id = gr.Textbox(value=brain_tab.label, visible=False)
|
409 |
+
|
410 |
+
with gr.Row().style(equal_height=True):
|
411 |
+
idx_brain = gr.Number(value=0, visible=False)
|
412 |
+
with gr.Column(scale=1, min_width=200):
|
413 |
+
x_brain = gr.Image(label="Observation", interactive=False).style(
|
414 |
+
height=HEIGHT
|
415 |
+
)
|
416 |
+
with gr.Column(scale=1, min_width=200):
|
417 |
+
cf_x_brain = gr.Image(
|
418 |
+
label="Counterfactual", interactive=False
|
419 |
+
).style(height=HEIGHT)
|
420 |
+
with gr.Column(scale=1, min_width=200):
|
421 |
+
cf_x_std_brain = gr.Image(
|
422 |
+
label="Counterfactual Uncertainty", interactive=False
|
423 |
+
).style(height=HEIGHT)
|
424 |
+
with gr.Column(scale=1, min_width=200):
|
425 |
+
effect_brain = gr.Image(
|
426 |
+
label="Direct Causal Effect", interactive=False
|
427 |
+
).style(height=HEIGHT)
|
428 |
+
with gr.Row():
|
429 |
+
with gr.Column(scale=2.55):
|
430 |
+
gr.Markdown(
|
431 |
+
"**Intervention**"
|
432 |
+
# + 20 * " "
|
433 |
+
# + "[arXiv paper](https://arxiv.org/abs/2306.15764)   |   [GitHub code](https://github.com/biomedia-mira/causal-gen)"
|
434 |
+
# + "  |   Hint: try 90% zoom"
|
435 |
+
)
|
436 |
+
with gr.Row():
|
437 |
+
with gr.Column(min_width=200):
|
438 |
+
do_a = gr.Checkbox(label="do(age)", value=False)
|
439 |
+
a = gr.Slider(
|
440 |
+
label="\u00A0",
|
441 |
+
value=50,
|
442 |
+
minimum=44,
|
443 |
+
maximum=73,
|
444 |
+
step=1,
|
445 |
+
interactive=False,
|
446 |
+
)
|
447 |
+
with gr.Column(min_width=200):
|
448 |
+
do_s = gr.Checkbox(label="do(sex)", value=False)
|
449 |
+
s = gr.Radio(
|
450 |
+
["female", "male"], label="", interactive=False
|
451 |
+
)
|
452 |
+
with gr.Row():
|
453 |
+
with gr.Column(min_width=200):
|
454 |
+
do_b = gr.Checkbox(label="do(brain volume)", value=False)
|
455 |
+
b = gr.Slider(
|
456 |
+
label="\u00A0",
|
457 |
+
value=1000,
|
458 |
+
minimum=850,
|
459 |
+
maximum=1550,
|
460 |
+
step=20,
|
461 |
+
interactive=False,
|
462 |
+
)
|
463 |
+
with gr.Column(min_width=200):
|
464 |
+
do_v = gr.Checkbox(
|
465 |
+
label="do(ventricle volume)", value=False
|
466 |
+
)
|
467 |
+
v = gr.Slider(
|
468 |
+
label="\u00A0",
|
469 |
+
value=40,
|
470 |
+
minimum=10,
|
471 |
+
maximum=125,
|
472 |
+
step=2,
|
473 |
+
interactive=False,
|
474 |
+
)
|
475 |
+
with gr.Row():
|
476 |
+
new_brain = gr.Button("New Observation")
|
477 |
+
reset_brain = gr.Button("Reset", variant="stop")
|
478 |
+
submit_brain = gr.Button("Submit", variant="primary")
|
479 |
+
with gr.Column(scale=1):
|
480 |
+
# gr.Markdown("### ")
|
481 |
+
causal_graph_brain = gr.Image(
|
482 |
+
label="Causal Graph", interactive=False
|
483 |
+
).style(height=340)
|
484 |
+
|
485 |
+
with gr.TabItem("Chest X-ray") as chest_tab:
|
486 |
+
chest_id = gr.Textbox(value=chest_tab.label, visible=False)
|
487 |
+
|
488 |
+
with gr.Row().style(equal_height=True):
|
489 |
+
idx_chest = gr.Number(value=0, visible=False)
|
490 |
+
with gr.Column(scale=1, min_width=200):
|
491 |
+
x_chest = gr.Image(label="Observation", interactive=False).style(
|
492 |
+
height=HEIGHT
|
493 |
+
)
|
494 |
+
with gr.Column(scale=1, min_width=200):
|
495 |
+
cf_x_chest = gr.Image(
|
496 |
+
label="Counterfactual", interactive=False
|
497 |
+
).style(height=HEIGHT)
|
498 |
+
with gr.Column(scale=1, min_width=200):
|
499 |
+
cf_x_std_chest = gr.Image(
|
500 |
+
label="Counterfactual Uncertainty", interactive=False
|
501 |
+
).style(height=HEIGHT)
|
502 |
+
with gr.Column(scale=1, min_width=200):
|
503 |
+
effect_chest = gr.Image(
|
504 |
+
label="Direct Causal Effect", interactive=False
|
505 |
+
).style(height=HEIGHT)
|
506 |
+
|
507 |
+
with gr.Row():
|
508 |
+
with gr.Column(scale=2.55):
|
509 |
+
gr.Markdown(
|
510 |
+
"**Intervention**"
|
511 |
+
# + 20 * " "
|
512 |
+
# + "[arXiv paper](https://arxiv.org/abs/2306.15764)   |   [GitHub code](https://github.com/biomedia-mira/causal-gen)"
|
513 |
+
# + "  |   Hint: try 90% zoom"
|
514 |
+
)
|
515 |
+
with gr.Row().style(equal_height=True):
|
516 |
+
with gr.Column(min_width=200):
|
517 |
+
do_a_chest = gr.Checkbox(label="do(age)", value=False)
|
518 |
+
a_chest = gr.Slider(
|
519 |
+
label="\u00A0", minimum=18, maximum=98, step=1
|
520 |
+
)
|
521 |
+
with gr.Column(min_width=200):
|
522 |
+
do_s_chest = gr.Checkbox(label="do(sex)", value=False)
|
523 |
+
s_chest = gr.Radio(
|
524 |
+
SEX_CAT_CHEST, label="", interactive=False
|
525 |
+
)
|
526 |
+
|
527 |
+
with gr.Row():
|
528 |
+
with gr.Column(min_width=200):
|
529 |
+
do_r_chest = gr.Checkbox(label="do(race)", value=False)
|
530 |
+
r_chest = gr.Radio(RACE_CAT, label="", interactive=False)
|
531 |
+
with gr.Column(min_width=200):
|
532 |
+
do_f_chest = gr.Checkbox(label="do(disease)", value=False)
|
533 |
+
f_chest = gr.Radio(FIND_CAT, label="", interactive=False)
|
534 |
+
|
535 |
+
with gr.Row():
|
536 |
+
new_chest = gr.Button("New Observation")
|
537 |
+
reset_chest = gr.Button("Reset", variant="stop")
|
538 |
+
submit_chest = gr.Button("Submit", variant="primary")
|
539 |
+
with gr.Column(scale=1):
|
540 |
+
# gr.Markdown("### ")
|
541 |
+
causal_graph_chest = gr.Image(
|
542 |
+
label="Causal Graph", interactive=False
|
543 |
+
).style(height=345)
|
544 |
+
|
545 |
+
# morphomnist
|
546 |
+
# do = [do_t, do_i, do_y]
|
547 |
+
# obs = [idx, x, t, i, y]
|
548 |
+
# cf_out = [cf_x, cf_x_std, effect]
|
549 |
+
|
550 |
+
# brain
|
551 |
+
do_brain = [do_s, do_a, do_b, do_v] # intervention checkboxes
|
552 |
+
obs_brain = [idx_brain, x_brain, s, a, b, v] # observed image/attributes
|
553 |
+
cf_out_brain = [cf_x_brain, cf_x_std_brain, effect_brain] # counterfactual outputs
|
554 |
+
|
555 |
+
# chest
|
556 |
+
do_chest = [do_r_chest, do_s_chest, do_f_chest, do_a_chest]
|
557 |
+
obs_chest = [idx_chest, x_chest, r_chest, s_chest, f_chest, a_chest]
|
558 |
+
cf_out_chest = [cf_x_chest, cf_x_std_chest, effect_chest]
|
559 |
+
|
560 |
+
# on start: load new observations & causal graph
|
561 |
+
demo.load(fn=get_brain_obs, inputs=None, outputs=obs_brain)
|
562 |
+
demo.load(fn=get_chest_obs, inputs=None, outputs=obs_chest)
|
563 |
+
|
564 |
+
demo.load(fn=brain_graph, inputs=do_brain, outputs=causal_graph_brain)
|
565 |
+
demo.load(fn=chest_graph, inputs=do_chest, outputs=causal_graph_chest)
|
566 |
+
|
567 |
+
# on tab select: load models
|
568 |
+
brain_tab.select(fn=load_model, inputs=brain_id, outputs=None)
|
569 |
+
chest_tab.select(fn=load_model, inputs=chest_id, outputs=None)
|
570 |
+
|
571 |
+
# "new" button: load new observations
|
572 |
+
new_chest.click(fn=get_chest_obs, inputs=None, outputs=obs_chest)
|
573 |
+
new_brain.click(fn=get_brain_obs, inputs=None, outputs=obs_brain)
|
574 |
+
|
575 |
+
# "new" button: reset causal graphs
|
576 |
+
new_brain.click(fn=brain_graph, inputs=do_brain, outputs=causal_graph_brain)
|
577 |
+
new_chest.click(fn=chest_graph, inputs=do_chest, outputs=causal_graph_chest)
|
578 |
+
|
579 |
+
# "new" button: reset cf output panels
|
580 |
+
for _k, _v in zip(
|
581 |
+
[new_brain, new_chest], [cf_out_brain, cf_out_chest]
|
582 |
+
):
|
583 |
+
_k.click(fn=lambda: (gr.update(value=None),) * 3, inputs=None, outputs=_v)
|
584 |
+
|
585 |
+
# "reset" button: reload current observations
|
586 |
+
reset_brain.click(fn=get_brain_obs, inputs=idx_brain, outputs=obs_brain)
|
587 |
+
reset_chest.click(fn=get_chest_obs, inputs=idx_chest, outputs=obs_chest)
|
588 |
+
|
589 |
+
# "reset" button: deselect intervention checkboxes
|
590 |
+
reset_brain.click(
|
591 |
+
fn=lambda: (gr.update(value=False),) * len(do_brain),
|
592 |
+
inputs=None,
|
593 |
+
outputs=do_brain,
|
594 |
+
)
|
595 |
+
reset_chest.click(
|
596 |
+
fn=lambda: (gr.update(value=False),) * len(do_chest),
|
597 |
+
inputs=None,
|
598 |
+
outputs=do_chest,
|
599 |
+
)
|
600 |
+
|
601 |
+
# "reset" button: reset cf output panels
|
602 |
+
for _k, _v in zip(
|
603 |
+
[reset_brain, reset_chest], [cf_out_brain, cf_out_chest]
|
604 |
+
):
|
605 |
+
_k.click(fn=lambda: plt.close("all"), inputs=None, outputs=None)
|
606 |
+
_k.click(fn=lambda: (gr.update(value=None),) * 3, inputs=None, outputs=_v)
|
607 |
+
|
608 |
+
# enable brain interventions when checkbox is selected & update graph
|
609 |
+
for _k, _v in zip(do_brain, [s, a, b, v]):
|
610 |
+
_k.change(fn=lambda x: gr.update(interactive=x), inputs=_k, outputs=_v)
|
611 |
+
_k.change(brain_graph, inputs=do_brain, outputs=causal_graph_brain)
|
612 |
+
|
613 |
+
# enable chest interventions when checkbox is selected & update graph
|
614 |
+
for _k, _v in zip(do_chest, [r_chest, s_chest, f_chest, a_chest]):
|
615 |
+
_k.change(fn=lambda x: gr.update(interactive=x), inputs=_k, outputs=_v)
|
616 |
+
_k.change(chest_graph, inputs=do_chest, outputs=causal_graph_chest)
|
617 |
+
|
618 |
+
# "submit" button: infer countefactuals
|
619 |
+
submit_brain.click(
|
620 |
+
fn=infer_brain_cf,
|
621 |
+
inputs=obs_brain + do_brain,
|
622 |
+
outputs=cf_out_brain + [s, a, b, v],
|
623 |
+
)
|
624 |
+
submit_chest.click(
|
625 |
+
fn=infer_chest_cf,
|
626 |
+
inputs=obs_chest + do_chest,
|
627 |
+
outputs=cf_out_chest + [r_chest, s_chest, f_chest, a_chest],
|
628 |
+
)
|
629 |
+
|
630 |
+
if __name__ == "__main__":
|
631 |
+
demo.queue()
|
632 |
+
demo.launch()
|