def cluster_site_PCA(site, siteName, tetrode, report=True, **kwargs): from jaratest.nick.spikesorting import pcasort oneTT = cms2.MultipleSessionsToCluster(site.animalName, site.get_session_ephys_filenames(), tetrode, '{}_{}'.format(site.date, siteName)) oneTT.load_all_waveforms() clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) if os.path.isfile(clusterFile): oneTT.set_clusters_from_file() else: (numSpikes, numChans, numSamples) = np.shape(oneTT.samples) allWaves = oneTT.samples.reshape(numSpikes, numChans * numSamples) sorter = pcasort.PCASpikeSorter(allWaves, **kwargs) oneTT.clusters = sorter.clusters oneTT.save_single_session_clu_files() if report: plt.clf() oneTT.save_multisession_report() return oneTT
def plot_time_report(clusterlist, animalName, sessionList, siteName): ''' clusterlist in format [(tetrode, cluster), (tetrode, cluster), etc.] #NOTE: I am here ''' for indPair, (tetrode, cluster) in enumerate(clusterlist): oneTT = cms.MultipleSessionsToCluster(animalName, sessionList, tetrode, siteName) oneTT.load_all_waveforms() clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) oneTT.set_clusters_from_file() normSpikeCount = 0 spikeTimestamps = oneTT.timestamps[oneTT.clusters == cluster] chunkStarts = [] chunkSpikeCounts = [] EPHYS_SAMPLING_RATE = 30000.0 #Get the start and end times of each session from the cont data #Cont channel 31 was recorded for each sesssion contFn = '109_CH31.continuous' sessionLims = np.empty((len(sessionList), 2)) for indSession, session in enumerate(sessionList): contFile = os.path.join(settings.EPHYS_PATH, animalName, session, contFn) dataCont = loadopenephys.DataCont(contFile) dataCont.timestamps = dataCont.timestamps / EPHYS_SAMPLING_RATE sessionLims[indSession, 0] = dataCont.timestamps[0] sessionLims[indSession, 1] = dataCont.timestamps[-1] for indLims, lims in enumerate(sessionLims): #Break the session into 2 min segments and discard the last non-full segment splits = np.arange(lims[0], lims[1], 120) chunks = zip(splits, splits[1:]) for indChunk, chunk in enumerate(chunks): spikesThisChunk = spikeTimestamps[( (spikeTimestamps > chunk[0]) & (spikeTimestamps < chunk[1]))] chunkStarts.append(chunk[0]) chunkSpikeCounts.append(len(spikesThisChunk)) if indLims == 1: normSpikeCount = len(spikesThisChunk) plt.subplot2grid((3, 1), (indPair, 0)) timebase = np.array(chunkStarts) / 60.0 timebase = timebase - timebase[0] spikeCounts = np.array(chunkSpikeCounts) / np.double(normSpikeCount) plt.plot(timebase, spikeCounts, 'ko-', lw=2) plt.hold(1) plt.axvline(x=(sessionLims[1, 1] - sessionLims[0, 0]) / 60.0, color='r', lw=3) plt.axhline(y=1, color='0.5', lw=1) plt.ylabel('tt{}c{}'.format(tetrode, cluster)) plt.ylim([0, 2.5])
def load_cluster_waveforms(cellInfo): oneTT = cms2.MultipleSessionsToCluster(cellInfo['subject'], cellInfo['ephysDirs'], cellInfo['tetrode'], '{}_{}um'.format(cellInfo['date'], cellInfo['depth'])) oneTT.load_all_waveforms() clusterFile = os.path.join(oneTT.clustersDir,'Tetrode%d.clu.1'%oneTT.tetrode) oneTT.set_clusters_from_file() tsThisCluster = oneTT.timestamps[oneTT.clusters==cellInfo['cluster']] wavesThisCluster = oneTT.samples[oneTT.clusters==cellInfo['cluster']] return tsThisCluster, wavesThisCluster
def cluster_many_sessions(subject, sessions, tetrode, idString, saveSingleSessionCluFiles=False): oneTT = cms.MultipleSessionsToCluster(subject, sessions, tetrode, idString) oneTT.load_all_waveforms() clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) if os.path.isfile(clusterFile): oneTT.set_clusters_from_file() else: oneTT.create_multisession_fet_files() oneTT.run_clustering() oneTT.save_multisession_report() if saveSingleSessionCluFiles: oneTT.save_single_session_clu_files() return oneTT
def cluster_site(site, siteName, tetrode, report=True): oneTT = cms2.MultipleSessionsToCluster(site.animalName, site.get_session_ephys_filenames(), tetrode, '{}_{}'.format(site.date, siteName)) oneTT.load_all_waveforms() #Do the clustering if necessary. clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) if os.path.isfile(clusterFile): oneTT.set_clusters_from_file() else: oneTT.create_multisession_fet_files() oneTT.run_clustering() oneTT.set_clusters_from_file() oneTT.save_single_session_clu_files() if report: plt.clf() oneTT.save_multisession_report() return oneTT #This is a little bit lazy, it should really spit out some attributes not the whole object
def nick_lan_main_report(siteObj, show=False, save=True, saveClusterReport=True): for tetrode in siteObj.goodTetrodes: oneTT = cms2.MultipleSessionsToCluster( siteObj.animalName, siteObj.get_session_filenames(), tetrode, '{}at{}um'.format(siteObj.date, siteObj.depth)) oneTT.load_all_waveforms() # Do the clustering if necessary. clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) if os.path.isfile(clusterFile): oneTT.set_clusters_from_file() else: oneTT.create_multisession_fet_files() oneTT.run_clustering() oneTT.set_clusters_from_file() oneTT.save_single_session_clu_files() possibleClusters = np.unique(oneTT.clusters) ee = ee3.EphysExperiment(siteObj.animalName, siteObj.date, experimenter=siteObj.experimenter) # Iterate through the clusters, making a new figure for each cluster. # for indClust, cluster in enumerate([3]): for indClust, cluster in enumerate(possibleClusters): mainRasterInds = siteObj.get_session_inds_one_type( plotType='raster', report='main') mainRasterSessions = [ siteObj.get_session_filenames()[i] for i in mainRasterInds ] mainRasterTypes = [ siteObj.get_session_types()[i] for i in mainRasterInds ] mainTCinds = siteObj.get_session_inds_one_type( plotType='tc_heatmap', report='main') mainTCsessions = [ siteObj.get_session_filenames()[i] for i in mainTCinds ] mainTCbehavIDs = [ siteObj.get_session_behavIDs()[i] for i in mainTCinds ] mainTCtypes = [siteObj.get_session_types()[i] for i in mainTCinds] # The main report for this cluster/tetrode/session plt.figure() for indRaster, rasterSession in enumerate(mainRasterSessions): plt.subplot2grid((6, 6), (indRaster, 0), rowspan=1, colspan=3) ee.plot_session_raster(rasterSession, tetrode, cluster=cluster, replace=1, ms=1) plt.ylabel('{}\n{}'.format(mainRasterTypes[indRaster], rasterSession.split('_')[1]), fontsize=10) ax = plt.gca() extraplots.set_ticks_fontsize(ax, 6) # We can only do one main TC for now. plt.subplot2grid((6, 6), (0, 3), rowspan=3, colspan=3) tcSession = mainTCsessions[0] tcBehavID = mainTCbehavIDs[0] ee.plot_session_tc_heatmap(tcSession, tetrode, tcBehavID, replace=1, cluster=cluster) plt.title("{0}\nBehavFileID = '{1}'".format(tcSession, tcBehavID), fontsize=10) nSpikes = len(oneTT.timestamps) nClusters = len(possibleClusters) #spikesEachCluster = np.empty((nClusters, nSpikes),dtype = bool) # if oneTT.clusters == None: # oneTT.set_clusters_from_file() # for indc, clusterID in enumerate (possibleClusters): #spikesEachCluster[indc, :] = (oneTT.clusters==clusterID) tsThisCluster = oneTT.timestamps[oneTT.clusters == cluster] wavesThisCluster = oneTT.samples[oneTT.clusters == cluster] # -- Plot ISI histogram -- plt.subplot2grid((6, 6), (4, 0), rowspan=1, colspan=3) spikesorting.plot_isi_loghist(tsThisCluster) plt.ylabel('c%d' % cluster, rotation=0, va='center', ha='center') plt.xlabel('') # -- Plot waveforms -- plt.subplot2grid((6, 6), (5, 0), rowspan=1, colspan=3) spikesorting.plot_waveforms(wavesThisCluster) # -- Plot projections -- plt.subplot2grid((6, 6), (4, 3), rowspan=1, colspan=3) spikesorting.plot_projections(wavesThisCluster) # -- Plot events in time -- plt.subplot2grid((6, 6), (5, 3), rowspan=1, colspan=3) spikesorting.plot_events_in_time(tsThisCluster) fig_path = oneTT.clustersDir fig_name = 'TT{0}Cluster{1}.png'.format(tetrode, cluster) full_fig_path = os.path.join(fig_path, fig_name) print full_fig_path # plt.tight_layout() if save: plt.savefig(full_fig_path, format='png') if show: plt.show() if not show: plt.close() if saveClusterReport: plt.figure() oneTT.save_multisession_report() plt.close()
sessionLims = np.empty((len(sessionList), 2)) for indSession, session in enumerate(sessionList): contFile = os.path.join(settings.EPHYS_PATH, exp.subject, session, contFn) dataCont = loadopenephys.DataCont(contFile) dataCont.timestamps = dataCont.timestamps / EPHYS_SAMPLING_RATE sessionLims[indSession, 0] = dataCont.timestamps[0] sessionLims[indSession, 1] = dataCont.timestamps[-1] siteName = '{}_all_sessions_clustered'.format(exp.date) # figure() clf() # for tetrode in [1]: for tetrode in range(1, 9): clf() oneTT = cms.MultipleSessionsToCluster(animalName, sessionList, tetrode, siteName) oneTT.load_all_waveforms() clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) if os.path.isfile(clusterFile): oneTT.set_clusters_from_file() else: oneTT.create_multisession_fet_files() oneTT.run_clustering() oneTT.set_clusters_from_file() # oneTT.save_single_session_clu_files() #Don't do this yet because it will overwrite plt.clf() oneTT.save_multisession_report() for indCluster, cluster in enumerate(np.unique(oneTT.clusters)): normSpikeCount = 0 spikeTimestamps = oneTT.timestamps[oneTT.clusters == cluster]
def generate_main_report(self, siteName): ''' Generate the reports for all of the sessions in this site. This is where we should interface with the multiunit clustering code, since all of the sessions that need to be clustered together have been defined at this point. FIXME: This method should be able to load some kind of report template perhaps, so that the types of reports we can make are not a limited. For instance, what happens when we just have rasters for a site and no tuning curve? Implementing this is a lower priority for now. Incorporated lan's code for plotting the cluster reports directly on the main report ''' #FIXME: import another piece of code to do this? #FIXME: OR, break into two functions: one that will do the multisite clustering, and one that #knows the type of report that we want. The first one can probably be a method of MSTC, the other #should either live in extraplots or should go in someone's directory for tetrode in self.goodTetrodes: oneTT = cms2.MultipleSessionsToCluster( self.animalName, self.get_session_filenames(), tetrode, '{}_{}'.format(self.date, siteName)) oneTT.load_all_waveforms() #Do the clustering if necessary. clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) if os.path.isfile(clusterFile): oneTT.set_clusters_from_file() else: oneTT.create_multisession_fet_files() oneTT.run_clustering() oneTT.set_clusters_from_file() oneTT.save_single_session_clu_files() possibleClusters = np.unique(oneTT.clusters) ee = EphysExperiment(self.animalName, self.date, experimenter=self.experimenter) #Iterate through the clusters, making a new figure for each cluster. #for indClust, cluster in enumerate([3]): for indClust, cluster in enumerate(possibleClusters): mainRasterInds = self.get_session_inds_one_type( plotType='raster', report='main') mainRasterSessions = [ self.get_session_filenames()[i] for i in mainRasterInds ] mainRasterTypes = [ self.get_session_types()[i] for i in mainRasterInds ] mainTCinds = self.get_session_inds_one_type( plotType='tc_heatmap', report='main') mainTCsessions = [ self.get_session_filenames()[i] for i in mainTCinds ] mainTCbehavIDs = [ self.get_session_behavIDs()[i] for i in mainTCinds ] mainTCtypes = [self.get_session_types()[i] for i in mainTCinds] plt.figure() #The main report for this cluster/tetrode/session for indRaster, rasterSession in enumerate(mainRasterSessions): plt.subplot2grid((6, 6), (indRaster, 0), rowspan=1, colspan=3) ee.plot_session_raster(rasterSession, tetrode, cluster=cluster, replace=1, ms=1) plt.ylabel('{}\n{}'.format(mainRasterTypes[indRaster], rasterSession.split('_')[1]), fontsize=10) ax = plt.gca() extraplots.set_ticks_fontsize(ax, 6) #We can only do one main TC for now. if len(mainTCsessions) > 0: plt.subplot2grid((6, 6), (0, 3), rowspan=3, colspan=3) #tcIndex = site.get_session_types().index('tuningCurve') tcSession = mainTCsessions[0] tcBehavID = mainTCbehavIDs[0] ee.plot_session_tc_heatmap(tcSession, tetrode, tcBehavID, replace=1, cluster=cluster) plt.title("{0}\nBehavFileID = '{1}'".format( tcSession, tcBehavID), fontsize=10) nSpikes = len(oneTT.timestamps) nClusters = len(possibleClusters) #spikesEachCluster = np.empty((nClusters, nSpikes),dtype = bool) #if oneTT.clusters == None: #oneTT.set_clusters_from_file() #for indc, clusterID in enumerate (possibleClusters): #spikesEachCluster[indc, :] = (oneTT.clusters==clusterID) tsThisCluster = oneTT.timestamps[oneTT.clusters == cluster] wavesThisCluster = oneTT.samples[oneTT.clusters == cluster] # -- Plot ISI histogram -- plt.subplot2grid((6, 6), (4, 0), rowspan=1, colspan=3) spikesorting.plot_isi_loghist(tsThisCluster) plt.ylabel('c%d' % cluster, rotation=0, va='center', ha='center') plt.xlabel('') # -- Plot waveforms -- plt.subplot2grid((6, 6), (5, 0), rowspan=1, colspan=3) spikesorting.plot_waveforms(wavesThisCluster) # -- Plot projections -- plt.subplot2grid((6, 6), (4, 3), rowspan=1, colspan=3) spikesorting.plot_projections(wavesThisCluster) # -- Plot events in time -- plt.subplot2grid((6, 6), (5, 3), rowspan=1, colspan=3) spikesorting.plot_events_in_time(tsThisCluster) plt.subplots_adjust(wspace=0.7) fig_path = oneTT.clustersDir fig_name = 'TT{0}Cluster{1}.png'.format(tetrode, cluster) full_fig_path = os.path.join(fig_path, fig_name) print full_fig_path #plt.tight_layout() plt.savefig(full_fig_path, format='png') #plt.show() plt.close() plt.figure() oneTT.save_multisession_report() plt.close()
def laser_tc_analysis(site, sitenum): ''' Data analysis function for laser/tuning curve experiments This function will take a RecordingSite object, do multisession clustering on it, and save all of the clusters back to the original session cluster directories. We can then use an EphysExperiment object (version 2) to load each session, select clusters, plot the appropriate plots, etc. This code is being removed from the EphysExperiment object because that object should be general and apply to any kind of recording experiment. This function does the data analysis for one specific kind of experiment. ''' #This is where I should incorporate Lan's sorting function #Construct a multiple session clustering object with the session list. for tetrode in site.goodTetrodes: oneTT = cms2.MultipleSessionsToCluster( site.animalName, site.get_session_filenames(), tetrode, '{}site{}'.format(site.date, sitenum)) oneTT.load_all_waveforms() #Do the clustering if necessary. clusterFile = os.path.join(oneTT.clustersDir, 'Tetrode%d.clu.1' % oneTT.tetrode) if os.path.isfile(clusterFile): oneTT.set_clusters_from_file() else: oneTT.create_multisession_fet_files() oneTT.run_clustering() oneTT.set_clusters_from_file() oneTT.save_single_session_clu_files() possibleClusters = np.unique(oneTT.clusters) #We also need to initialize an EphysExperiment object to get the sessions exp2 = ee2.EphysExperiment(site.animalName, site.date, experimenter=site.experimenter) #Iterate through the clusters, making a new figure for each cluster. for indClust, cluster in enumerate(possibleClusters): plt.figure() #The first noise burst raster plot plt.subplot2grid((4, 6), (0, 0), rowspan=1, colspan=3) nbIndex = site.get_session_types().index('noiseBurst') nbSession = site.get_session_filenames()[nbIndex] exp2.plot_session_raster(nbSession, tetrode, cluster=cluster, replace=1) plt.ylabel('Noise Bursts') plt.title(nbSession, fontsize=10) #The laser pulse raster plot plt.subplot2grid((4, 6), (1, 0), rowspan=1, colspan=3) lpIndex = site.get_session_types().index('laserPulse') lpSession = site.get_session_filenames()[lpIndex] exp2.plot_session_raster(lpSession, tetrode, cluster=cluster, replace=1) plt.ylabel('Laser Pulses') plt.title(lpSession, fontsize=10) #The laser train raster plot plt.subplot2grid((4, 6), (2, 0), rowspan=1, colspan=3) ltIndex = site.get_session_types().index('laserTrain') ltSession = site.get_session_filenames()[ltIndex] exp2.plot_session_raster(ltSession, tetrode, cluster=cluster, replace=1) plt.ylabel('Laser Trains') plt.title(ltSession, fontsize=10) #The tuning curve plt.subplot2grid((4, 6), (0, 3), rowspan=3, colspan=3) tcIndex = site.get_session_types().index('tuningCurve') tcSession = site.get_session_filenames()[tcIndex] tcBehavID = site.get_session_behavIDs()[tcIndex] exp2.plot_session_tc_heatmap(tcSession, tetrode, tcBehavID, replace=1, cluster=cluster) plt.title('Tetrode {0} Cluster {1}'.format(tetrode, cluster)) #The best freq presentation plt.subplot2grid((4, 6), (3, 0), rowspan=1, colspan=3) bfIndex = site.get_session_types().index('bestFreq') bfSession = site.get_session_filenames()[bfIndex] exp2.plot_session_raster(bfSession, tetrode, cluster=cluster, replace=1) plt.ylabel('Best Frequency') plt.title(bfSession, fontsize=10) #FIXME: Omitting the laser pulses at different intensities for now #Save the figure in the multisession clustering folder so that it is easy to find fig_path = oneTT.clustersDir fig_name = 'TT{0}Cluster{1}.png'.format(tetrode, cluster) full_fig_path = os.path.join(fig_path, fig_name) print full_fig_path plt.tight_layout() plt.savefig(full_fig_path, format='png') #plt.show() plt.close() plt.figure() oneTT.save_multisession_report() plt.close()
for indclus, clus in enumerate(ttClusters): clusDict = { 'subject': oneTT.animalName, 'sessionList': site1.get_session_ephys_filenames(), 'sessionNames': site1.get_session_types(), 'behavFiles': site1.get_session_behav_filenames(), 'tetrode': oneTT.tetrode, 'cluster': clus } db = db.append(clusDict, ignore_index=True) #Saving to csv is so easy db.to_csv('/home/nick/src/jaratest/nick/database/database.csv') #How to do a calculation and add columns to the database from jaratest.nick.ephysExperiments import clusterManySessions_v2 as cms isiless2 = [] for indcell, cell in db.iterrows(): oneTT = cms.MultipleSessionsToCluster( cell['subject'], cell['sessionList'], int(cell['tetrode']), '{}_{}'.format('2016-07-13', 'site1')) oneTT.load_all_waveforms() oneTT.set_clusters_from_file() tsThisCluster = oneTT.timestamps[oneTT.clusters == cell['cluster']] isiThisCluster = np.diff(tsThisCluster) violations = isiThisCluster < 0.002 vioPercent = sum(violations) / double(len(violations)) isiless2.append(100 * vioPercent)