-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_elaboration_utilities.py
164 lines (133 loc) · 6.31 KB
/
data_elaboration_utilities.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# utilities for elaborating the data as per the code
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA as PCA
from matplotlib import pyplot as plt
# creating test and training set from scratch (one source)
def splitTrainingTestSet(path, percent_train):
partition = dict()
labels = dict()
dataset = pd.read_csv(path, encoding="utf-8")
dataset.drop(["Row"], axis=1, inplace=True) # modify according to the structure of your data
numpy_cleaned = dataset.values # converting to numpy array
# creating the dictionary of labels
for i in numpy_cleaned:
labels[i[0]] = i[1]
split_idx = int(len(numpy_cleaned)*percent_train)
partition["training"] = numpy_cleaned[0:split_idx]
partition["test"] = numpy_cleaned[split_idx:]
return partition, labels
# creating test, validation and training set from scratch (one source)
def splitTrainingValidationTestSet(path, percent_train, percent_validation):
partition = dict()
labels = dict()
dataset = pd.read_csv(path, encoding="utf-8")
dataset.drop(["Row"], axis=1, inplace=True) # modify according to the structure of your data
numpy_cleaned = dataset.values # converting to numpy array
# creating the dictionary of labels
for i in numpy_cleaned:
labels[i[0]] = i[1]
split_idx = int(len(numpy_cleaned)*percent_train)
split_idx2 = int(len(numpy_cleaned)*percent_validation) + split_idx
partition["training"] = numpy_cleaned[0:split_idx]
partition["validation"] = numpy_cleaned[split_idx:split_idx2]
partition["test"] = numpy_cleaned[split_idx2:]
return partition, labels
# creating test and training set from two different sources
def loadTrainingTestSet(training_path, test_path):
partition = dict()
labels = dict()
for path in (training_path, test_path):
dataset = pd.read_csv(path, encoding="utf-8")
dataset.drop("Unnamed: 0", axis=1, inplace=True) # modify according to the structure of your data
dataset = dataset.values # converting to numpy array
# creating the dictionary of labels
for i in dataset:
labels[i[0]] = i[1]
#creating partition
if path == training_path:
partition["training"] = dataset
else:
partition["test"] = dataset
return partition, labels
# creating test, validation and training set from two different sources (validation is obtained from the test set)
def loadTrainingTestSetCreateValidation(training_path, test_path, percent_train, percent_validation):
partition = dict()
labels = dict()
for path in (training_path, test_path):
dataset = pd.read_csv(path, encoding="utf-8")
dataset.drop("Unnamed: 0", axis=1, inplace=True) # modify according to the structure of your data
dataset = dataset.values # converting to numpy array
# creating the dictionary of labels
for i in dataset:
labels[i[0]] = i[1]
#creating partition
if path == training_path:
partition["training"] = dataset
else:
split_idx = int(len(dataset)*percent_validation/(1 - percent_train))
partition["validation"] = dataset[:split_idx]
partition["test"] = dataset[split_idx:]
return partition, labels
def loadTrainingValidationTestSet(training_path, validation_path, test_path):
partition = dict()
labels = dict()
for path in (training_path, validation_path, test_path):
dataset = pd.read_csv(path, encoding="utf-8")
dataset.drop("Unnamed: 0", axis=1, inplace=True) # modify according to the structure of your data
dataset = dataset.values # converting to numpy array
# creating the dictionary of labels
for i in dataset:
labels[i[0]] = i[1]
#creating partition
if path == training_path:
partition["training"] = dataset
elif path == validation_path:
partition["validation"] = dataset
else:
partition["test"] = dataset
return partition, labels
# visualising how data (latent representation) evolves throughout the epochs
def visualizeLatentRepresentation(info, height, width, features, columns, rows, vis, plot):
plt.figure(figsize=(width, height))
legend = list()
for n in range(len(info)):
plt.subplot(rows, columns, n+1)
z, epoch = info[n]
z = z.numpy() # to use plt.plot
pca = PCA() # PCA
pca.fit(z);
z_pca = pca.transform(z)
for i in range(features):
plt.plot(z_pca[:, i], vis) # vis defines clearly the rendering
legend.append('z_pca[{}]'.format(i))
plt.title("Epoch: {}".format(epoch))
plt.legend(legend)
if plot:
plt.show()
# visualising how losses evolve throughout the epochs
def visualizeLossesOverEpochs(first, second, third, first_name, second_name, third_name, height, width, vis, plot):
plt.figure(figsize=(width, height))
plt.plot(first, vis)
plt.plot(second, vis)
plt.plot(third, vis)
plt.legend([first_name, second_name, third_name])
plt.title("Losses evolution")
if plot:
plt.show()
# visualising the tensors saved in txt files
def readLoss(path):
previous_loss = open(path, "r")
prev = previous_loss.read()
prev = prev.replace("array", "").replace("(", "").replace(")", "").replace("dtype=float32", "") \
.replace("[", "").replace("]", "").replace(" ", "").replace("tensor", "")
prev = prev.split(",")
compar = list()
for j in prev:
compar.append(j)
first = [x for x in compar if compar.index(x)%2 == 0]
second = [float(x) for x in first] # loss to compare to the current one
return second