-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel_controller.py
140 lines (115 loc) · 5.43 KB
/
excel_controller.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
import xlsxwriter
class ExcelSpikeTracer:
"""
Class used to control the creation of an excel file
"""
def __init__(self, filePath, filename, simTime, numHeaders, contentColor, headerColor, orientationFormat, boxTableSize):
"""
Init an object of type ExcelSpikeTracer
@param filepath: base path to the folder where the excel will be stored
@param filename: name of the excel file
@param simTime: duration in time (ms) of the simulation
@param numHeaders: numbers of headers
@param contentColor: default color used in the content boxes of the table (spikes values)
@param headerColor: default color used in the headers boxes of the table (row and column names)
@param orientationFormat: orientation of the time stamp: "vertical" or "horizontal"
@param boxTableSize: size of box in table
"""
self.fullPath = filePath + filename + ".xlsx"
self.excel = xlsxwriter.Workbook(self.fullPath)
self.worksheet = self.excel.add_worksheet()
self.headerFormat = self.create_format(headerColor)
self.contentFormat = self.create_format(contentColor)
self.simTime = int(simTime)
self.orientation = orientationFormat
self.numHeaders = numHeaders
self.boxTableSize = boxTableSize
self.write_header()
def create_format(self, color):
"""
Define a format for the content of the table
@param color: color used in the format created
@return: the default format used in the table with custom colors
"""
excelFormat = self.excel.add_format()
excelFormat.set_border()
excelFormat.set_bold()
excelFormat.set_align('center')
excelFormat.set_align('vcenter')
excelFormat.set_bg_color(color)
return excelFormat
def write_header(self):
"""
Create the first header of the table with a time mark for each time step of the simulation
@return:
"""
if self.orientation == "horizontal":
self.worksheet.set_column(0, self.simTime, self.boxTableSize)
for i in range(self.simTime):
self.worksheet.write(0, i + 1, i, self.headerFormat)
else:
self.worksheet.set_column(0, self.numHeaders, self.boxTableSize)
for i in range(self.simTime):
self.worksheet.write(i + 1, 0, i, self.headerFormat)
def print_spikes(self, index, name, spikes, color):
"""
Insert a row or column in the table marking with 1 the time stamp when the neuron fired
@param index: the row where to insert the data
@param name: the header of the row or column (neuron name)
@param spikes: array of time stamps that represents the spikes fired
@param color: color used to the marked boxes (when spikes happen)
@return:
"""
self.worksheet.write(index, 0, name, self.headerFormat)
valuesFormat = self.create_format(color)
for i in range(self.simTime):
if self.orientation == "horizontal":
if i in spikes:
self.worksheet.write(index, i + 1, 1, valuesFormat)
else:
self.worksheet.write(index, i + 1, "", self.contentFormat)
else:
if i in spikes:
self.worksheet.write(i + 1, index, 1, valuesFormat)
else:
self.worksheet.write(i + 1, index, "", self.contentFormat)
def print_row(self, index, isHeader, values, color):
"""
Insert a row in the table using the values passed as a parameters
@param index: the row where to insert the data
@param isHeader: if it is a header row o False if it is a content row
@param values: array of values to insert in the row
@param color: color used to fill the boxes in the row or list of color (one for each column)
@return:
"""
valuesFormat = []
if isinstance(color, list):
[valuesFormat.append(self.create_format(columnColor)) for columnColor in color]
else:
[valuesFormat.append(self.create_format(color)) for i in range(len(values))]
for i in range(0, len(values)):
if isHeader:
self.worksheet.write(index, i, values[i], valuesFormat[i])
else:
self.worksheet.write(index, i+1, values[i], valuesFormat[i])
def print_column(self, index, isHeader, values, color):
"""
Insert a column in the table using the values passed as a parameters
@param index: the column where to insert the data
@param isHeader: if it is a header row o False if it is a content row
@param values: array of values to insert in the column
@param color: color used to fill the boxes in the column or list of color (one for each row)
@return:
"""
valuesFormat = []
if isinstance(color, list):
[valuesFormat.append(self.create_format(rowColor)) for rowColor in color]
else:
[valuesFormat.append(self.create_format(color)) for i in range(len(values))]
for i in range(0, len(values)):
if isHeader:
self.worksheet.write(i, index, values[i], valuesFormat[i])
else:
self.worksheet.write(i+1, index, values[i], valuesFormat[i])
def closeExcel(self):
self.excel.close()