示例#1
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
示例#2
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
def model2array():
    """
    Runs on python 2.7 from command line 2 argvs -  argv[1] results , argv [2] output . Easy!
    Collects the data from each individual file from Frund et al. 2014 tooolbox and generates  a np.array compatible with
    python3+
    :return: Returns and exports np.array with kernels
    """
    path_data = sys.argv[1]
    path_output = sys.argv[2]

    #Start uploading results
    hf = history.history_impulses()
    kernels = []
    mouse_names = []
    data_files = []
    os.chdir(path_data)
    for file in glob.glob("*.pcl"):
        data_files.append(file)

    for mouse in data_files:
        results_ = cPickle.load(open(path_data + mouse, 'r'))
        M = results_['model_w_hist']
        C = statistics.Kernel_and_Slope_Collector(hf, M.hf0, range(1, M.hf0))
        kernels.append(C(M))
        mouse_names.append(mouse[:-8])

    kernels = pl.array(kernels).T
    cPickle.dump(kernels, open(path_output + '/all_kernels.pkl', 'w'))
    cPickle.dump(mouse_names, open(path_output + '/mouse_names.pkl', 'w'))

    kr = []
    kz = []

    for mouse in mouse_names:
        i = mouse_names.index(mouse)
        al = kernels[14, i]
        kr.append(kernels[:7, i] * al)
        kz.append(kernels[7:14, i] * al)

    #kr = np.hstack([np.vstack(mouse_names), np.vstack(kr)])
    #kz = np.hstack([np.vstack(mouse_names), np.vstack(kz)])
    cPickle.dump(kr, open(path_output + '/kr.pkl', 'w'))
    np.save(path_output + '/kr.npy', kr)
    cPickle.dump(kz, open(path_output + '/kz.pkl', 'w'))
    np.save(path_output + '/kz.npy', kz)
    return
示例#4
0
def analysis(d,
             w0,
             nsamples=200,
             perm_collector=statistics.EvaluationCollector):
    """Analyze a dataset

    :Parameters:
        *d*
            a history.DataSet instance (typically a subclass of history.DataSet)
        *w0*
            starting values for the first parameters in the model. The remaining parameters will
            start at 0. It seems to be a good idea to give starting values for the stimulus dependent
            parameters in the model and leave the parameters for history features at 0.
        *nsamples*
            number of samples for monte carlo procedures
        *perm_collector*
            the collector object for the permutation tests. The default one should do for all
            experiments with a design matrix in which the first column is 1 and the second column
            refers to the slope.

    :Example:
    >>> d,w,plotinfo = load_plaid ()
    >>> results = analysis ( d, w, 10 )
    >>> results.keys()
    ['model_nohist', 'model_w_hist', 'bootstrap', 'permutation_nh', 'permutation_wh']
    """

    if d.__dict__.has_key('detection') and d.detection:
        dnh = d.__class__(d.fname, threshold=len(d.th_features) > 0)
    else:
        dnh = d.__class__(d.fname)  # reload the data without history
    dwh = d
    if getattr(d, 'audio', False):
        easy, difficult = d.performance_filter()
    else:
        easy, difficult = None, None
    logging.info("Fitting models")

    if getattr(d, 'p0', np.array(False)).all():
        # This code will only run for monkeys
        M0 = model.history_model(dnh.r,
                                 dnh.X,
                                 applythreshold=dnh.th_features,
                                 w0=w0,
                                 p0=d.p0,
                                 lm=0.1,
                                 hf0=dnh.hf0,
                                 emiter=40)
        Mnh = model.history_model(dnh.r,
                                  dnh.X,
                                  applythreshold=dnh.th_features,
                                  w0=M0.w,
                                  p0=M0.pi,
                                  lm=.1,
                                  hf0=dnh.hf0,
                                  verbose=True,
                                  emiter=300)
        logging.info("likelihood for independent responses: %g" %
                     (Mnh.loglikelihood, ))
        Mwh = model.history_model(dwh.r,
                                  dwh.X,
                                  applythreshold=dwh.th_features,
                                  w0=M0.w,
                                  p0=M0.pi,
                                  lm=.1,
                                  hf0=dwh.hf0,
                                  verbose=True,
                                  emiter=300)
        logging.info("likelihood for history model: %g" %
                     (Mwh.loglikelihood, ))
    else:
        Mnh, Mwh = search_for_start(d.r,
                                    d.X,
                                    w0,
                                    d.th_features,
                                    d.hf0,
                                    storeopt=True)
        logging.info("likelihood for independent responses: %g" %
                     (Mnh.loglikelihood, ))
        logging.info("likelihood for history model: %g" %
                     (Mwh.loglikelihood, ))

    print "nh", Mnh.w, Mnh.pi
    print "wh", Mwh.w, Mwh.pi
    print Mwh.applythreshold

    # Monte Carlo testing
    if nsamples > 0:
        r_, X_ = dwh.permutation()
        Mnh_perm, Mwh_perm = search_for_start(r_, X_, w0, d.th_features, d.hf0)

        logging.info("Permutation with history")
        dnh.rng.set_state(
            dwh.rng.get_state()
        )  # Set the states of the two random number generators to the same values to get the exact same sequence of random numbers
        perm_collector = statistics.EvaluationCollector(Mwh, easy, difficult)
        permutation_wh = pl.array(
            statistics.mcsimulation(dwh.permutation,
                                    perm_collector,
                                    nsamples,
                                    Mwh_perm.w,
                                    Mwh_perm.pi,
                                    Mwh_perm.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dwh.hf0,
                                    applythreshold=Mwh.applythreshold))

        logging.info("Permutation without history")
        perm_collector = statistics.EvaluationCollector(Mnh, easy, difficult)
        permutation_nh = pl.array(
            statistics.mcsimulation(dnh.permutation,
                                    perm_collector,
                                    nsamples,
                                    Mnh_perm.w,
                                    Mnh_perm.pi,
                                    Mnh_perm.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dnh.hf0,
                                    applythreshold=Mwh.applythreshold))

        logging.info("Bootstrap")
        kcollector = statistics.Kernel_and_Slope_Collector(dwh.h,
                                                           dwh.hf0,
                                                           slopeindex=range(
                                                               1, dwh.hf0))
        bootstrap = pl.array(
            statistics.mcsimulation(dwh.bootstrap,
                                    kcollector,
                                    nsamples,
                                    Mwh.w,
                                    Mwh.pi,
                                    Mwh.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dwh.hf0,
                                    applythreshold=Mwh.applythreshold))
    else:
        permutation_wh = None
        permutation_nh = None
        bootstrap = None

    results = {
        'model_nohist': Mnh,
        'model_w_hist': Mwh,
        'permutation_wh': permutation_wh,
        'permutation_nh': permutation_nh,
        'bootstrap': bootstrap
    }

    return results
