def load_n_remove_missing_trials_2afc_behav(animal, behavSession, ephysSession,
                                            tetrode, cluster):
    '''Loads a 2afc behavior session, compare the number of trials recorded to the corresponding ephys session and removes trials that were not recorded in the ephys session.
    :param arg1: A string of the file name of the 2afc behavior session, this is the full filename in '{animal}_{paradigm}_{date}{behavsuffix}.h5' format.
    :param arg2: A string of the file name of the 2afc ephys session, this is the full filename, in {date}_XX-XX-XX format.
    :param arg3: An int in range(1,9) for tetrode number.
    :param arg4: An int for cluster number.
    :return: bData object with missing trials removed in all fields
    '''
    bData = load_behavior_flexcat(animal, behavSession)  #need extra methods

    eventData = load_event_data(animal, ephysSession)

    eventOnsetTimes = np.array(eventData.timestamps)
    soundOnsetEvents = (eventData.eventID == 1) & (eventData.eventChannel
                                                   == soundTriggerChannel)
    soundOnsetTimeEphys = eventOnsetTimes[soundOnsetEvents]
    soundOnsetTimeBehav = bData['timeTarget']

    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(
        soundOnsetTimeEphys, soundOnsetTimeBehav)
    # Remove missing trials
    bData.remove_trials(missingTrials)

    return bData
Esempio n. 2
0
def evaluate_2afc_sound_selectivity_celldb(cellDb):
    '''
    Analyse 2afc sound selectivity: use wilcoxon rank sum test to compare response to high vs low freqs.
    '''
    soundFreqSelPVal = np.ones(len(cellDb))  #default value 0
    for indCell, cell in cellDb.iterrows():
        cellObj = ephyscore.Cell(cell)
        sessiontype = 'behavior'  #2afc behavior
        #ephysData, bata = cellObj.load(sessiontype)
        sessionInd = cellObj.get_session_inds(sessiontype)[0]
        bdata = cellObj.load_behavior_by_index(sessionInd)
        possibleFreq = np.unique(bdata['targetFrequency'])
        numFreqs = len(possibleFreq)

        try:
            ephysData = cellObj.load_ephys_by_index(sessionInd)
        except (ValueError, IOError) as error:
            print(error)
            continue

        eventsDict = ephysData['events']
        spikeTimestamps = ephysData['spikeTimes']

        if spikeTimestamps.ndim == 0:  #There is only one spike, ! spikesanalysis.eventlocked_spiketimes cannot handle only one spike !
            continue

        soundOnsetTimes = eventsDict['{}On'.format(soundChannelType)]
        soundOnsetTimeBehav = bdata['timeTarget']

        # -- Check to see if ephys and behav recordings have same number of trials, remove missing trials from behav file -- #
        # Find missing trials
        missingTrials = behavioranalysis.find_missing_trials(
            soundOnsetTimes, soundOnsetTimeBehav)
        # Remove missing trials
        bdata.remove_trials(missingTrials)

        if len(soundOnsetTimes) != len(
                bdata['timeTarget']
        ):  #some error not handled by remove missing trials
            continue

        # -- Compare sound response for low vs high frequency -- #
        lowFreq = possibleFreq[0]
        highFreq = possibleFreq[1]
        lowFreqTrials = bdata['targetFrequency'] == lowFreq
        highFreqTrials = bdata['targetFrequency'] == highFreq
        (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
                spikesanalysis.eventlocked_spiketimes(spikeTimestamps,soundOnsetTimes,[0,0.1])
        nspkRespEachTrial = spikesanalysis.spiketimes_to_spikecounts(
            spikeTimesFromEventOnset, indexLimitsEachTrial, [0, 0.1])
        nspkRespEachTrial = nspkRespEachTrial.flatten()
        lowFreqRespEachTrial = nspkRespEachTrial[lowFreqTrials]
        highFreqRespEachTrial = nspkRespEachTrial[highFreqTrials]

        W, pVal = stats.ranksums(lowFreqRespEachTrial, highFreqRespEachTrial)

        soundFreqSelPVal[indCell] = pVal

    return soundFreqSelPVal
Esempio n. 3
0
def get_trials_each_cond_psycurve(oneCell):
    '''
    Given a cellInfo object, gets the trialsEachCond and colorEachCond vectors for raster and PSTH plotting. ONLY plotting trials for the two middle frequencies.
    '''
    # -- Calls load_remote_2afc_data(oneCell) to get the data, then plot raster -- #
    (eventData, spkData, bdata) = load_remote_2afc_data(oneCell)
    # -- Check to see if ephys has skipped trials, if so remove trials from behav data -- #
    eventOnsetTimes = np.array(eventData.timestamps)
    soundOnsetEvents = (eventData.eventID == 1) & (eventData.eventChannel
                                                   == soundTriggerChannel)
    soundOnsetTimeEphys = eventOnsetTimes[soundOnsetEvents]
    soundOnsetTimeBehav = bdata['timeTarget']

    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(
        soundOnsetTimeEphys, soundOnsetTimeBehav)
    # Remove missing trials
    bdata.remove_trials(missingTrials)

    # -- Select trials to plot from behavior file -- #
    possibleFreq = np.unique(bdata['targetFrequency'])
    numFreqs = len(possibleFreq)
    rightward = bdata['choice'] == bdata.labels['choice']['right']
    leftward = bdata['choice'] == bdata.labels['choice']['left']

    # -- Select trials of middle frequency to plot -- #
    middleFreqs = [
        possibleFreq[numFreqs / 2 - 1], possibleFreq[numFreqs / 2]
    ]  #selects middle frequencies, using int division resulting in int property. MAY FAIL IN THE FUTURE
    #pdb.set_trace()
    for middleFreq in middleFreqs:
        oneFreq = bdata[
            'targetFrequency'] == middleFreq  #vector for selecing trials presenting this frequency

        trialsToUseRight = rightward & oneFreq
        trialsToUseLeft = leftward & oneFreq
        condLabels = ['left choice', 'right choice']
        trialsEachCond = np.c_[trialsToUseLeft, trialsToUseRight]
        colorEachCond = [colorsDict['colorL'], colorsDict['colorR']]

    return bdata, trialsEachCond, colorEachCond
Esempio n. 4
0
def load_n_remove_missing_trials_2afc_behav(cellObj, behavClass=loadbehavior.FlexCategBehaviorData):
    '''Loads a 2afc behavior session, compare the number of trials recorded to the corresponding ephys session and removes trials that were not recorded in the ephys session.
    :param arg1: Cell object from ephyscore.
    :param arg2: behavClass (jaratoolbox.loadbehavior Class). The loading class to use, each class of behavData will have different methods.
    :return: bData object with missing trials removed in all fields
    '''
    sessionType = 'behavior'
    sessionInd = cellObj.get_session_inds(sessionType)[0]
    ephysData, bdata = cellObj.load_by_index(sessionInd, behavClass=behavClass)
   
    eventsDict = ephysData['events']
    spikeTimestamps = ephysData['spikeTimes']
    soundOnsetTimeEphys = eventsDict['{}On'.format(soundChannelType)]
    soundOnsetTimeBehav = bdata['timeTarget']
    
    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(soundOnsetTimeEphys,soundOnsetTimeBehav)
    # Remove missing trials
    bdata.remove_trials(missingTrials)

    return bdata
Esempio n. 5
0
            # -- 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)

            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']
            correctRightward = rightward & correct
            correctLeftward = leftward & correct

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

            ###################################################################################################
Esempio n. 6
0
            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]
            soundOnsetTimeBehav = bdata['timeTarget']

            # Find missing trials
            missingTrials = behavioranalysis.find_missing_trials(eventOnsetTimes,soundOnsetTimeBehav)
            # Remove missing trials
            bdata.remove_trials(missingTrials)
            numberOfTrials = len(bdata['choice'])

            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]
