-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
347 lines (295 loc) · 15.2 KB
/
dataset.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import torch
from torch.utils.data import Dataset
import numpy as np
import pandas as pd
import scipy.io as sio
import pickle
import re
import os
from tqdm import trange, tqdm
from glob import glob
from PIL import Image
from data_utils.lotka import get_lv_data
data_path = './data'
def get_dataset(args):
if args['task'] == 'rd':
train_dataset = ReactionDiffusionDataset(mode='train')
val_dataset = ReactionDiffusionDataset(mode='val')
args['input_dim'] = train_dataset[0][0].shape[0]
args['flatten'] = False
elif args['task'] == 'mt_rd':
train_dataset = MultiTimestepReactionDiffusionDataset(mode='train')
val_dataset = MultiTimestepReactionDiffusionDataset(mode='val')
args['input_dim'] = train_dataset[0][0].shape[1]
args['mt_data'] = True
elif args['task'] == 'lv':
train_dataset = ODEDataset(ode_name='lv', mode='train', noise=args['noise'], smoothing=args['smoothing'])
val_dataset = ODEDataset(ode_name='lv', mode='val', noise=args['noise'], smoothing=args['smoothing'])
args['input_dim'] = train_dataset[0][0].shape[-1]
elif args['task'] == 'mt_lv':
train_dataset = MTODEDataset(ode_name='lv', mode='train', noise=args['noise'], smoothing=args['smoothing'])
val_dataset = MTODEDataset(ode_name='lv', mode='val', noise=args['noise'], smoothing=args['smoothing'])
args['input_dim'] = train_dataset[0][0].shape[-1]
args['mt_data'] = True
elif args['task'] == 'selkov':
train_dataset = ODEDataset(ode_name='selkov', mode='train', noise=args['noise'], smoothing=args['smoothing'])
val_dataset = ODEDataset(ode_name='selkov', mode='val', noise=args['noise'], smoothing=args['smoothing'])
args['input_dim'] = train_dataset[0][0].shape[-1]
elif args['task'] == 'mt_selkov':
n_timesteps = 2
interval = 50
train_dataset = MTODEDataset(ode_name='selkov', mode='train', noise=args['noise'], smoothing=args['smoothing'], n_timesteps=n_timesteps, interval=interval)
val_dataset = MTODEDataset(ode_name='selkov', mode='val', noise=args['noise'], smoothing=args['smoothing'], n_timesteps=n_timesteps, interval=interval)
args['input_dim'] = train_dataset[0][0].shape[-1]
args['mt_data'] = True
elif args['task'] == 'dosc':
train_dataset = ODEDataset(ode_name='dosc', mode='train', noise=args['noise'], smoothing=args['smoothing'])
val_dataset = ODEDataset(ode_name='dosc', mode='val', noise=args['noise'], smoothing=args['smoothing'])
args['input_dim'] = train_dataset[0][0].shape[-1]
elif args['task'] == 'growth':
train_dataset = ODEDataset(ode_name='growth', mode='train', noise=args['noise'], smoothing=args['smoothing'])
val_dataset = ODEDataset(ode_name='growth', mode='val', noise=args['noise'], smoothing=args['smoothing'])
args['input_dim'] = train_dataset[0][0].shape[-1]
else:
raise NotImplementedError
return train_dataset, val_dataset, args
# Modified from SINDy AE: https://github.com/kpchamp/SindyAutoencoders/blob/master/examples/rd/example_reactiondiffusion.py
class ReactionDiffusionDataset(Dataset):
def __init__(self, path=f'{data_path}/reaction_diffusion.mat', random=False, mode='train', downsample=False):
data = sio.loadmat(path)
n_samples = data['t'].size
n = data['x'].size
N = n*n
data['uf'] += 1e-6*np.random.randn(data['uf'].shape[0], data['uf'].shape[1], data['uf'].shape[2])
data['duf'] += 1e-6*np.random.randn(data['duf'].shape[0], data['duf'].shape[1], data['duf'].shape[2])
if not random:
# consecutive samples
training_samples = np.arange(int(.8*n_samples))
val_samples = np.arange(int(.8*n_samples), int(.9*n_samples))
test_samples = np.arange(int(.9*n_samples), n_samples)
else:
# random samples
perm = np.random.permutation(int(.9*n_samples))
training_samples = perm[:int(.8*n_samples)]
val_samples = perm[int(.8*n_samples):]
test_samples = np.arange(int(.9*n_samples), n_samples)
if mode == 'train':
samples = training_samples
elif mode == 'val':
samples = val_samples
elif mode == 'test':
samples = test_samples
self.data = {'t': data['t'][samples],
'y1': data['x'].T,
'y2': data['y'].T,
'x': data['uf'][:,:,samples].reshape((N,-1)).T,
'dx': data['duf'][:,:,samples].reshape((N,-1)).T}
if downsample:
# reshape each x (10000) to (100, 100) and downsample to (28, 28)
from scipy.ndimage import zoom
x = self.data['x'].reshape((-1, 100, 100))
dx = self.data['dx'].reshape((-1, 100, 100))
downsampled_x = np.zeros((self.data['x'].shape[0], 28, 28))
downsampled_dx = np.zeros((self.data['x'].shape[0], 28, 28))
for i in range(self.data['x'].shape[0]):
downsampled_x[i] = zoom(x[i], 0.28)
downsampled_dx[i] = zoom(dx[i], 0.28)
self.data['x'] = downsampled_x.reshape((-1, 28*28))
self.data['dx'] = downsampled_dx.reshape((-1, 28*28))
self.data = {k: torch.FloatTensor(v) for k, v in self.data.items()}
def __len__(self):
return self.data['x'].shape[0]
def __getitem__(self, idx):
return self.data['x'][idx], self.data['dx'][idx], self.data['dx'][idx]
class MultiTimestepReactionDiffusionDataset(Dataset):
def __init__(self, path=f'{data_path}/reaction_diffusion.mat', n_timesteps=2, mode='train', downsample=False):
data = sio.loadmat(path)
n_samples = data['t'].size
n = data['x'].size
N = n*n
data['uf'] += 1e-6*np.random.randn(data['uf'].shape[0], data['uf'].shape[1], data['uf'].shape[2])
data['duf'] += 1e-6*np.random.randn(data['duf'].shape[0], data['duf'].shape[1], data['duf'].shape[2])
training_samples = np.arange(int(.8*n_samples))
val_samples = np.arange(int(.8*n_samples), int(.9*n_samples))
test_samples = np.arange(int(.9*n_samples), n_samples)
if mode == 'train':
samples = training_samples
elif mode == 'val':
samples = val_samples
elif mode == 'test':
samples = test_samples
if downsample:
# reshape each x (10000) to (100, 100) and downsample to (28, 28)
from scipy.ndimage import zoom
x = data['uf'].reshape(100, 100, -1)
dx = data['duf'].reshape(100, 100, -1)
downsampled_x = np.zeros((28, 28, x.shape[-1]))
downsampled_dx = np.zeros((28, 28, x.shape[-1]))
for i in range(x.shape[-1]):
downsampled_x[..., i] = zoom(x[..., i], 0.28)
downsampled_dx[..., i] = zoom(dx[..., i], 0.28)
data['uf'] = downsampled_x
data['duf'] = downsampled_dx
self.data = []
for i in range(n_timesteps, len(samples)):
self.data.append({'x': torch.FloatTensor(np.transpose(data['uf'][:,:,samples[i-n_timesteps:i]], axes=(2,0,1)).reshape((n_timesteps,-1))),
'dx': torch.FloatTensor(np.transpose(data['duf'][:,:,samples[i-n_timesteps:i]], axes=(2,0,1)).reshape((n_timesteps,-1)))})
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx]['x'], self.data[idx]['dx']
ode_dt_dict = {
'lv': 0.002,
'selkov': 0.002,
'dosc': 0.2,
'growth': 0.02,
'rd': 0.05,
}
class ODEDataset(Dataset):
def __init__(self, path=f'{data_path}', ode_name='lv', mode='train', noise=0.0, smoothing=None):
super().__init__()
smoothing_str = f'-{smoothing}' if smoothing is not None else ''
try:
print(f'Loading existing {ode_name} {mode} data...')
x = torch.load(f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
dx = torch.load(f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
except FileNotFoundError:
print(f'Load data failed. Generating {ode_name} {mode} data...')
n_ics = 200 if 'train' in mode else 20
num_steps = 10000
x, dx = get_lv_data(n_ics=n_ics, noise=noise, num_steps=num_steps, smoothing=smoothing, gp_sigma_in=0.1)
x = torch.FloatTensor(x)
dx = torch.FloatTensor(dx)
torch.save(x, f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
torch.save(dx, f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
x = x.to(torch.float32)
dx = dx.to(torch.float32)
n_ics, n_steps, input_dim = x.shape
self.n_ics, self.n_steps, self.input_dim = n_ics, n_steps, input_dim
self.x = x.reshape((n_ics*n_steps, input_dim))
self.dx = dx.reshape((n_ics*n_steps, input_dim))
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.dx[idx]
class MTODEDataset(Dataset):
def __init__(self, path=f'{data_path}', ode_name='lv', mode='train', n_timesteps=2, interval=10, noise=0.0, smoothing=None):
super().__init__()
smoothing_str = f'-{smoothing}' if smoothing is not None else ''
try:
print(f'Loading existing {ode_name} {mode} data...')
x = torch.load(f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
dx = torch.load(f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
except FileNotFoundError:
print(f'Load data failed. Generating {ode_name} {mode} data...')
n_ics = 200 if 'train' in mode else 20
num_steps = 10000
x, dx = get_lv_data(n_ics=n_ics, noise=noise, num_steps=num_steps, smoothing=smoothing, gp_sigma_in=0.1)
x = torch.FloatTensor(x)
dx = torch.FloatTensor(dx)
torch.save(x, f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
torch.save(dx, f'{path}/{ode_name}-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
x = x.to(torch.float32)
dx = dx.to(torch.float32)
self.n_timesteps = n_timesteps
if n_timesteps < 2:
raise ValueError('n_timesteps must be greater than 1 for multi-timestep dataset')
n_ics, n_steps, input_dim = x.shape
self.n_ics, self.n_steps, self.input_dim = n_ics, n_steps, input_dim
self.x = []
self.dx = []
for i in range(n_ics):
for j in range(n_steps-n_timesteps*interval):
self.x.append(x[i, j:j+n_timesteps*interval:interval, :].reshape((n_timesteps, input_dim)))
self.dx.append(dx[i, j:j+n_timesteps*interval:interval, :].reshape((n_timesteps, input_dim)))
self.x = torch.stack(self.x)
self.dx = torch.stack(self.dx)
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.dx[idx]
class LotkaVolterraDataset(Dataset):
def __init__(self, path=f'{data_path}', mode='train', noise=0.0, smoothing=None):
super().__init__()
smoothing_str = f'-{smoothing}' if smoothing is not None else ''
try:
print(f'Loading existing Lotka-Volterra {mode} data...')
x = torch.load(f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
dx = torch.load(f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
except FileNotFoundError:
print(f'Load data failed. Generating Lotka-Volterra {mode} data...')
n_ics = 200 if 'train' in mode else 20
num_steps = 10000
x, dx = get_lv_data(n_ics=n_ics, noise=noise, num_steps=num_steps, smoothing=smoothing, gp_sigma_in=0.1)
x = torch.FloatTensor(x)
dx = torch.FloatTensor(dx)
torch.save(x, f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
torch.save(dx, f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
n_ics, n_steps, input_dim = x.shape
self.n_ics, self.n_steps, self.input_dim = n_ics, n_steps, input_dim
self.x = x.reshape((n_ics*n_steps, input_dim))
self.dx = dx.reshape((n_ics*n_steps, input_dim))
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.dx[idx]
class MTLotkaVolterraDataset(Dataset):
def __init__(self, path=f'{data_path}', n_timesteps=2, interval=10, mode='train', noise=0.0, smoothing=None):
super().__init__()
smoothing_str = f'-{smoothing}' if smoothing is not None else ''
try:
print(f'Loading existing Lotka-Volterra {mode} data...')
x = torch.load(f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
dx = torch.load(f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
except FileNotFoundError:
print(f'Load data failed. Generating Lotka-Volterra {mode} data...')
n_ics = 200 if 'train' in mode else 20
num_steps = 10000
x, dx = get_lv_data(n_ics=n_ics, noise=noise, num_steps=num_steps, smoothing=smoothing, gp_sigma_in=0.1)
x = torch.FloatTensor(x)
dx = torch.FloatTensor(dx)
torch.save(x, f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-x.pt')
torch.save(dx, f'{path}/lv-{mode}-noise{int(100*noise):02d}{smoothing_str}-dx.pt')
self.n_timesteps = n_timesteps
if n_timesteps < 2:
raise ValueError('n_timesteps must be greater than 1 for multi-timestep dataset')
n_ics, n_steps, input_dim = x.shape
self.n_ics, self.n_steps, self.input_dim = n_ics, n_steps, input_dim
self.x = []
self.dx = []
for i in range(n_ics):
for j in range(n_steps-n_timesteps*interval):
self.x.append(x[i, j:j+n_timesteps*interval:interval, :].reshape((n_timesteps, input_dim)))
self.dx.append(dx[i, j:j+n_timesteps*interval:interval, :].reshape((n_timesteps, input_dim)))
self.x = torch.stack(self.x)
self.dx = torch.stack(self.dx)
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.dx[idx]
class SimpleLinear(Dataset):
"""
Simple Linear Example
n_samples: number of samples
coeff: coefficients of the linear equation
e.g. coeff = [a b
c d]
dx = ax+by
dy = cx+dy
"""
def __init__(self, n_samples, coeff=torch.tensor([[1.0, 0.0], [0.0, 1.0]])):
self.x = torch.randn(n_samples, 2) * 10
self.dx = self.x @ coeff.T
'''
# evolve with time step 0.01
for i in range(1, n_samples):
self.x[i, :] = self.x[i-1, :] + 0.01 * self.x[i-1, :] @ coeff.T
self.dx = self.x @ coeff.T
'''
# add noise
self.x += 1e-3 * torch.randn_like(self.x)
self.dx += 1e-3 * torch.randn_like(self.dx)
def __len__(self):
return self.x.shape[0]
def __getitem__(self, idx):
return self.x[idx], self.dx[idx], self.dx[idx]