예제 #1
0
 def bootstrapMedian ( data, N=5000 ):
     # determine 95% confidence intervals of the median
     M = len(data)
     percentile = [2.5,97.5]
     estimate = pl.zeros(N)
     for n in xrange (N):
         bsIndex = pl.randint ( 0, M, M )
         bsData = data[bsIndex]
         estimate[n] = pl.prctile ( bsData, 50 )
     CI = pl.prctile ( estimate, percentile )
     return CI
예제 #2
0
 def setNorm(self):
     "sets the whitepoint and blackpoint (uses raw data, not scaled)"
     x = N.sort(self.image[:].flatten())
     npts = x.shape[0]
     p01 = x[int(round(.01 * npts))]
     p99 = x[int(round(.99 * npts))]
     self.norm = P.normalize(vmin=p01, vmax=p99)
     #self.norm = P.normalize(-.1, 1.1)
     if hasattr(self, "overlay_img") and self.overlay_img:
         p01 = P.prctile(self.overlay_img[:], 1.0)
         p99 = P.prctile(self.overlay_img[:], 99.)
         self.overlay_norm = P.normalize(vmin=p01, vmax=p99)
예제 #3
0
 def setNorm(self):
     "sets the whitepoint and blackpoint (uses raw data, not scaled)"
     x = N.sort(self.image[:].flatten())
     npts = x.shape[0]
     p01 = x[int(round(.01*npts))]
     p99 = x[int(round(.99*npts))]
     self.norm = P.normalize(vmin = p01, vmax = p99)
     #self.norm = P.normalize(-.1, 1.1)
     if hasattr(self, "overlay_img") and self.overlay_img:
         p01 = P.prctile(self.overlay_img[:], 1.0)
         p99 = P.prctile(self.overlay_img[:], 99.)
         self.overlay_norm = P.normalize(vmin = p01, vmax = p99)
예제 #4
0
def compare_on_dataset(d, nafc=2, nruns=3):
    """Perform a comparison on a single dataset

    :Parameters:
        d
            dataset
        nafc
            number of alternatives
        nruns
            number of repetitions of the analysis

    TODO:
    This needs to be revised internally!
    """
    if nafc == 2:
        priors = ("Gauss(0,100)", "invGamma(.5,.5)", "Beta(2,30)")
    else:
        priors = ("Gauss(0,100)", "invGamma(.5,.5)", "Beta(2,30)", "Beta(2,30)")

    results = []
    for k in xrange(nruns):
        A = psi.ASIRInference(d, nafc=nafc, priors=priors, sigmoid=sigmoid, core=core)

        Am025, Am975 = pl.prctile(A.mcestimates[:, 0], (2.5, 97.5))
        Aw025, Aw975 = pl.prctile(A.mcestimates[:, 1], (2.5, 97.5))
        Amm, Awm = A.mcestimates[:, :2].mean(0)
        Ams, Aws = A.mcestimates[:, :2].std(0)

        M = psi.BayesInference(d, nafc=nafc, priors=priors, sigmoid=sigmoid, core=core, maxnsamples=1000000)
        M.sample(start=M.farstart)
        M.sample(start=M.farstart)

        Mm025, Mm975 = pl.prctile(M.mcestimates[:, 0], (2.5, 97.5))
        Mw025, Mw975 = pl.prctile(M.mcestimates[:, 1], (2.5, 97.5))
        Mmm, Mwm = M.mcestimates[:, :2].mean(0)
        Mms, Mws = M.mcestimates[:, :2].std(0)
        MR = M.Rhat()
        print MR

        result = "%g %g %g %g " % (Am025, Am975, Amm, Ams)
        result += "%g %g %g %g " % (Aw025, Aw975, Awm, Aws)
        result += "%g %g %g %g " % (Mm025, Mm975, Mmm, Mms)
        result += "%g %g %g %g " % (Mw025, Mw975, Mwm, Mws)
        result += "%g %g %g " % (M.Rhat(0), M.Rhat(1), M.Rhat(2))
        result += "%g %g " % (
            A._ASIRInference__inference["resampling-entropy"],
            A._ASIRInference__inference["duplicates"],
        )

        results.append(result)

    return results
예제 #5
0
def plotSensitivity ( BootstrapInferenceObject, ax=None ):
    """Visualize a sensitivity analysis to determine expanded bootstrap confidence intervals

    Sensitivity analysis is used for BootstrapInference objects to expand the confidence intervals
    in order to obtain more realistic coverage. This function calls the sensitivity_analysis() method
    of the BootstrapInferenceObject with default parameters. If other parameters are requested, the
    sensitivity_analysis() method should be called manually

    :Parameters:
        *BootstrapInferenceObject* :
            Inference object to be analyzed
        *ax* :
            pylab axes that should be used for plotting
    """
    if BootstrapInferenceObject.mcestimates is None:
        raise ValueError, "Sensitivity analysis requires monte carlo samples. Try to call the sample() method of your inference object."

    if ax==None:
        ax = p.axes()

    # Determine axes ranges
    prm1 = BootstrapInferenceObject.mcestimates[:,0]
    prm2 = BootstrapInferenceObject.mcestimates[:,1]
    ax.plot(prm1,prm2,'w.',markersize=1)
    xmin,xmax = ax.get_xlim()
    ymin,ymax = ax.get_ylim()
    ax.cla()

    # Plot the density estimate in the background
    x,y = N.mgrid[xmin:xmax:100j,ymin:ymax:100j]
    C = BootstrapInferenceObject.mcdensity(N.c_[N.ravel(x),N.ravel(y)].T)
    C.shape = 100,100
    ax.imshow( C.T,origin="lower",extent=(xmin,xmax,ymin,ymax), cmap=p.cm.gray_r )

    # Get the points and make sure, a sensitivity_analysis has indeed been run
    thres,slope,points = BootstrapInferenceObject.sensitivity_analysis(verbose=False)

    # plot the points
    ax.fill(points[:,0],points[:,1],fill=False,edgecolor="r",linewidth=2)
    ax.plot(prm1,prm2,"b.",markersize=2)
    ax.plot(points[:,0],points[:,1],'rd',markersize=5)
    ax.plot([BootstrapInferenceObject.estimate[0]],[BootstrapInferenceObject.estimate[1]],'ro',markersize=5)

    # plot marginal percentiles
    prm1lims = p.prctile ( BootstrapInferenceObject.mcestimates[:,0], (2.5,25,75,97.5) )
    prm2lims = p.prctile ( BootstrapInferenceObject.mcestimates[:,1], (2.5,25,75,97.5) )
    ax.plot( prm1lims, [ymin-0.05*(ymax-ymin)]*4, 'b-', [xmin-0.05*(xmax-xmin)]*4, prm2lims, 'b-' )
    ax.plot( prm1lims[1:3], [ymin-0.05*(ymax-ymin)]*2, 'b-', [xmin-0.05*(xmax-xmin)]*2, prm2lims[1:3], 'b-', linewidth=5 )

    # Draw axes
    drawaxes ( ax, ax.get_xticks(), "%g", ax.get_yticks(), "%g", BootstrapInferenceObject.parnames[0], BootstrapInferenceObject.parnames[1] )
예제 #6
0
def plotParameterDist(InferenceObject, parameter=0, ax=None):
    """Plot the distribution of parameters

    :Parameters:
        *InferenceObject* :
            either a BootstrapInference object or a BayesInference object
            containing the samples of the parameter distribtution
        *parameter* :
            index of the model parameter of interest
        *ax* :
            pylab.axes object where the plot should go
    """

    if InferenceObject.mcestimates is None:
        raise ValueError, "Plotting distribution of parameters requires monte carlo samples. Try to call the sample() method of your inference object."

    if ax is None:
        ax = prepare_axes(p.axes())

    samples = InferenceObject.mcestimates[:, parameter]
    h, b, ptch = ax.hist(samples, bins=20, normed=True, histtype="step", lw=2)

    if InferenceObject.__repr__().split()[1] in ["BayesInference", "ASIRInference"]:
        priorstr = InferenceObject.model["priors"]
        if not priorstr is None:
            priorstr = priorstr[parameter]
            m = re.search(r"(\w+)\((-?\d*\.?\d*[eE]?-?\d*),(-?\d*\.?\d*[eE]?-?\d*)\)", priorstr)
            if not m is None:
                dist, prm1, prm2 = m.groups()
                prm1, prm2 = float(prm1), float(prm2)
                x = N.mgrid[b.min() : b.max() : 100j]

                if dist.lower() == "gauss":
                    ax.plot(x, stats.norm.pdf(x, prm1, prm2))
                elif dist.lower() == "beta":
                    ax.plot(x, stats.beta.pdf(x, prm1, prm2))
                elif dist.lower() == "gamma":
                    ax.plot(x, stats.gamma.pdf(x, prm1, scale=prm2))
                elif dist.lower() == "ngamma":
                    ax.plot(x, stats.gamma.pdf(-x, prm1, scale=prm2))
                elif dist.lower() == "uniform":
                    ax.plot(x, stats.uniform.pdf(x, prm1, prm2))
                elif dist.lower() == "invgamma":
                    ax.plot(x, stats.invgamma.pdf(x, prm1, scale=prm2))

    # Highlight estimate and credibility intervals
    prm = InferenceObject.estimate[parameter]
    c25, c975 = p.prctile(samples, (2.5, 97.5))
    ym = ax.get_ylim()
    ax.plot([c25] * 2, ym, "b:", [c975] * 2, ym, "b:")
    ax.plot([prm] * 2, ym, "b")

    prname = InferenceObject.parnames[parameter]
    if prname in ["alpha", "beta", "gamma", "lambda"]:
        prname = r"\%s" % (prname,)
    message = r"$\hat{%s}" % (prname,)
    message += r"$=%.3f, CI(95)=(%.3f,%.3f)" % (prm, c25, c975)
    ax.set_title(message, **(rc.text + rc.alltext))
    ax.set_xlabel(InferenceObject.parnames[parameter], **(rc.label + rc.alltext))
    ax.set_ylabel("density estimate", **(rc.label + rc.alltext))