Esempio n. 7
0
def get_trials_each_cond_switching(oneCell, freqToPlot='middle', byBlock=True):
    '''
    Given a cellInfo object, the frequency to plot(string, value of 'middle' or 'all'), whether to plot by block (boolean), gets the trialsEachCond and colorEachCond vectors for raster and PSTH plotting.
    '''
    # -- Calls load_remote_2afc_data(oneCell) to get the data, then plot raster -- #
    (eventData, spkData, bdata) = load_remote_2afc_data(oneCell)
    # -- Check to see if ephys has skipped trials, if so remove trials from behav data -- #
    eventOnsetTimes = np.array(eventData.timestamps)
    soundOnsetEvents = (eventData.eventID == 1) & (eventData.eventChannel
                                                   == soundTriggerChannel)
    soundOnsetTimeEphys = eventOnsetTimes[soundOnsetEvents]
    soundOnsetTimeBehav = bdata['timeTarget']

    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(
        soundOnsetTimeEphys, soundOnsetTimeBehav)
    # Remove missing trials
    bdata.remove_trials(missingTrials)

    # -- Select trials to plot from behavior file -- #
    correct = bdata['outcome'] == bdata.labels['outcome']['correct']
    possibleFreq = np.unique(bdata['targetFrequency'])
    numFreqs = len(possibleFreq)
    # -- Select trials to plot based on desired frequencies to plot and whether to plot by block -- #
    if freqToPlot == 'middle':
        middleFreq = possibleFreq[
            numFreqs /
            2]  #selects middle frequency, using int division resulting in int property. MAY FAIL IN THE FUTURE
        #pdb.set_trace()
        oneFreq = bdata[
            'targetFrequency'] == middleFreq  #vector for selecing trials presenting this frequency
        correctOneFreq = oneFreq & correct

        # -- Find trials each block (if plotting mid frequency by block) or find trials each type (e.g. low-boundary, high-boundary; if not plotting by block) -- #
        if byBlock:
            bdata.find_trials_each_block()
            trialsEachBlock = bdata.blocks['trialsEachBlock']
            correctTrialsEachBlock = trialsEachBlock & correctOneFreq[:, np.
                                                                      newaxis]
            correctBlockSizes = sum(correctTrialsEachBlock)
            if (correctBlockSizes[-1] < minBlockSize
                ):  #A check to see if last block is too small to plot
                correctTrialsEachBlock = correctTrialsEachBlock[:, :-1]

            trialsEachCond = correctTrialsEachBlock
            if bdata['currentBlock'][0] == bdata.labels['currentBlock'][
                    'low_boundary']:
                colorEachCond = 5 * [
                    'g', 'r'
                ]  #assume there are not more than 5 blocks
            else:
                colorEachCond = 5 * ['r', 'g']

        else:
            currentBlock = bdata['currentBlock']
            blockTypes = [
                bdata.labels['currentBlock']['low_boundary'],
                bdata.labels['currentBlock']['high_boundary']
            ]
            trialsEachType = behavioranalysis.find_trials_each_type(
                currentBlock, blockTypes)
            midFreqCorrectBlockLow = correctOneFreq & trialsEachType[:, 0]
            midFreqCorrectBlockHigh = correctOneFreq & trialsEachType[:, 1]
            trialsEachCond = np.c_[midFreqCorrectBlockLow,
                                   midFreqCorrectBlockHigh]
            colorEachCond = ['g', 'r']

    # -- When plotting all 3 frequencies will not be plotting by block, just plot by type of block (low_boundary vs high_boundary) -- #
    elif freqToPlot == 'all':
        assert byBlock == False  #when plotting all frequencies will not be plotting by block
        lowFreq = possibleFreq[0]
        middleFreq = possibleFreq[1]
        highFreq = possibleFreq[2]
        leftward = bdata['choice'] == bdata.labels['choice']['left']
        rightward = bdata['choice'] == bdata.labels['choice']['right']

        trialsToUseLowFreq = ((bdata['targetFrequency'] == lowFreq) & correct
                              )  #low_boundary block
        trialsToUseHighFreq = ((bdata['targetFrequency'] == highFreq) & correct
                               )  #high_boundary block
        trialsToUseMidFreqLeft = leftward & (
            bdata['targetFrequency'] == middleFreq
        )  #mid freq correct trials in high_boundary block
        trialsToUseMidFreqRight = rightward & (
            bdata['targetFrequency'] == middleFreq
        )  #mid freq correct trials in low_boundary block
        trialsEachCond = np.c_[trialsToUseLowFreq, trialsToUseMidFreqLeft,
                               trialsToUseMidFreqRight, trialsToUseHighFreq]
        colorEachCond = ['y', 'r', 'g', 'b']

    return bdata, trialsEachCond, colorEachCond
