-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataInterface.py
More file actions
56 lines (48 loc) · 1.94 KB
/
Copy pathdataInterface.py
File metadata and controls
56 lines (48 loc) · 1.94 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
from time import gmtime, strftime
import numpy as np
import os
##
# @brief Class for data interface.
#
# @param name Name of folder used to save the date
#
class DataInterface:
def __init__(self, name='XOR'):
self._name = name
##
# @brief save numpy array data into the folder self._name
#
# @param data_name descricption of the data(error, weights matrix, )
# @param data_param Parameters of network and run of the dataset
#
# @return No return, filename is name\YYYY-MM-DD-HHmmSS_data_name.csv
def save(self, data, data_name, data_param=np.array([10, 1000, 100, 0.01]), param_description='parallel_learnings, test_period, iterations, eta'):
data_param_str = self.save_param(data_param)
print(data_param_str)
save_date = strftime('%Y-%m-%d-%H%M%S', gmtime())
# create directory if it doesn't exist
if not os.path.exists(self._name):
os.mkdir(self._name)
return np.savetxt(self._name + '\\' + save_date + '_' + data_name + '.csv', data, delimiter=",", header=data_param_str, footer=param_description)
##
# @brief transform np.array into string to save param
def save_param(self, data_param):
return np.array_str(data_param).split('[')[1].split(']')[0]
##
# @brief load data from a file
# @param filename The filename
#
# @return an np.array with parameters of acquisition and a dataset
#
def load(self, filename):
params = self.load_param(filename)
data = np.loadtxt(self._name + '\\' + filename, delimiter=',')
return params, data
##
# @brief Read the parameters line of csv file
def load_param(self, filename):
file = open(self._name + '\\' + filename)
first = file.readline()
param_str = first.split('# ')[1].split('\n')[0]
params = np.fromstring(param_str, dtype=int, sep=' ')
return params