def plot_psycurve_fit_and_data(bdata, plotColor):
    #Calculate the psychometric
    rightTrials = bdata['choice'] == bdata.labels['choice']['right']
    freqEachTrial = bdata['targetFrequency']
    valid = bdata['valid']
    (possibleValues, fractionHitsEachValue, ciHitsEachValue, nTrialsEachValue,
     nHitsEachValue) = behavioranalysis.calculate_psychometric(
         rightTrials, freqEachTrial, valid)

    #Calculate the psycurve fit
    estimate = behavioranalysis.psycurve_fit_from_bdata(bdata, plotFits=0)

    #Plot in log2 space(fit is already in this space)
    possibleValues = np.log2(possibleValues)

    #Plot the stuff
    ax = plt.gca()
    plt.hold(1)
    xRange = possibleValues[-1] - possibleValues[1]
    fitxval = np.linspace(possibleValues[0] - 0.1 * xRange,
                          possibleValues[-1] + 0.1 * xRange, 40)
    fityvals = extrastats.psychfun(fitxval, *estimate)

    upperWhisker = ciHitsEachValue[1, :] - fractionHitsEachValue
    lowerWhisker = fractionHitsEachValue - ciHitsEachValue[0, :]
    (pline, pcaps, pbars) = plt.errorbar(possibleValues,
                                         fractionHitsEachValue,
                                         yerr=[lowerWhisker, upperWhisker],
                                         color='k',
                                         fmt=None)
    pdots = plt.plot(possibleValues,
                     fractionHitsEachValue,
                     'o',
                     mec='none',
                     mfc='k',
                     ms=8)

    setp(pcaps, color=plotColor)
    setp(pbars, color=plotColor)
    setp(pdots, markerfacecolor=plotColor)

    if np.unique(freqEachTrial)[0] < 1000:  #If Hz
        ax.set_xticks(possibleValues)
        ax.set_xticklabels(np.unique(freqEachTrial))
        plt.xlabel('Frequency (Hz)')
    else:
        ax.set_xticks(possibleValues)
        freqLabels = [
            '{:.03}'.format(x) for x in np.unique(freqEachTrial) / 1000.0
        ]
        ax.set_xticklabels(freqLabels)
        plt.xlabel('Frequency (kHz)')

    ax.plot(fitxval, fityvals, color=plotColor)
    plt.ylim([0, 1])
    plt.ylabel('Fraction Rightward Trials')

    return estimate
Esempio n. 2
0
def plot_psycurve_fit_and_data_nohigh(bdata, plotFits=True):
    '''
    This version actually works. Calculates things in log2(freq) space.
    '''

    rightTrials = bdata['choice'] == bdata.labels['choice']['right']
    freqEachTrial = bdata['targetFrequency']
    valid = bdata['valid']

    (possibleValues, fractionHitsEachValue, ciHitsEachValue, nTrialsEachValue,
     nHitsEachValue) = behavioranalysis.calculate_psychometric(
         rightTrials, freqEachTrial, valid)

    plot(np.log2(possibleValues), fractionHitsEachValue, 'bo')
    hold(1)

    possibleFreq = np.log2(np.unique(freqEachTrial))
    lowerFreqConstraint = possibleFreq[1]
    upperFreqConstraint = possibleFreq[-2]
    maxFreq = max(possibleFreq)
    minFreq = min(possibleFreq)

    constraints = ('Uniform({}, {})'.format(lowerFreqConstraint,
                                            upperFreqConstraint),
                   'Uniform(0,5)', 'Uniform(0,1)', 'Uniform(0,1)')
    estimate = extrastats.psychometric_fit(possibleFreq[:-1],
                                           nTrialsEachValue[:-1],
                                           nHitsEachValue[:-1], constraints)

    xRange = possibleFreq[-1] - possibleFreq[1]
    fitxval = np.linspace(possibleFreq[0] - 0.1 * xRange,
                          possibleFreq[-1] + 0.1 * xRange, 40)

    fityvals = extrastats.psychfun(fitxval, *estimate)

    plot(fitxval, fityvals)
    ylim([0, 1])

    return estimate