def plot_rew_change_per_cell(oneCell,trialLimit=[],alignment='sound'):
    '''
    Plots raster and PSTH for one cell during reward_change_freq_dis task, split by block; alignment parameter should be set to either 'sound', 'center-out', or 'side-in'.
    '''    
    bdata = load_behav_per_cell(oneCell)
    (spikeTimestamps,waveforms,eventOnsetTimes,eventData) = load_ephys_per_cell(oneCell)

    # -- Check to see if ephys has skipped trials, if so remove trials from behav data 
    soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
    soundOnsetTimeEphys = eventOnsetTimes[soundOnsetEvents]
    soundOnsetTimeBehav = bdata['timeTarget']

    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(soundOnsetTimeEphys,soundOnsetTimeBehav)
    # Remove missing trials
    bdata.remove_trials(missingTrials)

    currentBlock = bdata['currentBlock']
    blockTypes = [bdata.labels['currentBlock']['same_reward'],bdata.labels['currentBlock']['more_left'],bdata.labels['currentBlock']['more_right']]
    #blockLabels = ['more_left', 'more_right']
    if(not len(trialLimit)):
        validTrials = np.ones(len(currentBlock),dtype=bool)
    else:
        validTrials = np.zeros(len(currentBlock),dtype=bool)
        validTrials[trialLimit[0]:trialLimit[1]] = 1

    trialsEachType = behavioranalysis.find_trials_each_type(currentBlock,blockTypes)
    
        
    if alignment == 'sound':
        soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
        EventOnsetTimes = eventOnsetTimes[soundOnsetEvents]
    elif alignment == 'center-out':
        soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
        EventOnsetTimes = eventOnsetTimes[soundOnsetEvents]
        diffTimes=bdata['timeCenterOut']-bdata['timeTarget']
        EventOnsetTimes+=diffTimes
    elif alignment == 'side-in':
        soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
        EventOnsetTimes = eventOnsetTimes[soundOnsetEvents]
        diffTimes=bdata['timeSideIn']-bdata['timeTarget']
        EventOnsetTimes+=diffTimes

    freqEachTrial = bdata['targetFrequency']
    possibleFreq = np.unique(freqEachTrial)
    
    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'] 
    incorrect = bdata['outcome']==bdata.labels['outcome']['error']  

    ######Split left and right trials into correct and  incorrect categories to look at error trials#########
    rightcorrect = rightward&correct&validTrials
    leftcorrect = leftward&correct&validTrials
    #righterror = rightward&incorrect&validTrials
    #lefterror = leftward&incorrect&validTrials

    rightcorrectBlockSameReward = rightcorrect&trialsEachType[:,0]
    rightcorrectBlockMoreLeft = rightcorrect&trialsEachType[:,1] 
    rightcorrectBlockMoreRight = rightcorrect&trialsEachType[:,2]
    leftcorrectBlockSameReward = leftcorrect&trialsEachType[:,0]
    leftcorrectBlockMoreLeft = leftcorrect&trialsEachType[:,1]
    leftcorrectBlockMoreRight = leftcorrect&trialsEachType[:,2]

    trialsEachCond = np.c_[leftcorrectBlockMoreLeft,rightcorrectBlockMoreLeft,leftcorrectBlockMoreRight,rightcorrectBlockMoreRight,leftcorrectBlockSameReward,rightcorrectBlockSameReward] 


    colorEachCond = ['g','r','m','b','y','darkgray']
    #trialsEachCond = np.c_[invalid,leftcorrect,rightcorrect,lefterror,righterror] 
    #colorEachCond = ['0.75','g','r','b','m'] 

    (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
spikesanalysis.eventlocked_spiketimes(spikeTimestamps,EventOnsetTimes,timeRange)
    
    ###########Plot raster and PSTH#################
    plt.figure()
    ax1 = plt.subplot2grid((8,5), (0, 0), rowspan=4,colspan=5)
    extraplots.raster_plot(spikeTimesFromEventOnset,indexLimitsEachTrial,timeRange,trialsEachCond=trialsEachCond,colorEachCond=colorEachCond,fillWidth=None,labels=None)
    plt.ylabel('Trials')
    plt.xlim(timeRange)

    plt.title('{0}_{1}_TT{2}_c{3}_{4}'.format(oneCell.animalName,oneCell.behavSession,oneCell.tetrode,oneCell.cluster,alignment))

    timeVec = np.arange(timeRange[0],timeRange[-1],binWidth)
    spikeCountMat = spikesanalysis.spiketimes_to_spikecounts(spikeTimesFromEventOnset,indexLimitsEachTrial,timeVec)
    smoothWinSize = 3
    ax2 = plt.subplot2grid((8,5), (4, 0),colspan=5,rowspan=2,sharex=ax1)
    extraplots.plot_psth(spikeCountMat/binWidth,smoothWinSize,timeVec,trialsEachCond=trialsEachCond,colorEachCond=colorEachCond,linestyle=None,linewidth=3,downsamplefactor=1)
    plt.xlabel('Time from {0} onset (s)'.format(alignment))
    plt.ylabel('Firing rate (spk/sec)')
   
    # -- Plot ISI histogram --
    plt.subplot2grid((8,5), (6,0), rowspan=1, colspan=2)
    spikesorting.plot_isi_loghist(spikeTimestamps)
    plt.ylabel('c%d'%oneCell.cluster,rotation=0,va='center',ha='center')
    plt.xlabel('')

    # -- Plot waveforms --
    plt.subplot2grid((8,5), (7,0), rowspan=1, colspan=3)
    spikesorting.plot_waveforms(waveforms)

    # -- Plot projections --
    plt.subplot2grid((8,5), (6,2), rowspan=1, colspan=3)
    spikesorting.plot_projections(waveforms)

    # -- Plot events in time --
    plt.subplot2grid((8,5), (7,3), rowspan=1, colspan=2)
    spikesorting.plot_events_in_time(spikeTimestamps)

    plt.subplots_adjust(wspace = 0.7)
    
    #plt.show()
    #fig_path = 
    #fig_name = 'TT{0}Cluster{1}{2}.png'.format(tetrode, cluster, '_2afc plot_each_type')
    #full_fig_path = os.path.join(fig_path, fig_name)
    #print full_fig_path
    plt.gcf().set_size_inches((8.5,11))
def plot_rew_change_byblock_per_cell(oneCell,trialLimit=[],alignment='sound',choiceSide='both'):
    '''
    Plots ephys data during behavior (reward_change_freq_discrim paradigm), data split according to the block in behavior and the choice (left or right). only plotting correct trials.
    oneCell is an CellInfo object as in celldatabase.
    'trialLimit' (e.g. [0, 600]) is the indecies of trials wish to be plotted.
    'choiceSide' is a string, either 'left' or 'right', to plot leftward and rightward trials, respectively. If not provided, will plot both sides.
    'alignment' selects which event to align spike data to, should be 'sound', 'center-out', or 'side-in'.
    '''
    
    SAMPLING_RATE=30000.0
    soundTriggerChannel = 0 # channel 0 is the sound presentation, 1 is the trial
    binWidth = 0.010 # Size of each bin in histogram in seconds

    #timeRange = [-0.2,0.8] # In seconds. Time range for rastor plot to plot spikes (around some event onset as 0)
    #timeRange = [-0.25,1.0]
    timeRange = [-0.4,1.2]
    
    bdata = load_behav_per_cell(oneCell)
    (spikeTimestamps,waveforms,eventOnsetTimes,eventData)=load_ephys_per_cell(oneCell)

    # -- Check to see if ephys has skipped trials, if so remove trials from behav data 
    soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
    soundOnsetTimeEphys = eventOnsetTimes[soundOnsetEvents]
    soundOnsetTimeBehav = bdata['timeTarget']

    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(soundOnsetTimeEphys,soundOnsetTimeBehav)
    # Remove missing trials
    bdata.remove_trials(missingTrials)
                
    currentBlock = bdata['currentBlock']
    
    if(not len(trialLimit)):
        validTrials = np.ones(len(currentBlock),dtype=bool)
    else:
        validTrials = np.zeros(len(currentBlock),dtype=bool)
        validTrials[trialLimit[0]:trialLimit[1]] = 1

    bdata.find_trials_each_block()
    trialsEachBlock = bdata.blocks['trialsEachBlock']
    #print trialsEachBlock
    nBlocks = bdata.blocks['nBlocks']

    #blockLabels = ['more_left', 'more_right']
    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'] 
    incorrect = bdata['outcome']==bdata.labels['outcome']['error'] 
    ######Split left and right trials into correct and  incorrect categories to look at error trials#########
    rightcorrect = rightward&correct&validTrials
    leftcorrect = leftward&correct&validTrials
    #righterror = rightward&incorrect&validTrials
    #lefterror = leftward&incorrect&validTrials
    colorEachCond=[]
    
    ####construct trialsEachCond and colorEachCond for ploting####
    for block in range(nBlocks):
        rightcorrectThisBlock = rightcorrect&trialsEachBlock[:,block]
        leftcorrectThisBlock = leftcorrect&trialsEachBlock[:,block]
        #trialTypeVec = leftcorrect*1+rightcorrect*2
        #trialTypePossibleValues = [1,2] #1 stands for left correct, 2 stands for right correct

        firstIndexThisBlock=np.nonzero(trialsEachBlock[:,block])[0][0]
        if currentBlock[firstIndexThisBlock]==bdata.labels['currentBlock']['more_left']:
            if choiceSide=='right':
                colorThisCond='r'
            elif choiceSide=='left':
                colorThisCond='g'
            elif choiceSide=='both':
                colorThisCond=['g','r']
        if currentBlock[firstIndexThisBlock]==bdata.labels['currentBlock']['more_right']:
            if choiceSide=='right':
                colorThisCond='b'
            elif choiceSide=='left':
                colorThisCond='m'
            elif choiceSide=='both':
                colorThisCond=['m','b']
        if currentBlock[firstIndexThisBlock]==bdata.labels['currentBlock']['same_reward']:
            if choiceSide=='right':
                colorThisCond='darkgray'
            elif choiceSide=='left':
                colorThisCond='y'
            elif choiceSide=='both':
                colorThisCond=['y','darkgray']

        #trialsEachTypeEachBlock = behavioranalysis.find_trials_each_type_each_block(trialTypeVec, trialTypePossibleValues,currentBlock,blockTypes)
        
        if block==0:
            #trialsEachCond=np.c_[leftcorrectThisBlock,rightcorrectThisBlock] 
            if choiceSide=='right':
                trialsEachCond=np.c_[rightcorrectThisBlock]
            elif choiceSide=='left':
                trialsEachCond=np.c_[leftcorrectThisBlock]              
            elif choiceSide=='both':
                trialsEachCond=np.c_[leftcorrectThisBlock,rightcorrectThisBlock]

        else:
            if choiceSide=='right':
                trialsEachCond=np.c_[trialsEachCond,rightcorrectThisBlock]
            elif choiceSide=='left':
                trialsEachCond=np.c_[trialsEachCond,leftcorrectThisBlock]              
            elif choiceSide=='both':
                trialsEachCond=np.c_[trialsEachCond,leftcorrectThisBlock,rightcorrectThisBlock]

        colorEachCond.append(colorThisCond)


    if alignment == 'sound':
        soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
        EventOnsetTimes = eventOnsetTimes[soundOnsetEvents]
    elif alignment == 'center-out':
        soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
        EventOnsetTimes = eventOnsetTimes[soundOnsetEvents]
        diffTimes=bdata['timeCenterOut']-bdata['timeTarget']
        EventOnsetTimes+=diffTimes
    elif alignment == 'side-in':
        soundOnsetEvents = (eventData.eventID==1) & (eventData.eventChannel==soundTriggerChannel)
        EventOnsetTimes = eventOnsetTimes[soundOnsetEvents]
        diffTimes=bdata['timeSideIn']-bdata['timeTarget']
        EventOnsetTimes+=diffTimes
            
    freqEachTrial = bdata['targetFrequency']
    possibleFreq = np.unique(freqEachTrial)
            
    (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
    spikesanalysis.eventlocked_spiketimes(spikeTimestamps,EventOnsetTimes,timeRange)

    plt.figure()
    ###########Plot raster and PSTH#################
    ax1 =  plt.subplot2grid((3,1), (0, 0), rowspan=2)
    pRaster,hcond,zline =extraplots.raster_plot(spikeTimesFromEventOnset,indexLimitsEachTrial,timeRange,trialsEachCond=trialsEachCond,
                       colorEachCond=colorEachCond,fillWidth=None,labels=None)
    #plt.setp(pRaster, ms=0.8)
    plt.ylabel('Trials')
    plt.xlim(timeRange)
    fig_title='{0}_{1}_TT{2}_c{3}_{4}_{5}'.format(oneCell.animalName,oneCell.behavSession,oneCell.tetrode,oneCell.cluster,alignment,choiceSide)
    plt.title(fig_title)

    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=1.5,downsamplefactor=1)

    plt.xlabel('Time from sound onset (s)')
    plt.ylabel('Firing rate (spk/sec)')
    #plt.show()
    
    #fig_path= 
    #full_fig_path = os.path.join(fig_path, fig_title)
    #print full_fig_path
    #plt.tight_layout()
    plt.gcf().set_size_inches((8.5,11))
def evaluate_movement_selectivity_celldb(cellDb,
                                         movementTimeRange,
                                         removeSideIn=False):
    '''
    Analyse 2afc data: calculate movement selectivity index and significance;
    movementTimeRange should be a list (in seconds, time from center-out).
    '''
    print 'Calculating movement selectivity index for 2afc session in time range: ' + str(
        movementTimeRange)

    movementModI = pd.Series(np.zeros(len(cellDb)),
                             index=cellDb.index)  #default value 0
    movementModS = pd.Series(np.ones(len(cellDb)),
                             index=cellDb.index)  #default value 1

    for indCell, cell in cellDb.iterrows():
        cellObj = ephyscore.Cell(cell)
        sessiontype = 'behavior'  #2afc behavior
        #ephysData, bata = cellObj.load(sessiontype)
        sessionInd = cellObj.get_session_inds(sessiontype)[0]
        bdata = cellObj.load_behavior_by_index(sessionInd)
        possibleFreq = np.unique(bdata['targetFrequency'])
        numFreqs = len(possibleFreq)

        try:
            ephysData = cellObj.load_ephys_by_index(sessionInd)
        except ValueError:
            continue

        eventsDict = ephysData['events']
        spikeTimestamps = ephysData['spikeTimes']

        if spikeTimestamps.ndim == 0:  #There is only one spike, ! spikesanalysis.eventlocked_spiketimes cannot handle only one spike !
            continue

        soundOnsetTimes = eventsDict['{}On'.format(soundChannelType)]
        soundOnsetTimeBehav = bdata['timeTarget']

        # -- Check to see if ephys and behav recordings have same number of trials, remove missing trials from behav file -- #
        # Find missing trials
        missingTrials = behavioranalysis.find_missing_trials(
            soundOnsetTimes, soundOnsetTimeBehav)
        # Remove missing trials
        bdata.remove_trials(missingTrials)

        if len(soundOnsetTimes) != len(
                bdata['timeTarget']
        ):  #some error not handled by remove missing trials
            continue

        # -- calculate movement selectivity -- #
        rightward = bdata['choice'] == bdata.labels['choice']['right']
        leftward = bdata['choice'] == bdata.labels['choice']['left']

        diffTimes = bdata['timeCenterOut'] - bdata['timeTarget']
        movementOnsetTimes = soundOnsetTimes + diffTimes

        responseTimesEachTrial = bdata['timeSideIn'] - bdata['timeCenterOut']
        responseTimesEachTrial[np.isnan(responseTimesEachTrial)] = 0
        sideInTrials = (responseTimesEachTrial <= movementTimeRange[-1])

        if removeSideIn:
            trialsToUseRight = rightward & (~sideInTrials)
            trialsToUseLeft = leftward & (~sideInTrials)
        else:
            trialsToUseRight = rightward
            trialsToUseLeft = leftward
        trialsEachCond = [trialsToUseLeft, trialsToUseRight]
        (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
                spikesanalysis.eventlocked_spiketimes(spikeTimestamps,movementOnsetTimes,timeRange)

        spikeCountMat = spikesanalysis.spiketimes_to_spikecounts(
            spikeTimesFromEventOnset, indexLimitsEachTrial, movementTimeRange)

        spikeCountEachTrial = spikeCountMat.flatten()
        spikeAvgLeftward = np.mean(spikeCountEachTrial[leftward])
        spikeAvgRightward = np.mean(spikeCountEachTrial[rightward])

        if ((spikeAvgRightward + spikeAvgLeftward) == 0):
            movementModI[indCell] = 0.0
            movementModS[indCell] = 1.0

        else:
            movementModI[indCell] = ((spikeAvgRightward - spikeAvgLeftward) /
                                     (spikeAvgRightward + spikeAvgLeftward))
            movementModS[indCell] = spikesanalysis.evaluate_modulation(
                spikeTimesFromEventOnset, indexLimitsEachTrial,
                movementTimeRange, trialsEachCond)[1]

    return movementModI, movementModS
                bdata = loadbehavior.BehaviorData(behaviorFilename)
                soundOnsetTimeBehav = bdata['timeTarget']

                print 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)
                soundOnsetTimeEphys = eventTimes[soundOnsetEvents]
                ######check if ephys and behav miss-aligned, if so, remove skipped trials####

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

                # Remove missing trials,all fields of bdata's results are modified after this
                bdata.remove_trials(missingTrials)
                print 'behav length',len(soundOnsetTimeBehav),'ephys length',len(soundOnsetTimeEphys)

                ######do the analysis based on what events to align spike data to#####
                if alignment == 'sound':
                    EventOnsetTimes = eventTimes[soundOnsetEvents]
                elif alignment == 'center-out':
                    EventOnsetTimes = eventTimes[soundOnsetEvents]
                    diffTimes=bdata['timeCenterOut']-bdata['timeTarget']
                    EventOnsetTimes+=diffTimes
                elif alignment == 'side-in':
                    EventOnsetTimes = eventTimes[soundOnsetEvents]
                    diffTimes=bdata['timeSideIn']-bdata['timeTarget']
Esempio n. 12
0
def main():
    global behavSession
    global subject
    global ephysSession
    global tetrode
    global cluster
    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=subject, paradigm=paradigm, sessionstr=behavSession)
            bdata = loadbehavior.BehaviorData(behaviorFilename)
            numberOfTrials = len(bdata['choice'])
            #print 'numberTrials ',numberOfTrials############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            # -- 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)
            #print 'missing ',len(missingTrials)############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            # 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
            #print 'timeDiff ',len(timeDiff)############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            #print 'eventOnsetTimes ',len(eventOnsetTimes)############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            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((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)
        plt.setp(ax4.get_xticklabels(), visible=False)
        raster_sound_psycurve(centerFrequencies[0])
        ax5 = plt.subplot2grid((7, 6), (2, 0), colspan=3, sharex=ax4)
        hist_sound_psycurve(centerFrequencies[0])
        ax6 = plt.subplot2grid((7, 6), (0, 3), colspan=3, rowspan=2)
        plt.setp(ax6.get_xticklabels(), visible=False)
        raster_sound_psycurve(centerFrequencies[1])
        ax7 = plt.subplot2grid((7, 6), (2, 3), colspan=3, sharex=ax6)
        hist_sound_psycurve(centerFrequencies[1])

        ax8 = plt.subplot2grid((7, 6), (3, 0), colspan=3, rowspan=2)
        plt.setp(ax8.get_xticklabels(), visible=False)
        raster_movement_psycurve(centerFrequencies[0])
        ax9 = plt.subplot2grid((7, 6), (5, 0), colspan=3, sharex=ax8)
        hist_movement_psycurve(centerFrequencies[0])
        ax10 = plt.subplot2grid((7, 6), (3, 3), colspan=3, rowspan=2)
        plt.setp(ax10.get_xticklabels(), visible=False)
        raster_movement_psycurve(centerFrequencies[1])
        ax11 = plt.subplot2grid((7, 6), (5, 3), colspan=3, sharex=ax10)
        hist_movement_psycurve(centerFrequencies[1])
        ###############################################################################
        #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 = '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
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

    print 'psycurve_tuning_report'

    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'])
                #print 'numberTrials ',numberOfTrials############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                # -- 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)
                #print 'missing ',len(missingTrials)############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                # 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
                #print 'timeDiff ',len(timeDiff)############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                #print 'eventOnsetTimes ',len(eventOnsetTimes)############################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                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/2), 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/2)), colspan = (numCols/2), rowspan = sizeRasters)
            plt.setp(ax6.get_xticklabels(), visible=False)
            plt.setp(ax6.get_yticklabels(), visible=False)
            raster_sound_psycurve(centerFrequencies[0])
            plt.title('Frequency Top: '+str(possibleFreq[centerFrequencies[0]])+' Bottom: '+str(possibleFreq[centerFrequencies[1]]))
            
            ax7 = plt.subplot2grid((numRows,numCols), (sizeRasters,(numCols/2)), colspan = (numCols/2), rowspan = sizeHists, sharex=ax6)
            hist_sound_psycurve(centerFrequencies[0])
            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((7,6), (3,0), colspan = 3, rowspan = 2)
            #plt.setp(ax8.get_xticklabels(), visible=False)
            #raster_movement_psycurve(centerFrequencies[0])
            #ax9 = plt.subplot2grid((7,6), (5,0), colspan = 3, sharex=ax8)
            #hist_movement_psycurve(centerFrequencies[0])

            ax10 = plt.subplot2grid((numRows,numCols), ((sizeRasters+sizeHists),(numCols/2)), colspan = (numCols/2), rowspan = sizeRasters)
            plt.setp(ax10.get_xticklabels(), visible=False)
            plt.setp(ax10.get_yticklabels(), visible=False)
            raster_sound_psycurve(centerFrequencies[1])

            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 = '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
