diff --git a/EStiMo_GUI_0123.py b/EStiMo_GUI_0123.py index 9af10d4..6118651 100644 --- a/EStiMo_GUI_0123.py +++ b/EStiMo_GUI_0123.py @@ -146,7 +146,7 @@ class NeurOneOffline(): sendqueue=False,ringbuf_factor=2,dump=False,avgPackets=1): self.ringbuffersize = ringbuffersize 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 eeg_chn = np.arange(0,num_electr,1) 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 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""" - offline = 'offline' + # offline = 'offline' #import NeurOne_v3 if offline=="offline": NO = NeurOneOffline() @@ -247,7 +247,7 @@ class AppForm(QMainWindow): def __init__(self, passed_params = None, parent=None): super().__init__() #self.setStyleSheet("background: whitesmoke") - # self.offline = 'NeurOne' #"BrainProducts"#False + self.offline = "offline" #'NeurOne' #"BrainProducts"#False self.time_start = time.time() QMainWindow.__init__(self, parent) self.montage_file_path = 'montage_18ch.csv' @@ -416,11 +416,12 @@ class AppForm(QMainWindow): self.remove_outliers = passed_params['remove_outliers'] self.ip = passed_params['ip'] 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_time = passed_params['exp_time'] self.if_percentage = passed_params['percentages'] self.received_thr_values = passed_params['thr_values'] + self.plot_len = passed_params['plot_len'] else: self.montage_file_path = 'montage_18ch.csv' 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.port = 50000 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.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.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.plot_len = 4 #length of data to plot (last seconds of data array) self.plot_dividing_factor = 100 self.previous_state = np.zeros(6) 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... #create data array - self.loaded = np.zeros([self.num_of_ch,self.data_len]) - self.loaded_full = np.zeros([self.num_of_ch+1,self.data_len]) - self.data = np.random.rand(self.num_of_ch,self.data_len) + self.loaded = np.full([self.num_of_ch,self.data_len], None) + self.loaded_full = np.full([self.num_of_ch+1,self.data_len], None) + 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.num=0 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)) times = time.time() - self.offline='offline' #remove this! + # self.offline='offline' #remove this! 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 loaded_temp = self.q.get()[incl]/10 # Load data @@ -761,7 +762,6 @@ class AppForm(QMainWindow): except ValueError: print("ValueError, wait...") return 0 - self.loaded_noeye = self.loaded.copy() step1 = time.time()-time_start @@ -787,7 +787,7 @@ class AppForm(QMainWindow): # [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:] = 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 if self.qmbx == None: self.qmbx = Waiting_window() # Small window with a message to wait @@ -826,9 +826,10 @@ class AppForm(QMainWindow): # print(len(self.loaded)) # 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 - 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[:, 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)))) + # 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]): # 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)], @@ -934,6 +935,7 @@ class AppForm(QMainWindow): #set data, last plot_len seconds plot_data = self.data[i,self.data_len-self.plot_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 #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!='': @@ -945,12 +947,12 @@ class AppForm(QMainWindow): else: 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 - 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,np.max(self.data[:,-self.plot_len*self.Fs:])+dif) #set ylim to fit everything on the plot if len(stim)>0: 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) # plt.figure() # 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_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.canvas = FigureCanvas(self.fig) @@ -1279,7 +1281,7 @@ class AppForm(QMainWindow): self.line[i], = self.axes.plot([] , color = 'black', linewidth=0.4) else: 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.axvspan((self.plot_len-1)*self.Fs, # self.plot_len*self.size_of_up, alpha=0.3, color='lightcoral') @@ -1405,9 +1407,10 @@ class Ui(QMainWindow): if __name__ == '__main__': app = QApplication(sys.argv) + app.setQuitOnLastWindowClosed(True) form = First_window(AppForm) #AppForm() form.show() - app.exec_() + sys.exit(app.exec_()) # cut time different from both sides # some deafult settings. Maybe remember last configuration? diff --git a/FirstWindow.py b/FirstWindow.py index e84ffd7..58c5ebe 100644 --- a/FirstWindow.py +++ b/FirstWindow.py @@ -286,6 +286,7 @@ class First_window(QMainWindow): 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_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 except Exception as e: ex_type, ex_value, ex_traceback = sys.exc_info() @@ -646,7 +647,8 @@ class First_window(QMainWindow): self.eog_ch_lab, self.line_eog_ch, eog_ch_layout = add_thing(self, "EOG channel number:", self.eog_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_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 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(exp_trig_layout) vbox.addLayout(exp_time_layout) + vbox.addLayout(plot_len_layout) scroll = QScrollArea() scroll.setWidget(text_last_ch) @@ -986,7 +989,8 @@ class First_window(QMainWindow): 'exp_trig': int(self.line_exp_trig.text()), 'exp_time': int(self.line_exp_time.text()), 'percentages': percentages, - 'thr_values': values + 'thr_values': values, + 'plot_len': int(self.plot_len_time.text()) } print(self.params_to_pass) diff --git a/Functions.py b/Functions.py index efa0ee9..4509a0a 100644 --- a/Functions.py +++ b/Functions.py @@ -75,6 +75,14 @@ def connect_sig(data1, data2, fs): fs: int 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) data2 = data2 startt = time.time() @@ -83,6 +91,7 @@ def connect_sig(data1, data2, fs): pts = list() data_ret = np.zeros(data1.shape) data_ret[:, :-fs] = data1[:,fs:] + if data2.shape[0]==0 or data2.shape[1]==0: return data2 if fs<2000: @@ -93,7 +102,7 @@ def connect_sig(data1, data2, fs): print("ARBEJDE IKKEEE") # data_ret = np.concatenate((data1, data2[:,-int(size):]),1) data_ret[:,-fs:] = data2[:, -fs:] - return data_ret, None + return data_ret, 800 #print('hehe', time.time()-startt) most_fr = most_frequent(np.array(pts)) #print('hehe', time.time()-startt) diff --git a/RDA.py b/RDA.py index 6edb91d..ac08dd1 100644 --- a/RDA.py +++ b/RDA.py @@ -15,6 +15,23 @@ import threading import queue 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 class Marker: @@ -146,7 +163,6 @@ def sampleLoop(obj): elif msgtype == 4: # Data message, extract data and markers (block, points, markerCount, data, markers) = GetData(rawdata, channelCount) - if block!=0: ds=block-oldblock if ds!=1: @@ -174,7 +190,13 @@ def sampleLoop(obj): # Put data at the end of actual buffer 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? - obj.updateRingBuffer(data_array,block) + 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) data1s = [] @@ -194,7 +216,7 @@ def sampleLoop(obj): class RDA(): 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 #con = socket(AF_INET, SOCK_STREAM) # Connect to recorder host via 32Bit RDA-port diff --git a/TMS_protocol.txt b/TMS_protocol.txt index dca2794..a6a5d5e 100644 --- a/TMS_protocol.txt +++ b/TMS_protocol.txt @@ -12,3 +12,4 @@ theta_range: [4,8] threshold_parameter: 2 expected_triggers: 10 expected_time: 2000 +plot_len: 4 diff --git a/__pycache__/FirstWindow.cpython-38.pyc b/__pycache__/FirstWindow.cpython-38.pyc index f1c07ac..5791e54 100644 Binary files a/__pycache__/FirstWindow.cpython-38.pyc and b/__pycache__/FirstWindow.cpython-38.pyc differ diff --git a/__pycache__/Functions.cpython-38.pyc b/__pycache__/Functions.cpython-38.pyc index d33361b..1366ff2 100644 Binary files a/__pycache__/Functions.cpython-38.pyc and b/__pycache__/Functions.cpython-38.pyc differ diff --git a/__pycache__/RDA.cpython-38.pyc b/__pycache__/RDA.cpython-38.pyc index 0286da0..6cb3465 100644 Binary files a/__pycache__/RDA.cpython-38.pyc and b/__pycache__/RDA.cpython-38.pyc differ