possibility to modify plot length added

This commit is contained in:
adamr 2023-05-31 10:51:21 +02:00
parent afa8d84680
commit 1480bf60ca
8 changed files with 64 additions and 25 deletions

View file

@ -146,7 +146,7 @@ class NeurOneOffline():
sendqueue=False,ringbuf_factor=2,dump=False,avgPackets=1): sendqueue=False,ringbuf_factor=2,dump=False,avgPackets=1):
self.ringbuffersize = ringbuffersize self.ringbuffersize = ringbuffersize
tmp_path = '/mnt/projects/P_BCT_EEG/DLPFCM1_iTBS/DLPFC/nobeep/subj_8/X47851_iTBS.vhdr'# tmp_path = '/mnt/projects/P_BCT_EEG/DLPFCM1_iTBS/DLPFC/nobeep/subj_8/X47851_iTBS.vhdr'#
#'/mnt/projects/P_BCT_EEG/DLPFCM1_iTBS/DLPFC/beep/subj_14/X13193_adam.vhdr' #'/mnt/projects/P_BCT_EEG/DLPFCM1_iTBS/DLPFC/beep/subj_6/X77384_iTBS.vhdr' #'/mnt/projects/P_BCT_EEG/DLPFCM1_iTBS/DLPFC/beep/subj_14/X13193_adam.vhdr' #'/mnt/projects/P_BCT_EEG/DLPFCM1_iTBS/DLPFC/beep/subj_6/X7738_iTBS.vhdr'
num_electr = 18 num_electr = 18
eeg_chn = np.arange(0,num_electr,1) eeg_chn = np.arange(0,num_electr,1)
hdr = mne.io.read_raw_brainvision(tmp_path) hdr = mne.io.read_raw_brainvision(tmp_path)
@ -215,7 +215,7 @@ def acquire_data(q, size, run, speed, downsample, sleep_time, ip = '192.168.200.
run: Value class from multiprocessing library. That value can be changed in main process run: Value class from multiprocessing library. That value can be changed in main process
downsample: boolean value. Says if data will be downsampled to 1000 Hz downsample: boolean value. Says if data will be downsampled to 1000 Hz
sleep_time: int, set how often function should refresh. Usually it takes a bit more that that""" sleep_time: int, set how often function should refresh. Usually it takes a bit more that that"""
offline = 'offline' # offline = 'offline'
#import NeurOne_v3 #import NeurOne_v3
if offline=="offline": if offline=="offline":
NO = NeurOneOffline() NO = NeurOneOffline()
@ -247,7 +247,7 @@ class AppForm(QMainWindow):
def __init__(self, passed_params = None, parent=None): def __init__(self, passed_params = None, parent=None):
super().__init__() super().__init__()
#self.setStyleSheet("background: whitesmoke") #self.setStyleSheet("background: whitesmoke")
# self.offline = 'NeurOne' #"BrainProducts"#False self.offline = "offline" #'NeurOne' #"BrainProducts"#False
self.time_start = time.time() self.time_start = time.time()
QMainWindow.__init__(self, parent) QMainWindow.__init__(self, parent)
self.montage_file_path = 'montage_18ch.csv' self.montage_file_path = 'montage_18ch.csv'
@ -416,11 +416,12 @@ class AppForm(QMainWindow):
self.remove_outliers = passed_params['remove_outliers'] self.remove_outliers = passed_params['remove_outliers']
self.ip = passed_params['ip'] self.ip = passed_params['ip']
self.port = passed_params['port'] self.port = passed_params['port']
self.offline = passed_params['offline'] self.offline = self.offline if not None else passed_params['offline']
self.exp_trig = passed_params['exp_trig'] self.exp_trig = passed_params['exp_trig']
self.exp_time = passed_params['exp_time'] self.exp_time = passed_params['exp_time']
self.if_percentage = passed_params['percentages'] self.if_percentage = passed_params['percentages']
self.received_thr_values = passed_params['thr_values'] self.received_thr_values = passed_params['thr_values']
self.plot_len = passed_params['plot_len']
else: else:
self.montage_file_path = 'montage_18ch.csv' self.montage_file_path = 'montage_18ch.csv'
self.time_between_bursts = int(settings_file[settings_file[0]=='time_between_trains'].values[0][1]) self.time_between_bursts = int(settings_file[settings_file[0]=='time_between_trains'].values[0][1])
@ -442,6 +443,7 @@ class AppForm(QMainWindow):
self.ip = '192.168.200.201' self.ip = '192.168.200.201'
self.port = 50000 self.port = 50000
self.offline = False self.offline = False
self.plot_len = 4 #length of data to plot (last seconds of data array)
self.unit_label = np.array(self.unit_label)[self.used_features] self.unit_label = np.array(self.unit_label)[self.used_features]
self.log_file_writer.writerow(['time', 'state', self.used_features]) self.log_file_writer.writerow(['time', 'state', self.used_features])
@ -490,7 +492,6 @@ class AppForm(QMainWindow):
self.theta_band= json.loads(settings_file[settings_file[0]=='theta_range'].values[0][1]) self.theta_band= json.loads(settings_file[settings_file[0]=='theta_range'].values[0][1])
self.colors = ['b', 'm', 'r', 'k', 'c', ] #colors used for lines if less that 6 of them self.colors = ['b', 'm', 'r', 'k', 'c', ] #colors used for lines if less that 6 of them
self.data_len = 30*self.Fs #length of the data array in seconds self.data_len = 30*self.Fs #length of the data array in seconds
self.plot_len = 4 #length of data to plot (last seconds of data array)
self.plot_dividing_factor = 100 self.plot_dividing_factor = 100
self.previous_state = np.zeros(6) self.previous_state = np.zeros(6)
if self.num_of_lines>5: #if more than 5 lines then colors of them from colormap if self.num_of_lines>5: #if more than 5 lines then colors of them from colormap
@ -513,9 +514,9 @@ class AppForm(QMainWindow):
self.create_main_frame() #create plots, buttons, figures etc... self.create_main_frame() #create plots, buttons, figures etc...
#create data array #create data array
self.loaded = np.zeros([self.num_of_ch,self.data_len]) self.loaded = np.full([self.num_of_ch,self.data_len], None)
self.loaded_full = np.zeros([self.num_of_ch+1,self.data_len]) self.loaded_full = np.full([self.num_of_ch+1,self.data_len], None)
self.data = np.random.rand(self.num_of_ch,self.data_len) self.data = np.full((self.num_of_ch,self.data_len), None)
self.trigg_data = np.zeros(self.data_len) #array to keep trigger data in self.trigg_data = np.zeros(self.data_len) #array to keep trigger data in
self.num=0 self.num=0
self.doit=0 #to count number of seconds after last stimuli in train self.doit=0 #to count number of seconds after last stimuli in train
@ -742,7 +743,7 @@ class AppForm(QMainWindow):
self.timer.setInterval(int(self.speed_general*1.03)) self.timer.setInterval(int(self.speed_general*1.03))
times = time.time() times = time.time()
self.offline='offline' #remove this! # self.offline='offline' #remove this!
if self.offline=="offline": if self.offline=="offline":
incl = [0,2,6,7,8,10,13,16,18,22,25,28,31,34,41,43,-3,-2,-1] # For offline only incl = [0,2,6,7,8,10,13,16,18,22,25,28,31,34,41,43,-3,-2,-1] # For offline only
loaded_temp = self.q.get()[incl]/10 # Load data loaded_temp = self.q.get()[incl]/10 # Load data
@ -761,7 +762,6 @@ class AppForm(QMainWindow):
except ValueError: except ValueError:
print("ValueError, wait...") print("ValueError, wait...")
return 0 return 0
self.loaded_noeye = self.loaded.copy() self.loaded_noeye = self.loaded.copy()
step1 = time.time()-time_start step1 = time.time()-time_start
@ -787,7 +787,7 @@ class AppForm(QMainWindow):
# [A,B] = ss.butter(2, 0.1/(self.Fs/2), 'highpass') # [A,B] = ss.butter(2, 0.1/(self.Fs/2), 'highpass')
# self.loaded[:self.num_of_ch,-4*self.Fs:] = ss.filtfilt(A, B, self.loaded[:self.num_of_ch, -4*self.Fs:]) # self.loaded[:self.num_of_ch,-4*self.Fs:] = ss.filtfilt(A, B, self.loaded[:self.num_of_ch, -4*self.Fs:])
# self.loaded[:self.num_of_ch,-4*self.Fs:] = self.loaded[:self.num_of_ch,-4*self.Fs:] - np.mean(self.loaded[:self.num_of_ch,-4*self.Fs:],1, keepdims=True) # self.loaded[:self.num_of_ch,-4*self.Fs:] = self.loaded[:self.num_of_ch,-4*self.Fs:] - np.mean(self.loaded[:self.num_of_ch,-4*self.Fs:],1, keepdims=True)
self.loaded[:self.num_of_ch,-4*self.Fs:] = ss.detrend(self.loaded[:self.num_of_ch,-4*self.Fs:]) self.loaded[:self.num_of_ch,-self.plot_len*self.Fs:] = ss.detrend(self.loaded[:self.num_of_ch,-self.plot_len*self.Fs:])
except ZeroDivisionError: # This error means that buffer is still not full except ZeroDivisionError: # This error means that buffer is still not full
if self.qmbx == None: if self.qmbx == None:
self.qmbx = Waiting_window() # Small window with a message to wait self.qmbx = Waiting_window() # Small window with a message to wait
@ -826,9 +826,10 @@ class AppForm(QMainWindow):
# print(len(self.loaded)) # print(len(self.loaded))
# Interpolation - pretty long line, but basically it chooses ranges and # Interpolation - pretty long line, but basically it chooses ranges and
# assign boundary value as a baseline and does that in (I guess) more optimal way than using loops # assign boundary value as a baseline and does that in (I guess) more optimal way than using loops
self.loaded[:, od+ind-int(int_from*self.Fs):od+ind+int(int_to*self.Fs)] = np.outer( # self.loaded[:, od+ind-int(int_from*self.Fs):od+ind+int(int_to*self.Fs)] = np.outer(
self.loaded[:,min(od+ind+int(int_to*self.Fs), 30000-1)], np.ones(min(size, int((int_from+int_to)*self.Fs)))) # self.loaded[:,min(od+ind+int(int_to*self.Fs), 30000-1)], np.ones(min(size, int((int_from+int_to)*self.Fs))))
# this way is even easier...
self.loaded[:, od+ind-int(int_from*self.Fs):od+ind+int(int_to*self.Fs)] = self.loaded[:,min(od+ind+int(int_to*self.Fs), 30000-1)].reshape(-1, 1)
# for i in range(self.loaded.shape[0]): # for i in range(self.loaded.shape[0]):
# self.loaded[i, od+ind-int(int_from*self.Fs):od+ind+int(int_to*self.Fs)] = np.linspace( # self.loaded[i, od+ind-int(int_from*self.Fs):od+ind+int(int_to*self.Fs)] = np.linspace(
# self.loaded[i,min(od+ind-int(int_from*self.Fs),30000-1)], # self.loaded[i,min(od+ind-int(int_from*self.Fs),30000-1)],
@ -934,6 +935,7 @@ class AppForm(QMainWindow):
#set data, last plot_len seconds #set data, last plot_len seconds
plot_data = self.data[i,self.data_len-self.plot_len* plot_data = self.data[i,self.data_len-self.plot_len*
self.Fs:self.data_len] self.Fs:self.data_len]
plot_data = plot_data[::self.plot_len] #lets speed up plotting by downsampling
#EMG has higher amplitude usually. A special case to make it smaller #EMG has higher amplitude usually. A special case to make it smaller
#self.emg_ch+1 because self.num_of_ch doesn't include trigger #self.emg_ch+1 because self.num_of_ch doesn't include trigger
if i==np.arange(self.num_of_ch)[self.emg_ch+1] and self.emg_ch!='': if i==np.arange(self.num_of_ch)[self.emg_ch+1] and self.emg_ch!='':
@ -945,12 +947,12 @@ class AppForm(QMainWindow):
else: else:
plot_data = plot_data/self.plot_dividing_factor + self.num_of_ch - i plot_data = plot_data/self.plot_dividing_factor + self.num_of_ch - i
# plot_data = plot_data/(3.5*np.max(np.abs(plot_data))) + self.num_of_ch - i # plot_data = plot_data/(3.5*np.max(np.abs(plot_data))) + self.num_of_ch - i
self.line[i].set_data(np.arange(0,self.plot_len*self.Fs), plot_data) self.line[i].set_data(np.arange(0,self.Fs), plot_data) #self.plot_len*
self.axes.set_ylim(0, self.num_of_ch+1) self.axes.set_ylim(0, self.num_of_ch+1)
#self.axes.set_ylim(0,np.max(self.data[:,-self.plot_len*self.Fs:])+dif) #set ylim to fit everything on the plot #self.axes.set_ylim(0,np.max(self.data[:,-self.plot_len*self.Fs:])+dif) #set ylim to fit everything on the plot
if len(stim)>0: if len(stim)>0:
for ind in stim: for ind in stim:
self.axes.axvline(ind) #plot vertical line for each trigger self.axes.axvline(int(ind/self.plot_len)) #plot vertical line for each trigger
self.num = len(stim) self.num = len(stim)
# plt.figure() # plt.figure()
# plt.plot(ss.detrend(self.data[10,self.data_len-self.plot_len* # plt.plot(ss.detrend(self.data[10,self.data_len-self.plot_len*
@ -1248,7 +1250,7 @@ class AppForm(QMainWindow):
self.axes.set_yticks(np.arange(1, (self.num_of_ch)*1.01, 1)) self.axes.set_yticks(np.arange(1, (self.num_of_ch)*1.01, 1))
self.axes.set_yticklabels(self.ch_names[::-1]) self.axes.set_yticklabels(self.ch_names[::-1])
self.axes.set_xticks(np.arange(self.Fs,self.plot_len*self.Fs, self.Fs)) self.axes.set_xticks(np.arange(int(self.Fs/self.plot_len), self.Fs, int(self.Fs/self.plot_len))) #self.plot_len*
self.axes.grid(True) self.axes.grid(True)
self.canvas = FigureCanvas(self.fig) self.canvas = FigureCanvas(self.fig)
@ -1279,7 +1281,7 @@ class AppForm(QMainWindow):
self.line[i], = self.axes.plot([] , color = 'black', linewidth=0.4) self.line[i], = self.axes.plot([] , color = 'black', linewidth=0.4)
else: else:
self.line[i], = self.axes.plot([] , color = 'silver', linewidth=0.3) self.line[i], = self.axes.plot([] , color = 'silver', linewidth=0.3)
self.axes.set_xlim(0, self.plot_len*self.Fs) self.axes.set_xlim(0, self.Fs) #self.plot_len*
self.axes.set_ylim(0, (self.num_of_ch+1)*1) self.axes.set_ylim(0, (self.num_of_ch+1)*1)
#self.axes.axvspan((self.plot_len-1)*self.Fs, #self.axes.axvspan((self.plot_len-1)*self.Fs,
# self.plot_len*self.size_of_up, alpha=0.3, color='lightcoral') # self.plot_len*self.size_of_up, alpha=0.3, color='lightcoral')
@ -1405,9 +1407,10 @@ class Ui(QMainWindow):
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication(sys.argv) app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
form = First_window(AppForm) #AppForm() form = First_window(AppForm) #AppForm()
form.show() form.show()
app.exec_() sys.exit(app.exec_())
# cut time different from both sides # cut time different from both sides
# some deafult settings. Maybe remember last configuration? # some deafult settings. Maybe remember last configuration?

View file

@ -286,6 +286,7 @@ class First_window(QMainWindow):
self.emg_ch_loaded = int(settings_file[settings_file[0]=='emg_channel'].values[0][1]) self.emg_ch_loaded = int(settings_file[settings_file[0]=='emg_channel'].values[0][1])
self.exp_trig_loaded = int(settings_file[settings_file[0]=='expected_triggers'].values[0][1]) self.exp_trig_loaded = int(settings_file[settings_file[0]=='expected_triggers'].values[0][1])
self.exp_time_loaded = int(settings_file[settings_file[0]=='expected_time'].values[0][1]) self.exp_time_loaded = int(settings_file[settings_file[0]=='expected_time'].values[0][1])
self.plot_len_loaded = int(settings_file[settings_file[0]=='plot_len'].values[0][1])
self.BoxChecked = False self.BoxChecked = False
except Exception as e: except Exception as e:
ex_type, ex_value, ex_traceback = sys.exc_info() ex_type, ex_value, ex_traceback = sys.exc_info()
@ -647,6 +648,7 @@ class First_window(QMainWindow):
self.emg_ch_lab, self.line_emg_ch, emg_ch_layout = add_thing(self, "EMG channel number:", self.emg_ch_loaded) self.emg_ch_lab, self.line_emg_ch, emg_ch_layout = add_thing(self, "EMG channel number:", self.emg_ch_loaded)
self.exp_trig_lab, self.line_exp_trig, exp_trig_layout = add_thing(self, "Number of bursts within the train:", self.exp_trig_loaded) self.exp_trig_lab, self.line_exp_trig, exp_trig_layout = add_thing(self, "Number of bursts within the train:", self.exp_trig_loaded)
self.exp_time_lab, self.line_exp_time, exp_time_layout = add_thing(self, "Expected time of a single train:", self.exp_time_loaded) self.exp_time_lab, self.line_exp_time, exp_time_layout = add_thing(self, "Expected time of a single train:", self.exp_time_loaded)
self.plot_len_lab, self.plot_len_time, plot_len_layout = add_thing(self, "Plot width [s]:", self.plot_len_loaded)
# You can add feature name if function was added to the function "features" in the main file # You can add feature name if function was added to the function "features" in the main file
features_names = ['None', 'Theta FFT Power', 'Alpha FFT Power', 'Beta FFT Power', features_names = ['None', 'Theta FFT Power', 'Alpha FFT Power', 'Beta FFT Power',
@ -754,6 +756,7 @@ class First_window(QMainWindow):
vbox.addLayout(emg_ch_layout) vbox.addLayout(emg_ch_layout)
vbox.addLayout(exp_trig_layout) vbox.addLayout(exp_trig_layout)
vbox.addLayout(exp_time_layout) vbox.addLayout(exp_time_layout)
vbox.addLayout(plot_len_layout)
scroll = QScrollArea() scroll = QScrollArea()
scroll.setWidget(text_last_ch) scroll.setWidget(text_last_ch)
@ -986,7 +989,8 @@ class First_window(QMainWindow):
'exp_trig': int(self.line_exp_trig.text()), 'exp_trig': int(self.line_exp_trig.text()),
'exp_time': int(self.line_exp_time.text()), 'exp_time': int(self.line_exp_time.text()),
'percentages': percentages, 'percentages': percentages,
'thr_values': values 'thr_values': values,
'plot_len': int(self.plot_len_time.text())
} }
print(self.params_to_pass) print(self.params_to_pass)

View file

@ -75,6 +75,14 @@ def connect_sig(data1, data2, fs):
fs: int fs: int
sampling rate sampling rate
""" """
print(data1.shape, data2.shape)
if all(data1[:, -fs] == None):
print(data2[:, -fs])
data_ret = data1.copy()
data_ret[:, :-fs] = data2[:, -fs].reshape(-1, 1)
data_ret[:,-fs:] = data2[:, -fs:]
return data_ret, 800
print(data2.shape) print(data2.shape)
data2 = data2 data2 = data2
startt = time.time() startt = time.time()
@ -83,6 +91,7 @@ def connect_sig(data1, data2, fs):
pts = list() pts = list()
data_ret = np.zeros(data1.shape) data_ret = np.zeros(data1.shape)
data_ret[:, :-fs] = data1[:,fs:] data_ret[:, :-fs] = data1[:,fs:]
if data2.shape[0]==0 or data2.shape[1]==0: if data2.shape[0]==0 or data2.shape[1]==0:
return data2 return data2
if fs<2000: if fs<2000:
@ -93,7 +102,7 @@ def connect_sig(data1, data2, fs):
print("ARBEJDE IKKEEE") print("ARBEJDE IKKEEE")
# data_ret = np.concatenate((data1, data2[:,-int(size):]),1) # data_ret = np.concatenate((data1, data2[:,-int(size):]),1)
data_ret[:,-fs:] = data2[:, -fs:] data_ret[:,-fs:] = data2[:, -fs:]
return data_ret, None return data_ret, 800
#print('hehe', time.time()-startt) #print('hehe', time.time()-startt)
most_fr = most_frequent(np.array(pts)) most_fr = most_frequent(np.array(pts))
#print('hehe', time.time()-startt) #print('hehe', time.time()-startt)

26
RDA.py
View file

@ -15,6 +15,23 @@ import threading
import queue import queue
import time import time
"""Packets are received every 20 ms in the size that it fits the sampling rate
e.g.:
for 1000 Hz packet size will be 20, because 20*50=1000
for 2500 Hz packet size will be 50, because 50*50=2500
for 50 kHz it will be 1000, because 1000*50=50000
"""
def average(arr, n, mode='mean'):
if mode=='max':
end = n * int(len(arr)/n)
return np.max(arr[:end].reshape(-1, n), 1)
arr = arr.T
data_raw_new = np.zeros((arr.shape[0], int(arr.shape[1]/n)))
for i in range(arr.shape[0]):
a = arr[i]
data_raw_new[i,:] = a.reshape(-1, n).mean(1)
return data_raw_new.T
# Marker class for storing marker information # Marker class for storing marker information
class Marker: class Marker:
@ -146,7 +163,6 @@ def sampleLoop(obj):
elif msgtype == 4: elif msgtype == 4:
# Data message, extract data and markers # Data message, extract data and markers
(block, points, markerCount, data, markers) = GetData(rawdata, channelCount) (block, points, markerCount, data, markers) = GetData(rawdata, channelCount)
if block!=0: if block!=0:
ds=block-oldblock ds=block-oldblock
if ds!=1: if ds!=1:
@ -174,6 +190,12 @@ def sampleLoop(obj):
# Put data at the end of actual buffer # Put data at the end of actual buffer
data_array = data1s.reshape([int(len(data1s)/channelCount), channelCount]) * np.array(resolutions) data_array = data1s.reshape([int(len(data1s)/channelCount), channelCount]) * np.array(resolutions)
data_array = np.vstack([data_array.T, marker_sig]).T #isn't that too slow? data_array = np.vstack([data_array.T, marker_sig]).T #isn't that too slow?
if obj.avgPackets:
resampling_coef = int((len(data)/channelCount)/20)
data1=average(data_array, resampling_coef, 'mean')
data1[:,-1]=average(data_array[:,-1], resampling_coef, 'max')
obj.updateRingBuffer(data1,block)
else:
obj.updateRingBuffer(data_array,block) obj.updateRingBuffer(data_array,block)
data1s = [] data1s = []
@ -194,7 +216,7 @@ def sampleLoop(obj):
class RDA(): class RDA():
def __init__(self,ip='127.0.0.1', port=51244, buffersize=2**10, sendqueue=False, def __init__(self,ip='127.0.0.1', port=51244, buffersize=2**10, sendqueue=False,
si=1/1000, ringbuffersize = 2**12, avgPackets=False): si=1/1000, ringbuffersize = 2**12, avgPackets=True):
# Create a tcpip socket # Create a tcpip socket
#con = socket(AF_INET, SOCK_STREAM) #con = socket(AF_INET, SOCK_STREAM)
# Connect to recorder host via 32Bit RDA-port # Connect to recorder host via 32Bit RDA-port

View file

@ -12,3 +12,4 @@ theta_range: [4,8]
threshold_parameter: 2 threshold_parameter: 2
expected_triggers: 10 expected_triggers: 10
expected_time: 2000 expected_time: 2000
plot_len: 4

Binary file not shown.

Binary file not shown.