def onset_delta(channel1, channel2, fs=1250): onset_ca1 = np.zeros((channel1.shape[1])) onset_sub = np.zeros((channel2.shape[1])) delta = np.zeros((channel1.shape[1])) for i in range(channel1.shape[1]): #ca1 #getting product (low_pass hilbert transform * lp power at ripple frequency) f, t, Sxx = signal.spectrogram(channel1[:, i], window=('gaussian', 30), fs=fs, nperseg=int(fs / 250), detrend='constant', scaling='spectrum') lp_power = lowpass(Sxx[1, :], 30, fs / (channel1.shape[0] / Sxx.shape[1])) lp_power_norm = lp_power / np.max(lp_power) lp_power_norm = signal.resample(lp_power_norm, channel1.shape[0]) lp_hilb = lowpass(abs(signal.hilbert(channel1[:, i])), 30, fs) lp_hilb_norm = lp_hilb / np.max(lp_hilb) product = lp_power_norm * (lp_hilb_norm + 1) # getting all peaks with differential deri = np.diff(product) peak_index = [] peak_size = [] for d in range(len(deri) - 1): if (deri[d, ]) > 0 and (deri[d + 1, ] < 0): peak_index.append(d + 1) peak_size.append(product[d + 1]) # sort the peaks according to the size (descending) peak_zip = list(zip(peak_index, peak_size)) peak_zip = sorted(peak_zip, key=lambda element: element[1], reverse=True) sorted_list = map(list, zip(*peak_zip)) sorted_list = list(sorted_list) peak_index = sorted_list[0] peak_size = sorted_list[1] # look which peak is most likely to be the real ripple peak peak_index_short = [] peak_size_short = [] for d in range(len(peak_size)): if (peak_index[d] > channel1.shape[0] / 3.5) and ( peak_index[d] < channel1.shape[0] - channel1.shape[0] / 3): peak_index_short.append(peak_index[d]) peak_size_short.append(peak_size[d]) final_peaks = [] for d in range(len(peak_index_short)): if (peak_size_short[d] > peak_size_short[0] * 0.05): final_peaks.append(peak_index_short[d]) c = np.min(final_peaks) # look for the onset while c > 0: if product[c, ] < (np.max(product) * 0.03): onset_ca1[i] = c c = 0 c -= 1 #Sub #getting product (low_pass hilbert transform * lp power at ripple frequency) f, t, Sxx = signal.spectrogram(channel2[:, i], window=('gaussian', 30), fs=fs, nperseg=int(fs / 250), detrend='constant', scaling='spectrum') lp_power = lowpass(Sxx[1, :], 30, fs / (channel2.shape[0] / Sxx.shape[1])) lp_power_norm = lp_power / np.max(lp_power) lp_power_norm = signal.resample(lp_power_norm, channel2.shape[0]) lp_hilb = lowpass(abs(signal.hilbert(channel2[:, i])), 30, fs) lp_hilb_norm = lp_hilb / np.max(lp_hilb) product = lp_power_norm * (lp_hilb_norm + 1) # getting the peak with differential deri = np.diff(product) peak_index = [] peak_size = [] for d in range(len(deri) - 1): if (deri[d, ]) > 0 and (deri[d + 1, ] < 0): peak_index.append(d + 1) peak_size.append(product[d + 1]) # sort the peaks according to the size (descending) peak_zip = list(zip(peak_index, peak_size)) peak_zip = sorted(peak_zip, key=lambda element: element[1], reverse=True) sorted_list = map(list, zip(*peak_zip)) sorted_list = list(sorted_list) peak_index = sorted_list[0] peak_size = sorted_list[1] # look which peak is most likely to be the real ripple peak peak_index_short = [] peak_size_short = [] for d in range(len(peak_size)): if (peak_index[d] > channel2.shape[0] / 3.5) and ( peak_index[d] < channel2.shape[0] - channel2.shape[0] / 3): peak_index_short.append(peak_index[d]) peak_size_short.append(peak_size[d]) final_peaks = [] for d in range(len(peak_index_short)): if peak_size_short[d] > peak_size_short[0] * 0.05: final_peaks.append(peak_index_short[d]) c = np.min(final_peaks) while c > 0: if product[c, ] < (np.max(product) * 0.03): onset_sub[i] = c c = 0 c -= 1 #delta if (onset_sub[i] > 0) and (onset_ca1[i] > 0): delta[i] = onset_sub[i] - onset_ca1[i] else: delta[i] = -999 plt.hist(delta) return delta, onset_ca1, onset_sub
automatic_outcome[i]=1 else: automatic_outcome[i]=0 data_sub_detection['automatic_outcome']=automatic_outcome del atypical,standard,automatic_outcome #%% EDA some example before running the gui i=10 time=np.linspace(0,0.500,data_sub_detection['ripple_ca1_shank_mean'].shape[0]) fs=data_sub_detection['fs'] f,t,Sxx= signal.spectrogram(data_sub_detection['ripple_ca1_shank_mean'][:,i],window=('gaussian',30), fs=data_sub_detection['fs'], nperseg=5, detrend='constant', scaling='spectrum') lp_power=lowpass(Sxx[1,:],30,fs/(data_sub_detection['ripple_ca1_shank_mean'].shape[0]/Sxx.shape[1])) lp_power_norm=lp_power/np.max(lp_power) lp_power_norm1=signal.resample(lp_power_norm,data_sub_detection['ripple_ca1_shank_mean'].shape[0]) lp_hilb=lowpass(abs(signal.hilbert(data_sub_detection['ripple_ca1_shank_mean'][:,i])),30,data_sub_detection['fs']) lp_hilb_norm1=lp_hilb/np.max(lp_hilb) #sub f,t,Sxx= signal.spectrogram(data_sub_detection['ripple_sub_shank_mean'][:,i],window=('gaussian',30), fs=data_sub_detection['fs'], nperseg=5, detrend='constant', scaling='spectrum') lp_power=lowpass(Sxx[1,:],30,fs/(data_sub_detection['ripple_sub_shank_mean'].shape[0]/Sxx.shape[1])) lp_power_norm=lp_power/np.max(lp_power) lp_power_norm2=signal.resample(lp_power_norm,data_sub_detection['ripple_sub_shank_mean'].shape[0]) lp_hilb=lowpass(abs(signal.hilbert(data_sub_detection['ripple_sub_shank_mean'][:,i])),30,data_sub_detection['fs']) lp_hilb_norm2=lp_hilb/np.max(lp_hilb) plt.figure() plt.subplot(3,1,1) plt.plot(time,data_sub_detection['ripple_ca1_shank_mean'][:,i]/np.max(data_sub_detection['ripple_ca1_shank_mean'][:,i]))
def cc_delta(channel1, channel2, fs=1250): cc_peak = np.zeros((channel1.shape[1])) cc_rip_peak = np.zeros((channel1.shape[1])) starting = [] #np.zeros((channel1.shape[1])) stopping = [] #np.zeros((channel1.shape[1])) for i in range(channel1.shape[1]): #ca1 #getting product (low_pass hilbert transform * lp power at ripple frequency) f, t, Sxx = signal.spectrogram(channel1[:, i], window=('gaussian', 30), fs=fs, nperseg=int(fs / 250), detrend='constant', scaling='spectrum') lp_power = lowpass(Sxx[1, :], 30, fs / (channel1.shape[0] / Sxx.shape[1])) lp_power_norm = lp_power / np.max(lp_power) lp_power_norm = signal.resample(lp_power_norm, channel1.shape[0]) lp_hilb = lowpass(abs(signal.hilbert(channel1[:, i])), 30, fs) lp_hilb_norm = lp_hilb / np.max(lp_hilb) product_ca1 = lp_power_norm * (lp_hilb_norm + 1) # getting all peaks with differential deri = np.diff(product_ca1) peak_index = [] peak_size = [] for d in range(len(deri) - 1): if (deri[d, ]) > 0 and (deri[d + 1, ] < 0): peak_index.append(d + 1) peak_size.append(product_ca1[d + 1]) # sort the peaks according to the size (descending) peak_zip = list(zip(peak_index, peak_size)) peak_zip = sorted(peak_zip, key=lambda element: element[1], reverse=True) sorted_list = map(list, zip(*peak_zip)) sorted_list = list(sorted_list) peak_index = sorted_list[0] peak_size = sorted_list[1] # look which peak is most likely to be the real ripple peak peak_index_short = [] peak_size_short = [] for d in range(len(peak_size)): if (peak_index[d] > channel1.shape[0] / 3.5) and ( peak_index[d] < channel1.shape[0] - channel1.shape[0] / 3): peak_index_short.append(peak_index[d]) peak_size_short.append(peak_size[d]) final_peaks = [] for d in range(len(peak_index_short)): if (peak_size_short[d] > peak_size_short[0] * 0.2): final_peaks.append(peak_index_short[d]) peak_ca1 = np.min(final_peaks) #Sub #getting product (low_pass hilbert transform * lp power at ripple frequency) f, t, Sxx = signal.spectrogram(channel2[:, i], window=('gaussian', 30), fs=fs, nperseg=int(fs / 250), detrend='constant', scaling='spectrum') lp_power = lowpass(Sxx[1, :], 30, fs / (channel2.shape[0] / Sxx.shape[1])) lp_power_norm = lp_power / np.max(lp_power) lp_power_norm = signal.resample(lp_power_norm, channel2.shape[0]) lp_hilb = lowpass(abs(signal.hilbert(channel2[:, i])), 30, fs) lp_hilb_norm = lp_hilb / np.max(lp_hilb) product_sub = lp_power_norm * (lp_hilb_norm + 1) # getting the peak with differential deri = np.diff(product_sub) peak_index = [] peak_size = [] for d in range(len(deri) - 1): if (deri[d, ]) > 0 and (deri[d + 1, ] < 0): peak_index.append(d + 1) peak_size.append(product_sub[d + 1]) # sort the peaks according to the size (descending) peak_zip = list(zip(peak_index, peak_size)) peak_zip = sorted(peak_zip, key=lambda element: element[1], reverse=True) sorted_list = map(list, zip(*peak_zip)) sorted_list = list(sorted_list) peak_index = sorted_list[0] peak_size = sorted_list[1] # look which peak is most likely to be the real ripple peak peak_index_short = [] peak_size_short = [] for d in range(len(peak_size)): if (peak_index[d] > channel2.shape[0] / 3.5) and ( peak_index[d] < channel2.shape[0] - channel2.shape[0] / 3): peak_index_short.append(peak_index[d]) peak_size_short.append(peak_size[d]) final_peaks = [] for d in range(len(peak_index_short)): if peak_size_short[d] > peak_size_short[0] * 0.2: final_peaks.append(peak_index_short[d]) peak_sub = np.min(final_peaks) #comparing with cc the region around (50ms) the peak in the two channels if peak_sub - peak_ca1 > 0: stopping.append(int(peak_sub + 10 * fs / 1000)) starting.append(int(peak_ca1 - 10 * fs / 1000)) else: stopping.append(int(peak_ca1 + 10 * fs / 1000)) starting.append(int(peak_sub - 10 * fs / 1000)) cc = np.correlate(product_sub[int(starting[i]):int(stopping[i]), ], product_ca1[int(starting[i]):int(stopping[i]), ], mode='same') temp_peak = np.argmax(cc) cc_peak[i] = temp_peak - len(cc) / 2 if channel1.shape[1] > 5: plt.hist(cc_peak, bins=int(channel1.shape[1] / 5)) else: plt.hist(cc_peak) return cc_peak, starting, stopping
def plot(self): time = np.linspace(-len(self.channel1) / 2 / self.fs, len(self.channel1) / 2 / self.fs, len(self.channel1)) if self.automatic_outcome == 1: #a.set_title('Atypical',fontsize=20) classe = 'Atypical' else: #a.set_title('Standard',fontsize=20) classe = 'Standard' fig = Figure(figsize=(10, 8)) #fig.suptitle('Classified as: %s - Ripple number: %d' %(classe, self.i),fontsize=22) a = fig.add_subplot(4, 2, 5) a.plot(time[180:420], self.channel1[180:420, ], color='black', label='CA1') a.plot(time[180:420], self.channel2[180:420, ], color='red', label='Sub') a.legend(loc='upper left') b = fig.add_subplot(4, 2, 1) #b.set_title('CA1',fontsize=20) b.plot(time[100:500], self.channel1[100:500], color='black', label='CA1') b.legend(loc='upper left') c = fig.add_subplot(4, 2, 3) #c.set_title('Subiculum',fontsize=20) c.plot(time[100:500], self.channel2[100:500], color='red', label='Sub') c.legend(loc='upper left') d = fig.add_subplot(4, 2, 7) # d.set_title('Lowpass Hilbert transform',fontsize=20) signal1 = lowpass(abs(signal.hilbert(self.channel1[100:500])), 30, 1250) d.plot(time[100:500], signal1 / np.max(signal1), color='black', label='CA1') signal2 = lowpass(abs(signal.hilbert(self.channel2[100:500])), 30, 1250) d.plot(time[100:500], signal2 / np.max(signal2), color='red', label='Sub') d.legend(loc='upper left') d.set_xlabel('Time (ms)', fontsize=18) e = fig.add_subplot(4, 2, 6) e.plot(time[180:420], self.raw_mean1[180:420], color='black', label='CA1') e.plot(time[180:420], self.raw_mean2[180:420], color='red', label='Sub') e.legend(loc='upper left') e.set_xlabel('Time (ms)', fontsize=18) f = fig.add_subplot(4, 2, 2) f.plot(time[100:500], self.raw_mean1[100:500], color='black', label='CA1') f.legend(loc='upper left') g = fig.add_subplot(4, 2, 4) g.plot(time[100:500], self.raw_mean2[100:500], color='red', label='Sub') g.legend(loc='upper left') #h=fig.add_subplot(4,2,8) #h.plot(time[100:500],self.raw1[100:500]/np.max(self.raw1),color='black') #h.plot(time[100:500],self.raw2[100:500]/np.max(self.raw2),color='red') width = self.window.winfo_screenwidth() height = self.window.winfo_screenheight() self.window.geometry("%dx%d" % (width, height)) self.window.state('zoomed') canvas = FigureCanvasTkAgg(fig, master=self.window) canvas.get_tk_widget().pack(fill=tk.BOTH, expand=1) canvas.show() fig.text(0.55, 0.195, 'Ripple number: %d' % self.i, fontsize=20) fig.text(0.55, 0.135, 'Classified as: %s' % classe, fontsize=20)
#%% cut_out = {} cut_out_ripple = {} cut_out_high = {} cut_out_low = {} cut_out_envelope = {} base_env = {} gap = 2000 counter = 0 for key, value in proc_data.items(): if (counter == 0) and (min(peak_times) < gap): peak_times = peak_times[1:] elif (counter == 0) and (max(peak_times) > value.shape[0] - gap): peak_times = peak_times[:-1] swr_list = [] for sw in range(len(peak_times)): swr_list.append(value[peak_times[sw] - gap:peak_times[sw] + gap]) swr_list = np.array(swr_list).reshape(len(peak_times), gap * 2) cut_out[key] = np.asarray(swr_list).T cut_out_ripple[key] = bandpass(np.asarray(swr_list).T, 150, 300) cut_out_low[key] = lowpass(np.array(swr_list).T, 1000) cut_out_high[key] = bandpass(np.array(swr_list).T, 800, 2000) #cut_out_proc2[key]=bandpass(np.array(swr_list).T,0.1,900) cut_out_envelope[key] = np.abs(signal.hilbert(cut_out_ripple[key], axis=0)) base_env[key] = np.std(cut_out_envelope[key]) counter += 1 del swr_list, counter, key, value
#%% import numpy as np import matplotlib.pyplot as plt from electrophysio.butter import lowpass cut_out_z_scored={} for k,v in cut_out_enve.items(): temp_z_score=np.zeros((cut_out_enve['ch1'].shape[0],cut_out_enve['ch1'].shape[1])) #tot_len=int(v.shape[0]) #segment=int(tot_len/8) for rip in range(v.shape[1]): filtered_sig=lowpass(v,30,1000) temp_mean=np.mean(filtered_sig[:,rip],axis=0) stdevia=np.std(filtered_sig[:,rip]) for i in range(v.shape[0]): temp_z_score[i,rip]=(filtered_sig[i,rip]-temp_mean)/stdevia cut_out_z_scored[k]=temp_z_score del k,v,temp_z_score #%% iceberg={} thres=2
#but ordered following the standard flow in the hippocampus position=['CA3','CA1','Sub'] #%% AUTOMATIC DETECTION OF SWR ORIGIN (starter) # on the very bottom there is a second option but it doesnt work as well as this if len(position)<3 or len(position)>4: raise ValueError('the length of the variable position must be 3 or 4') lp_envelope={} starter=[] for key,value in cut_out_envelope.items(): lp_envelope[key]=lowpass(value,30) center_cutout=int((cut_out['CA3'].shape[0])/2) gap2=int(fs/20) # 50 ms for sw in range(lp_envelope[position[0]].shape[1]): if len(position)==4: temp00=np.correlate(lp_envelope[position[0]][center_cutout-gap2:center_cutout+gap2,sw], lp_envelope[position[1]][center_cutout-gap2:center_cutout+gap2,sw],mode='same') temp00=np.argmax(temp00) temp01=np.correlate(lp_envelope[position[0]][center_cutout-gap2:center_cutout+gap2,sw], lp_envelope[position[2]][center_cutout-gap2:center_cutout+gap2,sw], mode='same') temp01=np.argmax(temp01) temp02=np.correlate(lp_envelope[position[0]][center_cutout-gap2:center_cutout+gap2,sw], lp_envelope[position[3]][center_cutout-gap2:center_cutout+gap2,sw], mode='same') temp02=np.argmax(temp02) if (temp00 < center_cutout-gap2) & (temp01< center_cutout-gap2): starter.append(position[0])