예제 #7
0
def kernelplot_Mod(d, results, infodict, ax1, ax2, ax3, legend='lower right'):
    """Plot historykernels"""
    M = results['model_w_hist']
    bootstrap = results['bootstrap']

    C = statistics.Kernel_and_Slope_Collector(d.h, d.hf0, range(1, d.hf0))
    K = C(M)
    print 'K', K
    print d.hf0
    print 'd.h.shape[0]', d.h.shape[0]

    print 'adding modulation kernels'
    hr = K[:d.h.shape[0]]
    hz = K[d.h.shape[0]:2 * d.h.shape[0]]
    hr_pupil = K[2 * d.h.shape[0]:3 * d.h.shape[0]]
    hz_pupil = K[3 * d.h.shape[0]:-2]
    hr_pupil *= K[-2]
    hz_pupil *= K[-2]

    if bootstrap is None:
        kernellen = (bootstrap.shape[1] - 2) / 2
        print kernellen
        al = bootstrap[:, -2]
        al.shape = (-1, 1)
        bootstrap[:, :-2] *= al  # Like that?
        print K[-2], pl.prctile(bootstrap[:, -2]), pl.mean(bootstrap[:, -2])

        hci = statistics.history_kernel_ci(bootstrap[:, kernellen:-2],
                                           bootstrap[:, :kernellen], hz, hr)
    else:
        hci = None

    kl = graphics.history_kernels(hz,
                                  hr,
                                  hci,
                                  ax1,
                                  "left/right",
                                  ground_truth=d.ground_truth)
    kl += graphics.history_kernels(hz,
                                   hr,
                                   hci,
                                   ax2,
                                   "correct/incorrect",
                                   ground_truth=d.ground_truth)

    labely, labelh = same_y(ax1, ax2)

    graphics.label_axes(title="(D) stimulus and response kernels",
                        xlabel="lag",
                        ylabel="equivalent stimulus strength",
                        legend=legend,
                        ax=ax1)
    graphics.label_axes(title="(E) correct and incorrect kernels",
                        xlabel="lag",
                        ylabel="equivalent stimulus strength",
                        legend=legend,
                        ax=ax2)
    # pl.setp ( (ax1,ax2), ylim=(-6,6) )

    return kl
예제 #8
0
    def make_range_frame (self):

        rx = self.axes.get_xlim()
        ry = self.axes.get_ylim()
        px = pl.prctile ( self.x )
        py = pl.prctile ( self.y )

        if self.trim:
            if px[2]-px[0]>1.5*(px[3]-px[1]):
                px[0] = self.x[self.x>px[2]-1.5*(px[3]-px[1])].min()
            if px[4]-px[2]>1.5*(px[3]-px[1]):
                px[4] = self.x[self.x<px[2]+1.5*(px[3]-px[1])].min()

        x = px-rx[0]
        x /= rx[1]-rx[0]
        y = py-ry[0]
        y /= ry[1]-ry[0]
        ex = .003
        ey = .003
        xline = [
                [(x[0],0),(x[1],0)],
                [(x[1],ey),(x[2]-ex,ey)],
                [(x[2]+ex,ey),(x[3],ey)],
                [(x[3],0),(x[4],0)]
                ]
        yline = [
                [(0,y[0]),(0,y[1])],
                [(ex,y[1]),(ex,y[2]-ey)],
                [(ex,y[2]+ey),(ex,y[3])],
                [(0,y[3]),(0,y[4])]
                ]
        widths = [1,1,1,1]
        range_lines = LineCollection(
                segments=pl.clip(xline+yline,0,1),
                linewidths=widths+widths,
                colors=[[0]*3]*2*len(widths) )
        range_lines.set_transform ( self.axes.transAxes )
        range_lines.set_zorder(10)

        self.axes.get_xaxis().tick_bottom()
        self.axes.get_yaxis().tick_left()
        self.axes.set_xticks(px)
        self.axes.set_yticks(py)
        self.axes.tick_params ( width=0 )

        return range_lines
예제 #9
0
def kernelplot(d, results, infodict, ax1, ax2, legend='lower right'):
    """Plot historykernels"""
    M = results['model_w_hist']
    bootstrap = results['bootstrap']

    # hr = d.gethistorykernel ( M.w[d.hf0:d.hf0+d.hlen], al )
    # hz = d.gethistorykernel ( M.w[d.hf0+d.hlen:],      al )
    C = statistics.Kernel_and_Slope_Collector(d.h, d.hf0, range(1, d.hf0))
    print d.hf0
    K = C(M)
    hr = K[:d.h.shape[0]]
    hz = K[d.h.shape[0]:-2]
    hz *= K[-2]
    hr *= K[-2]
    print "h_r[1]", hr[0]
    print "h_z[1]", hz[0]

    if not bootstrap is None:
        kernellen = (bootstrap.shape[1] - 2) / 2
        print kernellen
        al = bootstrap[:, -2]
        al.shape = (-1, 1)
        bootstrap[:, :-2] *= al  # Like that?
        print K[-2], pl.prctile(bootstrap[:, -2]), pl.mean(bootstrap[:, -2])

        hci = statistics.history_kernel_ci(bootstrap[:, kernellen:-2],
                                           bootstrap[:, :kernellen], hz, hr)
    else:
        hci = None

    kl = graphics.history_kernels(hz,
                                  hr,
                                  hci,
                                  ax1,
                                  "left/right",
                                  ground_truth=d.ground_truth)
    kl += graphics.history_kernels(hz,
                                   hr,
                                   hci,
                                   ax2,
                                   "correct/incorrect",
                                   ground_truth=d.ground_truth)

    labely, labelh = same_y(ax1, ax2)

    graphics.label_axes(title="(D) stimulus and response kernels",
                        xlabel="lag",
                        ylabel="equivalent stimulus strength",
                        legend=legend,
                        ax=ax1)
    graphics.label_axes(title="(E) correct and incorrect kernels",
                        xlabel="lag",
                        ylabel="equivalent stimulus strength",
                        legend=legend,
                        ax=ax2)
    # pl.setp ( (ax1,ax2), ylim=(-6,6) )

    return kl
예제 #10
0
def recode ( U, q ):
    """recode a series of the parameter of interest to a binary series
    :Parameters:
        U       chain of samples of the parameter of interest (1d-array!)
        q       quantile of interest
    :Output:
        Z       a series that is 1 for all U[t] less than the q-quantile
                of U and that is 0 otherwise
    """
    u = p.prctile ( U, 100*q )
    return (U<=u).astype("d")
예제 #11
0
def calculate_boxplot_stats ( x, **kwargs ):
    whis = kwargs.setdefault ( 'whis', 1.5 )
    bootstrap = kwargs.setdefault ( 'bootstrap', None )

    # Get median and quartiles
    q1,med,q3 = pl.prctile (x, [25,50,75] )
    # Get high extreme
    iq = q3-q1
    hi_val = q3+whis*iq
    wisk_hi = pl.compress ( x<=hi_val, x )
    if len(wisk_hi)==0:
        wisk_hi = q3
    else:
        wisk_hi = max(wisk_hi)
    # Get low extreme
    lo_val = q1-whis*iq
    wisk_lo = pl.compress ( x>=lo_val, x )
    if len(wisk_lo)==0:
        wisk_lo = q3
    else:
        wisk_lo = min(wisk_lo)

    # Get fliers
    flier_hi = pl.compress ( x>wisk_hi, x )
    flier_lo = pl.compress ( x<wisk_lo, x )

    if bootstrap is not None:
        # Do a bootstrap estimate of notch locations
        def bootstrapMedian ( data, N=5000 ):
            # determine 95% confidence intervals of the median
            M = len(data)
            percentile = [2.5,97.5]
            estimate = pl.zeros(N)
            for n in xrange (N):
                bsIndex = pl.randint ( 0, M, M )
                bsData = data[bsIndex]
                estimate[n] = pl.prctile ( bsData, 50 )
            CI = pl.prctile ( estimate, percentile )
            return CI
        CI = bootstrapMedian ( x, N=bootstrap )
        notch_max = CI[1]
        notch_min = CI[0]
    else:
        # Estimate notch locations using Gaussian-based asymptotic
        # approximation
        #
        # For discussion: McGill, R., Tukey, J.W., and
        # Larsen, W.A. (1978) "Variations of Boxplots", The
        # American Statistitian, 32:12-16
        notch_max = med + 1.57*iq/pl.sqrt(len(x))
        notch_min = med - 1.57*iq/pl.sqrt(len(x))
    return {'main':(wisk_lo,q1,med,q3,wisk_hi),
            'fliers':(flier_lo,flier_hi),
            'notch':(notch_min,notch_max)}
예제 #12
0
def recode(U, q):
    """recode a series of the parameter of interest to a binary series
    :Parameters:
        U       chain of samples of the parameter of interest (1d-array!)
        q       quantile of interest
    :Output:
        Z       a series that is 1 for all U[t] less than the q-quantile
                of U and that is 0 otherwise
    """
    u = p.prctile(U, 100 * q)
    return (U <= u).astype("d")