示例#5
0
def figure5 ():
    w,h = 29,8
    fig = pl.figure ( figsize=(fullwidth,h*fullwidth/w) )
    a,b,c = place_axes ( fig, 2, 1, [8,1,6,1,6,6], [6,0,6,0,6,0], [True]*6, [1.]*6, (w,h) )
    dummy_axes = fig.add_axes ( [2,2,1,1], xticks=(), yticks=() )
    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 )

    if os.path.exists ( 'sim_backup/all_kernels.pcl' ):
        print "Loading kernels"
        kernels = cPickle.load ( open ('sim_backup/all_kernels.pcl', 'r' ) )
    else:
        hf = data.h
        kernels = []
        for o in all_observers:
            backup_file = os.path.join ( "sim_backup",o+".pcl" )
            results_ =  cPickle.load ( open ( backup_file, 'r' ) )
            M = results_['model_w_hist']
            C = statistics.Kernel_and_Slope_Collector (
                    hf, M.hf0, range(1,M.hf0) )
            kernels.append ( C(M) )
        kernels = pl.array ( kernels ).T
        cPickle.dump ( kernels, open ( 'sim_backup/all_kernels.pcl', 'w' ) )

    kl = convenience.kernelplot ( data, results, plotinfo, a, dummy_axes, 'upper right' )
    a.set_title ( '' )
    a.get_legend().get_frame().set_linewidth(0)
    pl.setp ( kl, markersize=2 )
    for o in ["cn","fb","ip","pk","xh"]:
        if not o is observer:
            i = all_observers.index(o)
            al = kernels[14,i]
            kr = kernels[:7,i]*al
            kz = kernels[7:14,i]*al
            a.plot ( pl.arange ( 1, 8 ), kz, '-', color=graphics.stimulus_color )
            a.plot ( pl.arange ( 1, 8 ), kr, '-', color=graphics.response_color )
    a.set_xlabel ( 'Lag' )
    a.set_ylabel ( "Equivalent contrast [\%]" )
    a.set_ylim ( -1, 1 )
    a.set_yticks ( (-1,0,1) )
    a.set_xlim ( .5,7.5 )
    a.xaxis.set_major_formatter ( myformatter )
    a.yaxis.set_major_formatter ( myformatter )

    b.scatter ( kernels[0,:], kernels[7,:], c=all_colors, edgecolor=ecolors )

    for i,o in enumerate ( all_observers ):
        print o,kernels[0,i], kernels[7,i]

    b.set_xlabel ( "Previous response" )
    b.set_ylabel ( "Previous stimulus" )
    b.set_xlim ( -2,1 )
    b.set_ylim ( -1,2 )
    b.set_xticks ( (-2,-1,0,1) )
    b.set_yticks ( (-1,0,1,2) )
    b.xaxis.set_major_formatter ( myformatter )
    b.yaxis.set_major_formatter ( myformatter )

    # b.set_yticklabels ( (-2,"",0,"",2) )
    # b.set_xticklabels ( (-2,"",0,"",2) )
    # b.xaxis.set_major_locator ( tckr ( density=.3, figure=fig, which=0 ) )
    # b.yaxis.set_major_locator ( tckr ( density=.3, figure=fig, which=1 ) )

    c.scatter ( kernels[1:7,:].sum(0), kernels[8:14,:].sum(0), c=all_colors, edgecolor=ecolors )
    c.set_xlabel ( "Summed response weight" )
    c.set_ylabel ( "Summed stimulus weight" )
    c.set_xlim ( c.set_ylim ( -.25,.25 ) )
    c.set_xticks ( (-1,-.5,0,.5,1) )
    c.set_yticks ( (-1,-.5,0,.5,1) )
    c.set_yticklabels ( (-1,"",0,"",1) )
    c.set_xticklabels ( (-1,"",0,"",1) )

    p = []
    l = []
    for e,c in all_labels:
        p.append ( dummy_axes.plot( [0],[0],'o', color=c, markeredgecolor=c ) )
        l.append ( e )
    iobs = all_observers.index ( observer )
    p.append ( dummy_axes.plot ( [0],[0], 'o', color=all_colors[iobs], markeredgecolor=ecolors[iobs] ) )
    l.append ( 'Observer KP' )
    leg = fig.legend ( p, l, loc='right', bbox_transform=fig.transFigure, numpoints=1 )
    leg.get_frame().set_linewidth(0)

    fig.savefig ( 'figures/%s5.pdf' % (figname,) )
    fig.savefig ( 'figures/%s5.eps' % (figname,) )