def main():
    global behavSession
    global subject
    global tetrode
    global cluster
    global tuningBehavior  #behavior file name of tuning curve
    global tuningEphys  #ephys session name of tuning curve
    global bdata
    global eventOnsetTimes
    global spikeTimesFromEventOnset
    global indexLimitsEachTrial
    global spikeTimesFromMovementOnset
    global indexLimitsEachMovementTrial
    global titleText

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

                subject = oneCell.animalName
                behavSession = oneCell.behavSession
                ephysSession = oneCell.ephysSession
                tuningSession = oneCell.tuningSession
                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.FlexCategBehaviorData(behaviorFilename)
                #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)
                bdata.find_trials_each_block()

                ###############################################################################################
                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
                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(spkTimeStamps)
                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 / 2),
                                   rowspan=3 * sizeRasters)
            #plt.setp(ax4.get_xticklabels(), visible=False)
            #fig.axes.get_xaxis().set_visible(False)
            raster_tuning(ax4)
            axvline(x=0, ymin=0, ymax=1, color='r')
            plt.gca().set_xlim(tuning_timeRange)

            ax6 = plt.subplot2grid((numRows, numCols), (0, (numCols / 2)),
                                   colspan=(numCols / 2),
                                   rowspan=sizeRasters)
            plt.setp(ax6.get_xticklabels(), visible=False)
            plt.setp(ax6.get_yticklabels(), visible=False)
            raster_sound_block_switching()
            plt.title(
                'sound aligned, Top: middle freq in blocks, Bottom: all freqs')

            ax7 = plt.subplot2grid((numRows, numCols),
                                   (sizeRasters, (numCols / 2)),
                                   colspan=(numCols / 2),
                                   rowspan=sizeHists,
                                   sharex=ax6)
            hist_sound_block_switching(ax7)
            #plt.setp(ax7.get_yticklabels(), visible=False)
            ax7.yaxis.tick_right()
            ax7.yaxis.set_ticks_position('both')
            plt.setp(ax7.get_xticklabels(), visible=False)
            plt.gca().set_xlim(timeRange)

            ax10 = plt.subplot2grid((numRows, numCols),
                                    ((sizeRasters + sizeHists), (numCols / 2)),
                                    colspan=(numCols / 2),
                                    rowspan=sizeRasters)
            plt.setp(ax10.get_xticklabels(), visible=False)
            plt.setp(ax10.get_yticklabels(), visible=False)
            raster_sound_allFreq_switching()

            ax11 = plt.subplot2grid(
                (numRows, numCols),
                ((2 * sizeRasters + sizeHists), (numCols / 2)),
                colspan=(numCols / 2),
                rowspan=sizeHists,
                sharex=ax10)
            hist_sound_allFreq_switching(ax11)
            ax11.yaxis.tick_right()
            ax11.yaxis.set_ticks_position('both')
            ax11.set_xlabel('Time (sec)')
            #plt.setp(ax11.get_yticklabels(), visible=False)
            plt.gca().set_xlim(timeRange)

            ###############################################################################
            #plt.tight_layout()
            modulation_index_switching()
            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):  #makes sure output folder exists
                os.makedirs(directory)
            #print 'saving figure to %s'%fullFileName
            plt.gcf().savefig(fullFileName, format=figformat)

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

    print 'error with sessions: '
    for badSes in badSessionList:
        print badSes