예제 #13
0
def boxdotplot ( ax, data, color, ecolor, jitter=0.01, yshift=0 ):
    yc = jitter*pl.randn ( len(data) ) + yshift
    ax.scatter ( yc, data, s=5, c=color, edgecolor=ecolor )
    prc = pl.prctile ( data )
    ax.plot ( [yshift+1]*2, [prc[0],prc[-1]], 'k-', zorder=0 )
    jitter *= 5
    ax.fill ( yshift+pl.array([1-jitter,1+jitter,1+jitter,1-jitter]),
            [prc[1],prc[1],prc[3],prc[3]],
            facecolor='w', edgecolor='k', zorder=1 )
    ax.plot ( yshift+pl.array([1-jitter,1+jitter]), [prc[2]]*2, 'k-', zorder=2 )
    ax.plot ( [yshift+1], [pl.mean(data)], 'o', color='w', markeredgecolor='k' )

    ax = graphics.prepare_axes ( ax, haveon=('left',) )
예제 #14
0
def slopeplot(d, results, infodict, ax):
    """slope results of the permutation test"""

    ax = graphics.prepare_axes(ax, haveon=('bottom', ))

    h = np.histogram(results['permutation_wh'][:, 1])
    graphics.montecarlo_test(results['model_w_hist'].w[1],
                             h,
                             pl.prctile(results['permutation_wh'][:, 1], 95),
                             ax=ax,
                             labeling='slope')
    # ax.bar ( b[:-1], h, pl.diff(b), edgecolor=graphics.histogram_color, facecolor=graphics.histogram_color )
    # yrange = ax.get_ylim ()
    # ax.axvline ( results['model_w_hist'].w[1], ymin=yrange[0], ymax=yrange[1] )
    # ax.set_ylim ( yrange )
    # # ax.set_xlim ( .3,3 )
    # # ax.set_xticks ( [.5,1,1.5,2,2.5,3] )

    graphics.label_axes(title="(F) slope effects", xlabel=r"slope", ax=ax)
예제 #15
0
def plot_pvalues_calibration(pvalues, names, colors, outfile):
    #plot a list of (pvalues)
    #Quantiles
    clf()
    lengths = [len(p) for p in pvalues]
    binNumber = min(lengths)
    rank = array(range(1, binNumber + 1)) / float(binNumber)
    quantiles = 100 * (rank)

    #Plot
    for i in xrange(len(pvalues)):
        pvalue_list = pvalues[i]
        name = names[i]
        q = prctile(pvalue_list, p=quantiles)
        plt.scatter(rank,
                    q,
                    s=30,
                    c=colors[i],
                    edgecolor=colors[i],
                    marker='o',
                    label=name)

    #Diagonal
    linea = lineb = [0.000000000001, 10]
    plt.plot(linea, lineb, c="black")
    linec = array(lineb) / 2
    lined = array(lineb) * 2
    plt.plot(linea, linec, '--', c="grey")
    plt.plot(linea, lined, '--', c="grey")

    #Axes
    ax = plt.subplot(111)
    ax.set_xscale('log')
    ax.set_yscale('log')
    ax.set_xlim(min([1.0 / l for l in lengths]) * 1, 1)
    ax.set_ylim(min([1.0 / l for l in lengths]) * 1, 1)

    plt.legend(loc='upper left', scatterpoints=1)
    plt.xlabel(("Normalized rank / ideal $p$ values"), fontsize='x-large')
    plt.ylabel(('Reported $p$ values'), fontsize='x-large')

    plt.savefig(outfile)
예제 #16
0
def plot_pvalues_calibration(pvalues,names,colors,outfile):
    #plot a list of (pvalues)
    #Quantiles
    clf()
    lengths = [len(p) for p in pvalues]
    binNumber = min(lengths)
    rank = array(range(1,binNumber+1)) / float(binNumber)
    quantiles = 100*(rank)

    #Plot
    for i in xrange(len(pvalues)):
        pvalue_list = pvalues[i]
        name = names[i]
        q = prctile(pvalue_list, p=quantiles)
        plt.scatter(rank, q, s=30, c=colors[i], edgecolor=colors[i], marker='o', label=name)
        
    #Diagonal
    linea = lineb = [0.000000000001,10]
    plt.plot(linea, lineb, c="black")
    linec = array(lineb)/2
    lined = array(lineb)*2
    plt.plot(linea, linec, '--', c="grey")
    plt.plot(linea, lined, '--', c="grey")
    
    #Axes
    ax = plt.subplot(111)
    ax.set_xscale('log')
    ax.set_yscale('log')
    ax.set_xlim(min([1.0/l for l in lengths])*1, 1)
    ax.set_ylim(min([1.0/l for l in lengths])*1, 1)

    plt.legend(loc = 'upper left', scatterpoints=1)
    plt.xlabel(("Normalized rank / ideal $p$ values"),fontsize='x-large')
    plt.ylabel(('Reported $p$ values'),fontsize='x-large') 
    
    plt.savefig(outfile)
    
예제 #17
0
def compare_on_dataset(d, nafc=2, nruns=3):
    """Perform a comparison on a single dataset

    :Parameters:
        d
            dataset
        nafc
            number of alternatives
        nruns
            number of repetitions of the analysis

    TODO:
    This needs to be revised internally!
    """
    if nafc == 2:
        priors = ("Gauss(0,100)", "invGamma(.5,.5)", "Beta(2,30)")
    else:
        priors = ("Gauss(0,100)", "invGamma(.5,.5)", "Beta(2,30)",
                  "Beta(2,30)")

    results = []
    for k in xrange(nruns):
        A = psi.ASIRInference(d,
                              nafc=nafc,
                              priors=priors,
                              sigmoid=sigmoid,
                              core=core)

        Am025, Am975 = pl.prctile(A.mcestimates[:, 0], (2.5, 97.5))
        Aw025, Aw975 = pl.prctile(A.mcestimates[:, 1], (2.5, 97.5))
        Amm, Awm = A.mcestimates[:, :2].mean(0)
        Ams, Aws = A.mcestimates[:, :2].std(0)

        M = psi.BayesInference(d,
                               nafc=nafc,
                               priors=priors,
                               sigmoid=sigmoid,
                               core=core,
                               maxnsamples=1000000)
        M.sample(start=M.farstart)
        M.sample(start=M.farstart)

        Mm025, Mm975 = pl.prctile(M.mcestimates[:, 0], (2.5, 97.5))
        Mw025, Mw975 = pl.prctile(M.mcestimates[:, 1], (2.5, 97.5))
        Mmm, Mwm = M.mcestimates[:, :2].mean(0)
        Mms, Mws = M.mcestimates[:, :2].std(0)
        MR = M.Rhat()
        print MR

        result = "%g %g %g %g " % (Am025, Am975, Amm, Ams)
        result += "%g %g %g %g " % (Aw025, Aw975, Awm, Aws)
        result += "%g %g %g %g " % (Mm025, Mm975, Mmm, Mms)
        result += "%g %g %g %g " % (Mw025, Mw975, Mwm, Mws)
        result += "%g %g %g " % (M.Rhat(0), M.Rhat(1), M.Rhat(2))
        result += "%g %g " % (
            A._ASIRInference__inference["resampling-entropy"],
            A._ASIRInference__inference["duplicates"])

        results.append(result)

    return results
