def get_surface_channel_gamma(path, start=2., end=10., sampling_rate=2500, plot=False): mm = np.memmap(path, dtype=np.int16, mode='r') num_channels = get_channel_count(os.path.dirname(path)) chunk = get_chunk(mm, start, end, num_channels, sampling_rate) gm = [] good_channels = [] for ch in range(np.shape(chunk)[0]): if ch not in skip_channels: f, pXX = welch_power(chunk[ch, :], start=2, window=8) gm.extend([pXX[np.where(f > 40.)[0][0]]]) good_channels.extend([ch]) threshold = np.max( gm[::-1][:5] ) #assumes the last 5 are out of the brain; uses the max gamma on these channels as the threshold surface_channel = good_channels[mlab.cross_from_above( gaussian_filter1d(gm, 0), threshold)[0]] if plot: plt.plot(good_channels, gaussian_filter1d(gm, 2)) plt.gca().axhline(threshold, color='r') del mm return surface_channel
def get_surface_channel_freq(path,frequency_range=[1,5],start=2.,end=10.,sampling_rate=2500,filter_size=2,sigma=2.,plot=False,filter=False,probemap=None): mm = np.memmap(path, dtype=np.int16, mode='r') num_channels = get_channel_count(os.path.dirname(path),from_channel_map=False) chunk = get_chunk(mm,start,end,num_channels,sampling_rate) if probemap is not None: chunk = chunk[probemap,:] gm = [] good_channels = [] for ch in range(np.shape(chunk)[0]): if ch not in skip_channels: if filter: data = filtr(chunk[ch,:],0.1,300,sampling_rate,3) else: data = chunk[ch,:] f,pXX = welch_power(chunk[ch,:],start=2,window=8) gm.extend([np.mean(pXX[np.where((f>frequency_range[0])&(f<frequency_range[1]))[0]])]) good_channels.extend([ch]) #threshold = np.mean(gm[::-1][:5]) #assumes the last 5 are out of the brain; uses the max gamma on these channels as the threshold threshold = np.mean(gaussian_filter1d(gm,filter_size)[::-1][:5])+np.std(gaussian_filter1d(gm,filter_size)[::-1][:5])*sigma if plot: plt.plot(good_channels,gaussian_filter1d(gm,filter_size)) plt.gca().axhline(threshold,color='r') plt.xlabel('channel number') plt.ylabel('power in '+str(frequency_range[0])+' to '+str(frequency_range[1])+' band') try: surface_channel = good_channels[mlab.cross_from_above(gaussian_filter1d(gm,filter_size),threshold)[-1]] return surface_channel except: return None del mm return surface_channel
def get_surface_channel_spikeband(path, start=2., end=10., sampling_rate=30000, plot=False, filter_size=2, sigma=1., filter=False, probemap=None): mm = np.memmap(path, dtype=np.int16, mode='r') print(os.path.dirname(path)) num_channels = get_channel_count(os.path.dirname(path), from_channel_map=False) print(num_channels) chunk = get_chunk(mm, start, end, num_channels, sampling_rate) if probemap is not None: chunk = chunk[probemap, :] plt.imshow(chunk[:, :30000]) plt.gca().set_aspect(100) plt.figure() rms = [] good_channels = [] for ch in range(np.shape(chunk)[0]): if ch not in skip_channels: if filter: data = filtr(chunk[ch, :], 300, 6000, sampling_rate, 3) else: data = chunk[ch, :] rms.extend([RMS(data)]) good_channels.extend([ch]) threshold = np.mean(gaussian_filter1d(rms, filter_size)[::-1][:5]) + np.std( gaussian_filter1d(rms, filter_size)[::-1][:5] ) * sigma #assumes the last 5 are out of the brain; uses the mean + sd of these 5 as the threshold for pial surface # print(np.where(np.array(rms)<8.)) # print(good_channels[np.where(np.array(rms)<8.)[0].astype(int)]) if plot: plt.plot(good_channels, gaussian_filter1d(rms, filter_size)) plt.gca().axhline(threshold, color='r') plt.xlabel('channel number') plt.ylabel('spike band RMS') #print(np.where(np.array(rms)<6.)) del mm try: surface_channel = good_channels[mlab.cross_from_above( gaussian_filter1d(rms, filter_size), threshold)[0]] return surface_channel except: return None