def SPK_HFO_coupling(SPK,HFO,sample_rate,cluster = 'all',nbins = 32,color=None): if color ==None: colours = ['#000000','#0000FF','#FF0000','#8fbc8f','yellow'] ks = np.array([]) for ev in range(len(HFO.event)): loc = [int((x- HFO.event[ev].start_sec)*sample_rate) for x in SPK.__getlist__('tstamp') if x > HFO.event[ev].start_sec and x < HFO.event[ev].end_sec] if len(loc) > 0: waveform = HFO.event[ev].waveform[HFO.event[ev].start_idx:HFO.event[ev].end_idx,1] phs = np.angle(sig.hilbert(waveform)) k = phs[loc] ks = np.append(ks,k) clus = 1 plt.subplot(1,2,1,polar=True) plt.hist(ks,bins=nbins,facecolor=colours[clus]) plt.yticks([]) ax = plt.subplot(1,2,2,polar=False) nbin = np.concatenate((ks,ks+2*np.pi)) plt.hist(nbin,bins=2*nbins,facecolor=colours[clus]) plt.xticks(np.linspace(-np.pi,3*np.pi,9),[str(int(x)) for x in 180*np.linspace(-np.pi,3*np.pi,9)/np.pi]) plt.xlim([-np.pi,3*np.pi]) adjust_spines(ax, ['left','bottom'])
def rastergram(self, ax = None, spines = ['left','bottom'],time_edge = None, exclude = [],figure_size=(15,5),dpi=600, line = True): """ Plot rastergram Parameters ---------- ax: matplotlib axes None (default) - create a new figure ax - axes of figure where figure should plot spines: str ['left', 'bottom'] (default) - plot figure with left and bottom spines only time_edge: tupple Determine the x-axis limits. exclude: list Channels/Cluster to exclude from plot figure_size: tuple (5,5) (default) - Size of figure, tuple of integers with width, height in inches dpi: int 600 - DPI resolution """ htype = self.event[0].htype if ax == None: # Creating the figure f = plt.figure(figsize=figure_size,dpi=dpi) # creating the axes ax = f.add_subplot(111) if htype == 'Spike': cluster = self.__getlist__('cluster') elif htype == 'HFO': cluster = range(len(self.ch_labels)) if time_edge == None: time_edge = self.time_edge num_clus = int(np.max(cluster))+1 label = [] c_l = 0 for clus in [x for x in range(num_clus) if x not in exclude]: label.append(self.ch_labels[clus]) if htype == 'Spike': objs = [x for x in self.event if x.cluster == clus] elif htype == 'HFO': objs = [x for x in self.event if x.channel == clus] for ev in objs: rect = patches.Rectangle((ev.tstamp,c_l),0.001,.8, lw=0.5) ax.add_patch(rect) if line: plt.hlines(c_l+.5,time_edge[0],time_edge[1],colors='c') c_l += 1 ax.set_ylim(0,c_l) ax.set_xlim(time_edge[0],time_edge[-1]) plt.yticks(np.arange(c_l)+0.5,label, size=16) adjust_spines(ax, spines)
def ISI(self,ch=0,cl=1,t0=0,t1=1000,binsize=10,figure_size=(10,5)): plt.figure(figsize=figure_size) clus = np.round(self.times_cluster(ch,cl)*1000) t_vector = np.diff(clus) ax = plt.subplot(111) n, bins, duu = plt.hist(t_vector,np.arange(t0,t1,binsize)) plt.xticks(np.arange(t0,t1,10*binsize),np.arange(t0,t1,10*binsize)) plt.xlabel('miliseconds') plt.title('Inter-Spike Interval') adjust_spines(ax,['left','bottom'])
def auto_corr(self,ch=0,cl=1,t0=0,t1=1000,binsize=10,figure_size=(10,5)): plt.figure(figsize=figure_size) clus = np.round(self.times_cluster(ch,cl)*1000) t_matrix = numpy.matlib.repmat(clus, clus.shape[0], 1) aux = t_matrix - t_matrix.T t_vector = aux.reshape(-1) ax = plt.subplot(111) n, bins, duu = plt.hist(t_vector,np.arange(t0,t1,binsize)) plt.xticks(np.arange(t0,t1,10*binsize),np.arange(t0,t1,10*binsize)) plt.xlabel('miliseconds') plt.title('Auto-correlogram') adjust_spines(ax,['left','bottom'])
def cross_corr(self,ch1=0,cl1=1,ch2=0,cl2=2,t0=0,t1=1000,binsize=10,figure_size=(10,5)): plt.figure(figsize=figure_size) clus1 = np.round(self.times_cluster(ch1,cl1)*1000) clus2 = np.round(self.times_cluster(ch2,cl2)*1000) spk1matrix = numpy.matlib.repmat(clus1, clus2.shape[0], 1) spk2matrix = numpy.matlib.repmat(clus2, clus1.shape[0], 1) dif = spk1matrix - spk2matrix.T t_vector = dif.reshape(-1) ax = plt.subplot(111) n, bins, duu = plt.hist(t_vector,np.arange(t0,t1,binsize)) plt.xticks(np.arange(t0,t1,10*binsize),np.arange(t0,t1,10*binsize)) plt.xlabel('miliseconds') plt.title('Cross Correlogram') adjust_spines(ax,['left','bottom'])
def rastergram(self, ax = None, spines = ['left','bottom'],time_edge = None, exclude_chan = [],exclude_clus = [],figure_size=(15,10),dpi=600, line = True,common=False): """ Plot rastergram Parameters ---------- ax: matplotlib axes None (default) - create a new figure ax - axes of figure where figure should plot spines: str ['left', 'bottom'] (default) - plot figure with left and bottom spines only time_edge: tupple Determine the x-axis limits. exclude: list Channels/Cluster to exclude from plot figure_size: tuple (5,5) (default) - Size of figure, tuple of integers with width, height in inches dpi: int 600 - DPI resolution """ if ax == None: # Creating the figure f = plt.figure(figsize=figure_size,dpi=dpi) # creating the axes ax = f.add_subplot(111) cluster = self.__getlist__('cluster') channels = self.__getlist__('channel') if time_edge == None: time_edge = self.time_edge num_clus = int(np.max(cluster))+1 num_chan = int(np.max(channels))+1 label = [] c_l = 0 if common: label.append('Common_ref') objs = [x for x in self.event if x.channel == 'common'] ax.scatter(objs,c_l*np.ones(len(objs)),c='k',marker='|') if line: plt.hlines(c_l,time_edge[0],time_edge[1],colors='c') c_l += 1 else: for chan in [x for x in range(num_chan) if x not in exclude_chan]: for clus in [x for x in range(num_clus) if x not in exclude_clus]: objs = [x.tstamp for x in self.event if x.channel == chan and x.cluster == clus] if len(objs) > 0: label.append(self.ch_labels[chan]+'_'+str(clus)) ax.scatter(objs,c_l*np.ones(len(objs)),c='k',marker='|') if line: plt.hlines(c_l,time_edge[0],time_edge[1],colors='c') c_l += 1 ax.set_ylim(-.5,c_l) ax.set_xlim(time_edge[0],time_edge[-1]) plt.yticks(np.arange(c_l),label, size=16) plt.xticks(np.arange(self.time_edge[0],self.time_edge[1],600),np.arange(self.time_edge[0],self.time_edge[1],600)/60) plt.xlim([self.time_edge[0],self.time_edge[1]]) adjust_spines(ax, spines)
def rastergram(self, ax=None, spines=['left', 'bottom'], time_edge=None, exclude=[], figure_size=(15, 5), dpi=600, line=True): """ Plot rastergram Parameters ---------- ax: matplotlib axes None (default) - create a new figure ax - axes of figure where figure should plot spines: str ['left', 'bottom'] (default) - plot figure with left and bottom spines only time_edge: tupple Determine the x-axis limits. exclude: list Channels/Cluster to exclude from plot figure_size: tuple (5,5) (default) - Size of figure, tuple of integers with width, height in inches dpi: int 600 - DPI resolution """ htype = self.event[0].htype if ax == None: # Creating the figure f = plt.figure(figsize=figure_size, dpi=dpi) # creating the axes ax = f.add_subplot(111) if htype == 'Spike': cluster = self.__getlist__('cluster') elif htype == 'HFO': cluster = range(len(self.ch_labels)) if time_edge == None: time_edge = self.time_edge num_clus = int(np.max(cluster)) + 1 label = [] c_l = 0 for clus in [x for x in range(num_clus) if x not in exclude]: label.append(self.ch_labels[clus]) if htype == 'Spike': objs = [x for x in self.event if x.cluster == clus] elif htype == 'HFO': objs = [x for x in self.event if x.channel == clus] for ev in objs: rect = patches.Rectangle((ev.tstamp, c_l), 0.001, .8, lw=0.5) ax.add_patch(rect) if line: plt.hlines(c_l + .5, time_edge[0], time_edge[1], colors='c') c_l += 1 ax.set_ylim(0, c_l) ax.set_xlim(time_edge[0], time_edge[-1]) plt.yticks(np.arange(c_l) + 0.5, label, size=16) adjust_spines(ax, spines)
def phase_coupling(SPK,Data,low_cut,high_cut,cluster = 'all', ch=None,order = None,window = ('kaiser',0.5),nbins = 32,color=None, saveplot = None): ''' Caulculate the phase coupling in certain frequency band Parameters ---------- SPK: SpkObj Data: DataObj low_cut: int, Low cut frequency high_cut: int, High cut frequency ''' plt.figure(figsize=(10,10)) if len(Data.data.shape) == 1: signal = Data elif ch == None: raise Exception('Choose a ch') else: signal = pop_channel(Data,ch) filt = eegfilt(signal,low_cut,high_cut,order = order,window = window) phs = np.angle(sig.hilbert(filt.data)) #ang = np.linspace(0.0,2*np.pi,nbins, endpoint=False) if color ==None: colours = ['#000000','#0000FF','#FF0000','#8fbc8f','yellow'] if cluster == 'all': l = int(np.max(SPK.__getlist__('cluster'))+1) elif cluster == None: loc = [int(x*signal.sample_rate) for x in SPK.__getlist__('tstamp')] l = 1 else: idx = [int(i) for i,x in enumerate(SPK.__getlist__('cluster')) if x== cluster] loc = [int(x*signal.sample_rate) for i,x in enumerate(SPK.__getlist__('tstamp')) if i in idx] l = 1 c = 0 for clus in range(l): if l == 1: k = phs[loc] else: idx = [int(i) for i,x in enumerate(SPK.__getlist__('cluster')) if x== clus] loc = [int(x*signal.sample_rate) for i,x in enumerate(SPK.__getlist__('tstamp')) if i in idx] k = phs[loc] c += 1 plt.subplot(l,2,c,polar=True) plt.hist(k,bins=nbins,facecolor=colours[clus]) plt.yticks([]) c += 1 ax = plt.subplot(l,2,c,polar=False) nbin = np.concatenate((k,k+2*np.pi)) plt.hist(nbin,bins=2*nbins,facecolor=colours[clus]) plt.xticks(np.linspace(-np.pi,3*np.pi,9),[str(int(x)) for x in 180*np.linspace(-np.pi,3*np.pi,9)/np.pi]) plt.xlim([-np.pi,3*np.pi]) adjust_spines(ax, ['left','bottom']) r = np.sum(np.exp(1j*k)) r = np.abs(r)/k.shape[0] print 'Cluster ' + str(clus) + ' r: ' + str(r) if saveplot != None: if type(saveplot) == str: plt.savefig(saveplot, bbox_inches='tight') else: raise Exception('saveplot should be a string')