예제 #18
0
def figure1 ( ):
    w,h = 28,15
    fig = pl.figure ( figsize=(fullwidth,h*fullwidth/w) )

    a_,b_,d_ = place_axes ( fig, 1.5,9, [9,8,9], [6]*3,
            [True]+[False]+[True], [.5,1.5,1.], (w,h) )
    c1,c2,e1,e2,e3 = place_axes ( fig, 1.5,2, [5.2,5.2,5.2,5.2,5.2], [6]*5,
            [False]*5, [.5]*5, (w,h) )
    a_.text ( .05, laby, r"\textbf{a}", transform=a_.transAxes )
    b_.text ( .05, laby, r"\textbf{b}", transform=b_.transAxes )
    d_.text ( .05, laby, r"\textbf{c}", transform=d_.transAxes )
    c1.text ( .05, laby, r"\textbf{d}", transform=c1.transAxes )
    c2.text ( .05, laby, r"\textbf{e}", transform=c2.transAxes )
    e1.text ( .05, laby, r"\textbf{f}", transform=e1.transAxes )
    e2.text ( .05, laby, r"\textbf{g}", transform=e2.transAxes )
    e3.text ( .05, laby, r"\textbf{h}", transform=e3.transAxes )


    # Figure 1A ==============================
    print "nu0",results['model_nohist'].w,results['model_nohist'].nu
    print results['model_nohist'].applythreshold
    plotinfo_ = dict ( plotinfo )
    if observer in ["KP",'sim_KP','sim_KP_nh']:
        plotinfo_['conditions'] = (0,5)
    plotinfo_['xmin'] = -25
    plotinfo_['xmax'] =  25
    if observer in ['nico','konrad']:
        plotinfo_['xmin'] = -100
        plotinfo_['xmax'] =  100
    print plotinfo_
    convenience.pmfplot ( data, results, plotinfo_, a_, errors=False )
    if observer is 'pk':
        a_.set_xlim ( -30, 30  )
        a_.set_xticks ( (-30,-20,-10,0,10,20,30) )
    a_.set_title ('')
    a_.set_ylim ( -.03, 1.03 )
    a_.yaxis.set_major_formatter ( myformatter )
    a_.xaxis.set_major_formatter ( myformatter )
    a_.set_ylabel ( "Probability" )
    a_.set_xlabel ( "Transduced stimulus intensity" )

    # Figure 1B ===============================
    textfile.write ("Figure 1B:\n" )
    l_obs,c95,caic,cpe = convenience.permutationplot ( data, results, plotinfo, b_, noaic=True )
    b_.set_title ( '' )
    b_.set_xlabel ( "Log-likelihood" )
    b_.xaxis.set_major_formatter ( myformatter )
    b_.set_xlim ( trimmed_hlim ( results['permutation_wh'][:,0], l_obs, (-2000,1000),20 ) )
    for l in b_.get_children():
        if isinstance ( l, matplotlib.legend.Legend ):
            pl.setp(l, visible=False )
    print l_obs,c95,caic,cpe,caic-6
    textfile.write ( "  l_obs = %g\n  l_95%% = %g\n  l_AIC = %g\n  cpe = %g\n" % (l_obs,c95,caic,cpe) )

    if getattr ( data, 'audio', False ):
        easy,difficult = data.performance_filter ()
        M = statistics.EvaluationCollector ( results['model_w_hist'], easy, difficult )
    else:
        M = statistics.EvaluationCollector ( results['model_w_hist'] )
    M(results['model_w_hist'])

    print results['permutation_wh'].shape

    # Figure 1Ca
    textfile.write ( "Figure 1C:\n" )
    # Variance explained on difficult trials
    hist,c95 = statistics.historytest ( results['permutation_wh'][:,3] )
    graphics.montecarlo_test ( M.vdifficult, hist, c95, ax=c1, labeling='other' )
    c1.set_xlabel ( "Var. explained [\%]\n difficult stimuli", fontsize=8, multialignment='center' )
    c1.xaxis.set_major_formatter ( prcformatter )
    c1.set_xlim ( 0, max(M.vdifficult*1.1, hist[1].max()) )
    textfile.write ( "  variance explained on difficult trials: %g, crit: %g, cpe: %g\n" %\
            (M.vdifficult,c95,pl.mean ( results['permutation_wh'][:,3]<M.vdifficult)) )

    # figure 1Cb
    # Variance explained on easy trials
    hist,c95 = statistics.historytest ( results['permutation_wh'][:,4] )
    graphics.montecarlo_test ( M.veasy, hist, c95, ax=c2, labeling='other' )
    c2.set_xlabel ( "Var. explained [\%]\n easy stimuli", fontsize=8, multialignment='center' )
    c2.xaxis.set_major_formatter ( prcformatter )
    c2.set_xlim ( 0, max(M.veasy*1.1, hist[1].max()) )
    textfile.write ( "  variance explained on easy trials: %g, crit: %g, cpe: %g\n" % \
            (M.veasy,c95,pl.mean( results['permutation_wh'][:,4]<M.veasy)) )
    textfile.write ( "  variance explained by stimulus on easy trials: %g\n" % \
            (M.vstimulus,) )

    Mh = results['model_w_hist']
    M0 = results['model_nohist']
    print "LL/trial=",(Mh.loglikelihood-results['permutation_wh'][:,0].mean())
    current_stimulus = pl.dot ( Mh.X[:,1:Mh.hf0], Mh.w[1:Mh.hf0] )
    history_features = pl.dot ( Mh.X[:,Mh.hf0:],  Mh.w[Mh.hf0:] )
    decision_signal  = current_stimulus + history_features
    textfile.write ( "  predicted slope reduction: %g\n" % ((1-0.25*pl.var(history_features)),) )
    textfile.write ( "  actual slope reductions (al_wh/al_nh): %s\n" %  (str((Mh.w[1:Mh.hf0]/M0.w[1:M0.hf0]).tolist()),) )


    # figure 1D
    S,C,V = M.stimuli,M.conditions,M.variance_explained
    conditions = pl.unique ( C )
    print conditions,plotinfo_
    for j,c in enumerate(plotinfo_['conditions']):
        print j,c,plotinfo['colors']
        i = C==plotinfo_['indices'][c][1]
        d_.plot ( S[i], V[i], '.', color=plotinfo['colors'][j] )
    pl.setp ( d_, xlim=(0,max(-plotinfo['xmin'],plotinfo['xmax'])), xlabel='Stimulus intensity',
            ylabel='Var. explained [\%]', ylim=(-.03,1.03) )
    d_.yaxis.set_major_formatter ( prcformatter )
    d_.xaxis.set_major_formatter ( myformatter )
    d_.set_xticks ( (0,10,20,30) )
    d_.set_xlim ( -.1, 30.1 )

    # Figure 1E
    textfile.write ( "Figure 1E:\n" )
    # prediction from history+stimulus
    pS_samples = results['permutation_wh'][:,6]
    pSH_samples = results['permutation_wh'][:,7]
    hist,c95 = statistics.historytest ( results['permutation_wh'][:,7] )
    if pl.var ( pSH_samples ) < 1e-7:
        mpSH = pl.mean ( pSH_samples )
        hist = (pl.array ( [len(pSH_samples)] ),pl.array([mpSH-1e-3,mpSH+1e-3]))
    graphics.montecarlo_test ( M.pSH, hist, c95, Caic=M.pstim, ax=e1, labeling='other' )
    e1.set_xlabel ( 'Prediction acc. [\%]\ndifficult stimuli', fontsize=8, multialignment='center' )
    e1.xaxis.set_major_formatter ( prcformatter )
    e1.set_xlim ( trimmed_hlim ( pSH_samples, (M.pSH,M.pstim)))
    print pSH_samples.mean(),pSH_samples.std(),"---"

    textfile.write ( "  prediction accuracy H+S, difficult: %g, crit: %g, cpe: %g\n" %\
            (M.pSH, c95, pl.mean(pSH_samples<M.pSH)) )
    textfile.write ( "  prediction accuracy S, difficult: %g, crit: %g, cpe: %g\n" %\
            (M.pstim, pl.prctile(pS_samples,95), pl.mean(pS_samples<M.pstim)) )


    hist,c95 = statistics.historytest ( results['permutation_wh'][:,5] )
    graphics.montecarlo_test ( M.phist, hist, c95, Caic=M.pstim, ax=e2, labeling='other' )
    e2.set_xlabel ( 'Prediction acc. [\%]\ndifficult stimuli', fontsize=8, multialignment='center' )
    e2.xaxis.set_major_formatter ( prcformatter )
    pH_samples = results['permutation_wh'][:,5]
    e2.set_xlim ( trimmed_hlim ( pH_samples, M.phist))
    textfile.write ( "  prection accuracy H, difficult: %g, crit: %g, cpe: %g\n" %\
            (M.phist,c95,pl.mean ( pH_samples<M.phist)) )

    hist,c95 = statistics.historytest ( results['permutation_wh'][:,8] )
    graphics.montecarlo_test ( M.peasy, hist, c95, ax=e3, labeling='other' )
    e3.set_xlabel ( 'Prediction acc. [\%]\neasy stimuli', fontsize=8, multialignment='center' )
    e3.xaxis.set_major_formatter ( prcformatter )
    peasy_samples = results['permutation_wh'][:,8]
    e3.set_xlim ( trimmed_hlim ( peasy_samples, M.peasy))
    textfile.write ( "  prection accuracy H, easy: %g, crit: %g, cpe: %g\n" %\
            (M.peasy,c95,pl.mean ( peasy_samples<M.peasy)) )

    # a_.xaxis.set_major_locator (
    #         tckr ( density=0.45, figure=fig, which=0 ) )
    b_.xaxis.set_major_locator (
            tckr ( density=0.2, figure=fig, which=0 ) )
    for ax in (c1,c2):
        ax.xaxis.set_major_locator (
                tckr ( density=0.3, figure=fig, which=0 ) )
        ax.set_xlim ( 0, None )
    d_.xaxis.set_major_locator ( tckr ( density=0.4, figure=fig,which=0 ) )
    d_.set_xlim ( -.2, None )
    for ax in (e1,e2,e3):
        ax.xaxis.set_major_locator (
                MaxNLocator ( 5 ) )
                # tckr ( density=0.3, figure=fig, which=0 ) )
    if observer in ['pk']:
        e1.set_xticks ( pl.array((60,62,64,66))/100. )
        e1.set_xlim ( .60,.67 )

    pl.savefig ( "figures/%s1.pdf" % (figname,) )
    pl.savefig ( "figures/%s1.eps" % (figname,) )
