def plotISIDistributions(sessions,groups=None,sessionTypes=None,samplingRate=30000.0,save=False,fname=None,figsize=(10,6)): """Plots the isi distributions for all the cells in the given sessions""" fig = plt.figure(figsize=figsize) fig.subplots_adjust(left=0.05,right=.95) ISIs = {} ncells = 0 if sessionTypes != None: sessionTypes = dict(zip(sessions,sessionTypes)) for g in groups: for s in sessions: dataFile = h5py.File(os.path.expanduser('~/Documents/research/data/spikesorting/hmm/p=1e-20/%sg%.4d.hdf5' % (s,g)),'r') try: for c in dataFile['unitTimePoints'].keys(): isi = np.log(np.diff(dataFile['unitTimePoints'][c][:]/(samplingRate/1000))) cn = 'g%dc%d' % (g,int(c)) if cn in ISIs: ISIs[cn]['%s' %(s,)] = isi else: ISIs[cn] = {'%s' %(s,): isi} finally: dataFile.close() i = 1 ncells = len(ISIs.keys()) for c in ISIs.keys(): ax = Subplot(fig,1,ncells,i) formatAxis(ax) fig.add_axes(ax) ax.set_title(c) for k,v in ISIs[c].items(): if sessionTypes != None: L = sessionTypes[k] else: L = k n,b = np.histogram(v,bins=20,normed=True) plt.plot(b[:-1],n,label=L) i+=1 ax.set_xlabel('ISI [ms]') xl,xh = int(np.round((b[0]-0.5)*2))/2,int(np.round((b[-1]+0.5)*2))/2 xl = -0.5 dx = np.round(10.0*(xh-xl)/4.0)/10 xt_ = np.arange(xl,xh+1,dx) ax.set_xticks(xt_) ax.set_xticklabels(map(lambda s: r'$10^{%.1f}$' % (s,),xt_)) fig.axes[-1].legend() if save: if fname == None: fname = os.path.expanduser('~/Documents/research/figures/isi_comparison.pdf') fig.savefig(fname,bbox='tight')
def plotSpikes(qdata,save=False,fname='hmmSorting.pdf',tuning=False,figsize=(10,6)): allSpikes = qdata['allSpikes'] unitSpikes = qdata['unitSpikes'] spikeIdx = qdata['spikeIdx'] spikeIdx = qdata['unitTimePoints'] units = qdata['unitTimePoints'] spikeForms = qdata['spikeForms'] channels = qdata['channels'] uniqueIdx = qdata['uniqueIdx'] samplingRate = qdata.get('samplingRate',30000.0) """ mustClose = False if isinstance(dataFile,str): dataFile = h5py.File(dataFile,'r') mustClose = True data = dataFile['data'][:] """ keys = np.array(units.keys()) x = np.arange(32)[None,:] + 42*np.arange(spikeForms.shape[1])[:,None] xt = np.linspace(0,31,spikeForms.shape[-1])[None,:] + 42*np.arange(spikeForms.shape[1])[:,None] xch = 10 + 42*np.arange(len(channels)) for c in units.keys(): ymin,ymax = (5000,-5000) fig = plt.figure(figsize=figsize) fig.subplots_adjust(hspace=0.3) print "Unit: %s " %(str(c),) print "\t Plotting waveforms..." sys.stdout.flush() #allspikes = data[units[c][:,None]+np.arange(-10,22)[None,:],:] #allspikes = allSpikes[spikeIdx[c]] allspikes = qdata['unitSpikes'][c] otherunits = keys[keys!=c] #nonOverlapIdx = np.prod(np.array([~np.lib.arraysetops.in1d(spikeIdx[c],spikeIdx[c1]) for c1 in otherunits]),axis=0).astype(np.bool) #nonOverlapIdx = np.prod(np.array([pdist_threshold(spikeIdx[c],spikeIdx[c1],3) for c1 in otherunits]),axis=0).astype(np.bool) #nonOverlapIdx = uniqueIdx[c] nonOverlapIdx = qdata['nonOverlapIdx'][c] overlapIdx = np.lib.arraysetops.setdiff1d(np.arange(qdata['unitTimePoints'][c].shape[0]),nonOverlapIdx) #allspikes = allSpikes[np.lib.arraysetops.union1d(nonOverlapIdx,overlapIdx)] ax = Subplot(fig,2,3,1) fig.add_axes(ax) formatAxis(ax) #plt.plot(x.T,sp,'b') m = allspikes[:].mean(0) s = allspikes[:].std(0) plt.plot(x.T,m,'k',lw=1.5) #find the minimum point for this template ich = spikeForms[int(c)].min(1).argmin() ix = spikeForms[int(c)][ich,:].argmin() #plt.plot(x.T,spikeForms[int(c)][:,ix-10:ix+22].T,'r') plt.plot(x.T,np.roll(spikeForms[int(c)],10-ix,axis=1)[:,:32].T,'r') for i in xrange(x.shape[0]): plt.fill_between(x[i],m[:,i]-s[:,i],m[:,i]+s[:,i],color='b',alpha=0.5) yl = ax.get_ylim() ymin = min(ymin,yl[0]) ymax = max(ymax,yl[1]) ax.set_title('All spikes (%d)' % (allspikes.shape[0],)) ax = Subplot(fig,2,3,2) fig.add_axes(ax) formatAxis(ax) if len(nonOverlapIdx)>0: m = allspikes[:][nonOverlapIdx,:,:].mean(0) s = allspikes[:][nonOverlapIdx,:,:].std(0) plt.plot(x.T,m,'k',lw=1.5) for i in xrange(x.shape[0]): plt.fill_between(x[i],m[:,i]-s[:,i],m[:,i]+s[:,i],color='b',alpha=0.5) #plt.plot(x.T,spikeForms[int(c)][:,ix-10:ix+22].T,'r') plt.plot(x.T,np.roll(spikeForms[int(c)],10-ix,axis=1)[:,:32].T,'r') yl = ax.get_ylim() ymin = min(ymin,yl[0]) ymax = max(ymax,yl[1]) #for sp in allspikes[nonOverlapIdx,:,:]: # plt.plot(x.T,sp,'r') ax.set_title('Non-overlap spikes (%d)' %(nonOverlapIdx.shape[0],)) ax = Subplot(fig,2,3,3) fig.add_axes(ax) formatAxis(ax) if len(overlapIdx)>0: m = allspikes[:][overlapIdx,:,:].mean(0) s = allspikes[:][overlapIdx,:,:].std(0) plt.plot(x.T,m,'k',lw=1.5) for i in xrange(x.shape[0]): plt.fill_between(x[i],m[:,i]-s[:,i],m[:,i]+s[:,i],color='b',alpha=0.5) #plt.plot(x.T,spikeForms[int(c)][:,ix-10:ix+22].T,'r') plt.plot(x.T,np.roll(spikeForms[int(c)],10-ix,axis=1)[:,:32].T,'r') yl = ax.get_ylim() ymin = min(ymin,yl[0]) ymax = max(ymax,yl[1]) #for sp in allspikes[~nonOverlapIdx,:,:]: # plt.plot(x.T,sp,'g') ax.set_title('Overlap spikes (%d)' % ((overlapIdx).shape[0],)) for a in fig.axes: a.set_ylim((ymin,ymax)) a.set_xticks(xch) a.set_xticklabels(map(str,channels)) a.set_xlabel('Channels') for a in fig.axes[1:]: a.set_yticklabels([]) fig.axes[0].set_ylabel('Amplitude') """ isi distribution """ print "\t ISI distribution..." sys.stdout.flush() timepoints = qdata['unitTimePoints'][c][:]/(samplingRate/1000) if len(timepoints)<2: print "Too few spikes. Aborting..." continue isi = np.log(np.diff(timepoints)) n,b = np.histogram(isi,100) ax = Subplot(fig,2,3,4) fig.add_axes(ax) formatAxis(ax) ax.plot(b[:-1],n,'k') yl = ax.get_ylim() ax.vlines(0.0,0,yl[1],'r',lw=1.5) ax.set_xlabel('ISI [ms]') #get xticklabels xl,xh = int(np.round((b[0]-0.5)*2))/2,int(np.round((b[-1]+0.5)*2))/2 xl = -0.5 dx = np.round(10.0*(xh-xl)/5.0)/10 xt_ = np.arange(xl,xh+1,dx) ax.set_xticks(xt_) ax.set_xticklabels(map(lambda s: r'$10^{%.1f}$' % (s,),xt_)) """ auto-correlogram """ print "\t auto-correllogram..." sys.stdout.flush() if not 'autoCorr' in qdata: if isinstance(qdata,dict): qdata['autoCorr'] = {} else: qdata.create_group('autoCorr') if not c in qdata['autoCorr']: C = pdist_threshold2(timepoints,timepoints,50) qdata['autoCorr'][c] = C if not isinstance(qdata,dict): qdata.flush() else: C = qdata['autoCorr'][c][:] n,b = np.histogram(C[C!=0],np.arange(-50,50)) ax = Subplot(fig,2,3,5) fig.add_axes(ax) formatAxis(ax) ax.plot(b[:-1],n,'k') ax.fill_betweenx([0,n.max()],-1.0,1.0,color='r',alpha=0.3) ax.set_xlabel('Lag [ms]') if tuning: print "\tPlotting tuning..." sys.stdout.flush() #attempt to get tuning for the current session, based on PWD stimCounts,isiCounts,angles,spikedata = gt.getTuning(sptrain=timepoints) #reshape to number of orientations X number of reps, collapsing #across everything else #angles = np.append(angles,[angles[0]]) C = stimCounts['0'].transpose((1,0,2,3)) C = C.reshape(C.shape[0],C.size/C.shape[0]) ax = plt.subplot(2,3,6,polar=True) ax.errorbar(angles*np.pi/180,C.mean(1),C.std(1)) if save: if not os.path.isabs(fname): fn = os.path.expanduser('~/Documents/research/figures/SpikeSorting/hmm/%s' % (fname.replace('.pdf','Unit%s.pdf' %(str(c),)),)) else: fn = fname.replace('.pdf','Unit%s.pdf' %(str(c),)) fig.savefig(fn,bbox='tight') if not save: plt.draw() """