forked from nina-pataki/VizEEG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowClasses.py
285 lines (230 loc) · 13.8 KB
/
WindowClasses.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import pyqtgraph as pg
import numpy as np
from pyqtgraph.Qt import QtGui
import re
import Tkinter
class PlotWindow(QtGui.QMainWindow):
#TODO test if setAttribute(55) works
"""Class that creates a new window with a specific channel.
Optionally, it can display a time-frequency spectrum."""
def __init__(self, data, PSData=None): #data = [xData,yData,chans] where xData = range(lb,rb), yData = [maxdata, mindata], chans = [ch#]
"Initialises the Qt GUI and variables."
QtGui.QMainWindow.__init__(self)
root = Tkinter.Tk()
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
self.resize(sw/2, sh/2)
self.setWindowTitle("vizEEG")
cw = QtGui.QWidget()
self.plot = pg.PlotWidget()
self.setCentralWidget(cw)
self.layout = QtGui.QHBoxLayout()
self.col1 = QtGui.QVBoxLayout()
self.col2 = QtGui.QVBoxLayout()
self.layout.addLayout(self.col1)
self.layout.addLayout(self.col2)
cw.setLayout(self.layout)
self.col1.addWidget(self.plot)
self.data = data
self.plotSlider = pg.InfiniteLine(pos=0, bounds=[self.data[0][0],self.data[0][-1]], movable=True, pen='y')
self.plot.addItem(self.plotSlider)
self.PSData = PSData
self.plots = []
self.checkBoxes = []
self.labels = []
self.show()
def showData(self):
"Function that shows the plot."
shift = 0
for i in range(len(self.data[1])):
if self.data[1][i][1] is not None:
self.plots.append([self.plot.plot(x=self.data[0],y=self.data[1][i][0]+shift), self.plot.plot(x=self.data[0],y=self.data[1][i][1]+shift)])
else:
self.plots.append([self.plot.plot(x=self.data[0],y=self.data[1][i][0]+shift)])
self.labels.append(QtGui.QLabel("Channel "+str(self.data[2][i])))
shift += 5000
for l in self.labels:
self.col2.addWidget(l)
def showPowSpec(self):
"""Function that shows the plot and its time-frequency spectrum.
The user is asked for the colours that should be used in the spectrum.
If Cancel is clicked, the spectrum is displayed in gray scale."""
#Some more initialisation.
self.setWindowTitle("vizEEG - Time-frequency Spectrum display")
self.img = pg.ImageView()
self.col1.addWidget(self.img)
self.imgSlider = pg.InfiniteLine(pos=0, bounds=[0,self.PSData.shape[0]], movable=True, pen='y')
self.img.addItem(self.imgSlider)
self.plotSlider.sigDragged.connect(self.imgSliderFunc)
self.imgSlider.sigDragged.connect(self.plotSliderFunc)
self.r3 = None
colours, self.ok = self.colourInput("Choose a colouring for time-frequency spectum images or enter a different one in format RRR,GGG,BBB.\n Please, separate colours (2 or 3) with space.\n If Cancel is clicked spectrum will be displayed in gray scale.")
msgBox = QtGui.QMessageBox()
msgBox.setWindowTitle("vizEEG: Colour Error")
if self.ok:
#If the input was without an error, parse the user input.
parsed = re.findall("\d{0,3},\d{0,3},\d{0,3}", colours)
if len(parsed) < 2 or len(parsed) > 3:
self.ok = False
msgBox.setText("Colours input in an incorrect format.\nPlease, input 2 or 3 colours in format RRR,GGG,BBB separated by space, where RRR or GGG or BBB are numbers in range 0-255.")
msgBox.exec_()
else:
self.r1 = int(parsed[0].split(',')[0])
self.g1 = int(parsed[0].split(',')[1])
self.b1 = int(parsed[0].split(',')[2])
self.r2 = int(parsed[1].split(',')[0])
self.g2 = int(parsed[1].split(',')[1])
self.b2 = int(parsed[1].split(',')[2])
if len(parsed) == 3:
self.r3 = int(parsed[2].split(',')[0])
self.g3 = int(parsed[2].split(',')[1])
self.b3 = int(parsed[2].split(',')[2])
#Check if the input colours are in a proper format.
okRange = range(256)
areColsOk = self.r1 in okRange and self.r2 in okRange and self.g1 in okRange and self.g2 in okRange and self.b1 in okRange and self.b2 in okRange
if len(parsed) == 3:
areColsOk = areColsOk and self.r3 in okRange and self.g3 in okRange and self.b3 in okRange
if not areColsOk:
self.ok = False
msgBox.setText("Colours are out of RGB range. \nPlease, input 2 or 3 colours in format RRR,GGG,BBB separated by space, where RRR or GGG or BBB are numbers in range 0-255.")
msgBox.exec_()
#Create the gray scale image.
#makeRGBA outputs a tuple (imgArray,isThereAlphaChannel?)
TFRGBImg = pg.makeRGBA(self.PSData[:,:,self.data[2][0]], levels=[np.amin(self.PSData[:,:,self.data[2][0]]), np.amax(self.PSData[:,:,self.data[2][0]])])[0]
if self.ok:
#Colour the image with 2 colours.
TFRGBCol = np.zeros(TFRGBImg.shape, dtype=TFRGBImg.dtype)
if self.r3 is None:
TFRGBCol[:,:,0] = (TFRGBImg[:,:,0]/255.0)*self.r1 + (1 - (TFRGBImg[:,:,0].astype(int)/255.0))*self.r2
TFRGBCol[:,:,1] = (TFRGBImg[:,:,1]/255.0)*self.g1 + (1 - (TFRGBImg[:,:,1].astype(int)/255.0))*self.g2
TFRGBCol[:,:,2] = (TFRGBImg[:,:,2]/255.0)*self.b1 + (1 - (TFRGBImg[:,:,2].astype(int)/255.0))*self.b2
TFRGBCol[:,:,3] = 255
else:
#Colour the image with 3 colours.
a = ((TFRGBImg[:,:,0]/128)*(TFRGBImg[:,:,0]/255.0))+(abs(TFRGBImg[:,:,0].astype(int)/128 - 1)*(TFRGBImg[:,:,0]/128.0))
b = ((TFRGBImg[:,:,0]/128)*self.r3)+(abs(TFRGBImg[:,:,0].astype(int)/128 - 1)*self.r2)
c = 1 - (((TFRGBImg[:,:,0]/128)*(TFRGBImg[:,:,0]/255.0))+(abs(TFRGBImg[:,:,0].astype(int)/128 - 1)*(TFRGBImg[:,:,0]/128.0)))
d = ((TFRGBImg[:,:,0]/128)*self.r2)+(abs(TFRGBImg[:,:,0].astype(int)/128 - 1)*self.r1)
TFRGBCol[:,:,0] = (a * b) + (c * d)
a = ((TFRGBImg[:,:,1]/128)*(TFRGBImg[:,:,1]/255.0))+(abs(TFRGBImg[:,:,1].astype(int)/128 - 1)*(TFRGBImg[:,:,1]/128.0))
b = ((TFRGBImg[:,:,1]/128)*self.g3)+(abs(TFRGBImg[:,:,1].astype(int)/128 - 1)*self.g2)
c = 1 - (((TFRGBImg[:,:,1]/128)*(TFRGBImg[:,:,1]/255.0))+(abs(TFRGBImg[:,:,1].astype(int)/128 - 1)*(TFRGBImg[:,:,1]/128.0)))
d = ((TFRGBImg[:,:,1]/128)*self.g2)+(abs(TFRGBImg[:,:,1].astype(int)/128 - 1)*self.g1)
TFRGBCol[:,:,1] = (a * b) + (c * d)
a = ((TFRGBImg[:,:,2]/128)*(TFRGBImg[:,:,2]/255.0))+(abs(TFRGBImg[:,:,2].astype(int)/128 - 1)*(TFRGBImg[:,:,2]/128.0))
b = ((TFRGBImg[:,:,2]/128)*self.b3)+(abs(TFRGBImg[:,:,2].astype(int)/128 - 1)*self.b2)
c = 1 - (((TFRGBImg[:,:,2]/128)*(TFRGBImg[:,:,2]/255.0))+(abs(TFRGBImg[:,:,2].astype(int)/128 - 1)*(TFRGBImg[:,:,2]/128.0)))
d = ((TFRGBImg[:,:,2]/128)*self.b2)+(abs(TFRGBImg[:,:,2].astype(int)/128 - 1)*self.b1)
TFRGBCol[:,:,2] = (a * b) + (c * d)
TFRGBCol[:,:,3] = 255
self.img.setImage(TFRGBCol)
else:
self.img.setImage(TFRGBImg)
def colourInput(self, text):
colOptions = ["255,0,0 0,0,255", "0,255,0 255,0,0","0,0,255 0,255,0 255,0,0"]
return QtGui.QInputDialog.getItem(self, "vizEEG", text, colOptions, editable=True)
#Slider management functions.
def plotSliderUpdate(self, val):
self.plotSlider.setPos(val)
def imgSliderUpdate(self, val):
ratio = len(self.data[0])/float(self.PSData.shape[0])
self.imgSlider.setPos(np.ceil(val/ratio))
def imgSliderFunc(self):
self.imgSliderUpdate(self.plotSlider.value())
def plotSliderFunc(self):
ratio = int(len(self.data[0])/self.PSData.shape[0])
self.plotSliderUpdate(self.imgSlider.value()*ratio)
class CorrMatrixWindow(QtGui.QMainWindow):
"""Class for displaying the correlation matrix. If the user clicks Cancel during
the colour input dialog the matrix shows in gray scale.
"""
def __init__(self, matData, compWinSize, compWinStep):
"Initialise the GUI and variables."
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("vizEEG - Correlation Matrix display")
root = Tkinter.Tk()
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
self.resize(sw/2, sh/2)
# self.setAttribute(55)
self.img = pg.ImageView()
self.setCentralWidget(self.img)
self.cWSize = compWinSize
self.cWStep = compWinStep
self.matData = matData
self.r3 = None
colours, self.ok = self.colourInput("Choose a colouring for correlation matrix images or enter a different one in format RRR,GGG,BBB.\nPlease, separate colours (2 or 3) with space.\nIf Cancel is clicked matrix will be displayed in gray scale.")
msgBox = QtGui.QMessageBox()
msgBox.setWindowTitle("vizEEG: Colour Error")
if self.ok:
#Parse the user input.
parsed = re.findall("\d{0,3},\d{0,3},\d{0,3}", colours)
if len(parsed) < 2 or len(parsed) > 3:
self.ok = False
msgBox.setText("Colours input in an incorrect format.\nPlease, input 2 or 3 colours in format RRR,GGG,BBB separated by space, where RRR or GGG or BBB are numbers in range 0-255.")
msgBox.exec_()
else:
self.r1 = int(parsed[0].split(',')[0])
self.g1 = int(parsed[0].split(',')[1])
self.b1 = int(parsed[0].split(',')[2])
self.r2 = int(parsed[1].split(',')[0])
self.g2 = int(parsed[1].split(',')[1])
self.b2 = int(parsed[1].split(',')[2])
if len(parsed) == 3:
self.r3 = int(parsed[2].split(',')[0])
self.g3 = int(parsed[2].split(',')[1])
self.b3 = int(parsed[2].split(',')[2])
#Check if the colours are in the proper format.
okRange = range(256)
areColsOk = self.r1 in okRange and self.r2 in okRange and self.g1 in okRange and self.g2 in okRange and self.b1 in okRange and self.b2 in okRange
if len(parsed) == 3:
areColsOk = areColsOk and self.r3 in okRange and self.g3 in okRange and self.b3 in okRange
if not areColsOk:
self.ok = False
msgBox.setText("Colours are out of RGB range. \nPlease, input 2 or 3 colours in format RRR,GGG,BBB separated by space, where RRR or GGG or BBB are numbers in range 0-255.")
msgBox.exec_()
self.show()
def colourInput(self, text):
colOptions = ["255,0,0 0,0,255", "0,255,0 255,0,0","0,0,255 0,255,0 255,0,0"]
return QtGui.QInputDialog.getItem(self, "vizEEG", text, colOptions, editable=True)
def showData(self, slPos):
"Loads the data into a displaying object."
temp = slPos - (self.cWSize/2)
if temp < 0:
pos = 0
else:
if (temp%self.cWStep <= (self.cWStep/2)):
pos = temp / self.cWStep
else:
pos = (temp / self.cWStep) + 1
#Create the gray scale image.
matRGB = pg.makeRGBA(self.matData[:,:,pos], levels=[np.amin(self.matData[:,:,pos]), np.amax(self.matData[:,:,pos])])[0]
if self.ok:
#Colours the gray scale image with 2 colours.
matRGBCol = np.zeros(matRGB.shape, dtype=matRGB.dtype)
if self.r3 is None:
matRGBCol[:,:,0] = (matRGB[:,:,0]/255.0)*self.r1 + (1 - (matRGB[:,:,0]/255.0))*self.r2
matRGBCol[:,:,1] = (matRGB[:,:,1]/255.0)*self.g1 + (1 - (matRGB[:,:,1]/255.0))*self.g2
matRGBCol[:,:,2] = (matRGB[:,:,2]/255.0)*self.b1 + (1 - (matRGB[:,:,2]/255.0))*self.b2
matRGBCol[:,:,3] = 255
else:
#Or with 3 colours.
a = ((matRGB[:,:,0]/128)*(matRGB[:,:,0]/255.0))+(abs(matRGB[:,:,0].astype(int)/128 - 1)*(matRGB[:,:,0]/128.0))
b = ((matRGB[:,:,0]/128)*self.r3)+(abs(matRGB[:,:,0].astype(int)/128 - 1)*self.r2)
c = 1 - (((matRGB[:,:,0]/128)*(matRGB[:,:,0]/255.0))+(abs(matRGB[:,:,0].astype(int)/128 - 1)*(matRGB[:,:,0]/128.0)))
d = ((matRGB[:,:,0]/128)*self.r2)+(abs(matRGB[:,:,0].astype(int)/128 - 1)*self.r1)
matRGBCol[:,:,0] = (a * b) + (c * d)
a = ((matRGB[:,:,1]/128)*(matRGB[:,:,1]/255.0))+(abs(matRGB[:,:,1].astype(int)/128 - 1)*(matRGB[:,:,1]/128.0))
b = ((matRGB[:,:,1]/128)*self.g3)+(abs(matRGB[:,:,1].astype(int)/128 - 1)*self.g2)
c = 1 - (((matRGB[:,:,1]/128)*(matRGB[:,:,1]/255.0))+(abs(matRGB[:,:,1].astype(int)/128 - 1)*(matRGB[:,:,1]/128.0)))
d = ((matRGB[:,:,1]/128)*self.g2)+(abs(matRGB[:,:,1].astype(int)/128 - 1)*self.g1)
matRGBCol[:,:,1] = (a * b) + (c * d)
a = ((matRGB[:,:,2]/128)*(matRGB[:,:,2]/255.0))+(abs(matRGB[:,:,2].astype(int)/128 - 1)*(matRGB[:,:,2]/128.0))
b = ((matRGB[:,:,2]/128)*self.b3)+(abs(matRGB[:,:,2].astype(int)/128 - 1)*self.b2)
c = 1 - (((matRGB[:,:,2]/128)*(matRGB[:,:,2]/255.0))+(abs(matRGB[:,:,2].astype(int)/128 - 1)*(matRGB[:,:,2]/128.0)))
d = ((matRGB[:,:,2]/128)*self.b2)+(abs(matRGB[:,:,2].astype(int)/128 - 1)*self.b1)
matRGBCol[:,:,2] = (a * b) + (c * d)
matRGBCol[:,:,3] = 255
self.img.setImage(matRGBCol)
else:
self.img.setImage(matRGB)