예제 #19
0
def doit(input_fname, single_channel=False, start=None, stop=None, gain=1.0, offset=0.0, blur=0.0):
    if blur != 0:
        import scipy.ndimage.filters

    output_fname = os.path.splitext(input_fname)[0] + ".highcontrast.fmf"
    in_fmf = FMF.FlyMovie(input_fname)
    input_format = in_fmf.get_format()
    input_is_color = imops.is_coding_color(input_format)

    if single_channel is not None:
        if not input_is_color:
            warnings.warn("ignoring --single-channel option for non-color input")
            single_channel = False
        output_format = "MONO8"
    else:
        if input_is_color:
            output_format = "RGB8"
        else:
            output_format = "MONO8"
    out_fmf = FMF.FlyMovieSaver(output_fname, version=3, format=output_format)
    try:
        # pass 1 - get 5,95 percentiles
        if input_is_color:
            channels = [("red", 0), ("green", 1), ("blue", 2)]
        else:
            channels = [("gray", 0)]
        channel_dict = dict(channels)
        minvs = collections.defaultdict(list)
        maxvs = collections.defaultdict(list)
        if stop is None:
            stop = in_fmf.get_n_frames() - 1
        if start is None:
            start = 0
        n_frames = stop - start + 1
        n_samples = max(30, n_frames)
        for fno in np.linspace(start, stop, n_samples):
            fno = int(round(fno))
            in_fmf.seek(fno)
            frame, timestamp = in_fmf.get_next_frame()
            if input_is_color:
                frame = imops.to_rgb8(input_format, frame)
            else:
                frame = np.atleast_3d(frame)
            for channel_name, channel_idx in channels:
                minv, maxv = prctile(frame[:, :, channel_idx].ravel(), p=(5.0, 95.0))
                minvs[channel_name].append(minv)
                maxvs[channel_name].append(maxv)

        orig_center = {}
        orig_range = {}
        for channel_name, channel_idx in channels:
            minv = np.min(minvs[channel_name])
            maxv = np.max(maxvs[channel_name])
            orig_center[channel_name] = (minv + maxv) / 2.0
            orig_range[channel_name] = maxv - minv

        new_center = 127.5 + offset
        new_range = 127.5 * gain

        # pass 2 - rescale and save
        in_fmf.seek(0)
        for fno in range(start, stop + 1):
            frame, timestamp = in_fmf.get_next_frame()
            if input_is_color:
                frame = imops.to_rgb8(input_format, frame)
            else:
                frame = np.atleast_3d(frame)

            if single_channel is not None:
                # input is, by definition, color
                frame = frame[:, :, channel_dict[single_channel]]  # drop all but single_channel dim
                frame = frame.astype(np.float32)
                frame = (frame - orig_center[single_channel]) / orig_range[single_channel]
                frame = frame * new_range + new_center
                frame = np.atleast_3d(frame)  # add dim
            else:
                frame = frame.astype(np.float32)
                for channel_name, channel_idx in channels:
                    frame[:, :, channel_idx] = (frame[:, :, channel_idx] - orig_center[channel_name]) / orig_range[
                        channel_name
                    ]
                    frame[:, :, channel_idx] = frame[:, :, channel_idx] * new_range + new_center

            if blur != 0:
                for chan in range(frame.shape[2]):
                    # filter each channel independently
                    frame[:, :, chan] = scipy.ndimage.filters.gaussian_filter(frame[:, :, chan], blur)
            frame = np.clip(frame, 0, 255).astype(np.uint8)
            if output_format is "MONO8":
                # drop dimension
                assert frame.shape[2] == 1
                frame = frame[:, :, 0]
            out_fmf.add_frame(frame, timestamp)
        out_fmf.close()
    except:
        os.unlink(output_fname)
        raise
    in_fmf.close()
예제 #20
0
def figure3 ( ):
    w,h = 25,8.5
    fig = pl.figure ( figsize=(fullwidth,h*fullwidth/w) )

    # a,b,c,d = place_axes ( fig, 1.5,2, [9,9,5,5],[6]*4,
    #         [True]*2+[False]*2, [1.8,1.8,.5,.5], (w,h) )
    a,b,c = place_axes ( fig, 1.5,2, [9,9,5],[6]*3,
            [True]*2+[False], [1.8,1.8,.5], (w,h) )
    d = fig.add_axes ( [10,10,1,1] )
    a.text ( .05, laby, r"\textbf{a}", transform=a.transAxes )
    b.text ( .05, laby, r"\textbf{b}", transform=b.transAxes )
    c.text ( .05, laby, r"\textbf{c}", transform=c.transAxes )
    d.text ( .05, laby, r"\textbf{d}", transform=d.transAxes )
    M = results['model_w_hist']

    # Figures 3 A,B
    for condition in plotinfo['conditions']:
        condition = int ( condition )
        print "c",condition
        d_ = data.getsummary ( condition )
        # x = pl.mgrid[0:plotinfo['xmax']:100j]
        x = pl.mgrid[0:30:100j]
        # if len(data.th_features)>0:
        #     x = threshold.u_v ( x, results['model_w_hist'].nu )

        wfit  = results['model_w_hist'].w[plotinfo['indices'][condition]]
        w0fit = results['model_nohist'].w[plotinfo['indices'][condition]]
        pfit  = results['model_w_hist'].pi
        p0fit  = results['model_nohist'].pi
        x_ = threshold.u_v ( x, results['model_w_hist'].nu )
        x0 = threshold.u_v ( x, results['model_nohist'].nu )

        col = plotinfo['colors'][condition]
        pmf = 0.5*(pfit[1]+pfit[2]*model.logistic ( wfit[0]+wfit[1]*x_ )) + \
                0.5*(1-(pfit[1]+pfit[2]*model.logistic ( wfit[0]-wfit[1]*x_ )))
        p0f = 0.5*(p0fit[1]+p0fit[2]*model.logistic ( w0fit[0]+w0fit[1]*x0 )) + \
                0.5*(1-(p0fit[1]+p0fit[2]*model.logistic ( w0fit[0]-w0fit[1]*x0 )))
        print p0fit
        perror = (1-p0f-(1-pmf))/(1-p0f)

        a.plot ( x, pmf, color = col )
        a.plot ( x, p0f, color = col, linestyle='--' )
        b.plot ( x, pl.clip(perror,0,1e5), color = col )

    a.yaxis.set_major_formatter ( prcformatter )
    a.xaxis.set_major_formatter ( myformatter )
    a.set_xticks ( (0,10,20,30) )

    pl.setp ( (a,b), xlabel='Stimulus intensity' )
    a.set_ylabel ( 'Probability correct [\%]' )
    b.set_ylabel ( 'Error rate exp. [\%]' )
    b.set_xticks ( (0,10,20,30) )
    b.yaxis.set_major_locator ( tckr ( density=2, figure=fig, which=1 ) )
    b.yaxis.set_major_formatter ( prcformatter )
    b.xaxis.set_major_formatter ( myformatter )
    if observer in ['KP','sim_KP','sim_KP_nh']:
        b.set_ylim ( 0, .35 )
    if observer in ['pk']:
        pl.setp ( (a,b), xlim=(-.1,30.1) )

    # figure 3 C
    textfile.write ( "Figure 3C:\n" )
    z0 = 0
    C = statistics.EvaluationCollector ( M )
    ewh = C(results['model_w_hist'])
    enh = C(results['model_nohist'])
    hf0 = M.hf0
    # perm = results['permutation_wh']
    # # TODO: These indices have to be adapted to the revised collector
    # thresholds_wh = pl.array([C.get_thres ( perm[i,13+hf0:13+2*hf0], perm[i,12+hf0], perm[i,9:12], p=0.75 ) \
    #         for i in xrange ( 2000 )])
    # perm = results['permutation_nh']
    # thresholds_nh = pl.array([C.get_thres ( perm[i,13+hf0:13+2*hf0], perm[i,12+hf0], perm[i,9:12], p=0.75 ) \
    #         for i in xrange ( 2000 )])
    if thlev == .75:
        thind = 11
    elif thlev == .85:
        thind = 10+hf0
    else:
        raise ValueError

    for condition in xrange ( 1, M.hf0 ):
        s_wh = results['permutation_wh'][:,thind+condition]
        s_nh = results['permutation_nh'][:,thind+condition]
        # s_wh = thresholds_wh[:,condition]
        # s_nh = thresholds_nh[:,condition]
        s_ratio = s_wh/s_nh
        s_ratio_obs = ewh[thind+condition]/enh[thind+condition]
        # s_ratio_obs = results['model_w_hist'].w[condition]/results['model_nohist'].w[condition]
        z = (s_ratio_obs-pl.mean(s_ratio))/pl.std(s_ratio)
        cpe = pl.mean ( s_ratio < s_ratio_obs )
        ci = pl.prctile ( s_ratio, (2.5,97.5) )
        if z < z0 and ci[1]-ci[0] > 0:
            c0 = condition
            s_ratio_ = s_ratio
            s_ratio_obs_ = s_ratio_obs
            ci_ = ci
        textfile.write (
                "Condition %d\n  th75_ratio = %g\n  cpe = %g\n  percentiles of Null-Distribution: %g, %g\n" % \
                        (condition,s_ratio_obs,cpe,ci[0],ci[1]) )
    try:
        print "Using condition %d for figure 3C" % (c0,)
    except:
        c0 = 1
        s_ratio_ = s_ratio
        s_ratio_obs_ = s_ratio_obs
        ci_ = ci

    hist,bins = pl.histogram ( s_ratio_ )
    c.bar ( bins[:-1], hist, pl.diff ( bins ),
        edgecolor=graphics.histogram_color, facecolor=graphics.histogram_color )
    yrange = c.get_ylim ()
    # c.plot ( [1]*2, yrange, 'k:' )
    if s_ratio_obs<ci_[0]:
        c.plot ( [s_ratio_obs_]*2, (yrange[0],yrange[0]+0.85*(yrange[1]-yrange[0])), linewidth=2,
                color=graphics.observed_color )
        c.plot ( [s_ratio_obs_], [yrange[0]+0.95*(yrange[1]-yrange[0])], '*', color=graphics.observed_color )
    else:
        c.plot ( [s_ratio_obs_]*2, yrange, linewidth=2, color=graphics.observed_color )
    c.plot ( [ci_[0]]*2, yrange, color=graphics.C95_color )
    c.plot ( [ci_[1]]*2, yrange, color=graphics.C95_color )
    c.set_ylim ( *yrange )
    c.set_xlabel ( r'Threshold ratio' )
    c.xaxis.set_major_formatter ( myformatter )
    c.xaxis.set_major_formatter ( myformatter )
    # c.text ( .7, 0.7, r"$\frac{\theta_\mathrm{h}}{\theta_0}$",
    #         transform=c.transAxes )
    # c.set_xlim ( trimmed_hlim ( s_ratio_, s_ratio_obs_ ) )
    # c.xaxis.set_major_locator ( tckr ( density=0.4, figure=fig, which=0 ) )
    c.set_xlim ( .99, 1.01 )
    # c.xaxis.set_ticks ( (.95,1) )
    # c.set_xlim ( .85, 1.05 )
    c.xaxis.set_ticks ( (.99,1.,1.01) )


    # figure 3 D
    l_wh  = 0.5*results['permutation_wh'][:,[9,10]].sum(1)
    l_nh  = 0.5*results['permutation_nh'][:,[9,10]].sum(1)
    l_ratio = l_wh-l_nh
    l_ratio_obs = results['model_w_hist'].pi[[0,1]].sum()-results['model_nohist'].pi[[0,1]].sum()
    cpe = pl.mean ( l_ratio < l_ratio_obs )
    ci = pl.prctile ( l_ratio, (2.5,97.5) )
    textfile.write (
        "Figure 3D:\n  lapse_ratio = %g\n  cpe = %g\n  percentiles of Null-distribution: %g, %g\n  lapse_rate (w hist) = %g\n  lapse_rate (no hist) = %g\n" % \
                (l_ratio_obs,cpe,ci[0],ci[1],results['model_w_hist'].pi[[0,1]].sum(),results['model_nohist'].pi[[0,1]].sum()) )

    d = graphics.prepare_axes ( d, haveon=('bottom',) )
    # hist,bins = pl.histogram ( l_ratio )
    hist,bins = pl.histogram ( l_ratio, bins=good_lapse_bins ( l_ratio ) )
    # hist,bins = pl.histogram ( l_ratio, bins=pl.mgrid[-.0001:.0001:20j] )
    d.bar ( bins[:-1], hist, pl.diff(bins),
        edgecolor=graphics.histogram_color, facecolor=graphics.histogram_color, zorder=0 )
    yrange = d.get_ylim ()
    # d.plot ( [1]*2, yrange, 'k:' )
    if l_ratio_obs < ci[0] or l_ratio_obs > ci[1]:
        d.plot ( [l_ratio_obs]*2, [yrange[0], yrange[0]+0.85*(yrange[1]-yrange[0])],
                linewidth=2, color=graphics.observed_color)
        d.plot ( [l_ratio_obs], [yrange[0]+0.95*(yrange[1]-yrange[0])], '*', color=graphics.observed_color)
    else:
        print "lrobs",l_ratio_obs
        d.plot ( [l_ratio_obs]*2, yrange, color=graphics.observed_color, zorder=2)
    d.plot ([ci[0]]*2, yrange, color=graphics.C95_color, zorder=1 )
    d.plot ([ci[1]]*2, yrange, color=graphics.C95_color, zorder=1 )
    d.set_ylim ( yrange )

    d.set_xlabel ( r'Asymptote difference' )
    # d.text ( .7, 0.7, r"$\frac{\lambda_\mathrm{h}}{\lambda_0}$",
    #         transform=d.transAxes )
    # d.set_xlim ( trimmed_hlim ( l_ratio, l_ratio_obs, (0,5) ) )
    d.set_xlim ( -.003, .001 )
    d.xaxis.set_major_locator ( tckr ( density=0.4, figure=fig, which=0 ) )
    d.xaxis.set_ticks ( (-.002,0) )
    # d.set_xlim ( (.75, 1.25) )
    d.xaxis.set_major_formatter ( myformatter )

    a.set_ylim ( .49, 1.01 )

    pl.savefig ( "figures/%s3.pdf" % ( figname, ) )
    pl.savefig ( "figures/%s3.eps" % ( figname, ) )
