-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
169 lines (126 loc) · 4.65 KB
/
utils.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 functools
import logging
import operator
from collections import defaultdict
from typing import Iterable, List, Text, Optional
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
import torch
from sklearn.decomposition import PCA
def plot_raw(
data,
masks: List[List[int]],
labels: List[Text],
title: Text,
colors: Optional[torch.Tensor] = None,
custom_labels: List[Text] = [],
):
plot_pca = data.shape[1] > 2
if data.shape[1] > 1:
num_plots = 2 if plot_pca else 1
fig, ax = plt.subplots(1, num_plots, figsize=(8, 4), squeeze=False)
if plot_pca:
pca_model = PCA(n_components=2, whiten=True)
data_pca = pca_model.fit_transform(data)
for i, (mask, label) in enumerate(zip(masks, labels)):
# plot first two coordinates
if colors is not None:
color = f"C{colors[i]}"
else:
color = f"C{i}"
ax[0, 0].scatter(
data[mask, 0], data[mask, 1], alpha=0.5, label=label, color=color
)
ax[0, 0].axis("equal")
ax[0, 0].set(
xlabel="Coordinate 1",
ylabel="Coordinate 2",
title="First coordinates" if plot_pca else "",
)
if plot_pca:
ax[0, 1].scatter(
data_pca[mask, 0],
data_pca[mask, 1],
alpha=0.5,
label=label,
color=color,
)
ax[0, 1].axis("equal")
ax[0, 1].set(xlabel="Component 1", ylabel="Component 2", title="PCA")
else:
fig, ax = plt.subplots(1, 1, figsize=(5, 2))
for mask, label in zip(masks, labels):
# plot first (only) coordinate
ax.scatter(data[mask, 0], [0] * len(mask), alpha=0.5, label=label)
ax.axis("equal")
ax.set(xlabel="Coordinate 1", ylabel="Dummy", title="First coordinate")
fig.suptitle(title)
if custom_labels:
information_legend = plt.legend(
handles=[mpatches.Patch(color="gray", label=x) for x in custom_labels],
loc="lower left",
bbox_to_anchor=(-1.1, 1),
)
plt.gca().add_artist(information_legend)
functions_legend = plt.legend(ncol=2, loc="lower right", bbox_to_anchor=(1, 1))
for lh in functions_legend.legendHandles:
lh.set_alpha(1)
plt.show()
def plot_clusters(data, labels, title="Clusters"):
labels_to_idx = defaultdict(list)
for i, label in enumerate(labels):
labels_to_idx[label].append(i)
labels, masks = zip(*labels_to_idx.items())
plot_raw(data, masks, labels, title)
def plot_bar_list(L, L_labels=None, transform=True):
if not (L_labels):
L_labels = np.arange(len(L))
index = np.arange(len(L))
if transform:
COL = ["blue", "red"]
else:
COL = "blue"
plt.bar(index, [x.item() for x in L], color=COL)
plt.xticks(index, L_labels, fontsize=5)
plt.xlabel("Functions", fontsize=5)
plt.ylabel("MSELoss", fontsize=5)
plt.title("Loss per function")
plt.show()
def plot_pca_3d(x, data, xlabel, ylabel, zlabel, title):
pca = PCA(2)
predictions_pca = pca.fit_transform(data)
zs = predictions_pca[:, 0]
ys = predictions_pca[:, 1]
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.set(xlabel=xlabel, ylabel=ylabel, zlabel=zlabel, title=title)
ax.plot(x, ys, zs)
ax.legend()
plt.show()
def setup_logging():
logging.basicConfig(
format="%(asctime)s.%(msecs)03d %(levelname)-4s [%(filename)s:%(lineno)d] %(message)s",
datefmt="%d-%m-%Y:%H:%M:%S",
level=logging.INFO,
)
def batch_mse(target: torch.Tensor, prediction: torch.Tensor) -> torch.Tensor:
"""Assumes 2 tensors of shape (batch_size, sample_size) or `target` of size
(batch_size, sample_size) and `prediction` of shape (1, sample_size)."""
return torch.mean((target - prediction) ** 2, dim=1)
def reduce_prod(vals):
return functools.reduce(operator.mul, vals)
def batch_flatten(x):
return torch.reshape(x, [-1, reduce_prod(x.shape[1:])])
def join_vals(ints: Iterable[int], s=",") -> Text:
return s.join(f"{str_val(x)}" for x in ints)
def str_val(val) -> Text:
if isinstance(val, bool):
return "1" if val else "0"
elif isinstance(val, int):
return str(val)
elif isinstance(val, tuple) or isinstance(val, list):
return join_vals(val)
return str(val)
def kwargs_to_str(kwargs) -> Text:
return "__".join(f"{key}_{str_val(val)}" for key, val in kwargs.items())