示例#6
0
def analysis(d,
             w0,
             nsamples=200,
             perm_collector=statistics.EvaluationCollector):
    """Analyze a dataset

    :Parameters:
        *d*
            a history.DataSet instance (typically a subclass of history.DataSet)
        *w0*
            starting values for the first parameters in the model. The remaining parameters will
            start at 0. It seems to be a good idea to give starting values for the stimulus dependent
            parameters in the model and leave the parameters for history features at 0.
        *nsamples*
            number of samples for monte carlo procedures
        *perm_collector*
            the collector object for the permutation tests. The default one should do for all
            experiments with a design matrix in which the first column is 1 and the second column
            refers to the slope.

    :Example:
    >>> d,w,plotinfo = load_plaid ()
    >>> results = analysis ( d, w, 10 )
    >>> results.keys()
    ['model_nohist', 'model_w_hist', 'bootstrap', 'permutation_nh', 'permutation_wh']
    """

    # get a version of the data without history and without modulation
    if d.__dict__.has_key('detection') and d.detection:
        dnh = d.__class__(d.fname, threshold=len(d.th_features) > 0)
    else:
        dnh = d.__class__(d.fname, )  # reload the data without history
        dnh.modulation = False
        dnh.doublemodulation = False

    if d.modulation:
        dhmod = copy.copy(d)  # this includes everything
        dhmod.modulation = True
        dhmod.doublemodulation = False

        # the data with history, but without modulation
        dwh = copy.copy(d)
        dwh = dwh.__class__(dwh.fname, dwh.h, False, None, False)
        dwh.modulation = False

    elif d.doublemodulation:
        dhmod = copy.copy(d)  # this includes everything
        dhmod.modulation = False
        dhmod.doublemodulation = True

        # the data with history, but without modulation
        dwh = copy.copy(d)
        dwh = dwh.__class__(dwh.fname, dwh.h, False, None, False)
        dwh.modulation = False
    else:
        dwh = d

    # return indices of difficult trials (p > 0.75) and difficult trials (p < 0.55)
    easy, difficult = d.performance_filter()
    logging.info("Fitting models")

    # check if we have the right sizes here
    print "d", np.shape(d.X), np.shape(d.r)
    print "dnh", np.shape(dnh.X), np.shape(dnh.r)
    print "dwh", np.shape(dwh.X), np.shape(dwh.r)
    if d.modulation or d.doublemodulation:
        print "dhmod", np.shape(dhmod.X), np.shape(dhmod.r)
    # here, the sizes seem OK

    Mnh, Mwh, Mhmod = search_for_start(d.r,
                                       d.X,
                                       w0,
                                       d.th_features,
                                       d.hf0,
                                       storeopt=True,
                                       modulation=d.modulation,
                                       doublemodulation=d.doublemodulation)
    logging.info("likelihood for independent responses: %g" %
                 (Mnh.loglikelihood, ))
    logging.info("likelihood for history model: %g" % (Mwh.loglikelihood, ))
    if d.modulation or d.doublemodulation:
        logging.info("likelihood for modulation + history model: %g" %
                     (Mhmod.loglikelihood, ))

    # check the shape of the design matrix
    print "nh", Mnh.w, Mnh.pi, np.shape(Mnh.X)
    print "wh", Mwh.w, Mwh.pi, np.shape(Mwh.X)
    if d.modulation or d.doublemodulation:
        print "hmod", Mhmod.w, Mhmod.pi, np.shape(Mhmod.X)

    print 'start monte carlo'
    # Monte Carlo testing
    if nsamples > 0:

        # now, dhmod has too few columns

        if d.modulation or d.doublemodulation:
            print 'permuting with modulation'
            r_, X_ = dhmod.permutation()  # permute the whole thing
        else:
            r_, X_ = dwh.permutation()  # permute the whole thing

        Mnh_perm, Mwh_perm, Mhmod_perm = search_for_start(
            r_,
            X_,
            w0,
            d.th_features,
            d.hf0,
            modulation=d.modulation,
            doublemodulation=d.doublemodulation)

        # Set the states of the random number generators to the same values to get the exact same sequence of random numbers
        dnh.rng.set_state(dwh.rng.get_state())
        if d.modulation or d.doublemodulation:
            dhmod.rng.set_state(dwh.rng.get_state())

        logging.info("Permutation without history")
        perm_collector = statistics.EvaluationCollector(Mnh, easy, difficult)
        permutation_nh = pl.array(
            statistics.mcsimulation(dnh.permutation,
                                    perm_collector,
                                    nsamples,
                                    Mnh_perm.w,
                                    Mnh_perm.pi,
                                    Mnh_perm.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dnh.hf0,
                                    applythreshold=Mwh.applythreshold))

        logging.info("Permutation with history, no modulation")
        perm_collector = statistics.EvaluationCollector(Mwh, easy, difficult)
        permutation_wh = pl.array(
            statistics.mcsimulation(dwh.permutation,
                                    perm_collector,
                                    nsamples,
                                    Mwh_perm.w,
                                    Mwh_perm.pi,
                                    Mwh_perm.nu,
                                    verbose=logging.root.level < 20,
                                    hf0=dwh.hf0,
                                    applythreshold=Mwh.applythreshold))

        if d.modulation or d.doublemodulation:
            logging.info("Permutation with history and modulatory interaction")
            perm_collector = statistics.EvaluationCollector(
                Mhmod, easy, difficult)

            # this is where the bug occurs!
            # do we have enough info about dhmod.permutation?
            print(dhmod.modulation)
            permutation_hmod = pl.array(
                statistics.mcsimulation(dhmod.permutation,
                                        perm_collector,
                                        nsamples,
                                        Mhmod_perm.w,
                                        Mhmod_perm.pi,
                                        Mhmod_perm.nu,
                                        verbose=logging.root.level < 20,
                                        hf0=dhmod.hf0,
                                        applythreshold=Mhmod.applythreshold))

        logging.info("Bootstrap")
        if d.modulation or d.doublemodulation:
            kcollector = statistics.Kernel_and_Slope_Collector(
                dhmod.h, dhmod.hf0, slopeindex=range(1, dhmod.hf0))
            bootstrap = pl.array(
                statistics.mcsimulation(dhmod.bootstrap,
                                        kcollector,
                                        nsamples,
                                        Mhmod.w,
                                        Mhmod.pi,
                                        Mhmod.nu,
                                        verbose=logging.root.level < 20,
                                        hf0=dhmod.hf0,
                                        applythreshold=Mhmod.applythreshold))
        else:
            # only bootstrap without the modulatory interaction
            kcollector = statistics.Kernel_and_Slope_Collector(
                dwh.h, dwh.hf0, slopeindex=range(1, dwh.hf0))
            bootstrap = pl.array(
                statistics.mcsimulation(dwh.bootstrap,
                                        kcollector,
                                        nsamples,
                                        Mwh.w,
                                        Mwh.pi,
                                        Mwh.nu,
                                        verbose=logging.root.level < 20,
                                        hf0=dwh.hf0,
                                        applythreshold=Mwh.applythreshold))

    else:  # dont permute anything
        permutation_wh = None
        permutation_nh = None
        permutation_hmod = None
        bootstrap = None

    if d.modulation or d.doublemodulation:
        results = {
            'model_nohist': Mnh,
            'model_w_hist': Mwh,
            'model_h_mod': Mhmod,
            'permutation_wh': permutation_wh,
            'permutation_nh': permutation_nh,
            'permutation_hmod': permutation_hmod,
            'bootstrap': bootstrap
        }
    else:
        results = {
            'model_nohist': Mnh,
            'model_w_hist': Mwh,
            'permutation_wh': permutation_wh,
            'permutation_nh': permutation_nh,
            'bootstrap': bootstrap
        }

    return results