예제 #1
0
def get_session_bdata(cell, sessiontype):
    sessionInds = get_session_inds(cell, sessiontype)
    sessionInd = sessionInds[0] #FIXME: Just takes the first one for now
    behavFile = cell['behavior'][sessionInd]
    behavDataFilePath=os.path.join(settings.BEHAVIOR_PATH, cell['subject'], behavFile)
    bdata = loadbehavior.BehaviorData(behavDataFilePath,readmode='full')
    return bdata
    def __init__(self,
                 ephys_root,
                 numTetrodes,
                 behavior_directory=None,
                 behav_filename=None):
        '''
        Accepts the root directory containing all of the ephys files
        relevant to a particular experiment, and the directory
        containing the behavior files if these are relevant to the
        experiment.

        Arguments

        ephys_root (string) - The parent directory of all individual
        recording session directories

        numTetrodes (int) - Number of tetrodes

        behavior_directory (string) - Directory containing behavior files
        '''

        self.ephys_root = ephys_root
        self.numTetrodes = numTetrodes

        if behavior_directory != None:
            self.behavior_directory = behavior_directory
            self.behav_filename = behav_filename
            behavDataFileName = os.path.join(behavior_directory,
                                             behav_filename)
            self.bdata = loadbehavior.BehaviorData(behavDataFileName,
                                                   readmode='full')
예제 #3
0
    def get_session_behav_data(self, session, behavFileIdentifier):
        behaviorDir=os.path.join(self.localBehavPath, self.animalName)
        fullBehavFilename = ''.join([self.behavFileBaseName, behavFileIdentifier, '.h5'])
        behavDataFileName=os.path.join(behaviorDir, fullBehavFilename)

        #Extract the frequency presented each trial from the behavior data
        bdata = loadbehavior.BehaviorData(behavDataFileName,readmode='full')

        return bdata
def load_behavior_basic(animal, behavSession):
    '''Load behavior using the basic BehaviorData class of loadbehavior module.
    :param arg1: String containing animal name.
    :param arg2: A string of the name of the behavior session, this is the full filename in '{animal}_{paradigm}_{date}{behavsuffix}.h5' format. 
    :return: bData object (as defined in loadbehavior).
    '''
    behavFullPath = os.path.join(BEHAVIOR_PATH, animal, behavSession) 
    bData = loadbehavior.BehaviorData(behavFullPath)
    return bData
예제 #5
0
    def get_session_behavior(self, behavFileName):

        if self.mode=='online':
            behaviorDir=os.path.join(self.onlineBehavPath, self.animalName)
            fullBehavFilename = ''.join([self.behavFileBaseName, behavFileName, '.h5'])
            behavDataFilePath=os.path.join(behaviorDir, fullBehavFilename)

        elif self.mode=='offline': #The path should already be relative to the mouse
            print "Fixme: Stop dividing the behavior by experimenter and change this method"
            behavDataFilePath = os.path.join(settings.BEHAVIOR_PATH, self.experimenter, behavFileName)

        bdata = loadbehavior.BehaviorData(behavDataFilePath,readmode='full')
        return bdata
예제 #6
0
def load_remote_tuning_data(oneCell,
                            behavDir=BEHAVDIR_MOUNTED,
                            ephysDir=EPHYSDIR_MOUNTED):
    '''
    Given a CellInfo object and remote behavior and ephys directories, this function loads the associated tuning ephys and tuning behav data from the mounted jarastore drive. Returns eventOnsetTimes, spikeTimestamps, and bData objects.
    '''

    ### Get behavior data associated with tuning curve ###
    behavFileName = '{0}_{1}_{2}.h5'.format(oneCell.animalName, 'tuning_curve',
                                            oneCell.tuningBehavior)
    behavFile = os.path.join(behavDir, oneCell.animalName, behavFileName)
    bData = loadbehavior.BehaviorData(behavFile, readmode='full')

    ### Get events data ###
    fullEventFilename = os.path.join(ephysDir, oneCell.animalName,
                                     oneCell.tuningSession,
                                     'all_channels.events')
    eventData = loadopenephys.Events(fullEventFilename)

    ### Get event onset times ###
    eventTimestamps = np.array(
        eventData.timestamps
    ) / EPHYS_SAMPLING_RATE  #hard-coded ephys sampling rate!!
    evID = np.array(eventData.eventID)
    eventOnsetTimes = eventTimestamps[(evID == 1)]

    ### GEt spike data for just this cluster ###
    spikeFilename = os.path.join(ephysDir, oneCell.animalName,
                                 oneCell.tuningSession,
                                 'Tetrode{}.spikes'.format(oneCell.tetrode))
    spikeData = loadopenephys.DataSpikes(spikeFilename)
    spikeData.timestamps = spikeData.timestamps / EPHYS_SAMPLING_RATE
    clustersDir = os.path.join(ephysDir, oneCell.animalName,
                               oneCell.tuningSession) + '_kk'
    clusterFilename = os.path.join(clustersDir,
                                   'Tetrode{}.clu.1'.format(oneCell.tetrode))
    clusters = np.fromfile(clusterFilename, dtype='int32', sep=' ')[1:]
    spikeData.timestamps = spikeData.timestamps[clusters == oneCell.cluster]
    spikeData.samples = spikeData.samples[clusters == oneCell.cluster, :, :]
    spikeData.samples = spikeData.samples.astype(
        float) - 2**15  # FIXME: this is specific to OpenEphys
    # FIXME: This assumes the gain is the same for all channels and records
    spikeData.samples = (1000.0 / spikeData.gain[0, 0]) * spikeData.samples
    #spikeData = ephyscore.CellData(oneCell)
    #spikeTimestamps=spikeData.spikes.timestamps

    return (eventOnsetTimes, spikeData.timestamps, bData)
예제 #7
0
    def loadData(self):
        """
        Walks through all files in settings.BEHAVIOR_PATH and lazily loads in the
        relevent files to the "behavData" list where each index is a tuple formatted as, 
        (<full path of file>, <loaded behavior data>).

        Args:
            None
        Returns:
            None
        """

        for info in os.walk(settings.BEHAVIOR_PATH):
            path = info[0]

            # Get the files, if there are any
            for element in info[2]:
                split           = element.split(".")

                # If there's only one '.' in the filename, then we know it's not a .old.h5 file, or a file without an extension.
                if(len(split) == 2):
                    name, extension = element.split(".")

                    if(self.log):
                        logging.debug("Name: " + name + " Extension: " + extension)

                    for animal in self.subjects:

                        # Get the date from the name and format it in ISO format to compare to the current date.
                        experimentDate  = name.split("_")[-1]
                        isoDate         = experimentDate[:4] + "-" + experimentDate[4:6] + "-" + experimentDate[6:8]

                        if(self.log):
                            logging.debug("Comparing date: " + str(isoDate) + " to " + str(self.date) + " (today)")

                        # We only want data from today from an animal that we care about
                        if(self.date == extrafuncs.parse_isodate(isoDate) and extension == "h5" and animal in name):
                            try:
                                full_path = os.path.join(path, element)
                                self.behavData.append((full_path, loadbehavior.BehaviorData(full_path, readmode='full')))
                                if(self.log):
                                    logging.info("Successfully loaded data from: " + full_path)
                            except:
                                self.sendToAllSubscribers("Error when attempting to load " + full_path + ".", "Alert: Alarm error")
                                if(self.log):
                                    logging.error("Could not load " + full_path + ".")
예제 #8
0
    def get_session_behav_data(self, session, behavFileIdentifier=None):
        if not isinstance(
                session,
                str):  #FIXME: This is a hack but I think it will work for nto
            behavFileIdentifier = self.get_session_name(session,
                                                        dataType='behavior')
        else:
            if not behavFileIdentifier:
                print "Either supply a Session object as the session, or supply a valid behav file identifier"

        behaviorDir = os.path.join(self.localBehavPath, self.animalName)
        fullBehavFilename = ''.join(
            [self.behavFileBaseName, behavFileIdentifier, '.h5'])
        behavDataFileName = os.path.join(behaviorDir, fullBehavFilename)

        #Extract the frequency presented each trial from the behavior data
        bdata = loadbehavior.BehaviorData(behavDataFileName, readmode='full')

        return bdata
예제 #9
0
def get_session_bdata(cell, sessiontype):
    '''
    Load the behavior data from a cell for a single session.
    Args:
        cell (pandas.Series): One row from a pandas cell database created using generate_cell_database or by
                              manually constructing a pandas.Series object that contains the required fields.
                              This function requires the 'sessiontype' field, which must contain a list of strings.
        sessiontype (str): The type of session

    Returns:
        bdata (jaratoolbox.loadbehavior.BehaviorData): The behavior data for the session
    '''
    sessionInds = get_session_inds(cell, sessiontype)
    sessionInd = sessionInds[0]  #FIXME: Just takes the first one for now
    behavFile = cell['behavior'][sessionInd]
    behavDataFilePath = os.path.join(settings.BEHAVIOR_PATH, cell['subject'],
                                     behavFile)
    bdata = loadbehavior.BehaviorData(behavDataFilePath, readmode='full')
    return bdata
