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
146a6ea
8c4fe8b
146a6ea
 
8c4fe8b
 
 
146a6ea
 
8c4fe8b
 
146a6ea
 
8c4fe8b
 
146a6ea
 
 
8c4fe8b
 
146a6ea
 
8c4fe8b
 
 
 
 
 
 
 
 
 
 
146a6ea
 
8c4fe8b
 
 
146a6ea
8c4fe8b
 
 
 
 
146a6ea
 
 
 
 
 
 
 
 
 
8c4fe8b
 
146a6ea
8c4fe8b
146a6ea
8c4fe8b
146a6ea
8c4fe8b
146a6ea
8c4fe8b
146a6ea
8c4fe8b
 
 
 
 
 
 
146a6ea
8c4fe8b
 
 
 
146a6ea
8c4fe8b
 
 
 
 
 
146a6ea
8c4fe8b
146a6ea
8c4fe8b
146a6ea
8c4fe8b
 
146a6ea
 
 
 
 
 
 
8c4fe8b
 
 
 
146a6ea
8c4fe8b
146a6ea
8c4fe8b
146a6ea
8c4fe8b
 
 
 
 
 
146a6ea
8c4fe8b
146a6ea
 
8c4fe8b
 
 
146a6ea
8c4fe8b
 
 
 
 
 
 
 
 
 
146a6ea
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import torch
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

from PIL import Image

from matplotlib import rc, patches, colors

rc("font", **{"family": "serif", "serif": ["Roman"]})
rc("text", usetex=True)
rc("image", interpolation="none")
rc("text.latex", preamble=r"\usepackage{amsmath} \usepackage{amssymb}")

from datasets import get_attr_max_min

HAMMER = np.array(Image.open("./hammer.png").resize((35, 35))) / 255


class MidpointNormalize(colors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        colors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        v_ext = np.max([np.abs(self.vmin), np.abs(self.vmax)])
        x, y = [-v_ext, self.midpoint, v_ext], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y))


def postprocess(x):
    return ((x + 1.0) * 127.5).squeeze().detach().cpu().numpy()


def mnist_graph(*args):
    x, t, i, y = r"$\mathbf{x}$", r"$t$", r"$i$", r"$y$"
    ut, ui, uy = r"$\mathbf{U}_t$", r"$\mathbf{U}_i$", r"$\mathbf{U}_y$"
    zx, ex = r"$\mathbf{z}_{1:L}$", r"$\boldsymbol{\epsilon}$"

    G = nx.DiGraph()
    G.add_edge(t, x)
    G.add_edge(i, x)
    G.add_edge(y, x)
    G.add_edge(t, i)
    G.add_edge(ut, t)
    G.add_edge(ui, i)
    G.add_edge(uy, y)
    G.add_edge(zx, x)
    G.add_edge(ex, x)

    pos = {
        y: (0, 0),
        uy: (-1, 0),
        t: (0, 0.5),
        ut: (0, 1),
        x: (1, 0),
        zx: (2, 0.375),
        ex: (2, 0),
        i: (1, 0.5),
        ui: (1, 1),
    }

    node_c = {}
    for node in G:
        node_c[node] = "lightgrey" if node in [x, t, i, y] else "white"
    node_line_c = {k: "black" for k, _ in node_c.items()}
    edge_c = {e: "black" for e in G.edges}

    if args[0]:  # do_t
        edge_c[(ut, t)] = "lightgrey"
        # G.remove_edge(ut, t)
        node_line_c[t] = "red"
    if args[1]:  # do_i
        edge_c[(ui, i)] = "lightgrey"
        edge_c[(t, i)] = "lightgrey"
        # G.remove_edges_from([(ui, i), (t, i)])
        node_line_c[i] = "red"
    if args[2]:  # do_y
        edge_c[(uy, y)] = "lightgrey"
        # G.remove_edge(uy, y)
        node_line_c[y] = "red"

    fs = 30
    options = {
        "font_size": fs,
        "node_size": 3000,
        "node_color": list(node_c.values()),
        "edgecolors": list(node_line_c.values()),
        "edge_color": list(edge_c.values()),
        "linewidths": 2,
        "width": 2,
    }
    plt.close("all")
    fig, ax = plt.subplots(1, 1, figsize=(6, 4.1))  # , constrained_layout=True)
    # fig.patch.set_visible(False)
    ax.margins(x=0.06, y=0.15, tight=False)
    ax.axis("off")
    nx.draw_networkx(G, pos, **options, arrowsize=25, arrowstyle="-|>", ax=ax)
    # need to reuse x, y limits so that the graphs plot the same way before and after removing edges
    x_lim = (-1.348, 2.348)
    y_lim = (-0.215, 1.215)
    ax.set_xlim(x_lim)
    ax.set_ylim(y_lim)
    rect = patches.FancyBboxPatch(
        (1.75, -0.16),
        0.5,
        0.7,
        boxstyle="round, pad=0.05, rounding_size=0",
        linewidth=2,
        edgecolor="black",
        facecolor="none",
        linestyle="-",
    )
    ax.add_patch(rect)
    ax.text(1.85, 0.65, r"$\mathbf{U}_{\mathbf{x}}$", fontsize=fs)

    if args[0]:  # do_t
        fig.figimage(HAMMER, 0.26 * fig.bbox.xmax, 0.525 * fig.bbox.ymax, zorder=10)
    if args[1]:  # do_i
        fig.figimage(HAMMER, 0.5175 * fig.bbox.xmax, 0.525 * fig.bbox.ymax, zorder=11)
    if args[2]:  # do_y
        fig.figimage(HAMMER, 0.26 * fig.bbox.xmax, 0.2 * fig.bbox.ymax, zorder=12)

    fig.tight_layout()
    fig.canvas.draw()
    return np.array(fig.canvas.renderer.buffer_rgba())


