-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseq2seq_ts_generator.py
More file actions
64 lines (47 loc) · 1.67 KB
/
Copy pathseq2seq_ts_generator.py
File metadata and controls
64 lines (47 loc) · 1.67 KB
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
from tsaug import TimeWarp, Drift, Pool, AddNoise # , Crop, Quantize, Drift, Reverse
import numpy as np
from wfdb.processing import (
normalize_bound
)
my_augmenter = (TimeWarp(n_speed_change=1, prob=0.33)
+ Drift(max_drift=(0.05, 0.3))
+ Pool(prob=0.2)
+ AddNoise(prob=0.2))
def tsaug_generator(X_all, y_all, batch_size):
"""
Generate Time Series data with sequence labels
Data generator that yields training data as batches.
1. Randomly selects one sample from time series signals
2. Applies time series augmentations to X and Y
3. Normalizes result for X data
4. Reshapes data into correct format for training
Parameters
----------
X_all : 3D numpy array
(N, seqlen, features=1)
y_all : 3D numpy array (binary labels for my case)
(N, seqlen, classes=1)
batch_size : int
Number of training examples in the batch
Yields
------
(X, y) : tuple
Contains training samples with corresponding labels
"""
while True:
X = []
y = []
while len(X) < batch_size:
random_sig_idx = np.random.randint(0, X_all.shape[0])
x1 = X_all[random_sig_idx].flatten()
y1 = y_all[random_sig_idx].flatten()
# Augment X and y and normalize it again
X_aug, y_aug = my_augmenter.augment(x1, y1)
X_aug = normalize_bound(X_aug, lb=-1, ub=1)
X.append(X_aug)
y.append(y_aug)
X = np.asarray(X)
y = np.asarray(y)
X = X.reshape(X.shape[0], X.shape[1], 1)
y = y.reshape(y.shape[0], y.shape[1], 1).astype(int)
yield (X, y)