예제 #10
0
def mod_switching_summary(animalName, sessions):

    fn = '/home/nick/data/behavior/nick/{0}/{1}_2afc_{2}.h5'.format(animalName, animalName, date)
    #fn = '/var/tmp/data/santiago/nick/nick_2afc_20160330a.h5' #Not psycurve

    bdata = loadbehavior.BehaviorData(fn)

    numSoundType = len(np.unique(bdata['soundType']))
    soundTypes = [bdata.labels['soundType'][np.unique(bdata['soundType'])[x]] for x in range(numSoundType)]

    plt.clf()
    if any(bdata['psycurveMode']):
        for indType, soundType in enumerate(soundTypes):
            plt.subplot2grid((numSoundType, 2),  (indType, 0))
            plot_frequency_psycurve_soundtype(bdata, soundType)
            plt.title('{}, {}'.format(animalName, soundType))

    else:
        for indType, soundType in enumerate(soundTypes):
            plt.subplot2grid((numSoundType, 2),  (indType, 0))
            plot_summary_sound_type(bdata, soundType)
            plt.title('{}, {}'.format(animalName, soundType))


    for indType, soundType in enumerate(soundTypes):
        plt.subplot2grid((numSoundType, 2),  (indType, 1))
        plot_dynamics_sound_type(bdata, soundType)
        plt.title('{}, {}'.format(animalName, soundType))

    behavFolder = '/home/nick/data/behavior_reports'
    figname = '{}_{}.png'.format(animalName, date)
    filename = os.path.join(behavFolder, figname)

    plt.suptitle(date)
    plt.tight_layout()
    plt.savefig(filename)
def raster_tuning(ax):

    fullbehaviorDir = behaviorDir + subject + '/'
    behavName = subject + '_tuning_curve_' + tuningBehavior + '.h5'
    tuningBehavFileName = os.path.join(fullbehaviorDir, behavName)

    tuning_bdata = loadbehavior.BehaviorData(tuningBehavFileName,
                                             readmode='full')
    freqEachTrial = tuning_bdata['currentFreq']
    possibleFreq = np.unique(freqEachTrial)
    numberOfTrials = len(freqEachTrial)

    # -- The old way of sorting (useful for plotting sorted raster) --
    sortedTrials = []
    numTrialsEachFreq = [
    ]  #Used to plot lines after each group of sorted trials
    for indf, oneFreq in enumerate(
            possibleFreq
    ):  #indf is index of this freq and oneFreq is the frequency
        indsThisFreq = np.flatnonzero(
            freqEachTrial == oneFreq)  #this gives indices of this frequency
        sortedTrials = np.concatenate(
            (sortedTrials,
             indsThisFreq))  #adds all indices to a list called sortedTrials
        numTrialsEachFreq.append(
            len(indsThisFreq))  #finds number of trials each frequency has
    sortingInds = argsort(
        sortedTrials)  #gives array of indices that would sort the sortedTrials

    # -- Load event data and convert event timestamps to ms --
    tuning_ephysDir = os.path.join(settings.EPHYS_PATH, subject, tuningEphys)
    tuning_eventFilename = os.path.join(tuning_ephysDir, 'all_channels.events')
    tuning_ev = loadopenephys.Events(
        tuning_eventFilename)  #load ephys data (like bdata structure)
    tuning_eventTimes = np.array(
        tuning_ev.timestamps
    ) / SAMPLING_RATE  #get array of timestamps for each event and convert to seconds by dividing by sampling rate (Hz). matches with eventID and
    tuning_evID = np.array(
        tuning_ev.eventID
    )  #loads the onset times of events (matches up with eventID to say if event 1 went on (1) or off (0)
    tuning_eventOnsetTimes = tuning_eventTimes[
        tuning_evID ==
        1]  #array that is a time stamp for when the chosen event happens.
    #ev.eventChannel woul load array of events like trial start and sound start and finish times (sound event is 0 and trial start is 1 for example). There is only one event though and its sound start
    while (numberOfTrials < len(tuning_eventOnsetTimes)):
        tuning_eventOnsetTimes = tuning_eventOnsetTimes[:-1]

    #######################################################################################################
    ###################THIS IS SUCH A HACK TO GET SPKDATA FROM EPHYSCORE###################################
    #######################################################################################################

    thisCell = celldatabase.CellInfo(
        animalName=subject,  ############################################
        ephysSession=tuningEphys,
        tuningSession='DO NOT NEED THIS',
        tetrode=tetrode,
        cluster=cluster,
        quality=1,
        depth=0,
        tuningBehavior='DO NOT NEED THIS',
        behavSession=tuningBehavior)

    tuning_spkData = ephyscore.CellData(thisCell)
    tuning_spkTimeStamps = tuning_spkData.spikes.timestamps

    (tuning_spikeTimesFromEventOnset, tuning_trialIndexForEachSpike,
     tuning_indexLimitsEachTrial) = spikesanalysis.eventlocked_spiketimes(
         tuning_spkTimeStamps, tuning_eventOnsetTimes, tuning_timeRange)

    #print 'numTrials ',max(tuning_trialIndexForEachSpike)#####################################
    '''
        Create a vector with the spike timestamps w.r.t. events onset.

        (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = 
            eventlocked_spiketimes(timeStamps,eventOnsetTimes,timeRange)

        timeStamps: (np.array) the time of each spike.
        eventOnsetTimes: (np.array) the time of each instance of the event to lock to.
        timeRange: (list or np.array) two-element array specifying time-range to extract around event.

        spikeTimesFromEventOnset: 1D array with time of spikes locked to event.
    o    trialIndexForEachSpike: 1D array with the trial corresponding to each spike.
           The first spike index is 0.
        indexLimitsEachTrial: [2,nTrials] range of spikes for each trial. Note that
           the range is from firstSpike to lastSpike+1 (like in python slices)
        spikeIndices
    '''

    tuning_sortedIndexForEachSpike = sortingInds[
        tuning_trialIndexForEachSpike]  #Takes values of trialIndexForEachSpike and finds value of sortingInds at that index and makes array. This array gives an array with the sorted index of each trial for each spike

    # -- Calculate tuning --

    plot(tuning_spikeTimesFromEventOnset,
         tuning_sortedIndexForEachSpike,
         '.',
         ms=3)
    #axvline(x=0, ymin=0, ymax=1, color='r')

    #The cumulative sum of the list of specific frequency presentations,
    #used below for plotting the lines across the figure.
    numTrials = cumsum(numTrialsEachFreq)

    #Plot the lines across the figure in between each group of sorted trials
    for indf, num in enumerate(numTrials):
        ax.axhline(y=num, xmin=0, xmax=1, color='0.90', zorder=0)

    tickPositions = numTrials - mean(numTrialsEachFreq) / 2
    tickLabels = [
        "%0.2f" % (possibleFreq[indf] / 1000)
        for indf in range(len(possibleFreq))
    ]
    ax.set_yticks(tickPositions)
    ax.set_yticklabels(tickLabels)
    ax.set_ylim([-1, numberOfTrials])
    ylabel('Frequency Presented (kHz), {} total trials'.format(numTrials[-1]))
    #title(ephysSession+' T{}c{}'.format(tetrodeID,clusterID))
    xlabel('Time (sec)')
예제 #12
0
    def get_session_behavior(self, behavFileName):

        behavDataFilePath = os.path.join(self.behavPath, behavFileName)
        bdata = loadbehavior.BehaviorData(behavDataFilePath, readmode='full')
        return bdata
예제 #13
0
#behavSession = '20150322c'; ephysSession = '2015-03-22_17-40-55'
behavSession = '20150323a'; ephysSession = '2015-03-23_21-51-50'
#behavSession = '20150323b'; ephysSession = '2015-03-23_22-46-13'
tetrodes = [3,6]#[3,6]

nTetrodes = len(tetrodes)

#ephysSession=sort(os.listdir(fullephysRoot))[-1]
fullephysDir = os.path.join(settings.EPHYS_PATH,subject, ephysSession)
event_filename = os.path.join(fullephysDir, 'all_channels.events')

experimenter = settings.DEFAULT_EXPERIMENTER
paradigm = 'laser_tuning_curve'
behavDataFileName = loadbehavior.path_to_behavior_data(subject,experimenter,paradigm,behavSession)

