-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplotter.py
More file actions
150 lines (141 loc) · 6.28 KB
/
plotter.py
File metadata and controls
150 lines (141 loc) · 6.28 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
from matplotlib import pyplot as plt
import numpy as np
import torch
class Plotter():
"""Class for plotting the results of the model."""
def __init__(self, model, dataset):
"""Initializes the plotter.
Args:
model: model to plot the results for
args: args from the command line
dataset: dataset that contains the test set
"""
self.model = model
self.dataset = dataset
# set model to eval mode
self.model.eval()
@torch.compiler.disable()
def plot_fixed_points(self):
generated = self.model.evaluator.get_gen_data()
N, T, dx = generated.shape
n = int(np.ceil(np.sqrt(N)))
if dx == 3:
fps = self.model.evaluator.get_scyfi()
fig = plt.figure(figsize=(3*n, 3*n), layout='compressed')
for i, gen in enumerate(generated):
gen = gen.cpu().numpy()
ax = fig.add_subplot(n, n, i+1, projection='3d')
ax.plot(gen[:,0], gen[:,1], gen[:,2])
if fps[i] is not None:
ax.scatter(fps[i][:,0], fps[i][:,1], fps[i][:,2], marker='x', color='black')
ax.set_title("Subject {}".format(i))
return fig
else:
return None
@torch.compiler.disable()
def plot_power_spectrum(self):
"""Plots the power spectrum of the test set and the generated
trajectories.
"""
# compute power spectrum for first time, they are then saved within evaluator
gen_ps, gt_ps = self.model.evaluator.get_power_spectrum()
N, f, dz = gen_ps.shape
interval = self.model.evaluator.get_interval_of_interest()
fig = plt.figure(figsize=(4*N, self.model.dz), layout='compressed')
sfigs = fig.subfigures(1, N)
for i, (gt, gen) in enumerate(zip(gt_ps, gen_ps)):
sfig = sfigs[i] if N > 1 else sfigs
for j in range(gt.shape[-1]):
ax = sfig.add_subplot(gt.shape[-1], 1, j+1)
ax.plot(gt[:,j], label="ground truth")
ax.plot(np.linspace(0, len(gt), len(gen)), len(gen)/len(gt) * gen[:,j], label="generated")
ax.set_xlim(interval[i,j])
sfig.suptitle(f"Subject {i}")
return fig
@torch.compiler.disable()
def plot_trajectory(self):
"""Plots a test trajectory and a generated trajectory
of same length.
"""
ground_truth = self.dataset.get_test_data()
N, T, dz = ground_truth.shape
ground_truth = self.model.evaluator.get_test_data()[:, :T]
generated = self.model.evaluator.get_gen_data()[:,:T]
fig = plt.figure(figsize=(4*N, self.model.dz), layout='compressed')
sfigs = fig.subfigures(1, N)
for i, (gt, gen) in enumerate(zip(ground_truth, generated)):
gt = gt.cpu().numpy()
gen = gen.cpu().numpy()
sfig = sfigs[i] if N > 1 else sfigs
for j in range(gt.shape[-1]):
ax = sfig.add_subplot(gt.shape[-1], 1, j+1)
ax.plot(gt[:,j], label="ground truth")
ax.plot(gen[:,j], label="generated")
ax.legend()
sfig.suptitle(f"Subject {i}")
return fig
@torch.compiler.disable()
def plot_3D_trajectory(self):
"""Plots a test trajectory and a generated trajectory
of same length in 3D.
"""
if self.model.dx != 3:
return None
ground_truth = self.dataset.get_test_data()
N, T, dz = ground_truth.shape
n = int(np.ceil(np.sqrt(N)))
generated = self.model.evaluator.get_gen_data()[:,:T]
fig = plt.figure(figsize=(3*n, 3*n), layout='compressed')
for i, (gt, gen) in enumerate(zip(ground_truth, generated)):
gt = gt.cpu().numpy()
gen = gen.cpu().numpy()
ax = fig.add_subplot(n, n, i+1, projection='3d')
ax.plot(gt[:,0], gt[:,1], gt[:,2], label="ground truth")
ax.plot(gen[:,0], gen[:,1], gen[:,2], label="generated")
ax.set_title("Subject {}".format(i))
ax.legend()
return fig
@torch.compiler.disable()
def plot_hovmoller(self):
"""Plots a test trajectory and a generated trajectory
of same length in a hovmoller diagram."""
ground_truth = self.dataset.get_test_data()
N, T, dz = ground_truth.shape
generated = self.model.evaluator.get_gen_data()[:,:T]
fig = plt.figure(figsize=(9*N, int(T/100)), layout='compressed')
for i, (gt, gen) in enumerate(zip(ground_truth, generated)):
gt = gt.cpu().numpy()
gen = gen.cpu().numpy()
ax = fig.add_subplot(N, 1, i+1)
# concat trajectories along latent dim
trajectory = np.concatenate([gt, gen], axis=-1)
c = ax.imshow(trajectory.T, origin='lower', cmap='bwr', aspect='auto')
ax.axhline(dz, color='black', linewidth=1)
fig.colorbar(c, ax=ax)
ax.set_title("Subject {}".format(i))
ax.set_ylabel('T')
return fig
@torch.compiler.disable()
def plot_trajectory_train(self):
"""Copy of plot_trajectory but uses a single train instance
as ground truth
"""
ground_truth = self.dataset[0][0][None]
N, T, dz = ground_truth.shape
generated = self.model.evaluator.get_gen_data()[:,:T,:]
fig = plt.figure(figsize=(3*N, self.model.dz))
sfigs = fig.subfigures(1, N, layout='compressed')
for i, (gt, gen) in enumerate(zip(ground_truth, generated)):
gt = gt.cpu().numpy()
gen = gen.cpu().numpy()
sfig = sfigs[i] if N > 1 else sfigs
for j in range(gt.shape[-1]):
ax = sfig.add_subplot(gt.shape[-1], 1, j+1)
ax.plot(gt[:,j], label="ground truth")
ax.plot(gen[:,j], label="generated")
ax.legend()
sfig.suptitle(f"Subject {i}")
return fig
def plot_hierarchisation_stuff(self):
"""Plots interesting things from the hierarchisation scheme."""
return self.model.hierarchisation_scheme.plot_stuff()