-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathonline_microstates.py
271 lines (209 loc) · 8.14 KB
/
online_microstates.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
#!/usr/bin/env python3
#coding:utf-8
"""
Author: Arnaud Desvachez --<[email protected]>
Purpose: Online protocol for deep meditation state neurofeedback.
Created: 14.10.2019
"""
import mne
import os
import sys
import time
import numpy as np
import multiprocessing as mp
from importlib import import_module
from scipy.signal import argrelextrema, detrend
import matplotlib.pyplot as plt
import neurodecode.utils.pycnbi_utils as pu
from neurodecode import logger
from neurodecode.utils import q_common as qc
from neurodecode.gui.streams import redirect_stdout_to_queue
from neurodecode.stream_receiver.stream_receiver import StreamReceiver
os.environ['OMP_NUM_THREADS'] = '1' # actually improves performance for multitaper
mne.set_log_level('ERROR') # DEBUG, INFO, WARNING, ERROR, or CRITICAL
#----------------------------------------------------------------------
def check_config(cfg):
"""
Ensure that the config file contains the parameters
"""
critical_vars = {
'COMMON': ['DATA_PATH']
}
optional_vars = {
'AMP_NAME':None,
'AMP_SERIAL':None,
'GLOBAL_TIME': 1.0 * 60,
'NJOBS': 1,
}
for key in critical_vars['COMMON']:
if not hasattr(cfg, key):
logger.error('%s is a required parameter' % key)
raise RuntimeError
for key in optional_vars:
if not hasattr(cfg, key):
setattr(cfg, key, optional_vars[key])
logger.warning('Setting undefined parameter %s=%s' % (key, getattr(cfg, key)))
#----------------------------------------------------------------------
def find_lsl_stream(cfg, state):
"""
Find the amplifier name and its serial number to connect to
cfg = config file
state = GUI sharing variable
"""
if cfg.AMP_NAME is None and cfg.AMP_SERIAL is None:
amp_name, amp_serial = pu.search_lsl(state, ignore_markers=True)
else:
amp_name = cfg.AMP_NAME
amp_serial = cfg.AMP_SERIAL
return amp_name, amp_serial
#----------------------------------------------------------------------
def connect_lsl_stream(cfg, amp_name, amp_serial):
"""
Connect to the lsl stream corresponding to the provided amplifier
name and serial number
cfg = config file
amp_name = amplifier's name to connect to
amp_serial = amplifier's serial number
"""
sr = StreamReceiver(window_size=cfg.WINDOWSIZE, buffer_size=cfg.STREAMBUFFER, amp_serial=amp_serial, eeg_only=False, amp_name=amp_name)
return sr
#----------------------------------------------------------------------
def run(cfg, state=mp.Value('i', 1), queue=None):
"""
Online protocol for Alpha/Theta neurofeedback.
"""
redirect_stdout_to_queue(logger, queue, 'INFO')
# Wait the recording to start (GUI)
while state.value == 2: # 0: stop, 1:start, 2:wait
pass
# Protocol runs if state equals to 1
if not state.value:
sys.exit(-1)
#----------------------------------------------------------------------
def print_perc(percent):
white = (255, 255, 255)
# create a font object.
# 1st parameter is the font file
# which is present in pygame.
# 2nd parameter is size of the font
font = pygame.font.Font('freesansbold.ttf', 22)
# create a text suface object,
# on which text is drawn on it.
text = font.render(str(percent/10) + '%', True,white)
# create a rectangular object for the
# text surface object
textRect = text.get_rect()
# set the center of the rectangular object.
textRect.center = (w // 4, h // 4)
screen.blit(text,textRect)
#----------------------------------------------------------------------
# LSL stream connection
#----------------------------------------------------------------------
# chooose amp
amp_name, amp_serial = find_lsl_stream(cfg, state)
# Connect to lsl stream
sr = connect_lsl_stream(cfg, amp_name, amp_serial)
# Get sampling rate
sfreq = sr.get_sample_rate()
# Get trigger channel
trg_ch = sr.get_trigger_channel()
#----------------------------------------------------------------------
# Main
#----------------------------------------------------------------------
global_timer = qc.Timer(autoreset=False)
internal_timer = qc.Timer(autoreset=True)
while state.value == 1 and global_timer.sec() < cfg.GLOBAL_TIME:
#----------------------------------------------------------------------
# Data acquisition
#----------------------------------------------------------------------
sr.acquire()
raw, tslist = sr.get_window() # [samples x channels]
raw = raw.T # [channels x samples]
# Check if proper real-time acquisition
tsnew = np.where(np.array(tslist) > last_ts)[0]
if len(tsnew) == 0:
logger.warning('There seems to be delay in receiving data.')
time.sleep(1)
continue
#----------------------------------------------------------------------
# Data processing
#----------------------------------------------------------------------
# Compute the GFP
gfp = np.abs(detrend(np.mean(raw, 0))) # [1 x samples]
# Find GFP's peak
gfp_peaks = argrelextrema(gfp, np.greater, order=15) # Order needs to be optimized
# Assign dominant microstate
count = 0
micro_template = np.loadtxt("./Maps_4states_s2.txt", dtype=float)
# Missing first and last microstate --> TO change
for p in range(1, len(gfp_peaks[0])-1):
correletion = np.array()
for i in range(len(micro_template)):
correletion.append(np.corrcoef(raw[:, p], micro_template[i]))
if np.argmax(correletion) == cfg.MICRO2REGULATE:
start = gfp_peaks[0][p - 1] + (gfp_peaks[0][p] - gfp_peaks[0][p-1]) / 2
end = gfp_peaks[0][p] + (gfp_peaks[0][p+1] - gfp_peaks[0][p]) / 2
count = count + len(range(start, end))
# Percentage of the microstate of interest
percent = count / raw.shape[1] * 100
# Feedback
pygame.init()
pygame.display.set_caption('EEG microstate')
background = pygame.image.load('stars2.png')
background_size = background.get_size()
background_rect = background.get_rect()
screen = pygame.display.set_mode(background_size)
w,h = background_size
x = 0
y = 0
x1 = 0
y1 = -h
ship = pygame.image.load("space.png")
shiprect = ship.get_rect()
shiprect.center = (w//2,h//2)
running = True
i = 0
# set the pygame window name
pygame.display.set_caption('EEG microstate')
screen.blit(background,background_rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
y1 += percent
y += percent
screen.blit(background,(x,y))
screen.blit(background,(x1,y1))
if y > h:
y = -h
if y1 > h:
y1 = -h
screen.blit(ship,shiprect)
print_perc(percent)
pygame.display.flip()
pygame.display.update()
# ----------------------------------------------------------------------
def load_config(cfg_file):
"""
Dynamic loading of a config file.
Format the lib to fit the previous developed neurodecode code if subject specific file (not for the templates).
cfg_file: tuple containing the path and the config file name.
"""
cfg_file = os.path.split(cfg_file)
sys.path.append(cfg_file[0])
cfg_module = import_module(cfg_file[1].split('.')[0])
return cfg_module
#----------------------------------------------------------------------
def batch_run(cfg_module):
"""
For batch script
"""
cfg = load_config(cfg_module)
check_config(cfg)
run(cfg)
#----------------------------------------------------------------------
if __name__ == '__main__':
if len(sys.argv) < 2:
cfg_module = input('Config module name? ')
else:
cfg_module = sys.argv[1]
batch_run(cfg_module)