Esempio n. 15
0
def main():
    global behavSession
    global subject
    global tetrode
    global cluster
    global bdata
    global eventOnsetTimes
    global spikeTimesFromEventOnset
    global indexLimitsEachTrial
    global spikeTimesFromMovementOnset
    global indexLimitsEachMovementTrial
    global titleText

    print "SwitchingReport"
    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.FlexCategBehaviorData(behaviorFilename)
                #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)
                bdata.find_trials_each_block()
                

                ###############################################################################################
                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
                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(spkTimeStamps)
                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/2), rowspan = sizeRasters)
            plt.setp(ax4.get_xticklabels(), visible=False)
            raster_sound_block_switching()
            ax5 = plt.subplot2grid((numRows,numCols), (sizeRasters,0), colspan = (numCols/2), rowspan = sizeHists, sharex=ax4)
            hist_sound_block_switching()
            ax6 = plt.subplot2grid((numRows,numCols), (0,(numCols/2)), colspan = (numCols/2), rowspan = sizeRasters)
            plt.setp(ax6.get_xticklabels(), visible=False)
            raster_movement_block_switching()
            ax7 = plt.subplot2grid((numRows,numCols), (sizeRasters,(numCols/2)), colspan = (numCols/2), rowspan = sizeHists, sharex=ax6)
            hist_movement_block_switching()

            ax8 = plt.subplot2grid((numRows,numCols), ((sizeRasters+sizeHists),0), colspan = (numCols/2), rowspan = sizeRasters)
            plt.setp(ax8.get_xticklabels(), visible=False)
            raster_sound_allFreq_switching()     
            ax9 = plt.subplot2grid((numRows,numCols), ((2*sizeRasters+sizeHists),0), colspan = (numCols/2), rowspan = sizeHists, sharex=ax8)
            hist_sound_allFreq_switching()
            ax10 = plt.subplot2grid((numRows,numCols), ((sizeRasters+sizeHists),(numCols/2)), colspan = (numCols/2), rowspan = sizeRasters) 
            plt.setp(ax10.get_xticklabels(), visible=False)
            raster_sound_switching()
            ax11 = plt.subplot2grid((numRows,numCols), ((2*sizeRasters+sizeHists),(numCols/2)), colspan = (numCols/2), rowspan = sizeHists, sharex=ax10) 
            hist_sound_switching()
            ###############################################################################
            #plt.tight_layout()
            modulation_index_switching()
            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 = 'report_%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): #makes sure output folder exists
                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
