-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopo_widget.py
184 lines (159 loc) · 7.04 KB
/
topo_widget.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
import numpy as np
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui
import matplotlib.pyplot as plt
import matplotlib as mpl
import mne
from mne.viz import topomap
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.colorbar import ColorbarBase
from channel_loca_dict import channel_dict_2D
class TopographicWidget(QtGui.QWidget):
def __init__(self, ch_label, parent):
super().__init__()
self.parent = parent
self.fig, self.axes = plt.subplots(2, 4)
self.plots = list()
self.colorbar_ax = self.fig.add_subplot(2, 5, 5)
self.cmap = 'RdBu_r'
self.norm = mpl.colors.Normalize(vmin=0, vmax=100)
self.colorbar_ax.set_position([0.9, 0.04, 0.03, 0.15])
self.colorbar = ColorbarBase(self.colorbar_ax, cmap=self.cmap, norm=self.norm)
self.canvas = FigureCanvas(self.fig)
self.titles = ['Delta',
'Theta',
'Low Alpha',
'High Alpha',
'Low Beta',
'Mid Beta',
'High Beta',
'Gamma']
hlayout = QtGui.QHBoxLayout(self)
hlayout.addWidget(self.canvas)
self.show()
def draw(self, data, ch_label):
self.pos = np.array(list(channel_dict_2D.values()))
self.ch_names_ = list(channel_dict_2D.keys())
self.pos, self.outlines = topomap._check_outlines(self.pos, 'head')
center = 0.5 * (self.pos.max(axis=0) + self.pos.min(axis=0))
scale = 1.0 / (self.pos.max(axis=0) - self.pos.min(axis=0))
scale[0] = scale[0] * 1.3
self.plt_idx = [self.ch_names_.index(name) for name in ch_label]
ch_pos = np.array([self.pos[idx]*5/6 for idx in self.plt_idx])
mask = np.ones((len(ch_pos), 1), dtype=bool)
for idx in range(8):
x_idx = idx // 4
y_idx = idx % 4
if len(self.plots) <= idx:
self.axes[x_idx][y_idx].set_title(self.titles[idx])
if self.parent.ch_loc:
marker = "."
else:
marker = ""
im, cont, interp = mne.viz.topomap._plot_topomap(data[idx], ch_pos,
axes=self.axes[x_idx][y_idx], contours=10,
show=False, mask=mask, mask_params=dict(marker=marker),
head_pos=dict(center=center, scale=scale*1.1), cmap="RdBu_r",
names=ch_label, show_names=self.parent.ch_loc)
self.colorbar_ax.set_position([0.9, 0.04, 0.03, 0.15])
self.plots.append([im, cont, interp])
else:
im, cont, interp = self.plots[idx]
Zi = interp.set_values(data[idx])()
im.set_data(Zi)
# must be removed and re-added
if len(cont.collections) > 0:
tp = cont.collections[0]
visible = tp.get_visible()
patch_ = tp.get_clip_path()
color = tp.get_color()
lw = tp.get_linewidth()
for tp in cont.collections[:]:
tp.remove()
cont = self.axes[x_idx][y_idx].contour(interp.Xi, interp.Yi, Zi, 10,
colors=color, linewidths=lw)
for tp in cont.collections:
tp.set_visible(visible)
tp.set_clip_path(patch_)
self.plots[idx][1] = cont
self.plots[idx][2] = interp
self.fig.canvas.draw()
def update_color_bar(self, scale_min, scale_max):
norm = mpl.colors.Normalize(vmin=scale_min, vmax=scale_max)
self.colorbar.norm = norm
self.colorbar.draw_all()
class Menu_Bar(QtGui.QWidget):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.scale_min = 0
self.scale_max = 100
self.init_ui()
self.setup_signal_handler()
self.power_handler()
def init_ui(self):
grid_layout = QtGui.QGridLayout(self)
self.z_each_btn = QtGui.QPushButton("Z-Score to each band")
self.z_all_btn = QtGui.QPushButton("Z-Score to all bands")
self.power_btn = QtGui.QPushButton("Power")
self.ch_loc_btn = QtGui.QPushButton("Channel Location")
scale_group_box = QtGui.QGroupBox()
scale_layout = QtGui.QHBoxLayout(scale_group_box)
self.spin_min = QtGui.QSpinBox()
self.spin_min.setValue(0)
self.spin_max = QtGui.QSpinBox()
self.spin_max.setMaximum(10000)
self.spin_max.setValue(100)
scale_layout.addWidget(QtGui.QLabel("Scale: "))
scale_layout.addWidget(self.spin_min)
scale_layout.addWidget(QtGui.QLabel(" to "))
scale_layout.addWidget(self.spin_max)
scale_layout.addWidget(QtGui.QLabel("uV<sup>2</sup>"))
grid_layout.addWidget(self.z_each_btn, 0, 0, 1, 1)
grid_layout.addWidget(self.z_all_btn, 0 , 1, 1, 1)
grid_layout.addWidget(self.power_btn, 0, 2, 1, 1)
grid_layout.addWidget(scale_group_box, 0, 3, 1, 1)
grid_layout.addWidget(self.ch_loc_btn, 0, 4, 1, 1)
def setup_signal_handler(self):
self.ch_loc_btn.clicked.connect(self.ch_loc_handler)
self.z_each_btn.clicked.connect(self.z_each_handler)
self.z_all_btn.clicked.connect(self.z_all_handler)
self.power_btn.clicked.connect(self.power_handler)
self.spin_min.valueChanged.connect(self.scale_change_handler)
self.spin_max.valueChanged.connect(self.scale_change_handler)
def ch_loc_handler(self):
self.parent.ch_loc = not self.parent.ch_loc
def z_each_handler(self):
self.parent.display_type = "z_each"
self.z_each_btn.setEnabled(False)
self.z_all_btn.setEnabled(True)
self.power_btn.setEnabled(True)
self.spin_min.setMinimum(-5)
self.spin_min.setValue(-2)
self.spin_min.setEnabled(False)
self.spin_max.setValue(2)
self.spin_max.setEnabled(False)
def z_all_handler(self):
self.parent.display_type = "z_all"
self.z_each_btn.setEnabled(True)
self.z_all_btn.setEnabled(False)
self.power_btn.setEnabled(True)
self.spin_min.setMinimum(-5)
self.spin_min.setValue(-2)
self.spin_min.setEnabled(False)
self.spin_max.setValue(2)
self.spin_max.setEnabled(False)
def power_handler(self):
self.parent.display_type = "power"
self.z_each_btn.setEnabled(True)
self.z_all_btn.setEnabled(True)
self.power_btn.setEnabled(False)
self.spin_min.setMinimum(0)
self.spin_min.setValue(0)
self.spin_min.setEnabled(True)
self.spin_max.setValue(100)
self.spin_max.setEnabled(True)
def scale_change_handler(self):
self.scale_min = self.spin_min.value()
self.scale_max = self.spin_max.value()
self.parent.topo_widget.update_color_bar(self.scale_min, self.scale_max)