예제 #21
0
def plotParameterDist(InferenceObject, parameter=0, ax=None):
    """Plot the distribution of parameters

    :Parameters:
        *InferenceObject* :
            either a BootstrapInference object or a BayesInference object
            containing the samples of the parameter distribtution
        *parameter* :
            index of the model parameter of interest
        *ax* :
            pylab.axes object where the plot should go
    """

    if InferenceObject.mcestimates is None:
        raise ValueError, "Plotting distribution of parameters requires monte carlo samples. Try to call the sample() method of your inference object."

    if ax is None:
        ax = prepare_axes(p.axes())

    samples = InferenceObject.mcestimates[:, parameter]
    h, b, ptch = ax.hist(samples, bins=20, normed=True, histtype="step", lw=2)

    if InferenceObject.__repr__().split()[1] in [
            "BayesInference", "ASIRInference"
    ]:
        priorstr = InferenceObject.model["priors"]
        if not priorstr is None:
            priorstr = priorstr[parameter]
            m = re.search(
                r"(\w+)\((-?\d*\.?\d*[eE]?-?\d*),(-?\d*\.?\d*[eE]?-?\d*)\)",
                priorstr)
            if not m is None:
                dist, prm1, prm2 = m.groups()
                prm1, prm2 = float(prm1), float(prm2)
                x = N.mgrid[b.min():b.max():100j]

                if dist.lower() == "gauss":
                    ax.plot(x, stats.norm.pdf(x, prm1, prm2))
                elif dist.lower() == "beta":
                    ax.plot(x, stats.beta.pdf(x, prm1, prm2))
                elif dist.lower() == "gamma":
                    ax.plot(x, stats.gamma.pdf(x, prm1, scale=prm2))
                elif dist.lower() == "ngamma":
                    ax.plot(x, stats.gamma.pdf(-x, prm1, scale=prm2))
                elif dist.lower() == "uniform":
                    ax.plot(x, stats.uniform.pdf(x, prm1, prm2))
                elif dist.lower() == "invgamma":
                    ax.plot(x, stats.invgamma.pdf(x, prm1, scale=prm2))

    # Highlight estimate and credibility intervals
    prm = InferenceObject.estimate[parameter]
    c25, c975 = p.prctile(samples, (2.5, 97.5))
    ym = ax.get_ylim()
    ax.plot([c25] * 2, ym, 'b:', [c975] * 2, ym, 'b:')
    ax.plot([prm] * 2, ym, 'b')

    prname = InferenceObject.parnames[parameter]
    if prname in ["alpha", "beta", "gamma", "lambda"]:
        prname = r"\%s" % (prname, )
    message = r"$\hat{%s}" % (prname, )
    message += r"$=%.3f, CI(95)=(%.3f,%.3f)" % (prm, c25, c975)
    ax.set_title(message, **(rc.text + rc.alltext))
    ax.set_xlabel(InferenceObject.parnames[parameter],
                  **(rc.label + rc.alltext))
    ax.set_ylabel("density estimate", **(rc.label + rc.alltext))
예제 #22
0
def plotHistogram(simdata,
                  observed,
                  xname,
                  shortname=None,
                  ax=None,
                  hideobserved=False,
                  reference="bootstrap"):
    """plot a histogram and compare observed data to it

    :Parameters:
        *simdata* :
            an array of monte-carlo samples of the parameter of interest
        *observed* :
            observed value of the parameter of interest (for MCMC samples, it is often
            reasonable to use this as the value of 'no effect' or something)
        *xname* :
            name of the paramter of interest
        *shortname* :
            short name of the parameter of interest
        *ax* :
            axes object defining the area where the plot should go
        *hideobserved* :
            if this is True, the observed value is not plotted
        *reference* :
            reference of the data. Could be either a string 'bootstrap'/'bayes' or a number
            against which the histogram is tested

    :Output:
        returns a boolean value indicating whether or not the Null-Hypothesis that
            observed was drawn from the same distribution as simdata is true
    """
    if ax is None:
        ax = p.axes()

    if reference.lower()[:5] == "boots":
        reference = observed
    elif reference.lower()[:5] == "bayes":
        reference = 0

    # Remove nan
    simdata = N.nan_to_num(simdata)
    simdata = simdata[simdata != 0]

    # Make sure we have a useful shortname
    if shortname is None:
        shortname = xname

    # Correlations plots should be treated differently
    if shortname[0] == "R":
        ax.hist(simdata, bins=N.arange(-1, 1, .1))
        ax.set_xlim(-1, 1)
    else:
        ax.hist(simdata, bins=20)

    # Get the tics and ranges
    # xtics = p.getp(ax,"xticks")
    # ytics = p.getp(ax,"yticks")
    # xr = xtics.max()-xtics.min()
    # yy = [ytics.min(),ytics.max()+0.02*xr]
    yy = p.array(ax.get_ylim())
    yy[1] += 0.02 * (yy[1] - yy[0])

    # Plot percentile bars
    if not hideobserved:
        c = parameterdict({"color": rc.highlight['color']})
        ax.plot([observed] * 2, yy, **(c + rc.line + rc.allplots))
    if shortname == "D":
        p95 = p.prctile(simdata, 95)
        ax.plot([p95] * 2, yy, ':', color=rc.highlight["color"])
    else:
        p25, p975 = p.prctile(simdata, (2.5, 97.5))
        ax.plot([p25] * 2,
                yy,
                'r:', [p975] * 2,
                yy,
                ':',
                color=rc.highlight['color'])

    # Draw the full plot
    ax.set_ylim(yy)

    # Write diagnostics
    if shortname == "D":
        ax.set_title(
            "%s=%.3f, %s_crit=%.3f" % (shortname, observed, shortname, p95),
            **(rc.text + rc.alltext))
        if reference < p95:
            return True
        else:
            return False
    else:
        ax.set_title(
            "%s=%.3f, c(2.5%%)=%.3f, c(97.5%%)=%.3f" %
            (shortname, observed, p25, p975), **(rc.text + rc.alltext))

        if reference > p25 and reference < p975:
            return True
        else:
            return False