def brain_graph(*args):
    x, m, s, a, b, v = r"$\mathbf{x}$", r"$m$", r"$s$", r"$a$", r"$b$", r"$v$"
    um, us, ua, ub, uv = (
        r"$\mathbf{U}_m$",
        r"$\mathbf{U}_s$",
        r"$\mathbf{U}_a$",
        r"$\mathbf{U}_b$",
        r"$\mathbf{U}_v$",
    )
    zx, ex = r"$\mathbf{z}_{1:L}$", r"$\boldsymbol{\epsilon}$"

    G = nx.DiGraph()
    G.add_edge(m, x)
    G.add_edge(s, x)
    G.add_edge(b, x)
    G.add_edge(v, x)
    G.add_edge(zx, x)
    G.add_edge(ex, x)
    G.add_edge(a, b)
    G.add_edge(a, v)
    G.add_edge(s, b)
    G.add_edge(um, m)
    G.add_edge(us, s)
    G.add_edge(ua, a)
    G.add_edge(ub, b)
    G.add_edge(uv, v)

    pos = {
        x: (0, 0),
        zx: (-0.25, -1),
        ex: (0.25, -1),
        a: (0, 1),
        ua: (0, 2),
        s: (1, 0),
        us: (1, -1),
        b: (1, 1),
        ub: (1, 2),
        m: (-1, 0),
        um: (-1, -1),
        v: (-1, 1),
        uv: (-1, 2),
    }

    node_c = {}
    for node in G:
        node_c[node] = "lightgrey" if node in [x, m, s, a, b, v] else "white"
    node_line_c = {k: "black" for k, _ in node_c.items()}
    edge_c = {e: "black" for e in G.edges}

    if args[0]:  # do_m
        # G.remove_edge(um, m)
        edge_c[(um, m)] = "lightgrey"
        node_line_c[m] = "red"
    if args[1]:  # do_s
        # G.remove_edge(us, s)
        edge_c[(us, s)] = "lightgrey"
        node_line_c[s] = "red"
    if args[2]:  # do_a
        # G.remove_edge(ua, a)
        edge_c[(ua, a)] = "lightgrey"
        node_line_c[a] = "red"
    if args[3]:  # do_b
        # G.remove_edges_from([(ub, b), (s, b), (a, b)])
        edge_c[(ub, b)] = "lightgrey"
        edge_c[(s, b)] = "lightgrey"
        edge_c[(a, b)] = "lightgrey"
        node_line_c[b] = "red"
    if args[4]:  # do_v
        # G.remove_edges_from([(uv, v), (a, v), (b, v)])
        edge_c[(uv, v)] = "lightgrey"
        edge_c[(a, v)] = "lightgrey"
        edge_c[(b, v)] = "lightgrey"
        node_line_c[v] = "red"

    fs = 30
    options = {
        "font_size": fs,
        "node_size": 3000,
        "node_color": list(node_c.values()),
        "edgecolors": list(node_line_c.values()),
        "edge_color": list(edge_c.values()),
        "linewidths": 2,
        "width": 2,
    }

    plt.close("all")
    fig, ax = plt.subplots(1, 1, figsize=(5, 5))  # , constrained_layout=True)
    # fig.patch.set_visible(False)
    ax.margins(x=0.1, y=0.08, tight=False)
    ax.axis("off")
    nx.draw_networkx(G, pos, **options, arrowsize=25, arrowstyle="-|>", ax=ax)
    # need to reuse x, y limits so that the graphs plot the same way before and after removing edges
    x_lim = (-1.32, 1.32)
    y_lim = (-1.414, 2.414)
    ax.set_xlim(x_lim)
    ax.set_ylim(y_lim)
    rect = patches.FancyBboxPatch(
        (-0.5, -1.325),
        1,
        0.65,
        boxstyle="round, pad=0.05, rounding_size=0",
        linewidth=2,
        edgecolor="black",
        facecolor="none",
        linestyle="-",
    )
    ax.add_patch(rect)
    # ax.text(1.85, 0.65, r"$\mathbf{U}_{\mathbf{x}}$", fontsize=fs)

    if args[0]:  # do_m
        fig.figimage(HAMMER, 0.0075 * fig.bbox.xmax, 0.395 * fig.bbox.ymax, zorder=10)
    if args[1]:  # do_s
        fig.figimage(HAMMER, 0.72 * fig.bbox.xmax, 0.395 * fig.bbox.ymax, zorder=11)
    if args[2]:  # do_a
        fig.figimage(HAMMER, 0.363 * fig.bbox.xmax, 0.64 * fig.bbox.ymax, zorder=12)
    if args[3]:  # do_b
        fig.figimage(HAMMER, 0.72 * fig.bbox.xmax, 0.64 * fig.bbox.ymax, zorder=13)
    if args[4]:  # do_v
        fig.figimage(HAMMER, 0.0075 * fig.bbox.xmax, 0.64 * fig.bbox.ymax, zorder=14)
    else:  # b -> v
        a3 = patches.FancyArrowPatch(
            (0.86, 1.21),
            (-0.86, 1.21),
            connectionstyle="arc3,rad=.3",
            linewidth=2,
            arrowstyle="simple, head_width=10, head_length=10",
            color="k",
        )
        ax.add_patch(a3)
    # print(ax.get_xlim())
    # print(ax.get_ylim())
    fig.tight_layout()
    fig.canvas.draw()
    return np.array(fig.canvas.renderer.buffer_rgba())