bdata = loadbehavior.BehaviorData(behavDataFileName,readmode='full')
freqEachTrial = bdata['currentFreq']
intensityEachTrial = bdata['currentIntensity']
nTrials = len(freqEachTrial)

# -- Load event data and convert event timestamps to ms --
ev = loadopenephys.Events(event_filename) # load ephys data (like bdata structure)
eventTimes = np.array(ev.timestamps)/SAMPLING_RATE # convert to seconds by dividing by sampling rate (Hz)
evID = np.array(ev.eventID)  # loads the onset times of events (matches up with eventID to say if event 1 went on (1) or off (0)
eventOnsetTimes = eventTimes[evID==1] # array that is a time stamp for when the chosen event happens.

if nTrials != len(eventOnsetTimes):
    print 'Number of behavior trials and ephys trials do not match. The longest will be cut.'
    minNtrials = min(nTrials,len(eventOnsetTimes))
    nTrials = minNtrials
    freqEachTrial = freqEachTrial[:nTrials]
예제 #14
0
    def plot_tc_psth(self, session, tetrode, behavFileIdentifier, cluster=None):

        #FIXME: This method needs a lot of work
        
        SAMPLING_RATE = 30000.0
        PLOTTING_WINDOW = 0.1 #Window to plot, in seconds
        
        ephysSession = self.get_session_name(session)

        ephysDir=os.path.join(self.localEphysDir, ephysSession)
        event_filename=os.path.join(ephysDir, 'all_channels.events')

        behaviorDir=os.path.join(self.localBehavPath, self.animalName)
        fullBehavFilename = ''.join([self.behavFileBaseName, behavFileIdentifier, '.h5'])
        behavDataFileName=os.path.join(behaviorDir, fullBehavFilename)

        #Extract the frequency presented each trial from the behavior data
        bdata = loadbehavior.BehaviorData(behavDataFileName,readmode='full')
        freqEachTrial = bdata['currentFreq']
        intensityEachTrial = bdata['currentIntensity']

        possibleFreq = np.unique(freqEachTrial) 
        possibleIntensity = np.unique(intensityEachTrial)

        #Get the event timestamps from openEphys
        ev=loadopenephys.Events(event_filename)
        evID=np.array(ev.eventID)
        eventOnsetTimes=ev.timestamps[evID==1] #The timestamps of all of the stimulus onsets

        tetrode = 6 #The tetrode to plot
        spikeFilename = os.path.join(ephysDir, 'Tetrode{}.spikes'.format(tetrode))
        spikeData = loadopenephys.DataSpikes(spikeFilename)
        
        if cluster:
            clustersDir = os.path.join(settings.EPHYS_PATH,'%s/%s_kk/'%(self.animalName,ephysSession))
            clustersFile = os.path.join(clustersDir,'Tetrode%d.clu.1'%tetrode)
            spikeData.set_clusters(clustersFile)

            spikeTimestamps = spikeData.timestamps[spikeData.clusters==cluster]
        else:
            spikeTimestamps = spikeData.timestamps
            


        allSettingsSpikes = defaultdict(dict) #2D dictionary to hold the spiketimes arrays organized by frequency and intensity

        for indFreq, currentFreq in enumerate(possibleFreq):
            for indIntensity, currentIntensity in enumerate(possibleIntensity):

                #Determine which trials this setting was presented on. 
                trialsThisSetting = np.flatnonzero((freqEachTrial == currentFreq) & (intensityEachTrial == currentIntensity))

                #Get the onset timestamp for each of the trials of this setting. 
                timestampsThisSetting = eventOnsetTimes[trialsThisSetting]

                spikesAfterThisSetting = np.array([])
                #Loop through all of the trials for this setting, extracting the trace after each presentation
                for indts, eventTimestamp in enumerate(timestampsThisSetting):
                    spikes = spikeTimestamps[(spikeTimestamps >= eventTimestamp) & (spikeTimestamps <= eventTimestamp + SAMPLING_RATE * PLOTTING_WINDOW)]
                    spikes = spikes - eventTimestamp
                    spikes = spikes / 30 #Spikes in ms after the stimulus

                    spikesAfterThisSetting = np.concatenate((spikesAfterThisSetting, spikes))
                allSettingsSpikes[indFreq][indIntensity] = spikesAfterThisSetting #Put the spikes into the 2d dict
        figure()

        maxBinCount = []
        for indI, intensity in enumerate(possibleIntensity):
            for indF, frequency in enumerate(possibleFreq):
                h, bin_edges = histogram(allSettingsSpikes[indF][indI]) #Dict is ordered by freq and then by Int.
                maxBinCount.append(max(h))

        maxNumSpikesperBin = max(maxBinCount)

        for intensity in range(len(possibleIntensity)):
            #Subplot2grid plots from top to bottom, but we need to plot from bottom to top
            #on the intensity scale. So we make an array of reversed intensity indices.
            intensPlottingInds = range(len(possibleIntensity))[::-1]
            for frequency in range(len(possibleFreq)):
                if (intensity == len(possibleIntensity) - 1) & (frequency == len(possibleFreq) -1):
                    ax2 = subplot2grid((len(possibleIntensity), len(possibleFreq)), (intensPlottingInds[intensity], frequency))
                    if len(allSettingsSpikes[frequency][intensity]) is not 0:
                        ax2.hist(allSettingsSpikes[frequency][intensity])
                    else:
                        pass

                    ax2.set_ylim([0, maxNumSpikesperBin])
                    ax2.get_xaxis().set_ticks([])
                else:
                    ax = subplot2grid((len(possibleIntensity), len(possibleFreq)), (intensPlottingInds[intensity], frequency))
                    if len(allSettingsSpikes[frequency][intensity]) is not 0:
                        ax.hist(allSettingsSpikes[frequency][intensity])
                    else:
                        pass
                    ax.set_ylim([0, maxNumSpikesperBin])
                    ax.set_axis_off()

        def getXlabelpoints(n):
            rawArray = array(range(1, n+1))/float(n+1) #The positions in a perfect (0,1) world
            diffFromCenter = rawArray - 0.6
            partialDiffFromCenter = diffFromCenter * 0.175 #Percent change has to be determined empirically
            finalArray = rawArray - partialDiffFromCenter
            return finalArray

        #Not sure yet if similar modification to the locations will be necessary. 
        def getYlabelpoints(n):
            rawArray = array(range(1, n+1))/float(n+1) #The positions in a perfect (0,1) world
            return rawArray

        freqLabelPositions = getXlabelpoints(len(possibleFreq))
        for indp, position in enumerate(freqLabelPositions):
            figtext(position, 0.065, "%.1f"% (possibleFreq[indp]/1000), ha = 'center')

        intensLabelPositions = getYlabelpoints(len(possibleIntensity))
        for indp, position in enumerate(intensLabelPositions):
            figtext(0.075, position, "%d"% possibleIntensity[indp])

        figtext(0.525, 0.025, "Frequency (kHz)", ha = 'center')
        figtext(0.025, 0.5, "Intensity (dB SPL)", va = 'center', rotation = 'vertical')
        show()
예제 #15
0
stepArrays = []
trialNumArrays = []
performanceArrays = []
for subject in subjects:
    dataDir = os.path.join(settings.BEHAVIOR_PATH, subject)
    sessions = sorted(os.listdir(dataDir))
    trainingSteps = np.empty(len(sessions))
    trialStartEachSession = np.empty(len(sessions))
    percentCorrect = np.empty(len(sessions))
    trials = 0
    highestStep = 0
    for indSession, sessionFn in enumerate(sessions):
        sessionFullPath = os.path.join(dataDir, sessionFn)
        try:
            bdata = loadbehavior.BehaviorData(sessionFullPath)
        except KeyError:
            print "Key Error with session: {}".format(sessionFullPath)

        try:
            step, percentCorrectThisSession = behavior_step(bdata)
        except NoSoundTypeMode as e:  #Using the animal for other paradigms throws key errors
            if highestStep == 8:  #If we are at the end of the animal's career
                step, percentCorrectThisSession = -1, 0  #Animal was being used for different things
            else:
                print e
                raise KeyError('Problem before end of animal career')

        #Keep track of the highest step the animal has acheived.
        if step > highestStep:
            highestStep = step
