Data: Tabular Time Series Specialty: Endocrinology Laboratory: Blood Tests EHR: Demographics Diagnoses Medications Omics: Genomics Multi-omics Transcriptomics Wearable: Activity Clinical Purpose: Treatment Response Assessment Task: Biomarker Discovery
[5ef06f]: / src / move / visualization / vae_visualization.py

Download this file

269 lines (241 with data), 8.9 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
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
__all__ = ["plot_vae"]
from pathlib import Path
from typing import Optional
import matplotlib
import matplotlib.cm as cm
import matplotlib.figure
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import torch
def plot_vae(
path: Path,
savepath: Path,
filename: str,
title: str,
num_input: int,
num_hidden: int,
num_latent: int,
plot_edges=True,
input_sample: Optional[torch.Tensor] = None,
output_sample: Optional[torch.Tensor] = None,
mu: Optional[torch.Tensor] = None,
logvar: Optional[torch.Tensor] = None,
) -> matplotlib.figure.Figure:
"""
This function is aimed to visualize MOVE's architecture.
Args:
path: path where the trained model with defined weights is to be found
filename: name of the model
title: title of the figure
num_input: number of input nodes
num_hidden: number of output nodes
num_latent: number of latent nodes
plot_edges: plot edges, i.e. connections with assigned weights between nodes
input_sample: array with input values to fill with a mapped color value
output_sample : " " output " "
mu: " " mean (latent) " "
logvar: " " log variance " "
Returns:
figure
Notes:
k: input node index
j: hidden node index
i: latent node index
"""
model_weights = torch.load(path / filename)
G = nx.Graph()
# Position of the layers:
layer_distance = 10
node_distance = 550
latent_node_distance = 550
latent_sep = 5 * latent_node_distance
# Adding nodes to the graph ##############################
# Bias nodes
G.add_node(
"input_bias",
pos=(-6 * layer_distance, -3 * node_distance - num_input * node_distance / 2),
color=0.0,
)
G.add_node(
"mu_bias",
pos=(
-3 * layer_distance,
(num_hidden + 3) * node_distance
- num_hidden * node_distance / 2
+ latent_sep / 2,
),
color=0.0,
)
G.add_node(
"var_bias",
pos=(
-3 * layer_distance,
-3 * node_distance - num_hidden * node_distance / 2 - latent_sep / 2,
),
color=0.0,
)
G.add_node(
"sam_bias",
pos=(
0.5 * layer_distance,
-3 * latent_node_distance - num_latent * latent_node_distance / 2,
),
color=0.0,
)
G.add_node(
"out_bias",
pos=(3 * layer_distance, -3 * node_distance - num_hidden * node_distance / 2),
color=0.0,
)
# Actual nodes
for k in range(num_input):
G.add_node(
f"input_{k}",
pos=(
-6 * layer_distance,
k * node_distance - num_input * node_distance / 2,
),
color=[input_sample[k] if input_sample is not None else 0.0][0],
)
G.add_node(
f"output_{k}",
pos=(6 * layer_distance, k * node_distance - num_input * node_distance / 2),
color=[output_sample[k] if output_sample is not None else 0.0][0],
)
for j in range(num_hidden):
G.add_node(
f"encoder_hidden_{j}",
pos=(
-3 * layer_distance,
j * node_distance - num_hidden * node_distance / 2,
),
color=0.0,
)
G.add_node(
f"decoder_hidden_{j}",
pos=(
3 * layer_distance,
j * node_distance - num_hidden * node_distance / 2,
),
color=0.0,
)
for i in range(num_latent):
G.add_node(
f"mu_{i}",
pos=(0 * layer_distance, i * latent_node_distance + latent_sep / 2),
color=[mu[i] if mu is not None else 0.0][0],
)
G.add_node(
f"var_{i}",
pos=(0 * layer_distance, -i * latent_node_distance - latent_sep / 2),
color=[np.exp(logvar[i] / 2) if logvar is not None else 0.0][0],
)
G.add_node(
f"sam_{i}",
pos=(
0.5 * layer_distance,
i * latent_node_distance - num_latent * latent_node_distance / 2,
),
color=0.0,
)
# Adding weights to the graph #########################
if plot_edges:
for layer, values in model_weights.items():
if layer == "encoderlayers.0.weight":
for k in range(values.shape[1]): # input
for j in range(values.shape[0]): # encoder_hidden
G.add_edge(
f"input_{k}",
f"encoder_hidden_{j}",
weight=values.numpy()[j, k],
)
elif layer == "encoderlayers.0.bias":
for j in range(values.shape[0]): # encoder_hidden
G.add_edge(
"input_bias", f"encoder_hidden_{j}", weight=values.numpy()[j]
)
elif layer == "mu.weight":
for j in range(values.shape[1]): # encoder hidden
for i in range(values.shape[0]): # mu
G.add_edge(
f"encoder_hidden_{j}",
f"mu_{i}",
weight=values.numpy()[i, j],
)
elif layer == "mu.bias":
for i in range(values.shape[0]): # encoder_hidden
G.add_edge("mu_bias", f"mu_{i}", weight=values.numpy()[i])
elif layer == "var.weight":
for j in range(values.shape[1]): # encoder hidden
for i in range(values.shape[0]): # var
G.add_edge(
f"encoder_hidden_{j}",
f"var_{i}",
weight=values.numpy()[i, j],
)
elif layer == "var.bias":
for i in range(values.shape[0]): # encoder_hidden
G.add_edge("var_bias", f"var_{i}", weight=values.numpy()[i])
# Sampled layer from mu and var:
elif layer == "decoderlayers.0.weight":
for i in range(values.shape[1]): # sampled latent
for j in range(values.shape[0]): # decoder_hidden
G.add_edge(
f"sam_{i}",
f"decoder_hidden_{j}",
weight=values.numpy()[j, i],
)
# Sampled layer from mu and var:
elif layer == "decoderlayers.0.bias":
for j in range(values.shape[0]): # decoder_hidden
G.add_edge(
"sam_bias", f"decoder_hidden_{j}", weight=values.numpy()[j]
)
elif layer == "out.weight":
for j in range(values.shape[1]): # decoder_hidden
for k in range(values.shape[0]): # output
G.add_edge(
f"output_{k}",
f"decoder_hidden_{j}",
weight=values.numpy()[k, j],
)
elif layer == "out.bias":
for k in range(values.shape[0]): # output
G.add_edge("out_bias", f"output_{k}", weight=values.numpy()[k])
fig = plt.figure(figsize=(60, 60))
pos = nx.get_node_attributes(G, "pos")
color = list(nx.get_node_attributes(G, "color").values())
edge_color = list(nx.get_edge_attributes(G, "weight").values())
edge_width = list(nx.get_edge_attributes(G, "weight").values())
edge_cmap = matplotlib.colormaps["seismic"]
node_cmap = matplotlib.colormaps["seismic"]
abs_max = np.max([abs(np.min(color)), abs(np.max(color))])
abs_max_edge = np.max([abs(np.min(edge_color)), abs(np.max(edge_color))])
_ = cm.ScalarMappable(
cmap=node_cmap, norm=matplotlib.colors.Normalize(vmin=-abs_max, vmax=abs_max)
)
sm_edge = cm.ScalarMappable(
cmap=edge_cmap,
norm=matplotlib.colors.Normalize(vmin=-abs_max_edge, vmax=abs_max_edge),
)
nx.draw(
G,
pos=pos,
with_labels=True,
node_size=100,
node_color=color,
edge_color=edge_color,
width=edge_width,
font_color="black",
font_size=10,
edge_cmap=edge_cmap,
cmap=node_cmap,
vmin=-abs_max,
vmax=abs_max,
)
# plt.colorbar(sm_node, label="Node value", shrink = .2)
plt.colorbar(sm_edge, label="Edge value", shrink=0.2)
plt.tight_layout()
fig.savefig(savepath / f"{title}.png", format="png", dpi=200)
return fig