예제 #23
0
def figure4 ( ):
    w,h = 27,9
    fig = pl.figure ( figsize=(fullwidth,h*fullwidth/w) )
    a,b,c,d,e = place_axes ( fig, 2, 2.5,
            [4]*2+[1,6,4,1,4],
            [6]*2+[0]+[6]*2+[0]+[6],
            [False]*3+[True]*4,
            [1]*7, (w,h) )
    # a,b,c = place_axes ( fig, 2, 8,   [7,7,7],[6,6,6], [False,False,True], [1.5]*3, (w,h) )
    # d,e = place_axes ( fig, 2, 1.5, [7,7,4],[6,6,0], [True]*3, [1.5]*3, (w,h) )
    a.text ( .05, laby, r"\textbf{a}", transform=a.transAxes )
    b.text ( .05, laby, r"\textbf{b}", transform=b.transAxes )
    c.text ( .05, laby, r"\textbf{c}", transform=c.transAxes )
    d.text ( .05, laby, r"\textbf{d}", transform=d.transAxes )
    e.text ( .05, laby, r"\textbf{e}", transform=e.transAxes )

    textfile.write ( "Figure 4:\n=========\n" )

    ecolors = list ( all_colors )
    # ecolors[all_observers.index('gbh')] = 'k'
    ecolors[all_observers.index(observer)] = 'y'

    if os.path.exists ( 'sim_backup/fig4_data.pcl' ):
        print "Loading fits"
        data,logstring = cPickle.load ( open ('sim_backup/fig4_data.pcl', 'r' ) )
    else:
        data = []
        logstring = []
        ldiff = []
        vhist = []
        vstim = []
        pH = []
        pSH = []
        lm = []
        th = []
        # logl_h,logl_0,vdiff,veasy,pH,pS,peasy,lm,th
        ind = [0,1,3,4,5,6,8,9,12,14]
        import scipy.io
        for o in all_observers:
            backup_file = os.path.join ( "sim_backup",o+".pcl" )
            results =  cPickle.load ( open ( backup_file, 'r' ) )
            d_ = load_observer ( o )[0]
            M = results['model_w_hist']
            if getattr ( d_, 'audio', False ):
                easy,difficult = d_.performance_filter ()
                C = statistics.EvaluationCollector ( M, easy, difficult )
            else:
                C = statistics.EvaluationCollector ( M )
            perm = results['permutation_wh']

            print "nu",M.nu,results['model_nohist'].nu

            ev = C(M)
            vstim.append ( C.vstimulus )
            vhist.append ( (C.vdifficult,pl.mean(C.vdifficult>perm[:,3])) )
            pH.append ( (C.phist,pl.mean(C.phist>perm[:,5])) )
            pSH.append ( C.pSH )
            print vhist[-1]

            ev_ = C(results['model_nohist'])
            N = len(M.r)
            logstring.append ( "  %s: l_obs=%g, l_aic=%g, cpe=%g, N=%d" % (o,ev[0],ev_[0]+6,pl.mean(ev[0]>perm[:,0]),N) )
            dat = ev[ind]
            ldiff.append ( (ev[0]-pl.mean(perm[:,0]))/N )
            dat[0] /= N
            dat[1] = (ev_[0]+6)/N
            # dat[1] = pl.prctile ( results['permutation_wh'][:,0], (95,) )/N
            # dat[1] = pl.mean(perm[:,0])/N
            lm_ = ev[[9,10]].sum()-ev_[[9,10]].sum()
            print "lm",ev[[9,10]].sum(),ev_[[9,10]].sum(),lm_
            lm.append ( (lm_,pl.mean(perm[:,9]-results['permutation_nh'][:,9]<lm_)) )
            dat[7] = lm_
            print dat[-2]

            # find optimal
            z0 = 100
            za = -100
            C = statistics.EvaluationCollector ( M )
            ewh = C(results['model_w_hist'])
            enh = C(results['model_nohist'])
            hf0 = C.hf0
            if thlev == .75:
                thind = 11
            elif thlev == .85:
                thind = 10+hf0
            else:
                raise ValueError
            aind = 13+2*(hf0-1)
            print "aind",aind
            for condition in xrange ( 1, M.hf0 ):
                s_wh = results['permutation_wh'][:,thind+condition]
                s_nh = results['permutation_nh'][:,thind+condition]
                s_ratio = s_wh/s_nh
                s_ratio_obs = ewh[thind+condition]/enh[thind+condition]
                # s_ratio_obs = results['model_w_hist'].w[condition]/results['model_nohist'].w[condition]
                z = (s_ratio_obs-pl.mean(s_ratio))/pl.std(s_ratio)
                cpe = pl.mean ( s_ratio < s_ratio_obs )
                ci = pl.prctile ( s_ratio, (2.5,97.5) )
                if s_ratio_obs < z0 and ci[1]-ci[0] > 0:
                    c0 = condition
                    s_ratio_ = s_ratio
                    s_ratio_obs_ = s_ratio_obs
                    ci_ = ci
                    z0 = s_ratio_obs

                a_wh = results['permutation_wh'][:,aind+condition]
                a_nh = results['permutation_nh'][:,aind+condition]
                a_ratio = a_wh/a_nh
                a_ratio_obs = ewh[aind+condition]/enh[aind+condition]
                print ewh[aind+condition],enh[aind+condition],
                z = (a_ratio_obs-pl.mean(a_ratio))/pl.std(a_ratio)
                print z
                cpe = pl.mean ( a_ratio < a_ratio_obs )
                ci = pl.prctile ( a_ratio, (2.5,97.5) )
                if a_ratio_obs>za and ci[1]-ci[0] > 0:
                    a0 = condition
                    a_ratio_ = a_ratio
                    a_ratio_obs_ = a_ratio_obs
                    cia = ci
                    za = a_ratio_obs

            dat[8] = s_ratio_obs_
            dat[9] = a_ratio_obs_
            th.append ( (s_ratio_obs_, pl.mean ( s_ratio_<s_ratio_obs ) ) )

            data.append ( dat )
        data = pl.array ( data )

        mdict = {'prediction_acc_from_full_model': pSH}
        scipy.io.savemat ( 'prediction_full_model.mat', mdict )

        vhist = pl.array ( vhist )
        pH = pl.array ( pH )
        lm = pl.array ( lm )
        print "lm = ", repr(lm)
        print "a =", repr(data[:,9])
        th = pl.array ( th )

        logstring.append ( "  average increase in log_likelihood per trial: %g +/- %g" %(pl.mean (ldiff), pl.std(ldiff)) )
        logstring.append ( "  average increase in log_likelihood beyond AIC: %g +/- %g" % \
                (pl.mean(data[:,0]-data[:,1]),pl.std(data[:,0]-data[:,1])) )
        logstring.append ( "  variance explained by history: %g +/- %g , (significant: %d/%d, max: %g)" %\
                (pl.mean(vhist[:,0]),pl.std(vhist[:,0]),pl.sum(vhist[:,1]<.05),vhist.shape[0], vhist[:,0].max() ) )
        logstring.append ( "  variance explained by stimulus: %g +/- %g" %(pl.mean(vstim),pl.std(vstim)) )
        logstring.append ( "  prediction pS: %g +/- %g" % (pl.mean(data[:,5]),pl.std(data[:,5])) )
        logstring.append ( "  prediction pH: %g +/- %g (%d/%d significant)" % \
                (pl.mean(data[:,4]),pl.std(data[:,4]),pl.sum(pH[:,1]<0.05),pH.shape[0]) )
        logstring.append ( "  prediction pSH: %g +/- %g" % (pl.mean(pSH),pl.std(pSH)) )
        logstring.append ( "  prediction on easy trials: %g +/- %g" % (pl.mean(data[:,6]),pl.std(data[:,6])) )
        logstring.append ( "  variance on easy trials: %g +/- %g" % (pl.mean(data[:,3]),pl.std(data[:,3])) )

        logstring.append ( "  asymptote ratio: %g +/- %g (significant: %d/%d)" %\
                (pl.mean(lm[:,0]),pl.std(lm[:,0]),pl.sum(lm[:,1]<0.05), lm.shape[0] ) )
        logstring.append ( "  threshold ratio: %g +/- %g (significant: %d/%d)" %\
                (pl.mean(th[:,0]),pl.std(th[:,0]),pl.sum(th[:,1]<0.05), th.shape[0] ) )
        print th

        logstring = "\n".join ( logstring )

        cPickle.dump ( (data,logstring), open ( 'sim_backup/fig4_data.pcl', 'w' ) )
    textfile.write ( logstring )
    data = pl.array ( data )

    # a.scatter ( data[:,0], data[:,1], c=all_colors, edgecolor=ecolors )
    boxdotplot ( a, data[:,0]-data[:,1], color=all_colors, ecolor=ecolors, jitter=0.05 )
    for o,d_ in zip ( all_observers, data[:,0]-data[:,1] ):
        print o,d_
    a.set_xlim ( -.5, 2 )
    # a.plot ( [0,-1],[0,-1], 'k:' )
    a.set_ylabel ( "Likelihood increase\n from history", multialignment='center', fontsize=8 )
    # a.set_ylabel ( r'95th percentile likelihood', horizontalalignment='center', fontsize=8 )
    # a.set_xlim ( a.set_ylim ( -.7, -.2 ) )
    a.yaxis.set_major_locator ( tckr ( density=0.4, figure=fig, which=0 ) )
    a.yaxis.set_major_formatter ( myformatter )
    # a.set_xticks ( (0,.1,.2) )

    boxdotplot ( b, data[:,2], color=all_colors, ecolor=ecolors, jitter=0.05 )
    b.set_ylabel ( 'Var. explained [\%]', fontsize=8 )
    b.yaxis.set_major_formatter ( prcformatter )
    b.set_xlim ( -.5, 2)
    b.set_ylim ( -.03, .53 )
    textfile.write ( "\nFigure 4b:\n" )
    textfile.write ( "  variance explained by history on difficult trials: %g +/- %g\n" % ( pl.mean ( data[:,2] ), pl.std(data[:,2]) ) )

    c.scatter ( data[:,4], data[:,5], s=5, c=all_colors, edgecolor=ecolors )
    c.plot ( [0,1],[0,1], 'k:' )
    c.set_xlabel ( 'Prediction acc.\n from history',fontsize=8, multialignment='center' )
    c.set_ylabel ( 'Prediction acc.\n from stimulus', multialignment='center',fontsize=8 )
    c.set_xlim ( c.set_ylim ( 0.5, .7 ) )
    c.xaxis.set_major_formatter ( prcformatter )
    c.yaxis.set_major_formatter ( prcformatter )


    # d.plot ( [0,1.2],[1,1],  'k:', zorder=0 )
    # d.plot ( [1,1], [0,1.2], 'k:', zorder=0 )
    # d.scatter ( data[:,7], data[:,8], c=all_colors, edgecolor=ecolors )
    boxdotplot ( d, data[:,7], color=all_colors, ecolor=ecolors, jitter=0.05 )
    boxdotplot ( e, data[:,8], color=all_colors, ecolor=ecolors, jitter=0.05 )
    # d.set_xlabel ( 'Asymptote ratio',fontsize=8 )
    # d.set_ylabel ( 'Threshold ratio', horizontalalignment='center',fontsize=8 )
    d.set_ylabel ( "Asymptote difference" )
    e.set_ylabel ( "Threshold ratio" )

    print "lm =",repr(data[:,7])

    print "THR=",repr(data[:,8])
    textfile.write ( "  max threshold ratio: %g\n" % (data[:,-1].max(),) )
    # d.xaxis.set_major_locator ( tckr ( density=.5, figure=fig, which=0 ) )
    # d.yaxis.set_major_locator ( tckr ( density=.5, figure=fig, which=1 ) )
    d.set_ylim ( -.05, .05 )
    # e.set_ylim ( 0, 2 )
    # d.xaxis.set_major_formatter ( myformatter )
    d.yaxis.set_major_formatter ( myformatter)
    e.yaxis.set_major_formatter ( myformatter )

    p = []
    l = []
    dummy_axes = fig.add_axes ( [2,2,1,1], xticks=(), yticks=() )
    for e,c in all_labels:
        p.append ( dummy_axes.plot( [0],[0],'o', color=c, markeredgecolor=c ) )
        l.append ( e )
    # igbh = all_observers.index ( 'gbh' )
    iobs = all_observers.index ( observer )
    # p.append ( dummy_axes.plot ( [0],[0], 'o', color=all_colors[igbh], markeredgecolor=ecolors[igbh] ) )
    # l.append ( 'J\&W (2006), gbh' )
    p.append ( dummy_axes.plot ( [0],[0], 'o', color=all_colors[iobs], markeredgecolor=ecolors[iobs] ) )
    l.append ( 'Observer KP' )

    fig.legend ( p, l, bbox_transform=fig.transFigure, numpoints=1, ncol=5, loc=8 )

    fig.savefig ( 'figures/%s4.pdf' % (figname,) )
    fig.savefig ( 'figures/%s4.eps' % (figname,) )