def chest_graph(*args):
    x, a, d, r, s = r"$\mathbf{x}$", r"$a$", r"$d$", r"$r$", r"$s$"
    ua, ud, ur, us = (
        r"$\mathbf{U}_a$",
        r"$\mathbf{U}_d$",
        r"$\mathbf{U}_r$",
        r"$\mathbf{U}_s$",
    )
    zx, ex = r"$\mathbf{z}_{1:L}$", r"$\boldsymbol{\epsilon}$"

    G = nx.DiGraph()
    G.add_edge(ua, a)
    G.add_edge(ud, d)
    G.add_edge(ur, r)
    G.add_edge(us, s)
    G.add_edge(a, d)
    G.add_edge(d, x)
    G.add_edge(r, x)
    G.add_edge(s, x)
    G.add_edge(ex, x)
    G.add_edge(zx, x)
    G.add_edge(a, x)

    pos = {
        x: (0, 0),
        a: (-1, 1),
        d: (0, 1),
        r: (1, 1),
        s: (1, 0),
        ua: (-1, 2),
        ud: (0, 2),
        ur: (1, 2),
        us: (1, -1),
        zx: (-0.25, -1),
        ex: (0.25, -1),
    }

    node_c = {}
    for node in G:
        node_c[node] = "lightgrey" if node in [x, a, d, r, s] else "white"

    edge_c = {e: "black" for e in G.edges}
    node_line_c = {k: "black" for k, _ in node_c.items()}

    if args[0]:  # do_r
        # G.remove_edge(ur, r)
        edge_c[(ur, r)] = "lightgrey"
        node_line_c[r] = "red"
    if args[1]:  # do_s
        # G.remove_edges_from([(us, s)])
        edge_c[(us, s)] = "lightgrey"
        node_line_c[s] = "red"
    if args[2]:  # do_f (do_d)
        # G.remove_edges_from([(ud, d), (a, d)])
        edge_c[(ud, d)] = "lightgrey"
        edge_c[(a, d)] = "lightgrey"
        node_line_c[d] = "red"
    if args[3]:  # do_a
        # G.remove_edge(ua, a)
        edge_c[(ua, a)] = "lightgrey"
        node_line_c[a] = "red"

    fs = 30
    options = {
        "font_size": fs,
        "node_size": 3000,
        "node_color": list(node_c.values()),
        "edgecolors": list(node_line_c.values()),
        "edge_color": list(edge_c.values()),
        "linewidths": 2,
        "width": 2,
    }
    plt.close("all")
    fig, ax = plt.subplots(1, 1, figsize=(5, 5))  # , constrained_layout=True)
    # fig.patch.set_visible(False)
    ax.margins(x=0.1, y=0.08, tight=False)
    ax.axis("off")
    nx.draw_networkx(G, pos, **options, arrowsize=25, arrowstyle="-|>", ax=ax)
    # need to reuse x, y limits so that the graphs plot the same way before and after removing edges
    x_lim = (-1.32, 1.32)
    y_lim = (-1.414, 2.414)
    ax.set_xlim(x_lim)
    ax.set_ylim(y_lim)
    rect = patches.FancyBboxPatch(
        (-0.5, -1.325),
        1,
        0.65,
        boxstyle="round, pad=0.05, rounding_size=0",
        linewidth=2,
        edgecolor="black",
        facecolor="none",
        linestyle="-",
    )
    ax.add_patch(rect)
    ax.text(-0.9, -1.075, r"$\mathbf{U}_{\mathbf{x}}$", fontsize=fs)

    if args[0]:  # do_r
        fig.figimage(HAMMER, 0.72 * fig.bbox.xmax, 0.64 * fig.bbox.ymax, zorder=10)
    if args[1]:  # do_s
        fig.figimage(HAMMER, 0.72 * fig.bbox.xmax, 0.395 * fig.bbox.ymax, zorder=11)
    if args[2]:  # do_f
        fig.figimage(HAMMER, 0.363 * fig.bbox.xmax, 0.64 * fig.bbox.ymax, zorder=12)
    if args[3]:  # do_a
        fig.figimage(HAMMER, 0.0075 * fig.bbox.xmax, 0.64 * fig.bbox.ymax, zorder=13)

    fig.tight_layout()
    fig.canvas.draw()
    return np.array(fig.canvas.renderer.buffer_rgba())


