-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
94 lines (77 loc) · 3.07 KB
/
example.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
from NNetEn import NNetEn_entropy
import numpy as np
import re
'''
Default Configuration:
file : Path to the time series file
ds : Dataset ('D1' - MNIST-10, 'D2' - SARS-CoV-2-RBV1)
mu : Usage fraction of the selected database
method: One of 6 methods for forming a reservoir matrix
from the time series M1 ... M6.
metric: 'Acc' - accuracy metric,
'R2E' - R2 Efficiency metric,
'PE' - Pearson Efficiency metric.
epoch: The number of training epochs for the LogNNet neural network.
read_mode: Method to read file
('Rows' reading by rows or 'Columns' reading by columns)
'''
config = {
'file': 'Time_series_example.txt',
'ds': 'D1',
'mu': 1,
'method': 3,
'metric': 'Acc',
'epoch': 5,
'read_mode': 'Rows'
}
def reading_by_columns(mat: list) -> list:
matrix = []
for i in range(len(mat[0])):
matrix.append(list())
for j in range(len(mat)):
try:
matrix[i].append(mat[j][i])
except Exception as e:
print(e)
return matrix
def read_file_time_series(input_file: str, mode='Rows') -> list:
result = []
with open(input_file) as file:
line_list = file.readlines()
for lines in line_list:
lines = re.sub(r'[^(0-9.,\-eE \t)]', '', lines.strip())
lines = lines.replace(',', '.')
lines = re.sub(r'[ \t]+', ' ', lines, 0)
if lines != '':
result.append(lines.split(' '))
if mode == 'Columns':
result = reading_by_columns(result)
return [np.array(data).astype(float) for data in result]
def main():
# Print Configuration
print('Running calculations with configuration:')
print('Dataset - {}, mu - {}'.format(config['ds'], str(config['mu'])))
print('Method - {}, Metric - {}, Epoch - {}'.format
(str(config['method']), config['metric'], str(config['epoch'])))
print("Reading by {} from {}".format(config['read_mode'], config['file']))
# Reading the file containing time series
time_series = read_file_time_series(config['file'], config['read_mode'])
print("Read {} time series".format(len(time_series)))
# Creating a class with the selected database
NNetEn = NNetEn_entropy(database=config['ds'], mu=config['mu'])
# Calculation the NNetEn value
result = []
for series in time_series:
value = NNetEn.calculation(series, epoch=config['epoch'], method=config['method'],
metric=config['metric'], log=False)
print("NNetEn = {}".format(value))
result.append(value)
# Formation of the result file name
file_result = re.sub(r'^file', '',
'_'.join([str(key) + str(value)
for key, value in config.items()])) + '.txt'
print('Write results to file {}'.format(file_result))
with open(file_result, 'w') as file:
file.write("\n".join(str(item) for item in result))
if __name__ == '__main__':
main()