Diff of /code/utils.py [000000] .. [365bd4]

Switch to unified view

a b/code/utils.py
1
import h5py
2
import numpy as np
3
import random
4
5
WINDOW_SIZE = 100
6
7
def rescale_array(X):
8
    X = X / 20
9
    X = np.clip(X, -5, 5)
10
    return X
11
12
13
def aug_X(X):
14
    scale = 1 + np.random.uniform(-0.1, 0.1)
15
    offset = np.random.uniform(-0.1, 0.1)
16
    noise = np.random.normal(scale=0.05, size=X.shape)
17
    X = scale * X + offset + noise
18
    return X
19
20
def gen(dict_files, aug=False):
21
    while True:
22
        record_name = random.choice(list(dict_files.keys()))
23
        batch_data = dict_files[record_name]
24
        all_rows = batch_data['x']
25
26
        for i in range(10):
27
            start_index = random.choice(range(all_rows.shape[0]-WINDOW_SIZE))
28
29
            X = all_rows[start_index:start_index+WINDOW_SIZE, ...]
30
            Y = batch_data['y'][start_index:start_index+WINDOW_SIZE]
31
32
            X = np.expand_dims(X, 0)
33
            Y = np.expand_dims(Y, -1)
34
            Y = np.expand_dims(Y, 0)
35
36
            if aug:
37
                X = aug_X(X)
38
            X = rescale_array(X)
39
40
            yield X, Y
41
42
43
def chunker(seq, size=WINDOW_SIZE):
44
    return (seq[pos:pos + size] for pos in range(0, len(seq), size))