예제 #16
0
def main(oneCell):
    oneCell = allcells.cellDB[cellID]
    if (behavSession != oneCell.behavSession):


        subject = oneCell.animalName
        behavSession = oneCell.behavSession
        ephysSession = oneCell.ephysSession
        ephysRoot = os.path.join(ephysRootDir,subject)

        # -- Load Behavior Data --
        behaviorFilename = loadbehavior.path_to_behavior_data(subject,experimenter,paradigm,behavSession)
        bdata = loadbehavior.BehaviorData(behaviorFilename)
        numberOfTrials = len(bdata['choice'])

        # -- Load event data and convert event timestamps to ms --
        ephysDir = os.path.join(ephysRoot, ephysSession)
        eventFilename=os.path.join(ephysDir, 'all_channels.events')
        events = loadopenephys.Events(eventFilename) # Load events data
        eventTimes=np.array(events.timestamps)/SAMPLING_RATE #get array of timestamps for each event and convert to seconds by dividing by sampling rate (Hz). matches with eventID and 

        soundOnsetEvents = (events.eventID==1) & (events.eventChannel==soundTriggerChannel)


        eventOnsetTimes = eventTimes[soundOnsetEvents]

        rightward = bdata['choice']==bdata.labels['choice']['right']
        leftward = bdata['choice']==bdata.labels['choice']['left']
        invalid = bdata['outcome']==bdata.labels['outcome']['invalid']
        correct = bdata['outcome']==bdata.labels['outcome']['correct']
        correctRightward = rightward & correct
        correctLeftward = leftward & correct

        possibleFreq = np.unique(bdata['targetFrequency'])
        Freq = possibleFreq[Frequency]
        oneFreq = bdata['targetFrequency'] == possibleFreq[Frequency]

        trialsToUseRight = correctRightward & oneFreq
        trialsToUseLeft = correctLeftward & oneFreq

        trialsEachCond = np.c_[trialsToUseLeft,trialsToUseRight]; colorEachCond = ['g','r']




    # -- Load Spike Data From Certain Cluster --
    spkData = ephyscore.CellData(oneCell)
    spkTimeStamps = spkData.spikes.timestamps

    (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
        spikesanalysis.eventlocked_spiketimes(spkTimeStamps,eventOnsetTimes,timeRange)



    plt.clf()
    ax1 =  plt.subplot2grid((3,1), (0, 0), rowspan=2)
    extraplots.raster_plot(spikeTimesFromEventOnset,indexLimitsEachTrial,timeRange,trialsEachCond=trialsEachCond,colorEachCond=colorEachCond,fillWidth=None,labels=None)

    plt.ylabel('Trials')

    timeVec = np.arange(timeRange[0],timeRange[-1],binWidth)
    spikeCountMat = spikesanalysis.spiketimes_to_spikecounts(spikeTimesFromEventOnset,indexLimitsEachTrial,timeVec)

    smoothWinSize = 3
    ax2 = plt.subplot2grid((3,1), (2, 0), sharex=ax1)

    extraplots.plot_psth(spikeCountMat/binWidth,smoothWinSize,timeVec,trialsEachCond=trialsEachCond,
                         colorEachCond=colorEachCond,linestyle=None,linewidth=3,downsamplefactor=1)

    plt.xlabel('Time from sound onset (s)')
    plt.ylabel('Firing rate (spk/sec)')

    nameFreq = str(Freq)
    tetrodeClusterName = 'T'+str(oneCell.tetrode)+'c'+str(oneCell.cluster)
    plt.gcf().set_size_inches((8.5,11))
    figformat = 'png' #'png' #'pdf' #'svg'
    filename = 'rast_%s_%s_%s_%s.%s'%(subject,behavSession,nameFreq,tetrodeClusterName,figformat)
    fulloutputDir = outputDir+subject+'/'+ nameFreq +'/'
    fullFileName = os.path.join(fulloutputDir,filename)

    directory = os.path.dirname(fulloutputDir)
    if not os.path.exists(directory):
        os.makedirs(directory)
    print 'saving figure to %s'%fullFileName
    plt.gcf().savefig(fullFileName,format=figformat)
clf()

# 2 sound types, muscimol/saline
for indRow in range(len(salineSessions)):

    #Plot chords saline
    subplot(plotRows, plotCols, indRow * 4 + 1)

    soundType = 'chords'

    date = salineSessions[indRow]
    fn = '/home/nick/data/behavior/nick/{0}/{1}_2afc_{2}.h5'.format(
        animal, animal, date)
    #fn = '/var/tmp/data/santiago/nick/nick_2afc_20160330a.h5' #Not psycurve
    bdata = loadbehavior.BehaviorData(fn)
    behavioranalysis.plot_frequency_psycurve_soundtype(bdata, soundType)
    title('{} {}'.format(date, soundType))
    xlabel('')

    subplot(plotRows, plotCols, indRow * 4 + 2)

    soundType = 'amp_mod'

    date = salineSessions[indRow]
    fn = '/home/nick/data/behavior/nick/{0}/{1}_2afc_{2}.h5'.format(
        animal, animal, date)
    #fn = '/var/tmp/data/santiago/nick/nick_2afc_20160330a.h5' #Not psycurve
    bdata = loadbehavior.BehaviorData(fn)
    behavioranalysis.plot_frequency_psycurve_soundtype(bdata, soundType)
    title('{} {}'.format(date, soundType))
for cellParams in cellParamsList:
    mouseName = cellParams['firstParam']

    allcellsFileName = 'allcells_' + mouseName + '_quality'  #This is specific to Billy's final allcells files after adding cluster quality info
    sys.path.append(settings.ALLCELLS_PATH)
    allcells = importlib.import_module(allcellsFileName)

    ### Using cellDB methode to find the index of this cell in the cellDB ###
    cellIndex = allcells.cellDB.findcell(**cellParams)
    oneCell = allcells.cellDB[cellIndex]

    ## Get behavior data associated with 2afc session ###
    behavFileName = '{0}_{1}_{2}.h5'.format(oneCell.animalName, paradigm,
                                            oneCell.behavSession)
    behavFile = os.path.join(BEHAVIOR_PATH, oneCell.animalName, behavFileName)
    bdata = loadbehavior.BehaviorData(behavFile, readmode='full')

    ### Get events data ###
    fullEventFilename = os.path.join(EPHYS_PATH, oneCell.animalName,
                                     oneCell.ephysSession,
                                     'all_channels.events')
    eventData = loadopenephys.Events(fullEventFilename)
    ##### Get event onset times #####
    eventData.timestamps = np.array(
        eventData.timestamps
    ) / EPHYS_SAMPLING_RATE  #hard-coded ephys sampling rate!!

    ### GEt spike data of just this cluster ###
    spikeFilename = os.path.join(EPHYS_PATH, oneCell.animalName,
                                 oneCell.ephysSession,
                                 'Tetrode{}.spikes'.format(oneCell.tetrode))
예제 #19
0
def main():
    global behavSession
    global subject
    global ephysSession
    global tetrodeID
    global bdata
    global eventOnsetTimes
    global spikeTimesFromEventOnset
    global indexLimitsEachTrial
    global spikeTimesFromMovementOnset
    global indexLimitsEachMovementTrial
    for cellID in range(0, numOfCells):
        oneCell = allcells.cellDB[cellID]
        try:
            if (behavSession != oneCell.behavSession):

                subject = oneCell.animalName
                behavSession = oneCell.behavSession
                ephysSession = oneCell.ephysSession
                ephysRoot = os.path.join(ephysRootDir, subject)

                print behavSession

                # -- Load Behavior Data --
                behaviorFilename = loadbehavior.path_to_behavior_data(
                    subject, experimenter, paradigm, behavSession)
                bdata = loadbehavior.BehaviorData(behaviorFilename)
                numberOfTrials = len(bdata['choice'])

                # -- Load event data and convert event timestamps to ms --
                ephysDir = os.path.join(ephysRoot, ephysSession)
                eventFilename = os.path.join(ephysDir, 'all_channels.events')
                events = loadopenephys.Events(
                    eventFilename)  # Load events data
                eventTimes = np.array(
                    events.timestamps
                ) / SAMPLING_RATE  #get array of timestamps for each event and convert to seconds by dividing by sampling rate (Hz). matches with eventID and

                soundOnsetEvents = (events.eventID == 1) & (
                    events.eventChannel == soundTriggerChannel)

                eventOnsetTimes = eventTimes[soundOnsetEvents]

                possibleFreq = np.unique(bdata['targetFrequency'])
                numberOfFrequencies = len(possibleFreq)
                centerFrequencies = [(numberOfFrequencies / 2 - 1),
                                     numberOfFrequencies / 2]

                #################################################################################################
                centerOutTimes = bdata[
                    'timeCenterOut']  #This is the times that the mouse goes out of the center port
                soundStartTimes = bdata[
                    'timeTarget']  #This gives an array with the times in seconds from the start of the behavior paradigm of when the sound was presented for each trial
                timeDiff = centerOutTimes - soundStartTimes
                if (len(eventOnsetTimes) < len(timeDiff)):
                    timeDiff = timeDiff[:-1]
                eventOnsetTimesCenter = eventOnsetTimes + timeDiff
                #################################################################################################

            # -- Load Spike Data From Certain Cluster --
            spkData = ephyscore.CellData(oneCell)
            spkTimeStamps = spkData.spikes.timestamps

            (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
                spikesanalysis.eventlocked_spiketimes(spkTimeStamps,eventOnsetTimes,timeRange)

            (spikeTimesFromMovementOnset,movementTrialIndexForEachSpike,indexLimitsEachMovementTrial) = \
                spikesanalysis.eventlocked_spiketimes(spkTimeStamps,eventOnsetTimesCenter,timeRange)

            plt.clf()
            if (len(spkTimeStamps) > 0):
                ax1 = plt.subplot2grid((7, 6), (6, 0), colspan=2)
                spikesorting.plot_isi_loghist(spkData.spikes.timestamps)
                ax3 = plt.subplot2grid((7, 6), (6, 4), colspan=2)
                spikesorting.plot_events_in_time(spkData.spikes.timestamps)
                samples = spkData.spikes.samples.astype(float) - 2**15
                samples = (1000.0 / spkData.spikes.gain[0, 0]) * samples
                ax2 = plt.subplot2grid((7, 6), (6, 2), colspan=2)
                spikesorting.plot_waveforms(samples)

            ###############################################################################
            ax4 = plt.subplot2grid((7, 6), (0, 0), colspan=3, rowspan=2)
            raster_sound_psycurve(centerFrequencies[0])
            ax5 = plt.subplot2grid((7, 6), (2, 0), colspan=3)
            hist_sound_psycurve(centerFrequencies[0])
            ax6 = plt.subplot2grid((7, 6), (0, 3), colspan=3, rowspan=2)
            raster_sound_psycurve(centerFrequencies[1])
            ax7 = plt.subplot2grid((7, 6), (2, 3), colspan=3)
            hist_sound_psycurve(centerFrequencies[1])

            ax8 = plt.subplot2grid((7, 6), (3, 0), colspan=3, rowspan=2)
            raster_movement_psycurve(centerFrequencies[0])
            ax9 = plt.subplot2grid((7, 6), (5, 0), colspan=3)
            hist_movement_psycurve(centerFrequencies[0])
            ax10 = plt.subplot2grid((7, 6), (3, 3), colspan=3, rowspan=2)
            raster_movement_psycurve(centerFrequencies[1])
            ax11 = plt.subplot2grid((7, 6), (5, 3), colspan=3)
            hist_movement_psycurve(centerFrequencies[1])
            ###############################################################################
            #plt.tight_layout()

            tetrodeClusterName = 'T' + str(oneCell.tetrode) + 'c' + str(
                oneCell.cluster)
            plt.gcf().set_size_inches((8.5, 11))
            figformat = 'png'  #'png' #'pdf' #'svg'
            filename = 'report_centerFreq_%s_%s_%s.%s' % (
                subject, behavSession, tetrodeClusterName, figformat)
            fulloutputDir = outputDir + subject + '/'
            fullFileName = os.path.join(fulloutputDir, filename)

            directory = os.path.dirname(fulloutputDir)
            if not os.path.exists(directory):
                os.makedirs(directory)
            #print 'saving figure to %s'%fullFileName
            plt.gcf().savefig(fullFileName, format=figformat)

            #plt.show()

        except:
            #print "error with session "+oneCell.behavSession
            if (oneCell.behavSession not in badSessionList):
                badSessionList.append(oneCell.behavSession)

    print 'error with sessions: '
    for badSes in badSessionList:
        print badSes
예제 #20
0
#Get onset frame numbers
ttl = rtData['ttl_log']
ttl = ttl.ravel().astype(int)  #uint8 making things strange for diff
onsets = np.flatnonzero(
    np.diff(ttl.astype(int)) == 2) + 1  #Should catch the leading edges

#How many samples before and after onset to average
samplesBefore = 0
samplesAfter = 20
traceLen = samplesBefore + samplesAfter

#Load the behavior data
behavFn = loadbehavior.path_to_behavior_data('imag002', 'am_tuning_curve',
                                             '20181201b')
bdata = loadbehavior.BehaviorData(behavFn)

currentFreq = bdata['currentFreq']

if len(onsets) != len(currentFreq):
    currentFreq = currentFreq[:len(
        onsets)]  #Events will stop sooner than behavior.

possibleFreq = np.unique(currentFreq)
maxEachFreq = np.empty(len(possibleFreq))
minEachFreq = np.empty(len(possibleFreq))
axEachFreq = []
for indFreq, thisFreq in enumerate(possibleFreq):
    ax = plt.subplot(4, 3, indFreq + 1)
    axEachFreq.append(ax)
    onsetsThisFreq = onsets[np.flatnonzero(currentFreq == thisFreq)]
'''

from jaratoolbox import loadopenephys
from jaratoolbox import loadbehavior
import numpy as np
import matplotlib.pyplot as plt

EPHYS_SAMPLING_RATE = 30000.0

#ephysfn = '/home/languo/data/ephys/d1pi002/2015-08-14_15-42-36/all_channels.events'   #bData has 999 trials while len(eventOnsetTimes)=996
ephysfn = '/home/languo/data/ephys/d1pi002/2015-08-14_14-55-22/all_channels.events'
eventData = loadopenephys.Events(ephysfn)

#behavfn ='/home/languo/data/behavior/lan/d1pi002/d1pi002_laser_tuning_curve_20150814b.h5'  #bData has 999 trials while len(eventOnsetTimes)=996
behavfn = '/home/languo/data/behavior/lan/d1pi002/d1pi002_laser_tuning_curve_20150814a.h5'
bData = loadbehavior.BehaviorData(behavfn)

eventID = 1
eventChannel = 0
evID = np.array(eventData.eventID)
evChannel = np.array(eventData.eventChannel)
eventTimes = np.array(eventData.timestamps) / EPHYS_SAMPLING_RATE
eventOnsetTimes = eventTimes[(evID == eventID) & (evChannel == eventChannel)]

eventOnsetTimes = eventOnsetTimes - eventOnsetTimes[0]

indexLEET = bData.events['indexLastEventEachTrial']
indexFEET = np.hstack(
    (4, (indexLEET[:-1] + 1)))  #hard-coded index for start of first trial as 4

time_FEET = bData.events['eventTime'][indexFEET]
예제 #22
0
def load_behaviour_data(subject, fileName):
    behavFile = os.path.join(settings.BEHAVIOR_PATH,subject,fileName)
    bdata = loadbehavior.BehaviorData(behavFile,readmode='full')
    return bdata
def main():
    global behavSession
    global subject
    global ephysSession
    global tetrode
    global cluster
    global bdata
    global eventOnsetTimes
    global spikeTimesFromEventOnset
    global indexLimitsEachTrial
    global spikeTimesFromMovementOnset
    global indexLimitsEachMovementTrial
    global tuningBehavior  #behavior file name of tuning curve
    global tuningEphys  #ephys session name of tuning curve

    for cellID in range(0, numOfCells):
        oneCell = allcells.cellDB[cellID]
        try:
            if (behavSession != oneCell.behavSession):

                subject = oneCell.animalName
                behavSession = oneCell.behavSession
                ephysSession = oneCell.ephysSession
                ephysRoot = os.path.join(ephysRootDir, subject)
                tuningBehavior = oneCell.tuningBehavior
                tuningEphys = oneCell.tuningSession

                print behavSession

                # -- Load Behavior Data --
                behaviorFilename = loadbehavior.path_to_behavior_data(
                    subject=subject,
                    paradigm=paradigm,
                    sessionstr=behavSession)
                bdata = loadbehavior.BehaviorData(behaviorFilename)
                numberOfTrials = len(bdata['choice'])

                # -- Load event data and convert event timestamps to ms --
                ephysDir = os.path.join(ephysRoot, ephysSession)
                eventFilename = os.path.join(ephysDir, 'all_channels.events')
                events = loadopenephys.Events(
                    eventFilename)  # Load events data
                eventTimes = np.array(
                    events.timestamps
                ) / SAMPLING_RATE  #get array of timestamps for each event and convert to seconds by dividing by sampling rate (Hz). matches with eventID and

                soundOnsetEvents = (events.eventID == 1) & (
                    events.eventChannel == soundTriggerChannel)

                eventOnsetTimes = eventTimes[soundOnsetEvents]
                soundOnsetTimeBehav = bdata['timeTarget']

                # Find missing trials
                missingTrials = behavioranalysis.find_missing_trials(
                    eventOnsetTimes, soundOnsetTimeBehav)

                # Remove missing trials
                bdata.remove_trials(missingTrials)

                possibleFreq = np.unique(bdata['targetFrequency'])
                numberOfFrequencies = len(possibleFreq)
                centerFrequencies = [(numberOfFrequencies / 2 - 1),
                                     numberOfFrequencies / 2]

                #################################################################################################
                centerOutTimes = bdata[
                    'timeCenterOut']  #This is the times that the mouse goes out of the center port
                soundStartTimes = bdata[
                    'timeTarget']  #This gives an array with the times in seconds from the start of the behavior paradigm of when the sound was presented for each trial
                timeDiff = centerOutTimes - soundStartTimes

                if (len(eventOnsetTimes) < len(timeDiff)):
                    eventOnsetTimesCenter = eventOnsetTimes + timeDiff[:-1]
                elif (len(eventOnsetTimes) > len(timeDiff)):
                    eventOnsetTimesCenter = eventOnsetTimes[:-1] + timeDiff
                else:
                    eventOnsetTimesCenter = eventOnsetTimes + timeDiff
                #################################################################################################

            tetrode = oneCell.tetrode
            cluster = oneCell.cluster
            # -- Load Spike Data From Certain Cluster --
            spkData = ephyscore.CellData(oneCell)
            spkTimeStamps = spkData.spikes.timestamps

            (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
                spikesanalysis.eventlocked_spiketimes(spkTimeStamps,eventOnsetTimes,timeRange)

            (spikeTimesFromMovementOnset,movementTrialIndexForEachSpike,indexLimitsEachMovementTrial) = \
                spikesanalysis.eventlocked_spiketimes(spkTimeStamps,eventOnsetTimesCenter,timeRange)

            plt.clf()
            if (len(spkTimeStamps) > 0):
                ax1 = plt.subplot2grid((numRows, numCols),
                                       ((numRows - sizeClusterPlot), 0),
                                       colspan=(numCols / 3))
                spikesorting.plot_isi_loghist(spkData.spikes.timestamps)
                ax3 = plt.subplot2grid(
                    (numRows, numCols),
                    ((numRows - sizeClusterPlot), (numCols / 3) * 2),
                    colspan=(numCols / 3))
                spikesorting.plot_events_in_time(spkData.spikes.timestamps)
                samples = spkData.spikes.samples.astype(float) - 2**15
                samples = (1000.0 / spkData.spikes.gain[0, 0]) * samples
                ax2 = plt.subplot2grid(
                    (numRows, numCols),
                    ((numRows - sizeClusterPlot), (numCols / 3)),
                    colspan=(numCols / 3))
                spikesorting.plot_waveforms(samples)

            ###############################################################################
            ax4 = plt.subplot2grid((numRows, numCols), (0, 0),
                                   colspan=(numCols / 3),
                                   rowspan=3 * sizeRasters)
            #plt.setp(ax4.get_xticklabels(), visible=False)
            #raster_sound_psycurve(centerFrequencies[0])
            raster_tuning(ax4)
            axvline(x=0, ymin=0, ymax=1, color='r')
            plt.gca().set_xlim(tuning_timeRange)

            #ax5 = plt.subplot2grid((7,6), (2,0), colspan = 3, sharex=ax4)
            #hist_sound_psycurve(centerFrequencies[0])

            ax6 = plt.subplot2grid((numRows, numCols), (0, (numCols / 3)),
                                   colspan=(2 * numCols / 3),
                                   rowspan=sizeHalfRaster)
            raster_allfreq_sound_psycurve()
            ax6.set_ylabel('frequencies')
            ax6.yaxis.set_label_position("right")
            plt.setp(ax6.get_xticklabels(), visible=False)
            ax6.yaxis.tick_right()
            ax6.yaxis.set_ticks_position('both')
            plt.gca().set_xlim(timeRange)
            plt.title('Top: sound aligned, Bottom: movement aligned')

            ax7 = plt.subplot2grid((numRows, numCols),
                                   (sizeHalfRaster, (numCols / 3)),
                                   colspan=(2 * numCols / 3),
                                   rowspan=sizeHalfRaster)
            hist_allfreq_sound_psycurve(ax7)
            ax7.yaxis.tick_right()
            ax7.yaxis.set_ticks_position('both')
            plt.setp(ax7.get_xticklabels(), visible=False)
            plt.gca().set_xlim(timeRange)

            ax8 = plt.subplot2grid((numRows, numCols),
                                   (2 * sizeHalfRaster, (numCols / 3)),
                                   colspan=(2 * numCols / 3),
                                   rowspan=sizeHalfRaster)
            ax8.yaxis.tick_right()
            plt.setp(ax8.get_xticklabels(), visible=False)
            raster_allfreq_movement_psycurve()
            ax8.set_ylabel('frequencies')
            ax8.yaxis.set_label_position("right")

            ax10 = plt.subplot2grid((numRows, numCols),
                                    (3 * sizeHalfRaster, (numCols / 3)),
                                    colspan=(2 * numCols / 3),
                                    rowspan=sizeHalfRaster)
            #plt.setp(ax10.get_xticklabels(), visible=False)
            hist_allfreq_movement_psycurve(ax10)
            ax10.yaxis.tick_right()
            ax10.yaxis.set_ticks_position('both')
            plt.gca().set_xlim(timeRange)
            ax10.set_xlabel('Time (sec)')

            #ax11 = plt.subplot2grid((numRows,numCols), ((2*sizeRasters+sizeHists),(numCols/2)), colspan = (numCols/2), rowspan = sizeHists, sharex=ax10)
            #hist_sound_psycurve(centerFrequencies[1])
            #ax11.yaxis.tick_right()
            #ax11.yaxis.set_ticks_position('both')
            #plt.setp(ax11.get_yticklabels(), visible=False)
            #plt.gca().set_xlim(timeRange)
            ###############################################################################
            #plt.tight_layout()

            modulation_index_psycurve(centerFrequencies)
            plt.suptitle(titleText)

            tetrodeClusterName = 'T' + str(oneCell.tetrode) + 'c' + str(
                oneCell.cluster)
            plt.gcf().set_size_inches((8.5, 11))
            figformat = 'png'  #'png' #'pdf' #'svg'
            filename = reportname + '_%s_%s_%s.%s' % (
                subject, behavSession, tetrodeClusterName, figformat)
            fulloutputDir = outputDir + subject + '/'
            fullFileName = os.path.join(fulloutputDir, filename)

            directory = os.path.dirname(fulloutputDir)
            if not os.path.exists(directory):
                os.makedirs(directory)
            #print 'saving figure to %s'%fullFileName
            plt.gcf().savefig(fullFileName, format=figformat)

            #plt.show()

        except:
            if (oneCell.behavSession not in badSessionList):
                badSessionList.append(oneCell.behavSession)

    print 'error with sessions: '
    for badSes in badSessionList:
        print badSes
예제 #24
0
def overall_muscimol_performance(animal, muscimolSessions, salineSessions):

    '''
    DEPRECATED
    Plot psychometric and average performance reports for muscimol experiments
    INCOMPLETE: hard coded for chords and modulations right now. 
    '''

    #Plot the psycurves
    subplot(221)
    muscimol_plot_sound_type(animal, muscimolSessions, salineSessions, 'chords')

    subplot(223)
    muscimol_plot_sound_type(animal, muscimolSessions, salineSessions, 'amp_mod')


    #Overall muscimol and saline performance
    muscimolData = behavioranalysis.load_many_sessions(animal, muscimolSessions)
    muscimolChords = percent_correct_sound_type(muscimolData, 'chords')
    muscimolMod = percent_correct_sound_type(muscimolData, 'amp_mod')

    salineData = behavioranalysis.load_many_sessions(animal, salineSessions)
    salineChords = percent_correct_sound_type(salineData, 'chords')
    salineMod = percent_correct_sound_type(salineData, 'amp_mod')


    #Individual session muscimol and saline performance

    for indp, pair in enumerate(zip(salineSessions, muscimolSessions)):

        salineFile = loadbehavior.path_to_behavior_data(animal,settings.DEFAULT_EXPERIMENTER,'2afc',pair[0])
        salineData = loadbehavior.BehaviorData(salineFile,readmode='full')

        salineChords = percent_correct_sound_type(salineData, 'chords')
        salineMod = percent_correct_sound_type(salineData, 'amp_mod')

        muscimolFile = loadbehavior.path_to_behavior_data(animal,settings.DEFAULT_EXPERIMENTER,'2afc',pair[1])
        muscimolData = loadbehavior.BehaviorData(muscimolFile,readmode='full')

        muscimolChords = percent_correct_sound_type(muscimolData, 'chords')
        muscimolMod = percent_correct_sound_type(muscimolData, 'amp_mod')

        if indp==0:
            allPerfChords = array([salineChords, muscimolChords])
        else:
            allPerfChords = vstack([allPerfChords, array([salineChords, muscimolChords])])

        if indp==0:
            allPerfMod = array([salineMod, muscimolMod])
        else:
            allPerfMod = vstack([allPerfMod, array([salineMod, muscimolMod])])

    ## -- Chords performance
    subplot(222)
    bar([0, 1], [salineChords, muscimolChords], width=0.5, facecolor='w', edgecolor='k', linewidth = 2)
    #plot lines
    # for expt in allPerfChords:
    #     plot([0.25, 1.25], expt, '0.3', lw=2)

    #Plot points
    for point in allPerfChords[:,0]:
        plot(0.25, point, 'ko', ms=7)
    for point in allPerfChords[:,1]:
        plot(1.25, point, 'ro', ms=7)

    xlim([-0.5, 2])
    xticks([0.25, 1.25])
    ax = gca()
    ax.set_xticklabels(['Saline', 'Muscimol'])
    ylim([0, 100])
    ylabel('Percent Correct')

    ## -- Amp mod performance
    subplot(224)
    bar([0, 1], [salineMod, muscimolMod], width=0.5, facecolor='w', edgecolor='k', linewidth = 2)
    # for expt in allPerfMod:
    #     plot([0.25, 1.25], expt, '0.3', lw=2)

    #Plot points
    for point in allPerfMod[:,0]:
        plot(0.25, point, 'ko', ms=7)
    for point in allPerfMod[:,1]:
        plot(1.25, point, 'ro', ms=7)

    xlim([-0.5, 2])
    xticks([0.25, 1.25])
    ax = gca()
    ax.set_xticklabels(['Saline', 'Muscimol'])
    ylim([0, 100])
    ylabel('Percent Correct')

    return (allPerfChords, allPerfMod)
def raster_tuning(ax):

    fullbehaviorDir = behaviorDir + subject + '/'
    behavName = subject + '_tuning_curve_' + tuningBehavior + '.h5'
    tuningBehavFileName = os.path.join(fullbehaviorDir, behavName)

    tuning_bdata = loadbehavior.BehaviorData(tuningBehavFileName,
                                             readmode='full')
    freqEachTrial = tuning_bdata['currentFreq']
    possibleFreq = np.unique(freqEachTrial)
    numberOfTrials = len(freqEachTrial)

    # -- The old way of sorting (useful for plotting sorted raster) --
    sortedTrials = []
    numTrialsEachFreq = [
    ]  #Used to plot lines after each group of sorted trials
    for indf, oneFreq in enumerate(
            possibleFreq
    ):  #indf is index of this freq and oneFreq is the frequency
        indsThisFreq = np.flatnonzero(
            freqEachTrial == oneFreq)  #this gives indices of this frequency
        sortedTrials = np.concatenate(
            (sortedTrials,
             indsThisFreq))  #adds all indices to a list called sortedTrials
        numTrialsEachFreq.append(
            len(indsThisFreq))  #finds number of trials each frequency has
    sortingInds = argsort(
        sortedTrials)  #gives array of indices that would sort the sortedTrials

    # -- Load event data and convert event timestamps to ms --
    tuning_ephysDir = os.path.join(settings.EPHYS_PATH, subject, tuningEphys)
    tuning_eventFilename = os.path.join(tuning_ephysDir, 'all_channels.events')
    tuning_ev = loadopenephys.Events(
        tuning_eventFilename)  #load ephys data (like bdata structure)
    tuning_eventTimes = np.array(
        tuning_ev.timestamps
    ) / SAMPLING_RATE  #get array of timestamps for each event and convert to seconds by dividing by sampling rate (Hz). matches with eventID and
    tuning_evID = np.array(
        tuning_ev.eventID
    )  #loads the onset times of events (matches up with eventID to say if event 1 went on (1) or off (0)
    tuning_eventOnsetTimes = tuning_eventTimes[
        tuning_evID ==
        1]  #array that is a time stamp for when the chosen event happens.
    #ev.eventChannel woul load array of events like trial start and sound start and finish times (sound event is 0 and trial start is 1 for example). There is only one event though and its sound start
    while (numberOfTrials < len(tuning_eventOnsetTimes)):
        tuning_eventOnsetTimes = tuning_eventOnsetTimes[:-1]

    #######################################################################################################
    ###################THIS IS SUCH A HACK TO GET SPKDATA FROM EPHYSCORE###################################
    #######################################################################################################

    thisCell = celldatabase.CellInfo(
        animalName=subject,  ############################################
        ephysSession=tuningEphys,
        tuningSession='DO NOT NEED THIS',
        tetrode=tetrode,
        cluster=cluster,
        quality=1,
        depth=0,
        tuningBehavior='DO NOT NEED THIS',
        behavSession=tuningBehavior)

    tuning_spkData = ephyscore.CellData(thisCell)
    tuning_spkTimeStamps = tuning_spkData.spikes.timestamps

    (tuning_spikeTimesFromEventOnset, tuning_trialIndexForEachSpike,
     tuning_indexLimitsEachTrial) = spikesanalysis.eventlocked_spiketimes(
         tuning_spkTimeStamps, tuning_eventOnsetTimes, tuning_timeRange)

    #print 'numTrials ',max(tuning_trialIndexForEachSpike)#####################################
    '''
        Create a vector with the spike timestamps w.r.t. events onset.

        (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = 
            eventlocked_spiketimes(timeStamps,eventOnsetTimes,timeRange)

        timeStamps: (np.array) the time of each spike.
        eventOnsetTimes: (np.array) the time of each instance of the event to lock to.
        timeRange: (list or np.array) two-element array specifying time-range to extract around event.

        spikeTimesFromEventOnset: 1D array with time of spikes locked to event.
    o    trialIndexForEachSpike: 1D array with the trial corresponding to each spike.
           The first spike index is 0.
        indexLimitsEachTrial: [2,nTrials] range of spikes for each trial. Note that
           the range is from firstSpike to lastSpike+1 (like in python slices)
        spikeIndices
    '''

    tuning_sortedIndexForEachSpike = sortingInds[
        tuning_trialIndexForEachSpike]  #Takes values of trialIndexForEachSpike and finds value of sortingInds at that index and makes array. This array gives an array with the sorted index of each trial for each spike

    # -- Calculate tuning --
    #nSpikes = spikesanalysis.spiketimes_to_spikecounts(spikeTimesFromEventOnset,indexLimitsEachTrial,responseRange) #array of the number of spikes in range for each trial
    '''Count number of spikes on each trial in a given time range.

           spikeTimesFromEventOnset: vector of spikes timestamps with respect
             to the onset of the event.
           indexLimitsEachTrial: each column contains [firstInd,lastInd+1] of the spikes on a trial.
           timeRange: time range to evaluate. Spike times exactly at the limits are not counted.

           returns nSpikes
    '''
    '''
    meanSpikesEachFrequency = np.empty(len(possibleFreq)) #make empty array of same size as possibleFreq

    # -- This part will be replace by something like behavioranalysis.find_trials_each_type --
    trialsEachFreq = []
    for indf,oneFreq in enumerate(possibleFreq):
        trialsEachFreq.append(np.flatnonzero(freqEachTrial==oneFreq)) #finds indices of each frequency. Appends them to get an array of indices of trials sorted by freq

    # -- Calculate average firing for each freq --
    for indf,oneFreq in enumerate(possibleFreq):
        meanSpikesEachFrequency[indf] = np.mean(nSpikes[trialsEachFreq[indf]])
    '''
    #clf()
    #if (len(tuning_spkTimeStamps)>0):
    #ax1 = plt.subplot2grid((4,4), (3, 0), colspan=1)
    #spikesorting.plot_isi_loghist(spkData.spikes.timestamps)
    #ax3 = plt.subplot2grid((4,4), (3, 3), colspan=1)
    #spikesorting.plot_events_in_time(tuning_spkTimeStamps)
    #samples = tuning_spkData.spikes.samples.astype(float)-2**15
    #samples = (1000.0/tuning_spkData.spikes.gain[0,0]) *samples
    #ax2 = plt.subplot2grid((4,4), (3, 1), colspan=2)
    #spikesorting.plot_waveforms(samples)
    #ax4 = plt.subplot2grid((4,4), (0, 0), colspan=3,rowspan = 3)
    plot(tuning_spikeTimesFromEventOnset,
         tuning_sortedIndexForEachSpike,
         '.',
         ms=3)
    #axvline(x=0, ymin=0, ymax=1, color='r')

    #The cumulative sum of the list of specific frequency presentations,
    #used below for plotting the lines across the figure.
    numTrials = cumsum(numTrialsEachFreq)

    #Plot the lines across the figure in between each group of sorted trials
    for indf, num in enumerate(numTrials):
        ax.axhline(y=num, xmin=0, xmax=1, color='0.90', zorder=0)

    tickPositions = numTrials - mean(numTrialsEachFreq) / 2
    tickLabels = [
        "%0.2f" % (possibleFreq[indf] / 1000)
        for indf in range(len(possibleFreq))
    ]
    ax.set_yticks(tickPositions)
    ax.set_yticklabels(tickLabels)
    ax.set_ylim([-1, numberOfTrials])
    ylabel('Frequency Presented (kHz), {} total trials'.format(numTrials[-1]))
    #title(ephysSession+' T{}c{}'.format(tetrodeID,clusterID))
    xlabel('Time (sec)')
    '''

    ax5 = plt.subplot2grid((4,4), (0, 3), colspan=1,rowspan=3)
    ax5.set_xscale('log')
    plot(possibleFreq,meanSpikesEachFrequency,'o-')
    ylabel('Avg spikes in window {0}-{1} sec'.format(*responseRange))
    xlabel('Frequency')
    '''
    #show()
    '''
예제 #26
0
subjects = ['adap021', 'adap023', 'adap028', 'adap029', 'adap035']

#Init array to hold percent correct for 4 saline sessions for each animal
percCorrect = np.empty((len(subjects), 4))

for indSubject, subject in enumerate(subjects):
    datesDict = muscimol_sessions.animals[subject]
    alldates = []
    salinedates = datesDict['saline']
    muscimoldates = datesDict['muscimol']
    sessions = salinedates
    for indSession, session in enumerate(sessions):
        #Load behavior data for the sessioln
        bfile = loadbehavior.path_to_behavior_data(subject, '2afc', session)
        bdata = loadbehavior.BehaviorData(bfile)

        #Compute percent correct for valid trials
        valid = bdata['valid']
        correct = bdata['outcome'] == bdata.labels['outcome']['correct']
        validCorrect = (valid & correct)
        numValid = sum(valid)
        numCorrect = sum(validCorrect)
        percCorrect[indSubject,
                    indSession] = (numCorrect / float(numValid)) * 100

#Related-samples t-test between first and last saline day
firstSaline = percCorrect[:, 0]
lastSaline = percCorrect[:, -1]
zVal, pVal = stats.ttest_rel(firstSaline, lastSaline)
print "Related sample t-test between percent correct on first and last day, p = {}".format(
예제 #27
0
def light_discrim_behavior_report(subjects, sessions, outputDir=None):

    #allPerf(subjects, sessions)
    allPerf = np.zeros((len(subjects), len(sessions)))
    upper = np.zeros((len(subjects), len(sessions)))
    lower = np.zeros((len(subjects), len(sessions)))

    allPerfRightSide = np.zeros((len(subjects), len(sessions)))
    allPerfLeftSide = np.zeros((len(subjects), len(sessions)))

    for indSubject, subject in enumerate(subjects):
        for indSession, session in enumerate(sessions):
            bfile = loadbehavior.path_to_behavior_data(subject, 'lightDiscrim',
                                                       session)
            bdata = loadbehavior.BehaviorData(bfile)

            valid = bdata['valid']
            correct = bdata['outcome'] == bdata.labels['outcome']['correct']

            validCorrect = (valid & correct)

            numValid = sum(valid)
            numCorrect = sum(validCorrect)

            allPerf[indSubject, indSession] = numCorrect / float(numValid)

            # import ipdb; ipdb.set_trace()

            ci = proportion_confint(numCorrect, numValid)

            lower[indSubject, indSession] = ci[0]
            upper[indSubject, indSession] = ci[1]

            rightTrials = bdata['rewardSide'] == bdata.labels['rewardSide'][
                'right']
            leftTrials = bdata['rewardSide'] == bdata.labels['rewardSide'][
                'left']

            correctRight = (validCorrect & rightTrials)
            correctLeft = (validCorrect & leftTrials)

            allPerfRightSide[indSubject,
                             indSession] = sum(correctRight) / float(
                                 sum(rightTrials))
            allPerfLeftSide[indSubject, indSession] = sum(correctLeft) / float(
                sum(leftTrials))

    # plt.figure()
    plt.clf()
    for indSubject, subject in enumerate(subjects):

        plt.subplot(len(subjects), 1, indSubject + 1)
        thisPerf = allPerf[indSubject, :]
        plt.plot(100 * thisPerf, 'b-o')
        upperWhisker = np.array(upper[indSubject, :] - thisPerf)
        lowerWhisker = np.array(thisPerf - lower[indSubject, :])
        plt.errorbar(range(len(thisPerf)),
                     100 * thisPerf,
                     yerr=[100 * lowerWhisker, 100 * upperWhisker],
                     label='Overall')
        plt.plot(100 * allPerfLeftSide[indSubject, :], 'r-o', label='Left')
        plt.plot(100 * allPerfRightSide[indSubject, :], 'g-o', label='Right')
        plt.ylim([-5, 105])
        plt.xlim([-0.1, len(sessions) - 0.9])
        plt.title('{}'.format(subject))
        plt.ylabel('% correct')
        plt.axhline(100, color='0.7')
        plt.axhline(50, color='0.7')
        plt.axhline(0, color='0.7')

    plt.legend(loc=8)
    plt.xlabel('session')
    plt.show()

    if outputDir:
        subjectString = '-'.join(subjects)
        dateString = '_to_'.join([sessions[0], sessions[-1]])
        figName = '{}.png'.format('_'.join([subjectString, dateString]))
        fullFigPath = os.path.join(outputDir, figName)
        plt.savefig(fullFigPath)
예제 #28
0
    elif CASE==2:
        if 1:
            subjects = ['test011','test012','test013','test014','test015',
                        'test016','test017','test018','test019','test020']
        else:
            subjects = ['test050','test051','test052','test053','test054',
                        'test055','test056','test057','test058','test059']
        #subjects = ['test019','test020']
        #sessions = '20140321a' # No currentBlock
        sessions = '20140616a'
        behavior_summary(subjects,sessions,trialslim=[0,1200],outputDir='/tmp/')

    elif CASE==3:
        fname=loadbehavior.path_to_behavior_data('test052','santiago','2afc','20140911a')
        bdata=loadbehavior.BehaviorData(fname)
        (possibleFreq,pRightEach,ci,nTrialsEach,nRightwardEach) = OLD_calculate_psychometric(bdata,
                                                                                         parameterName='targetFrequency')
        print pRightEach
    elif CASE==4:
        allBehavData = load_many_sessions(['test020'],sessions=['20140421a','20140422a','20140423a'])
    elif CASE==5:
        param = np.array([7,4,7,5,7,4,7,5,7,4,7,8,8,8])
        possibleParam = np.unique(param)
        tet = find_trials_each_type(param,possibleParam)
        mask = param>4
        tet = tet & mask[:,np.newaxis]
        print possibleParam
        print tet
    elif CASE==6:
        #parameter1 = np.array([1,2,3,4,5,1,2,3,4,5])
    if (oneCell.behavSession in modIList): #checks to make sure the modI value is not recalculated
        continue
    try:
        if (behavSession != oneCell.behavSession):

            subject = oneCell.animalName
            behavSession = oneCell.behavSession
            ephysSession = oneCell.ephysSession
            ephysRoot = os.path.join(ephysRootDir,subject)

            print behavSession

            # -- Load Behavior Data --
            behaviorFilename = loadbehavior.path_to_behavior_data(subject,experimenter,paradigm,behavSession)
            bdata = loadbehavior.BehaviorData(behaviorFilename)

            # -- Load event data and convert event timestamps to ms --
            ephysDir = os.path.join(ephysRoot, ephysSession)
            eventFilename=os.path.join(ephysDir, 'all_channels.events')
            events = loadopenephys.Events(eventFilename) # Load events data
            eventTimes=np.array(events.timestamps)/SAMPLING_RATE #get array of timestamps for each event and convert to seconds by dividing by sampling rate (Hz). matches with eventID and 

            soundOnsetEvents = (events.eventID==1) & (events.eventChannel==soundTriggerChannel)

            eventOnsetTimes = eventTimes[soundOnsetEvents]

            rightward = bdata['choice']==bdata.labels['choice']['right']
            leftward = bdata['choice']==bdata.labels['choice']['left']
            valid = (bdata['outcome']==bdata.labels['outcome']['correct'])|(bdata['outcome']==bdata.labels['outcome']['error'])
            correct = bdata['outcome']==bdata.labels['outcome']['correct']
예제 #30
0
'''
Test corrupted behavior file.
'''

from jaratoolbox import loadbehavior

filev1 = '/data/behavior/test055/test055_2afc_20150313a.h5'
filev2 = '/data/old_behavior_santiago/test055/test055_2afc_20150313a.h5'
filev3 = '/mnt/jarahubdata/behavior/test055/test055_2afc_20150313a.h5'

bdata = loadbehavior.BehaviorData(filev3)