-
Notifications
You must be signed in to change notification settings - Fork 3
/
visualization.py
170 lines (127 loc) · 5.23 KB
/
visualization.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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import wandb
import os
import torch
import constants
# Set default settings on import
plt.rcParams.update({'font.size': 13})
plt.rc("axes", labelsize=18)
plt.rc("axes", titlesize=21)
plt.rc("legend", fontsize=18)
def save_plot(file_name, wandb_name, fig):
# Style
fig.tight_layout()
if file_name:
# Save .pdf-file
save_path = os.path.join(wandb.run.dir, "{}.pdf".format(file_name))
plt.savefig(save_path)
# Save image to wandb
wandb.log({wandb_name: wandb.Image(plt)})
plt.close(fig)
def plot_samples(sample_sets, file_name, wandb_name, labels=None, range_dataset=None,
title=None, both_y=False):
n_sets = len(sample_sets)
fig, axes = plt.subplots(nrows=1, ncols=n_sets, squeeze=False,
figsize=(7.*n_sets, 4.5))
# If not given, determine ranges from sample_set[0]
if not range_dataset:
range_dataset = sample_sets[0]
x_range = (min(range_dataset.x), max(range_dataset.x))
y_range = (min(range_dataset.y), max(range_dataset.y))
for set_i, (samples, ax) in enumerate(zip(sample_sets, axes[0])):
ax.scatter(samples.x, samples.y, s=3, color="green")
ax.set_xlim(*x_range)
ax.set_ylim(*y_range)
if labels:
ax.set(title=labels[set_i])
if both_y:
ax.set(xlabel="$y_1$", ylabel="$y_2$")
else:
ax.set(xlabel="$x$", ylabel="$y$")
if title:
fig.suptitle(title)
save_plot(file_name, wandb_name, fig)
def plot_positions(sample_sets, file_name, wandb_name, title=None):
n_sets = len(sample_sets)
n_batch = sample_sets[0].shape[0]
fig, axes = plt.subplots(nrows=1, ncols=n_sets, squeeze=False,
figsize=(7.*n_sets, 4.5))
reshaped_sets = [sample_set.reshape(n_batch,-1,2) for sample_set in sample_sets]
range_dataset = reshaped_sets[0]
x_range = (range_dataset[:,:,0].min(), range_dataset[:,:,0].max())
y_range = (range_dataset[:,:,1].min(), range_dataset[:,:,1].max())
for set_i, (samples, ax) in enumerate(zip(reshaped_sets, axes[0])):
for pos_i in range(samples.shape[1]):
ax.scatter(samples[:,pos_i,0], samples[:,pos_i,1], s=5,
marker=constants.SCATTER_MARKERS[pos_i])
ax.set_xlim(*x_range)
ax.set_ylim(*y_range)
ax.set(xlabel="$y_1,y_3,...$", ylabel="$y_2,y_4,...$")
if title:
fig.suptitle(title)
save_plot(file_name, wandb_name, fig)
def plot_pdfs(pdfs, file_name, wandb_name, support=(-1,1), labels=None,
n_points=constants.PLOT_POINTS, title=None):
if labels:
assert len(pdfs) == len(labels), "Amount of pdfs and labels given must agree"
xs = np.linspace(support[0], support[1], num=n_points)
pdf_ys = [np.array([pdf(x) for x in xs]) for pdf in pdfs]
fig = plt.figure(figsize=(4.5, 4.5))
ax = fig.add_subplot(111)
for pdf_i, ys in enumerate(pdf_ys):
line, = ax.plot(xs, ys, color=constants.COLORS[pdf_i],
ls=constants.LINE_STYLES[pdf_i], linewidth=3)
if labels:
line.set_label(labels[pdf_i])
if labels:
ax.legend(prop={"size": 12})
if title:
ax.set(title=title)
ax.set(xlabel="$y$", ylabel=("$p(y|x)$"))
save_plot(file_name, wandb_name, fig)
def plot_functions(lines, xs, file_name, wandb_name, title=None):
fig = plt.figure(figsize=(7., 4.5))
ax = fig.add_subplot(111)
for ys in lines:
line, = ax.plot(xs, ys, color="red")
if title:
ax.set(title=title)
ax.set(xlabel="$x$", ylabel="$y$")
save_plot(file_name, wandb_name, fig)
def plot_trajectories(trajs, file_name, wandb_name, labels=None, title=None,
range_trajs=None):
n_trajs = len(trajs)
n_samples = trajs[0].shape[0] # Size of batch dimension
fig, axes = plt.subplots(nrows=1, ncols=n_trajs, squeeze=False,
figsize=(5*n_trajs, 4.5))
# Determine axis ranges
if not (type(range_trajs) == torch.Tensor):
# If not given, use first in list
range_trajs = trajs[0]
reshaped_range_traj = range_trajs.reshape(n_samples, -1, 2)
range_xs = reshaped_range_traj[:,:,0]
range_ys = reshaped_range_traj[:,:,1]
x_range = (range_xs.min(), range_xs.max())
y_range = (range_ys.min(), range_ys.max())
# Make axis scales the same, as to not distort distances
axis_range = max((y_range[1] - y_range[0]), (x_range[1] - x_range[0]))
x_center = x_range[0] + (x_range[1] - x_range[0])/2
y_center = y_range[0] + (y_range[1] - y_range[0])/2
x_range = ((x_center - axis_range/2), (x_center + axis_range/2))
y_range = ((y_center - axis_range/2), (y_center + axis_range/2))
# Reshape data
reshaped_trajs = [traj.reshape(n_samples, -1, 2) for traj in trajs]
# Shape is (batch_size, n_steps, 2) (2 for 2D position (x,y))
for traj_i, (t, ax) in enumerate(zip(reshaped_trajs, axes[0])):
ax.plot(t[:,:,0].T, t[:,:,1].T, linewidth=3)
ax.set(xlabel="$y_1,y_3,...$", ylabel="$y_2,y_4,...$")
if labels:
ax.set(title=labels[traj_i])
ax.set_xlim(*x_range)
ax.set_ylim(*y_range)
if title:
fig.suptitle(title)
save_plot(file_name, wandb_name, fig)