def evaluate_movement_zScore_celldb(cellDb,
                                    baselineAlignment='sound',
                                    baselineTimeRange=[-0.1, 0],
                                    movementAlignment='center-out',
                                    movementTimeRange=[0, 0.3],
                                    removeSideIn=True):
    '''
    Analyse 2afc data: compare spike count during movement and baseline, compute z score and p value.
    baselineTimeRange and movementTimeRange should be lists (in seconds, time from center-out).
    '''
    print 'Calculating movement vs baseline z score and p value in time range: ' + str(
        movementTimeRange)

    movementZscore = np.zeros((len(cellDb), 2))  #default value 0
    movementPval = np.ones((len(cellDb), 2))  #default value 1
    baselineAveFr = np.zeros((len(cellDb), 2))
    movementAveFr = np.zeros((len(cellDb), 2))

    for indCell, cell in cellDb.iterrows():
        cellObj = ephyscore.Cell(cell)
        sessiontype = 'behavior'  #2afc behavior
        subject = cell.subject
        date = cell.date
        depth = cell.depth
        tetrode = cell.tetrode
        cluster = cell.cluster
        #ephysData, bata = cellObj.load(sessiontype)
        sessionInd = cellObj.get_session_inds(sessiontype)[0]
        bdata = cellObj.load_behavior_by_index(sessionInd)
        possibleFreq = np.unique(bdata['targetFrequency'])
        numFreqs = len(possibleFreq)

        try:
            ephysData = cellObj.load_ephys_by_index(sessionInd)
        except ValueError:
            continue

        eventsDict = ephysData['events']
        spikeTimestamps = ephysData['spikeTimes']

        if spikeTimestamps.ndim == 0:  #There is only one spike, ! spikesanalysis.eventlocked_spiketimes cannot handle only one spike !
            continue

        soundOnsetTimes = eventsDict['{}On'.format(soundChannelType)]
        soundOnsetTimeBehav = bdata['timeTarget']

        # -- Check to see if ephys and behav recordings have same number of trials, remove missing trials from behav file -- #
        # Find missing trials
        missingTrials = behavioranalysis.find_missing_trials(
            soundOnsetTimes, soundOnsetTimeBehav)
        # Remove missing trials
        bdata.remove_trials(missingTrials)

        if len(soundOnsetTimes) != len(
                bdata['timeTarget']
        ):  #some error not handled by remove missing trials
            continue

        # -- calculate movement selectivity -- #
        rightward = bdata['choice'] == bdata.labels['choice']['right']
        leftward = bdata['choice'] == bdata.labels['choice']['left']

        diffTimes = bdata['timeCenterOut'] - bdata['timeTarget']
        movementOnsetTimes = soundOnsetTimes + diffTimes

        responseTimesEachTrial = bdata['timeSideIn'] - bdata['timeCenterOut']
        responseTimesEachTrial[np.isnan(responseTimesEachTrial)] = 0
        sideInTrials = (responseTimesEachTrial <= movementTimeRange[-1])

        if removeSideIn:
            trialsToUseRight = rightward & (~sideInTrials)
            trialsToUseLeft = leftward & (~sideInTrials)
        else:
            trialsToUseRight = rightward
            trialsToUseLeft = leftward
        trialsEachCond = [trialsToUseLeft, trialsToUseRight]

        evlockFileCOut = '{0}_{1}_{2}_T{3}_c{4}_{5}.npz'.format(
            subject, date, depth, tetrode, cluster, movementAlignment)
        evlockFileCOutPath = os.path.join(ephysDir, STUDY_NAME,
                                          evlockFileFolder, evlockFileCOut)
        evlockDataCOut = np.load(evlockFileCOutPath)
        spikeTimesFromEventOnsetCOut = evlockDataCOut[
            'spikeTimesFromEventOnset']
        trialIndexForEachSpikeCOut = evlockDataCOut['trialIndexForEachSpike']
        indexLimitsEachTrialCOut = evlockDataCOut['indexLimitsEachTrial']
        spikeCountMatMovement = spikesanalysis.spiketimes_to_spikecounts(
            spikeTimesFromEventOnsetCOut, indexLimitsEachTrialCOut,
            movementTimeRange)
        spikeCountEachTrialMovement = spikeCountMatMovement.flatten()
        spikeCountLeftwardMovement = spikeCountEachTrialMovement[leftward]
        spikeCountRightwardMovement = spikeCountEachTrialMovement[rightward]
        movementTime = np.abs(movementTimeRange[0] - movementTimeRange[1])
        firingRateLeftwardEachTrialMovement = spikeCountLeftwardMovement / movementTime
        firingRateRightwardEachTrialMovement = spikeCountRightwardMovement / movementTime
        aveFrLeftwardMovement = np.mean(firingRateLeftwardEachTrialMovement)
        aveFrRightwardMovement = np.mean(firingRateRightwardEachTrialMovement)

        evlockFileSound = '{0}_{1}_{2}_T{3}_c{4}_{5}.npz'.format(
            subject, date, depth, tetrode, cluster, baselineAlignment)
        evlockFileSoundPath = os.path.join(ephysDir, STUDY_NAME,
                                           evlockFileFolder, evlockFileSound)
        evlockDataSound = np.load(evlockFileSoundPath)
        spikeTimesFromEventOnsetSound = evlockDataSound[
            'spikeTimesFromEventOnset']
        trialIndexForEachSpikeSound = evlockDataSound['trialIndexForEachSpike']
        indexLimitsEachTrialSound = evlockDataSound['indexLimitsEachTrial']
        spikeCountMatBaseline = spikesanalysis.spiketimes_to_spikecounts(
            spikeTimesFromEventOnsetSound, indexLimitsEachTrialSound,
            baselineTimeRange)
        spikeCountEachTrialBaseline = spikeCountMatBaseline.flatten()
        spikeCountLeftwardBaseline = spikeCountEachTrialBaseline[leftward]
        spikeCountRightwardBaseline = spikeCountEachTrialBaseline[rightward]
        baselineTime = np.abs(baselineTimeRange[0] - baselineTimeRange[1])
        firingRateLeftwardEachTrialBaseline = spikeCountLeftwardBaseline / baselineTime
        firingRateRightwardEachTrialBaseline = spikeCountRightwardBaseline / baselineTime
        aveFrLeftwardBaseline = np.mean(firingRateLeftwardEachTrialBaseline)
        aveFrRightwardBaseline = np.mean(firingRateRightwardEachTrialBaseline)

        zScore, pValue = stats.ranksums(firingRateLeftwardEachTrialBaseline,
                                        firingRateLeftwardEachTrialMovement)
        movementZscore[indCell, 0] = zScore
        movementPval[indCell, 0] = pValue

        zScore, pValue = stats.ranksums(firingRateRightwardEachTrialBaseline,
                                        firingRateRightwardEachTrialMovement)
        movementZscore[indCell, 1] = zScore
        movementPval[indCell, 1] = pValue

        baselineAveFr[indCell, 0] = aveFrLeftwardBaseline
        baselineAveFr[indCell, 1] = aveFrRightwardBaseline
        movementAveFr[indCell, 0] = aveFrLeftwardMovement
        movementAveFr[indCell, 1] = aveFrRightwardMovement

    return movementZscore, movementPval, baselineAveFr, movementAveFr
