-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
662 lines (526 loc) · 21.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/usr/bin/python
# -*- coding:utf8 -*-
import numpy as np
import math
import os
import json
import torch
from torch.utils.data import DataLoader, Dataset
from collections import defaultdict
from common import subsample_indexes
from common import onceexp
class WesternDataset(Dataset):
def __init__(self, df_list, length=1000, step=5, dilation=2):
"""
Args:
df_list:
length:
step: 数据segment切割窗口的移动步长
dilation: 浓密机数据采样频率(1 min)过高,dilation表示数据稀释间距
"""
if not isinstance(df_list, list):
df_list = [df_list]
df_split_all = []
begin_pos_pair = []
# 每个column对应的数据含义 ['c_in','c_out', 'v_out', 'v_in', 'pressure']
self.used_columns = ['4', '11', '14', '16', '17']
self.length = length
self.dilation = dilation
for df in df_list:
df_split_all = df_split_all + self.split_df(df[self.used_columns])
for i, df in enumerate(df_split_all):
for j in range(0, df.shape[0] - length * dilation + 1, step):
begin_pos_pair.append((i, j))
self.begin_pos_pair = begin_pos_pair
self.df_split_all = df_split_all
self.df_split_all = self.normalize(self.df_split_all)
def normalize(self, df_all_list):
df_all = df_all_list[0].append(df_all_list[1:], ignore_index=True)
mean = df_all.mean()
std = df_all.std()
return [(df - mean) / std for df in df_all_list]
def split_df(self, df):
"""
将存在空值的位置split开
Args:
df:
Returns: list -> [df1,df2,...]
"""
df_list = []
split_indexes = list(
df[df.isnull().T.any()].index
)
split_indexes = [-1] + split_indexes + [df.shape[0]]
for i in range(len(split_indexes) - 1):
if split_indexes[i + 1] - split_indexes[i] - 1 < self.length:
continue
new_df = df.iloc[split_indexes[i] + 1:split_indexes[i + 1]]
assert new_df.isnull().sum().sum() == 0
df_list.append(new_df)
return df_list
def __len__(self):
return len(self.begin_pos_pair)
def __getitem__(self, item):
df_index, pos = self.begin_pos_pair[item]
data_array = np.array(self.df_split_all[df_index].iloc[pos:pos + self.length * self.dilation], dtype=np.float32)
data_array = data_array[np.arange(self.length) * self.dilation]
# c_in = data_array[:, 0]
# c_out = data_array[:, 1]
c_in, c_out, v_out, v_in, pressure = [np.squeeze(x, axis=1) for x in np.hsplit(data_array, 5)]
v_in = v_in * 0.05
v_out = v_out * 0.05
external_input = np.stack(
[
c_in * c_in * c_in * v_in - c_out * c_out * c_out * v_out,
c_in * c_in * v_in - c_out * c_out * v_out,
c_in * v_in - c_out * v_out,
v_in - v_out,
v_in,
v_out,
c_in,
c_out
],
axis=1)
observation = pressure
return external_input, np.expand_dims(observation, axis=1)
class WesternDataset_1_4(WesternDataset):
def __len__(self):
return len(self.begin_pos_pair)
def __getitem__(self, item):
df_index, pos = self.begin_pos_pair[item]
data_array = np.array(self.df_split_all[df_index].iloc[pos:pos + self.length * self.dilation], dtype=np.float32)
data_array = data_array[np.arange(self.length) * self.dilation]
# c_in = data_array[:, 0]
# c_out = data_array[:, 1]
c_in, c_out, v_out, v_in, pressure = [np.squeeze(x, axis=1) for x in np.hsplit(data_array, 5)]
external_input = v_out
observation = np.stack(
[
c_out,
c_in,
v_in,
pressure
],
axis=1)
return np.expand_dims(external_input, axis=1), observation
class CstrDataset(Dataset):
def __init__(self, df, length=1000, step=5):
df_split_all = []
begin_pos = []
# 每个column对应的数据含义 ['in','out1', 'out2']
self.df = df
self.used_columns = ['0', '1', '2']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
# c_in = data_array[:, 0]
# c_out = data_array[:, 1]
data_in = np.array(data_df['0'], dtype=np.float32)
data_out = np.array(data_df[['1', '2']], dtype=np.float32)
# return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
return np.expand_dims(data_in, axis=1), data_out
class NLDataset(Dataset):
def __init__(self, df, length=1000, step=5):
df_split_all = []
begin_pos = []
# 每个column对应的数据含义 ['input','output']
self.df = df
self.used_columns = ['u', 'y']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
# c_in = data_array[:, 0]
# c_out = data_array[:, 1]
data_in = np.array(data_df['u'], dtype=np.float32)
data_out = np.array(data_df['y'], dtype=np.float32)
# return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
class IBDataset(Dataset):
def __init__(self, df, length=1000, step=5):
df_split_all = []
begin_pos = []
self.mean = 0.0
self.std = 0.0
# 每个column对应的数据含义 ['delta_v', 'delta_g', 'delta_h','f','c', 'reward']
self.df = df
self.used_columns = ['delta_v', 'delta_g', 'delta_h', 'v', 'g', 'h', 'f', 'c', 'reward']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
self.mean = df.mean()
self.std = df.std()
return (df - self.mean) / self.std
def normalize_record(self):
mean = self.mean
std = self.std
return [mean, std]
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
data_in = np.array(data_df[['delta_v', 'delta_g', 'delta_h']], dtype=np.float32)
data_out = np.array(data_df[['v', 'g', 'h', 'f', 'c', 'reward']], dtype=np.float32)
# return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
return data_in, data_out
class WindingDataset(Dataset):
def __init__(self, df, length=1000, step=5):
df_split_all = []
begin_pos = []
# 每个column对应的数据含义 ['in','out1', 'out2']
self.df = df
self.used_columns = ['0', '1', '2', '3', '4', '5', '6']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
# c_in = data_array[:, 0]
# c_out = data_array[:, 1]
data_in = np.array(data_df[['0', '1', '2', '3', '4']], dtype=np.float32)
data_out = np.array(data_df[['5', '6']], dtype=np.float32)
# return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
return data_in, data_out
class WesternConcentrationDataset(Dataset):
def __init__(self, df_list, length=1000, step=5, dilation=2):
"""
Args:
df_list:
length:
step: 数据segment切割窗口的移动步长
dilation: 浓密机数据采样频率(1 min)过高,dilation表示数据稀释间距
"""
if not isinstance(df_list, list):
df_list = [df_list]
df_split_all = []
begin_pos_pair = []
# 每个column对应的数据含义 ['c_in','c_out', 'v_out', 'v_in', 'pressure']
self.used_columns = ['4', '5', '7', '11', '14', '16', '17']
self.length = length
self.dilation = dilation
for df in df_list:
df_split_all = df_split_all + self.split_df(df[self.used_columns])
for i, df in enumerate(df_split_all):
for j in range(0, df.shape[0] - length * dilation + 1, step):
begin_pos_pair.append((i, j))
self.begin_pos_pair = begin_pos_pair
self.df_split_all = df_split_all
self.df_split_all = self.normalize(self.df_split_all)
def normalize(self, df_all_list):
df_all = df_all_list[0].append(df_all_list[1:], ignore_index=True)
mean = df_all.mean()
std = df_all.std()
return [(df - mean) / std for df in df_all_list]
def split_df(self, df):
"""
将存在空值的位置split开
Args:
df:
Returns: list -> [df1,df2,...]
"""
df_list = []
split_indexes = list(
df[df.isnull().T.any()].index
)
split_indexes = [-1] + split_indexes + [df.shape[0]]
for i in range(len(split_indexes) - 1):
if split_indexes[i + 1] - split_indexes[i] - 1 < self.length:
continue
new_df = df.iloc[split_indexes[i] + 1:split_indexes[i + 1]]
assert new_df.isnull().sum().sum() == 0
df_list.append(new_df)
return df_list
def __len__(self):
return len(self.begin_pos_pair)
def __getitem__(self, item):
df_index, pos = self.begin_pos_pair[item]
# data_array = np.array(self.df_split_all[df_index].iloc[pos:pos+self.length*self.dilation], dtype=np.float32)
# data_array = data_array[np.arange(self.length) * self.dilation]
# c_in = data_array[:, 0]
# c_out = data_array[:, 1]
# data_in = np.array(data_array[['4','5','7','14','16']], dtype=np.float32)
# data_out = np.array(data_array[['11', '17']], dtype=np.float32)
data_df = self.df_split_all[df_index].iloc[pos:pos + self.length * self.dilation]
def choose_and_dilation(df, length, dilation, indices):
return np.array(
df[indices], dtype=np.float32
)[np.arange(length) * dilation]
data_in = choose_and_dilation(data_df, self.length, self.dilation, ['4', '5', '7', '14', '16'])
data_out = choose_and_dilation(data_df, self.length, self.dilation, ['11', '17'])
# return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
return data_in, data_out
class SoutheastThickener(Dataset):
def __init__(self, data, length=90, step=5, dilation=1, dataset_type=None, ratio=None, io=None, seed=0,
smooth_alpha=0.3):
"""
Args:
data: data array
length: history + predicted
step: size of moving step
dilation:
dataset_type: train, val, test
ratio: default: 0.6, 0.2, 0.2
io: default 4-1
seed: default 0
"""
if not isinstance(seed, int):
seed = 0
if dataset_type is None:
dataset_type = 'train'
if ratio is None:
ratio = [0.6, 0.2, 0.2]
if io is None:
io = '4-1'
if io == '4-1':
# 进料浓度、出料浓度、进料流量、出料流量 -> 泥层压力
self.io = [[0, 1, 2, 3], [4]]
elif io == '3-2':
# 进料浓度、进料流量、出料流量 -> 出料浓度 、泥层压力
self.io = [[0, 2, 3], [1, 4]]
else:
raise NotImplementedError()
# region old version
# data = np.array(data, dtype=np.float32)
# endregion
# region new version
def iter(data):
for k, v in data.item().items():
for i in range(0, v.shape[0] - length * dilation + 1, step):
yield v[i:i+length]
data = np.stack([x for x in iter(data)], axis=0)
data = np.array(data, dtype=np.float32)
# endregion
self.smooth_alpha = smooth_alpha
for _ in self.io[1]:
# data.shape (N, 90, 5)
data[:, :, int(_)] = onceexp(data[:, :, int(_)].transpose(), self.smooth_alpha).transpose()
data, self.mean, self.std = self.normalize(data)
data = data[::step]
L = data.shape[0]
train_size, val_size = int(L*ratio[0]), int(L*ratio[1])
test_size = L - train_size - val_size
d1, d2, d3 = torch.utils.data.random_split(data, (train_size, val_size, test_size),
generator=torch.Generator().manual_seed(seed))
if dataset_type == 'train':
self.reserved_dataset = d1
elif dataset_type == 'val':
self.reserved_dataset = d2
elif dataset_type == 'test':
self.reserved_dataset = d3
else:
raise AttributeError()
self.dilation = dilation
self.step = step
def normalize(self, data):
mean = np.mean(data, axis=(0, 1))
std = np.std(data, axis=(0, 1))
return (data - mean) / std, mean, std
def __len__(self):
return len(self.reserved_dataset)
def __getitem__(self, item):
data_tuple = self.reserved_dataset.__getitem__(item)
# data_tuple = self.reserved_data[item * self.step]
data_in, data_out = [data_tuple[:, self.io[_]] for _ in range(2)]
return data_in, data_out
class CTSample:
def __init__(self, sp: float, base_tp=0.1, evenly=False):
self.sp = np.clip(sp, 0.01, 1.0)
self.base_tp = base_tp
self.evenly = evenly
def batch_collate_fn(self, batch):
external_input, observation = [torch.from_numpy(np.stack(x)) for x in zip(*batch)]
bs, l, _ = external_input.shape
time_steps = torch.arange(external_input.size(1)) * self.base_tp
data = torch.cat([external_input, observation], dim=-1)
new_data, tp = subsample_indexes(data, time_steps, self.sp, evenly=self.evenly)
external_input, observation = new_data[..., :external_input.shape[-1]], new_data[..., -observation.shape[-1]:]
# region [ati, t_{i} - t_{i-1}]
# tp = torch.cat([tp[..., 0:1], tp], dim=-1)
# dt = tp[..., 1:] - tp[..., :-1]
# endregion
# region [ati, t_{i+1} - t_{i}]
tp = torch.cat([tp, tp[..., -1:]], dim=-1)
dt = tp[..., 1:] - tp[..., :-1]
# endregion
def add_tp(x, tp):
return torch.cat([
x,
tp.repeat(bs, 1).unsqueeze(dim=-1)
], dim=-1)
external_input = add_tp(external_input, dt)
# observation = add_tp(observation, dt)
return external_input, observation
# PR-SSM Dataset: actuator, ballbeam, drive, dryer, gas_furnace
class ActuatorDataset(Dataset):
def __init__(self, df, length=1000, step=5):
begin_pos = []
# 每个column对应的数据含义 ['in','out']
self.df = df
self.used_columns = ['u', 'p']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
data_in = np.array(data_df['u'], dtype=np.float32)
data_out = np.array(data_df['p'], dtype=np.float32)
return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
class BallbeamDataset(Dataset):
def __init__(self, df, length=1000, step=5):
begin_pos = []
# 每个column对应的数据含义 ['in','out']
self.df = df
self.used_columns = ['0', '1']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
data_in = np.array(data_df['0'], dtype=np.float32)
data_out = np.array(data_df['1'], dtype=np.float32)
return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
class DriveDataset(Dataset):
def __init__(self, df, length=1000, step=5):
begin_pos = []
# 每个column对应的数据含义 ['in','out']
self.df = df
self.used_columns = ['u1', 'z1']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
data_in = np.array(data_df['u1'], dtype=np.float32)
data_out = np.array(data_df['z1'], dtype=np.float32)
return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
class DryerDataset(Dataset):
def __init__(self, df, length=1000, step=5):
begin_pos = []
# 每个column对应的数据含义 ['in','out']
self.df = df
self.used_columns = ['0', '1']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
data_in = np.array(data_df['0'], dtype=np.float32)
data_out = np.array(data_df['1'], dtype=np.float32)
return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
class GasFurnaceDataset(Dataset):
def __init__(self, df, length=1000, step=5):
begin_pos = []
# 每个column对应的数据含义 ['in','out']
self.df = df
self.used_columns = ['0', '1']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
data_in = np.array(data_df['0'], dtype=np.float32)
data_out = np.array(data_df['1'], dtype=np.float32)
return np.expand_dims(data_in, axis=1), np.expand_dims(data_out, axis=1)
class SarcosArmDataset(Dataset):
def __init__(self, df, length=1000, step=5):
begin_pos = []
# 每个column对应的数据含义 ['in','out']
self.df = df
self.used_columns = ['0', '1', '2', '3', '4', '5', '6',
'21', '22', '23', '24', '25', '26', '27']
self.length = length
for j in range(0, df.shape[0] - length + 1, step):
begin_pos.append(j)
self.begin_pos = begin_pos
self.df = self.normalize(self.df)
def normalize(self, df):
mean = df.mean()
std = df.std()
return (df - mean) / std
def __len__(self):
return len(self.begin_pos)
def __getitem__(self, item):
pos = self.begin_pos[item]
data_df = self.df.iloc[pos:pos + self.length]
data_in = np.array(data_df[['21', '22', '23', '24', '25', '26', '27']], dtype=np.float32)
data_out = np.array(data_df[['0', '1', '2', '3', '4', '5', '6']], dtype=np.float32)
return data_in, data_out