-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
232 lines (186 loc) · 6.96 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
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
from typing import List, Optional, Union
from math import prod
import torch
import wandb
import warnings
# normalization, pointwise gaussian
class UnitGaussianNormalizer:
def __init__(self, x, eps=0.00001, reduce_dim=[0], verbose=True):
super().__init__()
msg = ("neuralop.utils.UnitGaussianNormalizer has been deprecated. "
"Please use the newer neuralop.datasets.UnitGaussianNormalizer instead.")
warnings.warn(msg, DeprecationWarning)
n_samples, *shape = x.shape
self.sample_shape = shape
self.verbose = verbose
self.reduce_dim = reduce_dim
# x could be in shape of ntrain*n or ntrain*T*n or ntrain*n*T
self.mean = torch.mean(x, reduce_dim, keepdim=True).squeeze(0)
self.std = torch.std(x, reduce_dim, keepdim=True).squeeze(0)
self.eps = eps
if verbose:
print(
f"UnitGaussianNormalizer init on {n_samples}, reducing over {reduce_dim}, samples of shape {shape}."
)
print(f" Mean and std of shape {self.mean.shape}, eps={eps}")
def encode(self, x):
# x = x.view(-1, *self.sample_shape)
x -= self.mean
x /= self.std + self.eps
# x = (x.view(-1, *self.sample_shape) - self.mean) / (self.std + self.eps)
return x
def decode(self, x, sample_idx=None):
if sample_idx is None:
std = self.std + self.eps # n
mean = self.mean
else:
if len(self.mean.shape) == len(sample_idx[0].shape):
std = self.std[sample_idx] + self.eps # batch*n
mean = self.mean[sample_idx]
if len(self.mean.shape) > len(sample_idx[0].shape):
std = self.std[:, sample_idx] + self.eps # T*batch*n
mean = self.mean[:, sample_idx]
# x is in shape of batch*n or T*batch*n
# x = (x.view(self.sample_shape) * std) + mean
# x = x.view(-1, *self.sample_shape)
x *= std
x += mean
return x
def cuda(self):
self.mean = self.mean.cuda()
self.std = self.std.cuda()
return self
def cpu(self):
self.mean = self.mean.cpu()
self.std = self.std.cpu()
return self
def to(self, device):
self.mean = self.mean.to(device)
self.std = self.std.to(device)
return self
def count_model_params(model):
"""Returns the total number of parameters of a PyTorch model
Notes
-----
One complex number is counted as two parameters (we count real and imaginary parts)'
"""
return sum(
[p.numel() * 2 if p.is_complex() else p.numel() for p in model.parameters()]
)
def count_tensor_params(tensor, dims=None):
"""Returns the number of parameters (elements) in a single tensor, optionally, along certain dimensions only
Parameters
----------
tensor : torch.tensor
dims : int list or None, default is None
if not None, the dimensions to consider when counting the number of parameters (elements)
Notes
-----
One complex number is counted as two parameters (we count real and imaginary parts)'
"""
if dims is None:
dims = list(tensor.shape)
else:
dims = [tensor.shape[d] for d in dims]
n_params = prod(dims)
if tensor.is_complex():
return 2*n_params
return n_params
def wandb_login(api_key_file="../config/wandb_api_key.txt", key=None):
if key is None:
key = get_wandb_api_key(api_key_file)
wandb.login(key=key)
def set_wandb_api_key(api_key_file="../config/wandb_api_key.txt"):
import os
try:
os.environ["WANDB_API_KEY"]
except KeyError:
with open(api_key_file, "r") as f:
key = f.read()
os.environ["WANDB_API_KEY"] = key.strip()
def get_wandb_api_key(api_key_file="../config/wandb_api_key.txt"):
import os
try:
return os.environ["WANDB_API_KEY"]
except KeyError:
with open(api_key_file, "r") as f:
key = f.read()
return key.strip()
# Define the function to compute the spectrum
def spectrum_2d(signal, n_observations, normalize=True):
"""This function computes the spectrum of a 2D signal using the Fast Fourier Transform (FFT).
Paramaters
----------
signal : a tensor of shape (T * n_observations * n_observations)
A 2D discretized signal represented as a 1D tensor with shape
(T * n_observations * n_observations), where T is the number of time
steps and n_observations is the spatial size of the signal.
T can be any number of channels that we reshape into and
n_observations * n_observations is the spatial resolution.
n_observations: an integer
Number of discretized points. Basically the resolution of the signal.
Returns
--------
spectrum: a tensor
A 1D tensor of shape (s,) representing the computed spectrum.
"""
T = signal.shape[0]
signal = signal.view(T, n_observations, n_observations)
if normalize:
signal = torch.fft.fft2(signal)
else:
signal = torch.fft.rfft2(
signal, s=(n_observations, n_observations), normalized=False
)
# 2d wavenumbers following PyTorch fft convention
k_max = n_observations // 2
wavenumers = torch.cat(
(
torch.arange(start=0, end=k_max, step=1),
torch.arange(start=-k_max, end=0, step=1),
),
0,
).repeat(n_observations, 1)
k_x = wavenumers.transpose(0, 1)
k_y = wavenumers
# Sum wavenumbers
sum_k = torch.abs(k_x) + torch.abs(k_y)
sum_k = sum_k
# Remove symmetric components from wavenumbers
index = -1.0 * torch.ones((n_observations, n_observations))
k_max1 = k_max + 1
index[0:k_max1, 0:k_max1] = sum_k[0:k_max1, 0:k_max1]
spectrum = torch.zeros((T, n_observations))
for j in range(1, n_observations + 1):
ind = torch.where(index == j)
spectrum[:, j - 1] = (signal[:, ind[0], ind[1]].sum(dim=1)).abs() ** 2
spectrum = spectrum.mean(dim=0)
return spectrum
Number = Union[float, int]
def validate_scaling_factor(
scaling_factor: Union[None, Number, List[Number]],
n_dim: int,
n_layers: Optional[int] = None,
) -> Union[None, List[float], List[List[float]]]:
"""
Parameters
----------
scaling_factor : None OR float OR list[float]
n_dim : int
n_layers : int or None; defaults to None
If None, return a single list (rather than a list of lists)
with `factor` repeated `dim` times.
"""
if scaling_factor is None:
return None
if isinstance(scaling_factor, (float, int)):
if n_layers is None:
return [float(scaling_factor)] * n_dim
return [[float(scaling_factor)] * n_dim] * n_layers
if (
isinstance(scaling_factor, list)
and len(scaling_factor) > 0
and all([isinstance(s, (float, int)) for s in scaling_factor])
):
return [[float(s)] * n_dim for s in scaling_factor]
return None