def vae_preprocess(args, pa):
    if "ukbb" in args.hps:
        # preprocessing ukbb parents for the vae which was originally trained using
        # log standardized parents. The pgm was trained using [-1,1] normalization
        # first undo [-1,1] parent preprocessing back to original range
        for k, v in pa.items():
            if k != "mri_seq" and k != "sex":
                pa[k] = (v + 1) / 2  # [-1,1] -> [0,1]
                _max, _min = get_attr_max_min(k)
                pa[k] = pa[k] * (_max - _min) + _min
        # log_standardize parents for vae input
        for k, v in pa.items():
            logpa_k = torch.log(v.clamp(min=1e-12))
            if k == "age":
                pa[k] = (logpa_k - 4.112339973449707) / 0.11769197136163712
            elif k == "brain_volume":
                pa[k] = (logpa_k - 13.965583801269531) / 0.09537758678197861
            elif k == "ventricle_volume":
                pa[k] = (logpa_k - 10.345998764038086) / 0.43127763271331787
    # concatenate parents expand to input res for conditioning the vae
    pa = torch.cat(
        [pa[k] if len(pa[k].shape) > 1 else pa[k][..., None] for k in args.parents_x],
        dim=1,
    )
    pa = (
        pa[..., None, None].repeat(1, 1, *(args.input_res,) * 2).to(args.device).float()
    )
    return pa


def preprocess_brain(args, obs):
    obs["x"] = (obs["x"][None, ...].float().to(args.device) - 127.5) / 127.5  # [-1,1]
    # for all other variables except x
    for k in [k for k in obs.keys() if k != "x"]:
        obs[k] = obs[k].float().to(args.device).view(1, 1)
        if k in ["age", "brain_volume", "ventricle_volume"]:
            k_max, k_min = get_attr_max_min(k)
            obs[k] = (obs[k] - k_min) / (k_max - k_min)  # [0,1]
            obs[k] = 2 * obs[k] - 1  # [-1,1]
    return obs


def get_fig_arr(x, width=4, height=4, dpi=144, cmap="Greys_r", norm=None):
    fig = plt.figure(figsize=(width, height), dpi=dpi)
    ax = plt.axes([0, 0, 1, 1], frameon=False)
    if cmap == "Greys_r":
        ax.imshow(x, cmap=cmap, vmin=0, vmax=255)
    else:
        ax.imshow(x, cmap=cmap, norm=norm)
    ax.axis("off")
    fig.canvas.draw()
    return np.array(fig.canvas.renderer.buffer_rgba())


def normalize(x, x_min=None, x_max=None, zero_one=False):
    if x_min is None:
        x_min = x.min()
    if x_max is None:
        x_max = x.max()
    x = (x - x_min) / (x_max - x_min)  # [0,1]
    return x if zero_one else 2 * x - 1  # else [-1,1]