Esempio n. 3
0
def plot_frequency_psycurve_soundtype(bdata, soundType, fontsize=12):
    '''
    Show psychometric curve (for any arbitrary sound type)
    '''

    trialsSoundType = bdata['soundType']==bdata.labels['soundType'][soundType]
    choice = bdata['choice']
    choiceRight = choice==bdata.labels['choice']['right']
    choiceRightSoundType = choiceRight[trialsSoundType]
    targetFrequencySoundType = bdata['targetFrequency'][trialsSoundType]
    valid = bdata['valid']
    validSoundType = valid[trialsSoundType]
    (possibleValues,fractionHitsEachValue,ciHitsEachValue,nTrialsEachValue,nHitsEachValue)=\
        behavioranalysis.calculate_psychometric(choiceRightSoundType,targetFrequencySoundType,validSoundType)
    (pline, pcaps, pbars, pdots) = extraplots.plot_psychometric(1e-3*possibleValues,
                                                                fractionHitsEachValue,
                                                                ciHitsEachValue,
                                                                xTickPeriod=1)
    plt.xlabel('Frequency (kHz)',fontsize=fontsize)
    plt.ylabel('Rightward trials (%)',fontsize=fontsize)
    extraplots.set_ticks_fontsize(plt.gca(),fontsize)
    return (pline, pcaps, pbars, pdots)
Esempio n. 4
0
def plot_psycurve_fit_and_data(bdata, plotFits=True):
    rightTrials = bdata['choice'] == bdata.labels['choice']['right']
    freqEachTrial = bdata['targetFrequency']
    valid = bdata['valid']

    (possibleValues, fractionHitsEachValue, ciHitsEachValue, nTrialsEachValue,
     nHitsEachValue) = behavioranalysis.calculate_psychometric(
         rightTrials, freqEachTrial, valid)

    #Calculate the lower and upper freq in the bdata to set the correct constraints
    # possibleFreq = np.unique(freqEachTrial)
    possibleFreq = np.log2(np.unique(freqEachTrial))
    maxFreq = max(possibleFreq)
    minFreq = min(possibleFreq)

    #Calculating with constraints
    # constraints = ( 'Uniform({}, {})'.format(minFreq, maxFreq),'Uniform(0,20)' ,'Uniform(0,1)', 'Uniform(0,1)')
    constraints = ('Uniform({}, {})'.format(minFreq, maxFreq), 'Uniform(0,10)',
                   'Uniform(0,0.2)', 'Uniform(0,0.2)')
    estimate = extrastats.psychometric_fit(possibleValues, nTrialsEachValue,
                                           nHitsEachValue, constraints)

    if plotFits:
        xRange = possibleFreq[-1] - possibleFreq[1]
        fitxval = np.linspace(possibleFreq[0] - 0.1 * xRange,
                              possibleFreq[-1] + 0.1 * xRange, 40)

        #The fitted points
        fityvals = extrastats.psychfun(fitxval, estimate[0], estimate[1],
                                       estimate[2], estimate[3])

        plot(fitxval, fityvals)
        # hold(1)
        # extraplots.plot_psychometric(possibleFreq, fractionHitsEachValue, ciHitsEachValue)
        plot(np.log2(possibleFreq), fractionHitsEachValue, 'bo')

    return estimate
Esempio n. 5
0
    animal, muscimolSessions)
salineDataObjs, salineSoundTypes = behavioranalysis.load_behavior_sessions_sound_type(
    animal, salineSessions)

#Testing with a single bdata object
hold(1)
bdata = salineDataObjs[0]
bdata = muscimolDataObjs[0]

#Calculate hit trials, freq each trial, valid - then calc psychometric
rightTrials = bdata['choice'] == bdata.labels['choice']['right']
freqEachTrial = bdata['targetFrequency']
valid = bdata['valid']

(possibleValues, fractionHitsEachValue, ciHitsEachValue, nTrialsEachValue,
 nHitsEachValue) = behavioranalysis.calculate_psychometric(
     rightTrials, freqEachTrial, valid)

extraplots.plot_psychometric(possibleValues, fractionHitsEachValue,
                             ciHitsEachValue)

#calculating the estimate without constraints
estimate = extrastats.psychometric_fit(possibleValues, nTrialsEachValue,
                                       nHitsEachValue)

#Calculating with constraints
constraints = ('Uniform(8, 32)', 'Uniform(0,20)', 'Uniform(0,1)',
               'Uniform(0,1)')
estimate = extrastats.psychometric_fit(possibleValues, nTrialsEachValue,
                                       nHitsEachValue, constraints)

#The x values over which to evaluate the fitted function
Esempio n. 6
0
choiceRight = choice==bdata.labels['choice']['right']

choiceRightAmpMod = choiceRight[trialsAmpMod]
choiceRightTones = choiceRight[trialsTones]

targetFrequencyAmpMod = bdata['targetFrequency'][trialsAmpMod]
targetFrequencyTones = bdata['targetFrequency'][trialsTones]

valid = bdata['valid']
validAmpMod = valid[trialsAmpMod]
validTones = valid[trialsTones]