예제 #24
0
def _animation(fig):
    # Number of particles
    Num = 50
    # Step size
    stepSize = 0.02
    # Sensor Noise
    sNoi = 0.001
    # Movement Noise
    mNoi = 0.01
    # Min percentile for keeping particle
    minP = 25
    # Locations
    pts = rand(Num) + 1j * rand(Num)
    # Weights
    wgt = ones(Num)
    # Line
    abLine = asarray([.2 + 0.5j, .8 + 0.7j])
    # Actual position
    pos = 0.5 + 0.5j
    while True:
        ### "robot" simulation
        # Move "robot"
        step = (randn() + randn() * 1j) * stepSize
        pos += step
        # Get "robot" sensor measurement
        dpos = lineDist([pos], abLine[0], abLine[1])
        spos = lineSensorResponse(dpos, sNoi)
        ### Particle filter
        # Move particles
        pts += step + (randn(Num) + 1j * randn(Num)) * mNoi
        # Get particle sensor measurements
        d = lineDist(pts, abLine[0], abLine[1])
        s = lineSensorResponse(d, sNoi)
        # Adjust weights, keeping matching sensors with heigher weight
        #   We penalize sensor mismatch
        #   We penalize all high weights. Because average weight is reset to 1.0,
        #     this implies that in absence of
        #wgt = 1.0+abs(spos-s)*0.5+wgt*0.1
        wgt = wgt * 0.6 + 0.4 / (1.0 + abs(spos - s) * 0.5)
        wgt = Num * wgt / sum(wgt)
        # Find best particle
        best = argmax(wgt)
        # Replace low weight particles
        idx = find(wgt < prctile(wgt, minP))
        if any(idx):
            pts[idx] = pts[best]
            wgt[idx] = 1.0
        fig.clf()
        a = fig.add_subplot(121)
        a.set_title('weights')
        a.plot(abLine.real, abLine.imag, 'o-k', lw=3)
        a.scatter(pts.real,
                  pts.imag,
                  s=10 + wgt * wgt * 4,
                  c=linspace(0, 1, Num),
                  alpha=0.5)
        a.plot(pos.real, pos.imag, 'r+', ms=15, mew=3)
        a.plot(pts[best].real, pts[best].imag, 'bx', ms=15, mew=2)
        a.axis('equal')
        a.axis([-0.5, 1.5, -0.5, 1.5])
        a = fig.add_subplot(122)
        a.bar(arange(Num), wgt)
        yield
예제 #25
0
def plotHistogram ( simdata, observed, xname, shortname=None, ax=None, hideobserved=False, reference="bootstrap" ):
    """plot a histogram and compare observed data to it

    :Parameters:
        *simdata* :
            an array of monte-carlo samples of the parameter of interest
        *observed* :
            observed value of the parameter of interest (for MCMC samples, it is often
            reasonable to use this as the value of 'no effect' or something)
        *xname* :
            name of the paramter of interest
        *shortname* :
            short name of the parameter of interest
        *ax* :
            axes object defining the area where the plot should go
        *hideobserved* :
            if this is True, the observed value is not plotted
        *reference* :
            reference of the data. Could be either a string 'bootstrap'/'bayes' or a number
            against which the histogram is tested

    :Output:
        returns a boolean value indicating whether or not the Null-Hypothesis that
            observed was drawn from the same distribution as simdata is true
    """
    if ax is None:
        ax = p.axes()

    if reference.lower()[:5]==  "boots":
        reference = observed
    elif reference.lower()[:5]=="bayes":
        reference = 0

    # Remove nan
    simdata = N.nan_to_num ( simdata )
    simdata = simdata[simdata!=0]

    # Make sure we have a useful shortname
    if shortname is None:
        shortname = xname

    # Correlations plots should be treated differently
    if shortname[0] == "R":
        ax.hist ( simdata, bins=N.arange(-1,1,.1) )
        ax.set_xlim ( -1, 1 )
    else:
        ax.hist ( simdata, bins=20 )

    # Get the tics and ranges
    # xtics = p.getp(ax,"xticks")
    # ytics = p.getp(ax,"yticks")
    # xr = xtics.max()-xtics.min()
    # yy = [ytics.min(),ytics.max()+0.02*xr]
    yy = p.array(ax.get_ylim ())
    yy[1] += 0.02*(yy[1]-yy[0])

    # Plot percentile bars
    if not hideobserved:
        c = parameterdict ( {"color": rc.highlight['color']} )
        ax.plot ( [observed]*2, yy, **(c+rc.line+rc.allplots) )
    if shortname=="D":
        p95 = p.prctile ( simdata, 95 )
        ax.plot ( [p95]*2, yy, ':', color=rc.highlight["color"] )
    else:
        p25,p975 = p.prctile ( simdata, (2.5,97.5) )
        ax.plot ( [p25]*2, yy, 'r:', [p975]*2, yy, ':', color=rc.highlight['color'] )

    # Draw the full plot
    ax.set_ylim ( yy )

    # Write diagnostics
    if shortname=="D":
        ax.set_title ( "%s=%.3f, %s_crit=%.3f" % (shortname, observed, shortname, p95 ), **(rc.text+rc.alltext) )
        if reference < p95:
            return True
        else:
            return False
    else:
        ax.set_title ( "%s=%.3f, c(2.5%%)=%.3f, c(97.5%%)=%.3f" % (shortname,observed,p25,p975), **(rc.text+rc.alltext) )

        if reference>p25 and reference<p975:
            return True
        else:
            return False