def get_trials_each_cond_reward_change(oneCell,
                                       freqToPlot='left',
                                       byBlock=True):
    '''
    Given a cellInfo object, the frequency to plot(string, value of 'left','right', or 'both'), whether to plot by block (boolean), gets the trialsEachCond and colorEachCond vectors for raster and PSTH plotting.
    '''
    # -- Calls loading functions to get the ephys and behav data -- #
    (spikeTimestamps, waveforms, eventOnsetTimes,
     eventData) = load_ephys_per_cell(oneCell)
    bdata = load_behav_per_cell(oneCell)

    # -- Check to see if ephys has skipped trials, if so remove trials from behav data -- #
    eventOnsetTimes = np.array(eventData.timestamps)
    soundOnsetEvents = (eventData.eventID == 1) & (eventData.eventChannel
                                                   == soundTriggerChannel)
    soundOnsetTimeEphys = eventOnsetTimes[soundOnsetEvents]
    soundOnsetTimeBehav = bdata['timeTarget']

    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(
        soundOnsetTimeEphys, soundOnsetTimeBehav)
    # Remove missing trials
    bdata.remove_trials(missingTrials)

    # -- Select trials to plot from behavior file -- #
    correct = bdata['outcome'] == bdata.labels['outcome']['correct']
    possibleFreq = np.unique(bdata['targetFrequency'])
    numFreqs = len(possibleFreq)
    colorCondDict = {
        'leftMoreLeftFreq': 'g',
        'rightMoreLeftFreq': 'm',
        'sameRewardLeftFreq': 'y',
        'leftMoreRightFreq': 'r',
        'rightMoreRightFreq': 'b',
        'sameRewardRightFreq': 'darkgrey'
    }
    # -- Select trials to plot based on desired frequencies to plot and whether to plot by block -- #
    ### Recordings during reward change usually have 2 frequencies, low freq means go to left, right freq means go to right ###
    if freqToPlot != 'both':
        if freqToPlot == 'left':
            freq = possibleFreq[0]

        elif freqToPlot == 'right':
            freq = possibleFreq[1]

        oneFreq = bdata[
            'targetFrequency'] == freq  #vector for selecing trials presenting this frequency
        correctOneFreq = oneFreq & correct

        # -- Find trials each block (if plotting mid frequency by block) or find trials each type (e.g. low-boundary, high-boundary; if not plotting by block) -- #
        if byBlock:
            bdata.find_trials_each_block()
            trialsEachBlock = bdata.blocks['trialsEachBlock']
            correctTrialsEachBlock = trialsEachBlock & correctOneFreq[:, np.
                                                                      newaxis]
            correctBlockSizes = sum(correctTrialsEachBlock)
            if (correctBlockSizes[-1] < minBlockSize
                ):  #A check to see if last block is too small to plot
                correctTrialsEachBlock = correctTrialsEachBlock[:, :-1]

            trialsEachCond = correctTrialsEachBlock

            if bdata['automationMode'][0] == bdata.labels['automationMode'][
                    'same_left_right']:
                if freqToPlot == 'left':
                    colorEachCond = 3 * [
                        colorCondDict['sameRewardLeftFreq'],
                        colorCondDict['leftMoreLeftFreq'],
                        colorCondDict['rightMoreLeftFreq']
                    ]
                if freqToPlot == 'right':
                    colorEachCond = 3 * [
                        colorCondDict['sameRewardRightFreq'],
                        colorCondDict['leftMoreRightFreq'],
                        colorCondDict['rightMoreRightFreq']
                    ]
            elif bdata['automationMode'][0] == bdata.labels['automationMode'][
                    'same_right_left']:
                if freqToPlot == 'left':
                    colorEachCond = 3 * [
                        colorCondDict['sameRewardLeftFreq'],
                        colorCondDict['rightMoreLeftFreq'],
                        colorCondDict['leftMoreLeftFreq']
                    ]
                if freqToPlot == 'right':
                    colorEachCond = 3 * [
                        colorCondDict['sameRewardRightFreq'],
                        colorCondDict['rightMoreRightFreq'],
                        colorCondDict['leftMoreRightFreq']
                    ]
            elif bdata['automationMode'][0] == bdata.labels['automationMode'][
                    'left_right_left']:
                if freqToPlot == 'left':
                    colorEachCond = 3 * [
                        colorCondDict['leftMoreLeftFreq'],
                        colorCondDict['rightMoreLeftFreq']
                    ]
                if freqToPlot == 'right':
                    colorEachCond = 3 * [
                        colorCondDict['leftMoreRightFreq'],
                        colorCondDict['rightMoreRightFreq']
                    ]

        else:
            currentBlock = bdata['currentBlock']
            blockTypes = [
                bdata.labels['currentBlock']['same_reward'],
                bdata.labels['currentBlock']['more_left'],
                bdata.labels['currentBlock']['more_right']
            ]
            trialsEachType = behavioranalysis.find_trials_each_type(
                currentBlock, blockTypes)
            oneFreqCorrectBlockSameReward = correctOneFreq & trialsEachType[:,
                                                                            0]
            oneFreqCorrectBlockMoreLeft = correctOneFreq & trialsEachType[:, 1]
            oneFreqCorrectBlockMoreRight = correctOneFreq & trialsEachType[:,
                                                                           2]
            trialsEachCond = np.c_[oneFreqCorrectBlockSameReward,
                                   oneFreqCorrectBlockMoreLeft,
                                   oneFreqCorrectBlockMoreRight]
            if freqToPlot == 'left':
                colorEachCond = [
                    colorCondDict['sameRewardLeftFreq'],
                    colorCondDict['leftMoreLeftFreq'],
                    colorCondDict['rightMoreLeftFreq']
                ]
            elif freqToPlot == 'right':
                colorEachCond = [
                    colorCondDict['sameRewardRightFreq'],
                    colorCondDict['leftMoreRightFreq'],
                    colorCondDict['rightMoreRightFreq']
                ]

    # -- When plotting all 3 frequencies will not be plotting by block, just plot by type of block (low_boundary vs high_boundary) -- #
    elif freqToPlot == 'both':
        assert byBlock == False  #when plotting all frequencies will not be plotting by block
        leftFreq = possibleFreq[0]
        rightFreq = possibleFreq[1]
        currentBlock = bdata['currentBlock']
        blockTypes = [
            bdata.labels['currentBlock']['same_reward'],
            bdata.labels['currentBlock']['more_left'],
            bdata.labels['currentBlock']['more_right']
        ]
        trialsEachType = behavioranalysis.find_trials_each_type(
            currentBlock, blockTypes)

        correctLeftFreq = leftFreq & correct
        correctRightFreq = rightFreq & correct
        trialsToUseLeftFreqSameReward = correctLeftFreq & trialsEachType[:, 0]
        trialsToUseLeftFreqLeftMore = correctLeftFreq & trialsEachType[:, 1]
        trialsToUseLeftFreqRightMore = correctLeftFreq & trialsEachType[:, 2]
        trialsToUseRightFreqSameReward = correctRightFreq & trialsEachType[:,
                                                                           0]
        trialsToUseRightFreqLeftMore = correctRightFreq & trialsEachType[:, 1]
        trialsToUseRightFreqRightMore = correctRightFreq & trialsEachType[:, 2]

        trialsEachCond = np.c_[trialsToUseLeftFreqSameReward,
                               trialsToUseLeftFreqLeftMore,
                               trialsToUseLeftFreqRightMore,
                               trialsToUseRightFreqSameReward,
                               trialsToUseRightFreqLeftMore,
                               trialsToUseRightFreqRightMore]
        colorEachCond = [
            colorCondDict['sameRewardLeftFreq'],
            colorCondDict['leftMoreLeftFreq'],
            colorCondDict['rightMoreLeftFreq'],
            colorCondDict['sameRewardRightFreq'],
            colorCondDict['leftMoreRightFreq'],
            colorCondDict['rightMoreRightFreq']
        ]

    return bdata, trialsEachCond, colorEachCond
    spikeData.samples = (1000.0 / spikeData.gain[0, 0]) * spikeData.samples
    #spikeData = ephyscore.CellData(oneCell) #This defaults to settings ephys path
    spikeTimestamps = spikeData.timestamps

    # -- Check to see if ephys has skipped trials, if so remove trials from behav data -- #
    eventOnsetTimes = np.array(eventData.timestamps)
    soundOnsetEvents = (eventData.eventID == 1) & (eventData.eventChannel
                                                   == soundTriggerChannel)
    soundOnsetTimeEphys = eventOnsetTimes[soundOnsetEvents]
    soundOnsetTimeBehav = bdata['timeTarget']

    diffTimes = bdata['timeCenterOut'] - bdata['timeTarget']
    #movementOnsetTimeEphys = soundOnsetTimeEphys + diffTimes

    # Find missing trials
    missingTrials = behavioranalysis.find_missing_trials(
        soundOnsetTimeEphys, soundOnsetTimeBehav)
    # Remove missing trials
    bdata.remove_trials(missingTrials)

    # -- Calculate and store intermediate data for tuning raster -- #
    freqEachTrial = bdata['targetFrequency']
    valid = bdata['valid'].astype('bool')
    possibleFreq = np.unique(freqEachTrial)
    trialsEachFreq = behavioranalysis.find_trials_each_type(
        freqEachTrial, possibleFreq)
    trialsEachFreq = trialsEachFreq & valid[:, np.
                                            newaxis]  #Use only valid trials where sound is played in full

    (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
            spikesanalysis.eventlocked_spiketimes(spikeTimestamps,soundOnsetTimeEphys,timeRange)
Esempio n. 19
0
def evaluate_2afc_sound_response_celldb(cellDb):
    '''
    Analyse 2afc sound response: calculate sound response Z score for each freq, store frequencies presented and corresponding Z scores.
    '''
    if not ('behavZscore' in cellDb.columns):
        print 'Calculating sound response Z scores for 2afc session'

        behavDict = {
            'behavFreqs': [],
            'behavZscore': [],
            'behavPval': [],
            'behavRespIndex': [],
            'behavAveResp': []
        }
        #movementModI = np.zeros(len(cellDb)) #default value 0
        #movementModS = np.ones(len(cellDb)) #default value 1

        for indCell, cell in cellDb.iterrows():
            cellObj = ephyscore.Cell(cell)
            sessiontype = 'behavior'  #2afc behavior
            #ephysData, bata = cellObj.load(sessiontype)
            sessionInd = cellObj.get_session_inds(sessiontype)[0]
            bdata = cellObj.load_behavior_by_index(sessionInd)
            possibleFreq = np.unique(bdata['targetFrequency'])
            numFreqs = len(possibleFreq)

            try:
                ephysData = cellObj.load_ephys_by_index(sessionInd)
            except (ValueError, IOError) as error:
                print(error)
                spikeData = (0, 0)
                behavDict['behavFreqs'].append(possibleFreq)
                behavDict['behavZscore'].append(np.zeros(numFreqs))
                behavDict['behavPval'].append(np.ones(numFreqs))
                behavDict['behavRespIndex'].append(np.zeros(numFreqs))
                behavDict['behavAveResp'].append(np.zeros(numFreqs))
                continue

            eventsDict = ephysData['events']
            spikeTimestamps = ephysData['spikeTimes']

            if spikeTimestamps.ndim == 0:  #There is only one spike, ! spikesanalysis.eventlocked_spiketimes cannot handle only one spike !
                behavDict['behavFreqs'].append(possibleFreq)
                behavDict['behavZscore'].append(np.zeros(numFreqs))
                behavDict['behavPval'].append(np.ones(numFreqs))
                behavDict['behavRespIndex'].append(np.zeros(numFreqs))
                behavDict['behavAveResp'].append(np.zeros(numFreqs))
                continue

            soundOnsetTimes = eventsDict['{}On'.format(soundChannelType)]
            soundOnsetTimeBehav = bdata['timeTarget']

            # -- Check to see if ephys and behav recordings have same number of trials, remove missing trials from behav file -- #
            # Find missing trials
            missingTrials = behavioranalysis.find_missing_trials(
                soundOnsetTimes, soundOnsetTimeBehav)
            # Remove missing trials
            bdata.remove_trials(missingTrials)

            if len(soundOnsetTimes) != len(
                    bdata['timeTarget']
            ):  #some error not handled by remove missing trials
                behavDict['behavFreqs'].append(possibleFreq)
                behavDict['behavZscore'].append(np.zeros(numFreqs))
                behavDict['behavPval'].append(np.ones(numFreqs))
                behavDict['behavRespIndex'].append(np.zeros(numFreqs))
                behavDict['behavAveResp'].append(np.zeros(numFreqs))
                continue

            # -- Calculate Z score of sound response for each frequency -- #
            zScores = []
            pVals = []
            responseEachFreq = []
            responseInds = []

            for freq in possibleFreq:
                # -- Only use valid trials of one frequency to estimate response index -- #
                oneFreqTrials = (bdata['targetFrequency']
                                 == freq) & bdata['valid'].astype('bool')
                oneFreqSoundOnsetTimes = soundOnsetTimes[oneFreqTrials]
                (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \
                    spikesanalysis.eventlocked_spiketimes(spikeTimestamps,oneFreqSoundOnsetTimes,timeRange)
                # Generate the spkCountMatrix where each row is one trial, each column is a time bin to count spikes in, in this case one time bin for baseline and one time bin for sound period
                #pdb.set_trace()
                nspkBase = spikesanalysis.spiketimes_to_spikecounts(
                    spikeTimesFromEventOnset, indexLimitsEachTrial, baseRange)
                nspkResp = spikesanalysis.spiketimes_to_spikecounts(
                    spikeTimesFromEventOnset, indexLimitsEachTrial, respRange)
                print nspkBase.shape, nspkResp.shape

                # Calculate response index (S-B)/(S+B) where S and B are ave response during the sound window and baseline window, respectively
                responseIndex = (np.mean(nspkResp) - np.mean(nspkBase)) / (
                    np.mean(nspkResp) + np.mean(nspkBase))
                responseInds.append(responseIndex)
                responseEachFreq.append(np.mean(
                    nspkResp))  #Store mean response to each stim frequency
                print 'ave firing rate for baseline and sound periods are', np.mean(
                    nspkBase), np.mean(
                        nspkResp), 'response index is', responseIndex

                # Calculate statistic using ranksums test
                zStat, pValue = stats.ranksums(nspkResp, nspkBase)
                print zStat, pValue
                zScores.append(zStat)
                pVals.append(pValue)

            behavDict['behavFreqs'].append(possibleFreq)
            behavDict['behavZscore'].append(zScores)
            behavDict['behavPval'].append(pVals)
            behavDict['behavRespIndex'].append(responseInds)
            behavDict['behavAveResp'].append(responseEachFreq)

    return behavDict