#Tones
figure()
(possibleValues,fractionHitsEachValue,ciHitsEachValue,nTrialsEachValue,nHitsEachValue)=\
    behavioranalysis.calculate_psychometric(choiceRightTones,targetFrequencyTones,validTones)

(pline, pcaps, pbars, pdots) = extraplots.plot_psychometric(1e-3*possibleValues,
                                                            fractionHitsEachValue,
                                                            ciHitsEachValue,
                                                            xTickPeriod=1)



#Amp Mod
figure()
(possibleValues,fractionHitsEachValue,ciHitsEachValue,nTrialsEachValue,nHitsEachValue)=\
    behavioranalysis.calculate_psychometric(choiceRightAmpMod,targetFrequencyAmpMod,validAmpMod)

(pline, pcaps, pbars, pdots) = extraplots.plot_psychometric(1e-3*possibleValues,
                                                            fractionHitsEachValue,
Esempio n. 7
0
muscimolData = behavioranalysis.load_many_sessions(animal, muscimolSessions)
salineData = behavioranalysis.load_many_sessions(animal, salineSessions)

soundTypes = ['amp_mod', 'chords']

soundType = 'chords'
trialsSoundTypeMusAmp = muscimolData['soundType'] == muscimolData.labels[
    'soundType'][soundType]
rightTrialsMusAmp = (muscimolData['choice'] == muscimolData.labels['choice']
                     ['right'])[trialsSoundTypeMusAmp]
freqEachTrialMusAmp = muscimolData['targetFrequency'][trialsSoundTypeMusAmp]
validMusAmp = muscimolData['valid'][trialsSoundTypeMusAmp]

(possibleValues, fractionHitsEachValue, ciHitsEachValue, nTrialsEachValue,
 nHitsEachValue) = behavioranalysis.calculate_psychometric(
     rightTrialsMusAmp, freqEachTrialMusAmp, validMusAmp)

M = pypsignifit.psignidata.BootstrapInference(zip(possibleValues,
                                                  nHitsEachValue,
                                                  nTrialsEachValue),
                                              sample=True,
                                              nafc=1,
                                              plotprm={
                                                  "color": "r",
                                                  "label": "Condition 0"
                                              })

trialsSoundTypeSalAmp = salineData['soundType'] == salineData.labels[
    'soundType'][soundType]
rightTrialsSalAmp = (salineData['choice'] == salineData.labels['choice']
                     ['right'])[trialsSoundTypeSalAmp]
salineSessions = ['20160412a', '20160414a', '20160416a']
animal = 'amod002'

muscimolData = behavioranalysis.load_many_sessions(animal, muscimolSessions)
salineData = behavioranalysis.load_many_sessions(animal, salineSessions)

soundTypes = ['amp_mod', 'chords']

soundType = 'chords'
trialsSoundTypeMusAmp = muscimolData['soundType']==muscimolData.labels['soundType'][soundType]
rightTrialsMusAmp = (muscimolData['choice']==muscimolData.labels['choice']['right'])[trialsSoundTypeMusAmp]
freqEachTrialMusAmp = muscimolData['targetFrequency'][trialsSoundTypeMusAmp]
validMusAmp = muscimolData['valid'][trialsSoundTypeMusAmp]


(possibleValues,fractionHitsEachValue,ciHitsEachValue,nTrialsEachValue,nHitsEachValue) = behavioranalysis.calculate_psychometric(rightTrialsMusAmp, freqEachTrialMusAmp, validMusAmp)

M = pypsignifit.psignidata.BootstrapInference(zip(possibleValues, nHitsEachValue, nTrialsEachValue), sample=True, nafc=1, plotprm={"color": "r", "label": "Condition 0"})

trialsSoundTypeSalAmp = salineData['soundType']==salineData.labels['soundType'][soundType]
rightTrialsSalAmp = (salineData['choice']==salineData.labels['choice']['right'])[trialsSoundTypeSalAmp]
freqEachTrialSalAmp = salineData['targetFrequency'][trialsSoundTypeSalAmp]
validSalAmp = salineData['valid'][trialsSoundTypeSalAmp]

(possibleValues,fractionHitsEachValue,ciHitsEachValue,nTrialsEachValue,nHitsEachValue) = behavioranalysis.calculate_psychometric(rightTrialsSalAmp, freqEachTrialSalAmp, validSalAmp)

S = pypsignifit.psignidata.BootstrapInference(zip(possibleValues, nHitsEachValue, nTrialsEachValue), sample=True, nafc=1, plotprm={"color": "k", "label": "Condition 0"})


figure()
# pypsignifit.psigniplot.plotPMF(B)