-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
65 lines (50 loc) · 2.42 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
import pickle
import numpy as np
import pandas as pd
def calc_kld(generated_data, ground_truth, bins, range_min, range_max):
pd_gt, _ = np.histogram(ground_truth, bins=bins, density=True, range=(range_min, range_max))
pd_gen, _ = np.histogram(generated_data, bins=bins, density=True, range=(range_min, range_max))
kld = 0
for x1, x2 in zip(pd_gt, pd_gen):
if x1 != 0 and x2 == 0:
kld += x1
elif x1 == 0 and x2 != 0:
kld += x2
elif x1 != 0 and x2 != 0:
kld += x1 * np.log(x1 / x2)
return np.abs(kld)
def prepare_dataset(dataset, condition_size=None):
if dataset == "lorenz":
with open("./datasets/lorenz/lorenz_dataset.pickle", "rb") as infile:
dataset = pickle.load(infile)
x_train = np.concatenate(list(dataset["x_train"].values()))
y_train = np.concatenate(list(dataset["y_train"].values()))
x_val = np.concatenate(list(dataset["x_val"].values()))
y_val = np.concatenate(list(dataset["y_val"].values()))
x_test = np.concatenate(list(dataset["x_test"].values()))
y_test = np.concatenate(list(dataset["y_test"].values()))
elif dataset == "mg":
raw_dataset = pd.read_csv("./datasets/mg/MackyG17.csv")
raw_dataset = np.transpose(raw_dataset.values)[0]
x = [raw_dataset[i - condition_size:i] for i in range(condition_size, raw_dataset.shape[0])]
x = np.array(x)
y = raw_dataset[condition_size:]
x_train = x[:int(x.shape[0] * 0.5)]
y_train = y[:int(x.shape[0] * 0.5)]
x_val = x[int(x.shape[0] * 0.5):int(x.shape[0] * 0.6)]
y_val = y[int(x.shape[0] * 0.5):int(x.shape[0] * 0.6)]
x_test = x[int(x.shape[0] * 0.6):]
y_test = y[int(x.shape[0] * 0.6):]
elif dataset == "itd":
with open("./datasets/itd/a5m.pickle", "rb") as in_file:
raw_dataset = pickle.load(in_file).astype(float)
x = [raw_dataset[i - condition_size:i] for i in range(condition_size, raw_dataset.shape[0])]
x = np.array(x)
y = raw_dataset[condition_size:]
x_train = x[:int(x.shape[0] * 0.5)]
y_train = y[:int(x.shape[0] * 0.5)]
x_val = x[int(x.shape[0] * 0.5):int(x.shape[0] * 0.6)]
y_val = y[int(x.shape[0] * 0.5):int(x.shape[0] * 0.6)]
x_test = x[int(x.shape[0] * 0.6):]
y_test = y[int(x.shape[0] * 0.6):]
return x_train, y_train, x_val, y_val, x_test, y_test