예제 #1
0
파일: gA.py 프로젝트: erinaldi/c51scripts
def gA_bs(psql,params):
    # read data
    mq = params['gA_fit']['ml']
    basak = copy.deepcopy(params['gA_fit']['basak'])
    tag = params['gA_fit']['ens']['tag']
    stream = params['gA_fit']['ens']['stream']
    Nbs = params['gA_fit']['nbs']
    Mbs = params['gA_fit']['mbs']
    nstates = params['gA_fit']['nstates']
    barp = params[tag]['proton'][mq]
    print "fitting for gA mq %s, basak %s, ens %s%s, Nbs %s, Mbs %s" %(str(mq),str(basak),str(tag),str(stream),str(Nbs),str(Mbs))
    # read two point
    SSl = np.array([psql.data('dwhisq_corr_baryon',idx) for idx in [barp['meta_id']['SS'][i] for i in basak]])
    PSl = np.array([psql.data('dwhisq_corr_baryon',idx) for idx in [barp['meta_id']['PS'][i] for i in basak]])
    T = len(SSl[0,0])
    # plot data
    if params['flags']['plot_data']:
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS[0])), c51.make_gvars(SS), '%s %s ss' %(basak[b],str(mq)))
            c51.scatter_plot(np.arange(len(PS[0])), c51.make_gvars(PS), '%s %s ps' %(basak[b],str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(c51.make_gvars(SS), 1, 'log')
            meff_ps = eff.effective_mass(c51.make_gvars(PS), 1, 'log')
            xlim = [2, len(meff_ss)/3-2]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)), meff_ss, '%s %s ss effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)), meff_ps, '%s %s ps effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = eff.scaled_correlator(c51.make_gvars(SS), E0, phase=1.0)
            scaled_ps = eff.scaled_correlator(c51.make_gvars(PS), E0, phase=1.0)
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ss)), scaled_ss, '%s %s ss scaled correlator (take sqrt to get Z0_s)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ps)), scaled_ps, '%s %s ps scaled correlator (divide by Z0_s to get Z0_p)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            plt.show()
    # concatenate data
    SS = SSl[0]
    PS = PSl[0]
    for i in range(len(SSl)-1):
        SS = np.concatenate((SS,SSl[i+1]),axis=1)
        PS = np.concatenate((PS,PSl[i+1]),axis=1)
    boot0 = np.concatenate((SS, PS), axis=1)
    # read priors
    prior = c51.baryon_priors(barp['priors'],basak,nstates)
    ## read trange
    trange = barp['trange']
    ## fit boot0
    boot0gv = c51.make_gvars(boot0)
    boot0p = c51.dict_of_tuple_to_gvar(prior)
    fitfcn = c51.fit_function(T,nstates)
    boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.twopt_baryon_ss_ps,basak=params['gA_fit']['basak'])
    print boot0fit['rawoutput'][0]
예제 #2
0
def fit_mixed_meson(psql,params,meson, datagv,bootn_fit=False,g=None):
    tag = params['mixed_fit']['ens']['tag']
    stream = params['mixed_fit']['ens']['stream']
    nstates = params['mixed_fit']['nstates']
    ml = "0.%s" %tag.split('m')[1]
    ms = "0.%s" %tag.split('m')[2]
    #print ml, ms
    if meson in ['phi_ju']:
        mq1 = ml
        mq2 = params['mixed_fit']['ml']
    elif meson in ['phi_js']:
        mq1 = ml
        mq2 = params['mixed_fit']['ms']
    elif meson in ['phi_ru']:
        mq1 = ms
        mq2 = params['mixed_fit']['ml']
    elif meson in ['phi_rs']:
        mq1 = ms
        mq2 = params['mixed_fit']['ms']
    #print 'fitting two point', mq1, mq2
    mesp = params[tag][meson]['%s_%s' %(str(mq1),str(mq2))]
    T = len(datagv)
    # plot effective mass / scaled correlator
    if params['flags']['plot_data']:
        # unfolded correlator data
        c51.scatter_plot(np.arange(len(datagv)), datagv, '%s_%s ps' %(str(mq1),str(mq2)))
        plt.show()
        # effective mass
        eff = c51.effective_plots(T)
        meff = eff.effective_mass(datagv, 1, 'cosh')
        xlim = [3, len(meff)/2-2]
        ylim = c51.find_yrange(meff, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff)), meff, '%s_%s effective mass' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        plt.show()
        # scaled correlator
        E0 = mesp['priors'][1]['E0'][0]
        scaled = eff.scaled_correlator(datagv, E0, phase=1.0)
        ylim = c51.find_yrange(scaled, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled)), scaled, '%s_%s scaled correlator (A0)' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        plt.show()
    # read priors
    if bootn_fit:
        if g == 0:
            priorn = mesp['priors']
        else:
            priorn = dict()
            for n in mesp['priors'].keys():
                priorn[n] = {}
                for k in mesp['priors'][n].keys():
                    priorn[n][k] = [np.random.normal(mesp['priors'][n][k][0],mesp['priors'][n][k][1]), mesp['priors'][n][k][1]]
    else:
        priorn = mesp['priors']
    prior = c51.meson_priors(priorn,nstates)
    # read init
    try:
        tstring = '%s_%s' %(mesp['trange']['tmin'][0], mesp['trange']['tmax'][0])
        f_init = open('./mixed_posterior/mixed_%s_%s.yml' %(tag,tstring), 'r')
        init = yaml.load(f_init)
        f_init.close()
    except:
        init = None
    # read trange
    trange = mesp['trange']
    # fit boot0
    boot0gv = datagv
    boot0p = c51.dict_of_tuple_to_gvar(prior)
    #print "T:", T
    fitfcn = c51.fit_function(T,params['mixed_fit']['nstates'])
    boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.mixed_twopt_osc,init=init,bayes=params['flags']['bayes'])
    #print boot0fit['rawoutput'][0]
    if params['flags']['boot0_update']:
        post = boot0fit['rawoutput'][0].pmean
        mes_dump = dict()
        tstring = "%s_%s" %(trange['tmin'][0],trange['tmax'][0])
        for k in post.keys():
            mes_dump[k] = float(post[k])
        f_dump = open('./mixed_posterior/mixed_%s_%s.yml' %(tag,tstring), 'w+')
        yaml.dump(mes_dump, f_dump)
        f_dump.flush()
        f_dump.close()
    if params['flags']['stability_plot']:
        c51.stability_plot(boot0fit,'E0','%s_%s' %(str(mq1),str(mq2)))
        plt.show()
    if params['flags']['tabulate']:
        tbl_print = collections.OrderedDict()
        tbl_print['tmin'] = boot0fit['tmin']
        tbl_print['tmax'] = boot0fit['tmax']
        tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['A0'] = [boot0fit['pmean'][t]['A0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dA0'] = [boot0fit['psdev'][t]['A0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['E1'] = [boot0fit['pmean'][t]['E1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dE1'] = [boot0fit['psdev'][t]['E1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['A1'] = [boot0fit['pmean'][t]['A1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dA1'] = [boot0fit['psdev'][t]['A1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['chi2/dof'] = np.array(boot0fit['chi2'])/np.array(boot0fit['dof'])
        tbl_print['logGBF'] = boot0fit['logGBF']
        print tabulate(tbl_print, headers='keys')
    return {'mixed_meson_fit': boot0fit['rawoutput'][0]}
예제 #3
0
def fit_hisq_meson(psql,params,meson, datagv,bootn_fit=False,g=None):
    if meson in ['phi_jj_5', 'phi_jj_I']:
        mq1 = params['hisq_fit']['ml']
        mq2 = mq1
    elif meson in ['phi_jr_5', 'phi_jr_I']:
        mq1 = params['hisq_fit']['ml']
        mq2 = params['hisq_fit']['ms']
    elif meson in ['phi_rr_5', 'phi_rr_I']:
        mq1 = params['hisq_fit']['ms']
        mq2 = mq1
    print 'fitting two point', mq1, mq2
    tag = params['hisq_fit']['ens']['tag']
    stream = params['hisq_fit']['ens']['stream']
    nstates = params['hisq_fit']['nstates']
    mesp = params[tag][meson]['%s_%s' %(str(mq1),str(mq2))]
    T = len(datagv)
    # plot effective mass / scaled correlator
    if params['flags']['plot_data']:
        # unfolded correlator data
        c51.scatter_plot(np.arange(len(datagv)), datagv, '%s_%s ss' %(str(mq1),str(mq2)))
        plt.show()
        # effective mass
        eff = c51.effective_plots(T)
        meff = eff.effective_mass(datagv, 1, 'cosh')
        xlim = [3, len(meff)/2-2]
        ylim = c51.find_yrange(meff, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff)), meff, '%s_%s effective mass' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        plt.show()
        # scaled correlator
        E0 = mesp['priors'][1]['E0'][0]
        scaled = eff.scaled_correlator_v2(datagv, E0, phase=1.0)
        ylim = c51.find_yrange(scaled, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled)), scaled, '%s_%s scaled correlator (A0)' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        plt.show()
    # read priors
    if bootn_fit:
        if g == 0:
            priorn = mesp['priors']
        else:
            priorn = dict()
            for n in mesp['priors'].keys():
                priorn[n] = {}
                for k in mesp['priors'][n].keys():
                    priorn[n][k] = [np.random.normal(mesp['priors'][n][k][0],mesp['priors'][n][k][1]), mesp['priors'][n][k][1]]
    else:
        priorn = mesp['priors']
    prior = c51.meson_priors(priorn,nstates)
    # read init
    try:
        tstring = '%s_%s' %(mesp['trange']['tmin'][0], mesp['trange']['tmax'][0])
        f_init = open('./hisq_posterior/hisq_%s_%s.yml' %(tag,tstring), 'r')
        init = yaml.load(f_init)
        f_init.close()
    except:
        init = None
    # read trange
    trange = mesp['trange']
    # fit boot0
    boot0gv = datagv
    boot0p = c51.dict_of_tuple_to_gvar(prior)
    fitfcn = c51.fit_function(T,params['hisq_fit']['nstates'])
    boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.mixed_twopt_osc,init=init,bayes=params['flags']['bayes'])
    if params['flags']['boot0_update']:
        print boot0fit['rawoutput'][0]
        post = boot0fit['rawoutput'][0].pmean
        mes_dump = dict()
        tstring = "%s_%s" %(trange['tmin'][0],trange['tmax'][0])
        for k in post.keys():
            mes_dump[k] = float(post[k])
        f_dump = open('./hisq_posterior/hisq_%s_%s.yml' %(tag,tstring), 'w+')
        yaml.dump(mes_dump, f_dump)
        f_dump.flush()
        f_dump.close()
    if params['flags']['stability_plot']:
        c51.stability_plot(boot0fit,'E0','%s_%s' %(str(mq1),str(mq2)))
        plt.show()
    if params['flags']['tabulate']:
        tbl_print = collections.OrderedDict()
        tbl_print['tmin'] = boot0fit['tmin']
        tbl_print['tmax'] = boot0fit['tmax']
        tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['A0'] = [boot0fit['pmean'][t]['A0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dA0'] = [boot0fit['psdev'][t]['A0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['E1'] = [boot0fit['pmean'][t]['E1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dE1'] = [boot0fit['psdev'][t]['E1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['A1'] = [boot0fit['pmean'][t]['A1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dA1'] = [boot0fit['psdev'][t]['A1'] for t in range(len(boot0fit['pmean']))]
        tbl_print['chi2/dof'] = [boot0fit['chi2'][t]/boot0fit['dof'][t] for t in range(len(boot0fit['pmean']))]
        tbl_print['logGBF'] = boot0fit['logGBF']
        print tabulate(tbl_print, headers='keys')
        if params['flags']['fitline_plot']:
            p = boot0fit['rawoutput'][0].p
            t = np.linspace(0, 30, 1000)
            # output fit curve
            fitline = fitfcn.mixed_twopt_osc(t,p)
            meff = np.arccosh( (np.roll(fitline,-1)+np.roll(fitline,1)) / (2*fitline) )
            f_plot = open('./hisq_fitline/%s_mixed_twopt.csv' %(tag), 'w+')
            string = 't, y, +-\n'
            for i in range(len(t)):
                string += '%s, %s, %s\n' %(t[i], meff[i].mean, meff[i].sdev)
            f_plot.write(string)
            f_plot.flush()
            f_plot.close()
    return {'hisq_meson_fit': boot0fit['rawoutput'][0]}
예제 #4
0
def fit_axial(psql,
              params,
              dparams,
              meson,
              datagv,
              inherit_prior=None,
              flow=False):
    if meson == 'axial_ll':
        mq1 = params['axial_fit']['ml']
        mq2 = mq1
        dmeson = 'pion'
    elif meson == 'axial_ls':
        mq1 = params['axial_fit']['ml']
        mq2 = params['axial_fit']['ms']
        dmeson = 'kaon'
    print 'fitting axial two point', mq1, mq2
    tag = params['axial_fit']['ens']['tag']
    stream = params['axial_fit']['ens']['stream']
    nstates = params['axial_fit']['nstates']
    axialp = params[tag][meson]['%s_%s' % (str(mq1), str(mq2))]
    mesonp = dparams[tag][dmeson]['%s_%s' % (str(mq1), str(mq2))]
    T = len(datagv) / 3
    SS = datagv[:T]
    PS = datagv[T:2 * T]
    ax = datagv[2 * T:]
    # plot effective mass / scaled correlator
    if params['flags']['plot_data']:
        # unfolded correlator data
        c51.scatter_plot(np.arange(T), ax, '%s_%s ps' % (str(mq1), str(mq2)))
        plt.show()
        # effective mass
        eff = c51.effective_plots(T)
        meff_ps = eff.effective_mass(ax[1:T / 2], 1, 'cosh')
        xlim = [2, len(meff_ps) / 2 - 2]
        ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff_ps)),
                         meff_ps,
                         '$%s %s$ ps effective mass' % (str(mq1), str(mq2)),
                         xlim=xlim,
                         ylim=ylim)
        plt.show()
        # scaled correlator
        E0 = mesonp['priors'][1]['E0'][0]
        print E0
        z_s = np.sqrt(eff.scaled_correlator_v2(SS, E0, phase=1.0))
        scaled_ax = eff.scaled_correlator_v2(ax, E0, phase=1.0) / z_s
        ylim = c51.find_yrange(scaled_ax, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled_ax)),
                         scaled_ax,
                         '$%s %s$ f0 scaled axial correlator' %
                         (str(mq1), str(mq2)),
                         xlim=xlim,
                         ylim=ylim)
        plt.show()
    # read priors
    if inherit_prior == None:
        prior = c51.meson_priors(mesonp['priors'], nstates)
        axp = c51.meson_priors(axialp['priors'], nstates)
        for k in axp.keys():
            prior[k] = axp[k]
        prior = c51.dict_of_tuple_to_gvar(prior)
    else:
        prior = c51.meson_priors(mesonp['priors'], nstates)
        axp = c51.meson_priors(axialp['priors'], nstates)
        for k in axp.keys():
            prior[k] = axp[k]
        prior = c51.dict_of_tuple_to_gvar(prior)
        for k in inherit_prior.keys():
            prior[k] = inherit_prior[k]
    # read trange
    trange = axialp['trange']
    dtrange = mesonp['trange']
    print "PRIOR:", prior
    # fit
    fitfcn = c51.fit_function(T, params['axial_fit']['nstates'])
    if flow:
        boot0fit = c51.fitscript_v3(dtrange,
                                    trange,
                                    T,
                                    datagv,
                                    prior,
                                    fitfcn.dwhisq_twopt_osc_axial,
                                    axial=True)
    else:
        boot0fit = c51.fitscript_v3(dtrange,
                                    trange,
                                    T,
                                    datagv,
                                    prior,
                                    fitfcn.dwhisq_twopt_axial,
                                    axial=True)
    #print boot0fit['rawoutput'][0]
    if params['flags']['stability_plot']:
        c51.stability_plot(boot0fit, 'E0', '%s_%s' % (str(mq1), str(mq2)))
        c51.stability_plot(boot0fit, 'F0', '%s_%s' % (str(mq1), str(mq2)))
        plt.show()
    if params['flags']['tabulate']:
        tbl_print = collections.OrderedDict()
        tbl_print['tmin'] = boot0fit['tmin']
        tbl_print['tmax'] = boot0fit['tmax']
        tbl_print['Fphi'] = [
            fphi(boot0fit['post'][t]['F0'], boot0fit['post'][t]['E0']).mean
            for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dFphi'] = [
            fphi(boot0fit['post'][t]['F0'], boot0fit['post'][t]['E0']).sdev
            for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['E0'] = [
            boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dE0'] = [
            boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['Z0_s'] = [
            boot0fit['pmean'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dZ0_s'] = [
            boot0fit['psdev'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['Z0_p'] = [
            boot0fit['pmean'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dZ0_p'] = [
            boot0fit['psdev'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['F0'] = [
            boot0fit['pmean'][t]['F0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dF0'] = [
            boot0fit['psdev'][t]['F0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['F0*Z0_s'] = [(boot0fit['rawoutput'][t].p['F0'] *
                                 boot0fit['rawoutput'][t].p['Z0_s']).mean
                                for t in range(len(boot0fit['rawoutput']))]
        tbl_print['dF0*Z0_s'] = [(boot0fit['rawoutput'][t].p['F0'] *
                                  boot0fit['rawoutput'][t].p['Z0_s']).sdev
                                 for t in range(len(boot0fit['rawoutput']))]
        tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
            boot0fit['dof'])
        tbl_print['logGBF'] = boot0fit['logGBF']
        print tabulate(tbl_print, headers='keys')
    return {'axial_fit': boot0fit['rawoutput'][0]}
예제 #5
0
def fit_decay_bs(psql,params,meson, gv_SS, gv_PS):
    if meson=='pion':
        mq1 = params['decay_ward_fit']['ml']
        mq2 = mq1
    elif meson == 'kaon':
        mq1 = params['decay_ward_fit']['ml']
        mq2 = params['decay_ward_fit']['ms']
    elif meson == 'etas':
        mq1 = params['decay_ward_fit']['ms']
        mq2 = mq1
    print 'fitting two point', mq1, mq2
    tag = params['decay_ward_fit']['ens']['tag']
    stream = params['decay_ward_fit']['ens']['stream']
    nstates = params['decay_ward_fit']['nstates']
    mesp = params[tag][meson]['%s_%s' %(str(mq1),str(mq2))]
    T = len(gv_SS)
    # plot effective mass / scaled correlator
    if params['flags']['plot_data']:
        # unfolded correlator data
        c51.scatter_plot(np.arange(len(gv_SS)), gv_SS, '%s_%s ss folded' %(str(mq1),str(mq2)))
        c51.scatter_plot(np.arange(len(gv_PS)), gv_PS, '%s_%s ps folded' %(str(mq1),str(mq2)))
        plt.show()
        # effective mass
        eff = c51.effective_plots(T)
        meff_ss = eff.effective_mass(gv_SS, 1, 'cosh')
        meff_ps = eff.effective_mass(gv_PS, 1, 'cosh')
        xlim = [3, len(meff_ss)/2-2]
        ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff_ss)), meff_ss, '%s_%s ss effective mass' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff_ps)), meff_ps, '%s_%s ps effective mass' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        plt.show()
        # scaled correlator
        E0 = mesp['priors']['1']['E0'][0]
        scaled_ss = eff.scaled_correlator(gv_SS, E0, phase=1.0)
        scaled_ps = eff.scaled_correlator(gv_PS, E0, phase=1.0)
        ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled_ss)), scaled_ss, '%s_%s ss scaled correlator (take sqrt to get Z0_s)' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled_ps)), scaled_ps, '%s_%s ps scaled correlator (divide by Z0_s to get Z0_p)' %(str(mq1),str(mq2)), xlim = xlim, ylim = ylim)
        plt.show()
    # concatenate data
    boot0gv = np.concatenate((gv_SS, gv_PS))
    # read priors
    prior = c51.meson_priors(mesp['priors'],nstates)
    # read trange
    trange = mesp['trange']
    # fit boot0
    boot0p = c51.dict_of_tuple_to_gvar(prior)
    fitfcn = c51.fit_function(T,params['decay_ward_fit']['nstates'])
    boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.twopt_fitfcn_ss_ps)
    if params['flags']['stability_plot']:
        c51.stability_plot(boot0fit,'E0','%s_%s' %(str(mq1),str(mq2)))
        plt.show()
    if params['flags']['tabulate']:
        tbl_print = collections.OrderedDict()
        tbl_print['tmin'] = boot0fit['tmin']
        tbl_print['tmax'] = boot0fit['tmax']
        tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
        tbl_print['Z0_s'] = [boot0fit['pmean'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dZ0_s'] = [boot0fit['psdev'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))]
        tbl_print['Z0_p'] = [boot0fit['pmean'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))]
        tbl_print['dZ0_p'] = [boot0fit['psdev'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))]
        tbl_print['chi2/dof'] = np.array(boot0fit['chi2'])/np.array(boot0fit['dof'])
        tbl_print['logGBF'] = boot0fit['logGBF']
        print tabulate(tbl_print, headers='keys')
    # submit boot0 to db
    if params['flags']['write']:
        corr_lst = [mesp['meta_id']['SS'],mesp['meta_id']['PS']]
        fit_id = c51.select_fitid('meson',params)
        for t in range(len(boot0fit['tmin'])):
            init_id = psql.initid(boot0fit['p0'][t])
            prior_id = psql.priorid(boot0fit['prior'][t])
            tmin = boot0fit['tmin'][t]
            tmax = boot0fit['tmax'][t]
            result = c51.make_result(boot0fit,t)
            psql.submit_boot0('meson',corr_lst,fit_id,tmin,tmax,init_id,prior_id,result,params['flags']['update'])
    if len(boot0fit['tmin'])!=1 or len(boot0fit['tmax'])!=1:
        print "sweeping over time range"
        print "skipping bootstrap"
        return 0
    else: pass
    if False: #params['flags']['write']:
        bsresult = []
        psql.chkbsprior(tag,stream,Nbs,boot0fit['prior'][0])
        for g in tqdm.tqdm(range(Nbs)):
            # make bs gvar dataset
            n = g+1
            bs_id,draw = psql.fetchonebs(nbs=n,Mbs=Mbs,ens=tag,stream=stream)
            bootn = boot0[draw]
            bootngv = c51.make_gvars(bootn)
            # read randomized priors
            bsprior_id,bsp = psql.bspriorid(tag,stream,g,boot0fit['prior'][0])
            bsp = c51.dict_of_tuple_to_gvar(bsp)
            # read initial guess
            init = c51.read_init(boot0fit,0) #{"mres":boot0fit['pmean'][0]['mres']}
            #Fit
            bsfit = c51.fitscript_v2(trange,T,bootngv,bsp,fitfcn.twopt_fitfcn_ss_ps,init)
            tmin = bsfit['tmin'][0]
            tmax = bsfit['tmax'][0]
            boot0_id = psql.select_boot0('meson',corr_lst,fit_id,tmin,tmax,init_id,prior_id)
            bsinit_id = psql.initid(bsfit['p0'][0])
            result = c51.make_result(bsfit,0) #"""{"mres":%s, "chi2":%s, "dof":%s}""" %(bsfit['pmean'][t]['mres'],bsfit['chi2'][t],bsfit['dof'][t])
            psql.submit_bs('meson_bs',boot0_id,bs_id,Mbs,bsinit_id,bsprior_id,result,params['flags']['update'])
    return {'meson_fit': boot0fit['rawoutput'][0]}
예제 #6
0
def fit_decay_bs(psql,
                 params,
                 meson,
                 gv_SS,
                 gv_PS,
                 bootn_fit=False,
                 g=None,
                 flow=False):
    if meson == 'pion':
        mq1 = params['decay_ward_fit']['ml']
        mq2 = mq1
    elif meson == 'kaon':
        mq1 = params['decay_ward_fit']['ml']
        mq2 = params['decay_ward_fit']['ms']
    elif meson == 'etas':
        mq1 = params['decay_ward_fit']['ms']
        mq2 = mq1
    tag = params['decay_ward_fit']['ens']['tag']
    stream = params['decay_ward_fit']['ens']['stream']
    nstates = params['decay_ward_fit']['nstates']
    mesp = params[tag][meson]['%s_%s' % (str(mq1), str(mq2))]
    T = len(gv_SS)
    # plot effective mass / scaled correlator
    if params['flags']['plot_data']:
        # unfolded correlator data
        c51.scatter_plot(np.arange(len(gv_SS)), gv_SS,
                         '%s %s' % (str(mq1), str(mq2)), '$t$', 'C(t)_{SS}')
        c51.scatter_plot(np.arange(len(gv_PS)), gv_PS,
                         '%s %s' % (str(mq1), str(mq2)), '$t$', 'C(t)_{PS}')
        plt.show()
        # effective mass
        eff = c51.effective_plots(T)
        meff_ss = eff.effective_mass(gv_SS, 1, 'cosh')
        meff_ps = eff.effective_mass(gv_PS, 1, 'cosh')
        xlim = [3, len(meff_ss) / 2 - 2]
        ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff_ss)),
                         meff_ss,
                         '%s %s' % (str(mq1), str(mq2)),
                         '$t$',
                         '$M^{eff}_{SS}$',
                         xlim=xlim,
                         ylim=ylim)
        ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff_ps)),
                         meff_ps,
                         '%s %s' % (str(mq1), str(mq2)),
                         '$t$',
                         '$M^{eff}_{PS}$',
                         xlim=xlim,
                         ylim=ylim)
        plt.show()
        # scaled correlator
        E0 = mesp['priors'][1]['E0'][0]
        print E0
        scaled_ss = np.sqrt(eff.scaled_correlator_v2(gv_SS, E0, phase=1.0))
        scaled_ps = eff.scaled_correlator_v2(gv_PS, E0, phase=1.0) / scaled_ss
        ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled_ss)),
                         scaled_ss,
                         '%s %s' % (str(mq1), str(mq2)),
                         '$t$',
                         '$Z0_s$',
                         xlim=xlim,
                         ylim=ylim)
        ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled_ps)),
                         scaled_ps,
                         '%s %s' % (str(mq1), str(mq2)),
                         '$t$',
                         '$Z0_p$',
                         xlim=xlim,
                         ylim=ylim)
        plt.show()
    # concatenate data
    boot0gv = np.concatenate((gv_SS, gv_PS))
    # read priors
    if bootn_fit:
        if g == 0:
            priorn = mesp['priors']
        else:
            priorn = dict()
            for n in mesp['priors'].keys():
                priorn[n] = {}
                for k in mesp['priors'][n].keys():
                    priorn[n][k] = [
                        np.random.normal(mesp['priors'][n][k][0],
                                         mesp['priors'][n][k][1]),
                        mesp['priors'][n][k][1]
                    ]
    else:
        priorn = mesp['priors']
    prior = c51.meson_priors(priorn, nstates)
    # read init
    try:
        tstring = '%s_%s' % (mesp['trange']['tmin'][0],
                             mesp['trange']['tmax'][0])
        f_init = open('./meson_posterior/meson_%s_%s.yml' % (tag, tstring),
                      'r')
        init = yaml.load(f_init)
        f_init.close()
    except:
        init = None
    # read trange
    trange = mesp['trange']
    # fit boot0
    boot0p = c51.dict_of_tuple_to_gvar(prior)
    fitfcn = c51.fit_function(T, params['decay_ward_fit']['nstates'])
    if flow:
        boot0fit = c51.fitscript_v2(trange,
                                    T,
                                    boot0gv,
                                    boot0p,
                                    fitfcn.dwhisq_twopt_osc_ss_ps,
                                    bayes=params['flags']['bayes'])
    else:
        boot0fit = c51.fitscript_v2(trange,
                                    T,
                                    boot0gv,
                                    boot0p,
                                    fitfcn.twopt_fitfcn_ss_ps,
                                    init=init,
                                    bayes=params['flags']['bayes'])
    #print boot0fit['rawoutput'][0]
    if params['flags']['boot0_update']:
        post = boot0fit['rawoutput'][0].pmean
        mes_dump = dict()
        tstring = "%s_%s" % (trange['tmin'][0], trange['tmax'][0])
        for k in post.keys():
            mes_dump[k] = float(post[k])
        f_dump = open('./meson_posterior/meson_%s_%s.yml' % (tag, tstring),
                      'w+')
        yaml.dump(mes_dump, f_dump)
        f_dump.flush()
        f_dump.close()
    if params['flags']['stability_plot']:
        c51.stability_plot(boot0fit, 'E0', '%s_%s' % (str(mq1), str(mq2)))
        plt.show()
    if params['flags']['tabulate']:
        tbl_print = collections.OrderedDict()
        tbl_print['tmin'] = boot0fit['tmin']
        tbl_print['tmax'] = boot0fit['tmax']
        tbl_print['E0'] = [
            boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dE0'] = [
            boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['Z0_s'] = [
            boot0fit['pmean'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dZ0_s'] = [
            boot0fit['psdev'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['Z0_p'] = [
            boot0fit['pmean'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dZ0_p'] = [
            boot0fit['psdev'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
            boot0fit['dof'])
        tbl_print['logGBF'] = boot0fit['logGBF']
        print tabulate(tbl_print, headers='keys')
    return {'meson_fit': boot0fit['rawoutput'][0]}
예제 #7
0
파일: gA.py 프로젝트: cchang5/c51scripts
def gA_bs(psql,params):
    # read data
    mq = params['gA_fit']['ml']
    basak = copy.deepcopy(params['gA_fit']['basak'])
    tag = params['gA_fit']['ens']['tag']
    stream = params['gA_fit']['ens']['stream']
    Nbs = params['gA_fit']['nbs']
    Mbs = params['gA_fit']['mbs']
    nstates = params['gA_fit']['nstates']
    tau = params['gA_fit']['tau']
    barp = params[tag]['proton'][mq]
    fhbp = params[tag]['gA'][mq]
    print "fitting for gA mq %s, basak %s, ens %s%s, Nbs %s, Mbs %s" %(str(mq),str(basak),str(tag),str(stream),str(Nbs),str(Mbs))
    # read two point
    SSl = np.array([psql.data('dwhisq_corr_baryon',idx) for idx in [barp['meta_id']['SS'][i] for i in basak]])
    PSl = np.array([psql.data('dwhisq_corr_baryon',idx) for idx in [barp['meta_id']['PS'][i] for i in basak]])
    T = len(SSl[0,0])
    # read fh correlator
    fhSSl = np.array([psql.data('dwhisq_fhcorr_baryon',idx) for idx in [fhbp['meta_id']['SS'][i] for i in basak]])
    fhPSl = np.array([psql.data('dwhisq_fhcorr_baryon',idx) for idx in [fhbp['meta_id']['PS'][i] for i in basak]])
    # concatenate and make gvars to preserve correlations
    SS = SSl[0]
    PS = PSl[0]
    for i in range(len(SSl)-1): # loop over basak operators
        SS = np.concatenate((SS,SSl[i+1]),axis=1)
        PS = np.concatenate((PS,PSl[i+1]),axis=1)
    fhSS = fhSSl[0]
    fhPS = fhPSl[0]
    for i in range(len(fhSSl)-1):
        fhSS = np.concatenate((fhSS,fhSSl[i+1]),axis=1)
        fhPS = np.concatenate((fhPS,fhPSl[i+1]),axis=1)
    boot0 = np.concatenate((SS, PS, fhSS, fhPS), axis=1)
    # make gvars
    gvboot0 = c51.make_gvars(boot0)
    spec = gvboot0[:len(gvboot0)/2]
    fh = gvboot0[len(gvboot0)/2:]
    # R(t)
    Rl = fh/spec
    # dmeff [R(t+tau) - R(t)] / tau
    dM = (np.roll(Rl,-tau)-Rl)/float(tau)
    # plot data
    if params['flags']['plot_data']:
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS[0])), c51.make_gvars(SS), '%s %s ss' %(basak[b],str(mq)))
            c51.scatter_plot(np.arange(len(PS[0])), c51.make_gvars(PS), '%s %s ps' %(basak[b],str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(c51.make_gvars(SS), 1, 'log')
            meff_ps = eff.effective_mass(c51.make_gvars(PS), 1, 'log')
            xlim = [2, len(meff_ss)/3-2]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)), meff_ss, '%s %s ss effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)), meff_ps, '%s %s ps effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = eff.scaled_correlator(c51.make_gvars(SS), E0, phase=1.0)
            scaled_ps = eff.scaled_correlator(c51.make_gvars(PS), E0, phase=1.0)
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ss)), scaled_ss, '%s %s ss scaled correlator (take sqrt to get Z0_s)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ps)), scaled_ps, '%s %s ps scaled correlator (divide by Z0_s to get Z0_p)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            plt.show()
    if params['flags']['fit_twopt']:
        # data already concatenated previously
        # read priors
        prior = c51.baryon_priors(barp['priors'],basak,nstates)
        ## read trange
        trange = barp['trange']
        ## fit boot0
        boot0gv = spec
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T,nstates)
        boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.twopt_baryon_ss_ps,basak=params['gA_fit']['basak'])
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit,'E0','%s' %str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            blist = []
            for b in params['gA_fit']['basak']:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' %b] = [boot0fit['pmean'][t]['%s_Z0s'%b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0s' %b] = [boot0fit['psdev'][t]['%s_Z0s' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_Z0p' %b] = [boot0fit['pmean'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0p' %b] = [boot0fit['psdev'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2'])/np.array(boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array([[barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],[barp['meta_id']['PS'][i] for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('baryon',nstates=nstates,basak=params['gA_fit']['basak'])
            for t in range(len(boot0fit['tmin'])):
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['tmin'][t]
                tmax = boot0fit['tmax'][t]
                result = c51.make_result(boot0fit,t)
                psql.submit_boot0('proton',corr_lst,fit_id,tmin,tmax,init_id,prior_id,result,params['flags']['update'])
    # plot fh correlator
    if params['flags']['plot_fhdata']:
        for b in range(len(basak)):
            # raw correlator dC_lambda/dlambda
            fhSS = fhSSl[b]
            fhPS = fhPSl[b]
            c51.scatter_plot(np.arange(len(fhSS[0])), c51.make_gvars(fhSS), '%s %s fh ss' %(basak[b],str(mq)))
            c51.scatter_plot(np.arange(len(fhPS[0])), c51.make_gvars(fhPS), '%s %s fh ps' %(basak[b],str(mq)))
            plt.show()
            # R(t)
            RSS = (Rl[:len(Rl)/2])[b*len(Rl)/(2*len(basak)):(b+1)*len(Rl)/(2*len(basak))]
            RPS = (Rl[len(Rl)/2:])[b*len(Rl)/(2*len(basak)):(b+1)*len(Rl)/(2*len(basak))]
            c51.scatter_plot(np.arange(len(RSS)), RSS, '%s %s R(t) ss' %(basak[b],str(mq)))
            c51.scatter_plot(np.arange(len(RPS)), RPS, '%s %s R(t) ps' %(basak[b],str(mq)))
            plt.show()
            # dmeff R(t+tau) - R(t)
            dMSS = (dM[:len(dM)/2])[b*len(dM)/(2*len(basak)):(b+1)*len(dM)/(2*len(basak))]
            dMPS = (dM[len(dM)/2:])[b*len(dM)/(2*len(basak)):(b+1)*len(dM)/(2*len(basak))]
            c51.scatter_plot(np.arange(len(dMSS)), dMSS, '%s %s [R(t+%s)-R(t)]/%s ss' %(basak[b],str(mq),str(tau),str(tau)))
            c51.scatter_plot(np.arange(len(dMPS)), dMPS, '%s %s [R(t+%s)-R(t)]/%s ps' %(basak[b],str(mq),str(tau),str(tau)))
            plt.show()
    # fit fh correlator
    if params['flags']['fit_gA']:
        # data concatenated previously
        # read priors
        prior = c51.fhbaryon_priors(barp['priors'],fhbp['priors'],basak,nstates)
        print prior
        # read trange
        trange = barp['trange']
        fhtrange = fhbp['trange']
        # fit boot0
        boot0gv = np.concatenate((spec, dM))
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T,nstates,tau)
        boot0fit = c51.fitscript_v3(trange,fhtrange,T,boot0gv,boot0p,fitfcn.baryon_fhbaryon_ss_ps,basak=params['gA_fit']['basak'])
        #print {k: boot0fit['p0'][0][k] for k in [bk for n in range(nstates) for bk in barp['priors'][n+1].keys()]}
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit,'E0','%s' %str(mq))
            c51.stability_plot(boot0fit,'gA00','%s' %str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['fhtmin'] = boot0fit['fhtmin']
            tbl_print['fhtmax'] = boot0fit['fhtmax']
            #tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            #tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            tbl_print['gA00'] = [boot0fit['pmean'][t]['gA00'] for t in range(len(boot0fit['pmean']))]
            tbl_print['dgA00'] = [boot0fit['psdev'][t]['gA00'] for t in range(len(boot0fit['pmean']))]
            #blist = []
            #for b in params['gA_fit']['basak']:
            #    blist.append(b[2:])
            #    blist.append(b[:2])
            #blist = np.unique(blist)
            #for b in blist:
            #    tbl_print['%s_Z0s' %b] = [boot0fit['pmean'][t]['%s_Z0s'%b] for t in range(len(boot0fit['pmean']))]
            #    tbl_print['%s_dZ0s' %b] = [boot0fit['psdev'][t]['%s_Z0s' %b] for t in range(len(boot0fit['pmean']))]
            #    tbl_print['%s_Z0p' %b] = [boot0fit['pmean'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            #    tbl_print['%s_dZ0p' %b] = [boot0fit['psdev'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2'])/np.array(boot0fit['dof'])
            tbl_print['chi2'] = boot0fit['chi2']
            tbl_print['chi2f'] = boot0fit['chi2f']
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array([[fhbp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],[fhbp['meta_id']['PS'][i] for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('fhbaryon',nstates=nstates,tau=params['gA_fit']['tau'])
            for t in range(len(boot0fit['tmin'])):
                baryon_corr_lst = np.array([[barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],[barp['meta_id']['PS'][i] for i in params['gA_fit']['basak']]]).flatten()
                baryon_fit_id = c51.select_fitid('baryon',nstates=nstates,basak=params['gA_fit']['basak'])
                baryon_tmin = trange['tmin'][0]
                baryon_tmax = trange['tmax'][0]
                baryon_p0 = {k: boot0fit['p0'][0][k] for k in [bk for n in range(nstates) for bk in barp['priors'][n+1].keys()]}
                baryon_init_id = psql.initid(baryon_p0)
                baryon_prior_id = psql.priorid(c51.dict_of_tuple_to_gvar(c51.baryon_priors(barp['priors'],basak,nstates)))
                baryon_id = psql.select_boot0("proton", baryon_corr_lst, baryon_fit_id, baryon_tmin, baryon_tmax, baryon_init_id, baryon_prior_id)
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['fhtmin'][t]
                tmax = boot0fit['fhtmax'][t]
                result = c51.make_result(boot0fit,t)
                print tmin, tmax
                psql.submit_fhboot0('fhproton',corr_lst,baryon_id,fit_id,tmin,tmax,init_id,prior_id,result,params['flags']['update'])
        if params['flags']['csvformat']:
            for t in range(len(boot0fit['fhtmin'])):
                print nstates,',',boot0fit['tmin'][t],',',boot0fit['tmax'][t],',',boot0fit['fhtmin'][t],',',boot0fit['fhtmax'][t],',',boot0fit['pmean'][t]['gA00'],',',boot0fit['psdev'][t]['gA00'],',',(np.array(boot0fit['chi2'])/np.array(boot0fit['dof']))[t],',',(np.array(boot0fit['chi2'])/np.array(boot0fit['chi2f']))[t],',',boot0fit['logGBF'][t]
예제 #8
0
def corrfit(psql, params, meson):
    mq = params['a13_fit']['ml']
    print 'fitting a13', mq
    tag = params['a13_fit']['ens']['tag']
    stream = params['a13_fit']['ens']['stream']
    Nbs = params['a13_fit']['nbs']
    Mbs = params['a13_fit']['mbs']
    mesp = params[tag][meson][mq]
    # read data
    SS = c51.fold(psql.data('dwhisq_corr_meson', mesp['meta_id']['SS']))
    PS = c51.fold(psql.data('dwhisq_corr_meson', mesp['meta_id']['PS']))
    T = len(SS[0])
    # plot effective mass / scaled correlator
    if params['flags']['plot_data']:
        # unfolded correlator data
        c51.scatter_plot(np.arange(len(SS[0])), c51.make_gvars(SS),
                         '%s ss folded' % (str(mq)))
        c51.scatter_plot(np.arange(len(PS[0])), c51.make_gvars(PS),
                         '%s ps folded' % (str(mq)))
        plt.show()
        # effective mass
        eff = c51.effective_plots(T)
        meff_ss = eff.effective_mass(c51.make_gvars(SS), 1, 'cosh')
        meff_ps = eff.effective_mass(c51.make_gvars(PS), 1, 'cosh')
        xlim = [2, 12]
        ylim = [0, 2]  #c51.find_yrange(meff_ss, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff_ss)),
                         meff_ss,
                         '%s ss effective mass' % (str(mq)),
                         xlim=xlim,
                         ylim=ylim)
        ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(meff_ps)),
                         meff_ps,
                         '%s ps effective mass' % (str(mq)),
                         xlim=xlim,
                         ylim=ylim)
        plt.show()
        # scaled correlator
        E0 = mesp['priors'][1]['E0'][0]
        scaled_ss = eff.scaled_correlator(c51.make_gvars(SS), E0, phase=1.0)
        scaled_ps = eff.scaled_correlator(c51.make_gvars(PS), E0, phase=1.0)
        ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
        c51.scatter_plot(np.arange(len(scaled_ss)),
                         scaled_ss,
                         '%s ss scaled correlator (take sqrt to get Z0_s)' %
                         (str(mq)),
                         xlim=xlim,
                         ylim=ylim)
        ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
        c51.scatter_plot(
            np.arange(len(scaled_ps)),
            scaled_ps,
            '%s ps scaled correlator (divide by Z0_s to get Z0_p)' % (str(mq)),
            xlim=xlim,
            ylim=ylim)
        plt.show()
    # concatenate data
    boot0 = np.concatenate((SS, PS), axis=1)
    # read priors
    prior = mesp['priors']
    # read trange
    trange = mesp['trange']
    # fit boot0
    boot0gv = c51.make_gvars(boot0)
    boot0p = c51.dict_of_tuple_to_gvar(prior)
    fitfcn = c51.fit_function(T, params['decay_ward_fit']['nstates'])
    boot0fit = c51.fitscript_v2(trange, T, boot0gv, boot0p,
                                fitfcn.twopt_fitfcn_ss_ps)
    if params['flags']['stability_plot']:
        c51.stability_plot(boot0fit, 'E0', '%s_%s' % (str(mq1), str(mq2)))
        plt.show()
    if params['flags']['tabulate']:
        tbl_print = collections.OrderedDict()
        tbl_print['tmin'] = boot0fit['tmin']
        tbl_print['tmax'] = boot0fit['tmax']
        tbl_print['E0'] = [
            boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dE0'] = [
            boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['Z0_s'] = [
            boot0fit['pmean'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dZ0_s'] = [
            boot0fit['psdev'][t]['Z0_s'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['Z0_p'] = [
            boot0fit['pmean'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['dZ0_p'] = [
            boot0fit['psdev'][t]['Z0_p'] for t in range(len(boot0fit['pmean']))
        ]
        tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
            boot0fit['dof'])
        tbl_print['logGBF'] = boot0fit['logGBF']
        print tabulate(tbl_print, headers='keys')
    # submit boot0 to db
    if params['flags']['write']:
        corr_lst = [mesp['meta_id']['SS'], mesp['meta_id']['PS']]
        fit_id = c51.select_fitid('meson', params)
        for t in range(len(boot0fit['tmin'])):
            init_id = psql.initid(boot0fit['p0'][t])
            prior_id = psql.priorid(boot0fit['prior'][t])
            tmin = boot0fit['tmin'][t]
            tmax = boot0fit['tmax'][t]
            result = c51.make_result(boot0fit, t)
            psql.submit_boot0('meson', corr_lst, fit_id, tmin, tmax, init_id,
                              prior_id, result, params['flags']['update'])
    if len(boot0fit['tmin']) != 1 or len(boot0fit['tmax']) != 1:
        print "sweeping over time range"
        print "skipping bootstrap"
        return 0
    else:
        pass
    if params['flags']['write']:
        bsresult = []
        psql.chkbsprior(tag, stream, Nbs, boot0fit['prior'][0])
        for g in tqdm.tqdm(range(Nbs)):
            # make bs gvar dataset
            n = g + 1
            bs_id, draw = psql.fetchonebs(nbs=n,
                                          Mbs=Mbs,
                                          ens=tag,
                                          stream=stream)
            bootn = boot0[draw]
            bootngv = c51.make_gvars(bootn)
            # read randomized priors
            bsprior_id, bsp = psql.bspriorid(tag, stream, g,
                                             boot0fit['prior'][0])
            bsp = c51.dict_of_tuple_to_gvar(bsp)
            # read initial guess
            init = c51.read_init(boot0fit,
                                 0)  #{"mres":boot0fit['pmean'][0]['mres']}
            #Fit
            bsfit = c51.fitscript_v2(trange, T, bootngv, bsp,
                                     fitfcn.twopt_fitfcn_ss_ps, init)
            tmin = bsfit['tmin'][0]
            tmax = bsfit['tmax'][0]
            boot0_id = psql.select_boot0('meson', corr_lst, fit_id, tmin, tmax,
                                         init_id, prior_id)
            bsinit_id = psql.initid(bsfit['p0'][0])
            result = c51.make_result(
                bsfit, 0
            )  #"""{"mres":%s, "chi2":%s, "dof":%s}""" %(bsfit['pmean'][t]['mres'],bsfit['chi2'][t],bsfit['dof'][t])
            psql.submit_bs('meson_bs', boot0_id, bs_id, Mbs, bsinit_id,
                           bsprior_id, result, params['flags']['update'])
    return 0
예제 #9
0
파일: fh.py 프로젝트: cchang5/c51scripts
def fit_proton(psql,params,gvboot0):
    # read data
    mq = params['gA_fit']['ml']
    basak = copy.deepcopy(params['gA_fit']['basak'])
    tag = params['gA_fit']['ens']['tag']
    stream = params['gA_fit']['ens']['stream']
    Nbs = params['gA_fit']['nbs']
    Mbs = params['gA_fit']['mbs']
    nstates = params['gA_fit']['nstates']
    tau = params['gA_fit']['tau']
    barp = params[tag]['proton'][mq]
    # make gvars
    spec = gvboot0[:len(gvboot0)/2]
    T = len(spec)/(2*len(basak))
    # plot data
    if params['flags']['plot_data']:
        SSl = spec[:len(spec)/2].reshape((len(basak),T))
        PSl = spec[len(spec)/2:].reshape((len(basak),T))
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS)), SS, '%s %s ss' %(basak[b],str(mq)))
            c51.scatter_plot(np.arange(len(PS)), PS, '%s %s ps' %(basak[b],str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(SS, 1, 'log')
            meff_ps = eff.effective_mass(PS, 1, 'log')
            xlim = [2, len(meff_ss)/3]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)), meff_ss, '%s %s ss effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)), meff_ps, '%s %s ps effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            #print stuff
            #print 'meff_ss'
            #f_meff = open('/Users/cchang5/Documents/Papers/FH/c51_p1/paper/figures/meff_ss.csv', 'w+')
            #string = 't_meff, meff, +-\n'
            #for t in range(len(meff_ss)):
            #    string += '%s, %s, %s\n' %(str(t), str(meff_ss[t].mean), str(meff_ss[t].sdev))
            #f_meff.write(string)
            #f_meff.flush()
            #f_meff.close()
            #print 'meff_ps'
            #for t in range(len(meff_ps)):
            #    print t, meff_ps[t].mean, meff_ps[t].sdev
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = np.sqrt(eff.scaled_correlator_v2(SS, E0, phase=1.0))
            scaled_ps = eff.scaled_correlator_v2(PS, E0, phase=1.0)/scaled_ss
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ss)), scaled_ss, '%s %s ss scaled correlator (Z0_s)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ps)), scaled_ps, '%s %s ps scaled correlator (Z0_p)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            #print stuff
            #print 'scaled_ss'
            #f_scaled = open('/Users/cchang5/Documents/Papers/FH/c51_p1/paper/figures/scaled_ss.csv', 'w+')
            #string = 't_scaled, scaled_corr, +-\n'
            #for t in range(len(scaled_ss)):
            #    string += '%s, %s, %s\n' %(str(t), str(scaled_ss[t].mean), str(scaled_ss[t].sdev))
            #f_scaled.write(string)
            #f_scaled.flush()
            #f_scaled.close()
            #print 'scaled_ps'
            #for t in range(len(meff_ps)):
            #    print t, scaled_ps[t].mean, scaled_ps[t].sdev
            plt.show()
    if params['flags']['fit_twopt']:
        # data already concatenated previously
        # read priors
        prior = c51.baryon_priors(barp['priors'],basak,nstates)
        ## read trange
        trange = barp['trange']
        ## fit boot0
        boot0gv = spec
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T,nstates)
        boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.dwhisq_twopt_ss_ps,basak=params['gA_fit']['basak'])
        print boot0fit['rawoutput'][0]
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit,'E0','%s' %str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            blist = []
            for b in params['gA_fit']['basak']:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' %b] = [boot0fit['pmean'][t]['%s_Z0s'%b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0s' %b] = [boot0fit['psdev'][t]['%s_Z0s' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_Z0p' %b] = [boot0fit['pmean'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0p' %b] = [boot0fit['psdev'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2'])/np.array(boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            tbl_print['logGBF'] = boot0fit['logGBF']
            tbl_print['Q'] = boot0fit['Q']
            print tabulate(tbl_print, headers='keys')
            print "tmax, E0, +-, chi2dof, Q, logGBF"
            for i in range(len(boot0fit['tmin'])):
                print '%s, %s, %s, %s, %s, %s' %(str(boot0fit['tmax'][i]), str(boot0fit['pmean'][i]['E0']), str(boot0fit['psdev'][i]['E0']), boot0fit['chi2'][i]/boot0fit['dof'][i], boot0fit['Q'][i], boot0fit['logGBF'][i])
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array([[barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],[barp['meta_id']['PS'][i] for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('baryon',nstates=nstates,basak=params['gA_fit']['basak'])
            for t in range(len(boot0fit['tmin'])):
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['tmin'][t]
                tmax = boot0fit['tmax'][t]
                result = c51.make_result(boot0fit,t)
                psql.submit_boot0('proton',corr_lst,fit_id,tmin,tmax,init_id,prior_id,result,params['flags']['update'])
        return {'nucleon_fit': boot0fit['rawoutput'][0]}
    return 0
예제 #10
0
def decay_bs(psql, params, meson):
    if meson == "pion":
        mq1 = params["decay_ward_fit"]["ml"]
        mq2 = mq1
    elif meson == "kaon":
        mq1 = params["decay_ward_fit"]["ml"]
        mq2 = params["decay_ward_fit"]["ms"]
    elif meson == "etas":
        mq1 = params["decay_ward_fit"]["ms"]
        mq2 = mq1
    print "fitting twopoint", mq1, mq2
    tag = params["decay_ward_fit"]["ens"]["tag"]
    stream = params["decay_ward_fit"]["ens"]["stream"]
    Nbs = params["decay_ward_fit"]["nbs"]
    Mbs = params["decay_ward_fit"]["mbs"]
    mesp = params[tag][meson]["%s_%s" % (str(mq1), str(mq2))]
    # read data
    SS = c51.fold(psql.data("dwhisq_corr_meson", mesp["meta_id"]["SS"]))
    PS = c51.fold(psql.data("dwhisq_corr_meson", mesp["meta_id"]["PS"]))
    T = len(SS[0])
    # plot effective mass / scaled correlator
    if params["flags"]["plot_data"]:
        # unfolded correlator data
        c51.scatter_plot(np.arange(len(SS[0])), c51.make_gvars(SS), "%s_%s ss folded" % (str(mq1), str(mq2)))
        c51.scatter_plot(np.arange(len(PS[0])), c51.make_gvars(PS), "%s_%s ps folded" % (str(mq1), str(mq2)))
        plt.show()
        # effective mass
        eff = c51.effective_plots(T)
        meff_ss = eff.effective_mass(c51.make_gvars(SS), 1, "cosh")
        meff_ps = eff.effective_mass(c51.make_gvars(PS), 1, "cosh")
        xlim = [3, len(meff_ss) / 2 - 2]
        ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
        c51.scatter_plot(
            np.arange(len(meff_ss)), meff_ss, "%s_%s ss effective mass" % (str(mq1), str(mq2)), xlim=xlim, ylim=ylim
        )
        ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
        c51.scatter_plot(
            np.arange(len(meff_ps)), meff_ps, "%s_%s ps effective mass" % (str(mq1), str(mq2)), xlim=xlim, ylim=ylim
        )
        plt.show()
        # scaled correlator
        E0 = mesp["priors"]["E0"][0]
        scaled_ss = eff.scaled_correlator(c51.make_gvars(SS), E0, phase=1.0)
        scaled_ps = eff.scaled_correlator(c51.make_gvars(PS), E0, phase=1.0)
        ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
        c51.scatter_plot(
            np.arange(len(scaled_ss)),
            scaled_ss,
            "%s_%s ss scaled correlator (take sqrt to get Z0_s)" % (str(mq1), str(mq2)),
            xlim=xlim,
            ylim=ylim,
        )
        ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
        c51.scatter_plot(
            np.arange(len(scaled_ps)),
            scaled_ps,
            "%s_%s ps scaled correlator (divide by Z0_s to get Z0_p)" % (str(mq1), str(mq2)),
            xlim=xlim,
            ylim=ylim,
        )
        plt.show()
    # concatenate data
    boot0 = np.concatenate((SS, PS), axis=1)
    # read priors
    prior = mesp["priors"]
    # read trange
    trange = mesp["trange"]
    # fit boot0
    boot0gv = c51.make_gvars(boot0)
    boot0p = c51.dict_of_tuple_to_gvar(prior)
    fitfcn = c51.fit_function(T, params["decay_ward_fit"]["nstates"])
    boot0fit = c51.fitscript_v2(trange, T, boot0gv, boot0p, fitfcn.twopt_fitfcn_ss_ps)
    if params["flags"]["stability_plot"]:
        c51.stability_plot(boot0fit, "E0", "%s_%s" % (str(mq1), str(mq2)))
        plt.show()
    if params["flags"]["tabulate"]:
        tbl_print = collections.OrderedDict()
        tbl_print["tmin"] = boot0fit["tmin"]
        tbl_print["tmax"] = boot0fit["tmax"]
        tbl_print["E0"] = [boot0fit["pmean"][t]["E0"] for t in range(len(boot0fit["pmean"]))]
        tbl_print["dE0"] = [boot0fit["psdev"][t]["E0"] for t in range(len(boot0fit["pmean"]))]
        tbl_print["Z0_s"] = [boot0fit["pmean"][t]["Z0_s"] for t in range(len(boot0fit["pmean"]))]
        tbl_print["dZ0_s"] = [boot0fit["psdev"][t]["Z0_s"] for t in range(len(boot0fit["pmean"]))]
        tbl_print["Z0_p"] = [boot0fit["pmean"][t]["Z0_p"] for t in range(len(boot0fit["pmean"]))]
        tbl_print["dZ0_p"] = [boot0fit["psdev"][t]["Z0_p"] for t in range(len(boot0fit["pmean"]))]
        tbl_print["chi2/dof"] = np.array(boot0fit["chi2"]) / np.array(boot0fit["dof"])
        tbl_print["logGBF"] = boot0fit["logGBF"]
        print tabulate(tbl_print, headers="keys")
    # submit boot0 to db
    if params["flags"]["write"]:
        corr_lst = [mesp["meta_id"]["SS"], mesp["meta_id"]["PS"]]
        fit_id = c51.select_fitid("meson", params)
        for t in range(len(boot0fit["tmin"])):
            init_id = psql.initid(boot0fit["p0"][t])
            prior_id = psql.priorid(boot0fit["prior"][t])
            tmin = boot0fit["tmin"][t]
            tmax = boot0fit["tmax"][t]
            result = c51.make_result(boot0fit, t)
            psql.submit_boot0(
                "meson", corr_lst, fit_id, tmin, tmax, init_id, prior_id, result, params["flags"]["update"]
            )
    if len(boot0fit["tmin"]) != 1 or len(boot0fit["tmax"]) != 1:
        print "sweeping over time range"
        print "skipping bootstrap"
        return 0
    else:
        pass
    if params["flags"]["write"]:
        bsresult = []
        psql.chkbsprior(tag, stream, Nbs, boot0fit["prior"][0])
        for g in tqdm.tqdm(range(Nbs)):
            # make bs gvar dataset
            n = g + 1
            bs_id, draw = psql.fetchonebs(nbs=n, Mbs=Mbs, ens=tag, stream=stream)
            bootn = boot0[draw]
            bootngv = c51.make_gvars(bootn)
            # read randomized priors
            bsprior_id, bsp = psql.bspriorid(tag, stream, g, boot0fit["prior"][0])
            bsp = c51.dict_of_tuple_to_gvar(bsp)
            # read initial guess
            init = c51.read_init(boot0fit, 0)  # {"mres":boot0fit['pmean'][0]['mres']}
            # Fit
            bsfit = c51.fitscript_v2(trange, T, bootngv, bsp, fitfcn.twopt_fitfcn_ss_ps, init)
            tmin = bsfit["tmin"][0]
            tmax = bsfit["tmax"][0]
            boot0_id = psql.select_boot0("meson", corr_lst, fit_id, tmin, tmax, init_id, prior_id)
            bsinit_id = psql.initid(bsfit["p0"][0])
            result = c51.make_result(
                bsfit, 0
            )  # """{"mres":%s, "chi2":%s, "dof":%s}""" %(bsfit['pmean'][t]['mres'],bsfit['chi2'][t],bsfit['dof'][t])
            psql.submit_bs("meson_bs", boot0_id, bs_id, Mbs, bsinit_id, bsprior_id, result, params["flags"]["update"])
    return 0
예제 #11
0
파일: fh.py 프로젝트: erinaldi/c51scripts
def fit_proton(psql, params, gvboot0):
    # read data
    mq = params['gA_fit']['ml']
    basak = copy.deepcopy(params['gA_fit']['basak'])
    tag = params['gA_fit']['ens']['tag']
    stream = params['gA_fit']['ens']['stream']
    Nbs = params['gA_fit']['nbs']
    Mbs = params['gA_fit']['mbs']
    nstates = params['gA_fit']['nstates']
    tau = params['gA_fit']['tau']
    barp = params[tag]['proton'][mq]
    # make gvars
    spec = gvboot0[:len(gvboot0) / 2]
    T = len(spec) / (2 * len(basak))
    # plot data
    if params['flags']['plot_data']:
        SSl = spec[:len(spec) / 2].reshape((len(basak), T))
        PSl = spec[len(spec) / 2:].reshape((len(basak), T))
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS)), SS,
                             '%s %s ss' % (basak[b], str(mq)))
            c51.scatter_plot(np.arange(len(PS)), PS,
                             '%s %s ps' % (basak[b], str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(SS, 1, 'log')
            meff_ps = eff.effective_mass(PS, 1, 'log')
            xlim = [2, len(meff_ss) / 3]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)),
                             meff_ss,
                             '%s %s ss effective mass' % (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)),
                             meff_ps,
                             '%s %s ps effective mass' % (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            #print stuff
            #print 'meff_ss'
            #f_meff = open('/Users/cchang5/Documents/Papers/FH/c51_p1/paper/figures/meff_ss.csv', 'w+')
            #string = 't_meff, meff, +-\n'
            #for t in range(len(meff_ss)):
            #    string += '%s, %s, %s\n' %(str(t), str(meff_ss[t].mean), str(meff_ss[t].sdev))
            #f_meff.write(string)
            #f_meff.flush()
            #f_meff.close()
            #print 'meff_ps'
            #for t in range(len(meff_ps)):
            #    print t, meff_ps[t].mean, meff_ps[t].sdev
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = np.sqrt(eff.scaled_correlator_v2(SS, E0, phase=1.0))
            scaled_ps = eff.scaled_correlator_v2(PS, E0, phase=1.0) / scaled_ss
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ss)),
                             scaled_ss,
                             '%s %s ss scaled correlator (Z0_s)' %
                             (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ps)),
                             scaled_ps,
                             '%s %s ps scaled correlator (Z0_p)' %
                             (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            #print stuff
            #f_scaled = open('/Users/cchang5/Documents/Papers/FH/c51_p1/paper/figures/scaled_corr.csv', 'w+')
            #string = 't_scaled, scaled_ss, +-, scaled_ps, +-\n'
            #for t in range(len(scaled_ss)):
            #    string += '%s, %s, %s, %s, %s\n' %(t, scaled_ss[t].mean, scaled_ss[t].sdev, scaled_ps[t].mean, scaled_ps[t].sdev)
            #f_scaled.write(string)
            #f_scaled.flush()
            #f_scaled.close()
            plt.show()
    if params['flags']['fit_twopt']:
        # data already concatenated previously
        # read priors
        prior = c51.baryon_priors(barp['priors'], basak, nstates)
        ## read trange
        trange = barp['trange']
        ## fit boot0
        boot0gv = spec
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T, nstates)
        boot0fit = c51.fitscript_v2(trange,
                                    T,
                                    boot0gv,
                                    boot0p,
                                    fitfcn.dwhisq_twopt_ss_ps,
                                    basak=params['gA_fit']['basak'])
        print boot0fit['rawoutput'][0]
        if params['flags']['fitline_plot']:
            p = boot0fit['rawoutput'][0].p
            t = np.linspace(0, T / 2, 100 * T / 2)
            b = params['gA_fit']['basak'][0]
            # output fit curve
            ss = fitfcn.dwhisq_twopt(t, p, b, 's', 's')
            ps = fitfcn.dwhisq_twopt(t, p, b, 'p', 's')
            scaled_ss = np.sqrt(ss * np.exp(p['E0'] * t))
            scaled_ps = ps * np.exp(p['E0'] * t) / scaled_ss
            meffss = np.log(ss / np.roll(ss, -100)) / t[100]
            meffps = np.log(ps / np.roll(ps, -100)) / t[100]
            f_plot = open('./fh_fitline/%s_twopt.csv' % (tag), 'w+')
            string = 't, css, +-, cps, +-\n'
            for i in range(len(t)):
                string += '%s, %s, %s, %s, %s\n' % (
                    t[i], meffss[i].mean, meffss[i].sdev, meffps[i].mean,
                    meffps[i].sdev)
            f_plot.write(string)
            f_plot.flush()
            f_plot.close()
            f_plot = open('./fh_fitline/%s_scaled2pt.csv' % (tag), 'w+')
            # data scaled two point: propagate E0 error
            SSl = spec[:len(spec) / 2].reshape((len(basak), T))[0]
            PSl = spec[len(spec) / 2:].reshape((len(basak), T))[0]
            t = np.linspace(0, T - 1, T)
            scaled_ss = np.sqrt(SSl * np.exp(p['E0'] * t))
            scaled_ps = PSl * np.exp(p['E0'] * t) / scaled_ss
            string = 't, zss, +-, zps, +-\n'
            for i in range(len(t)):
                string += '%s, %s, %s, %s, %s\n' % (
                    t[i], scaled_ss[i].mean, scaled_ss[i].sdev,
                    scaled_ps[i].mean, scaled_ps[i].sdev)
            f_plot = open(
                '/Users/cchang5/Documents/Papers/FH/c51_p1/paper/figures/scaled_corr.csv',
                'w+')
            f_plot.write(string)
            f_plot.flush()
            f_plot.close()
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit, 'E0', '%s' % str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['E0'] = [
                boot0fit['pmean'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            tbl_print['dE0'] = [
                boot0fit['psdev'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            blist = []
            for b in params['gA_fit']['basak']:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' % b] = [
                    boot0fit['pmean'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0s' % b] = [
                    boot0fit['psdev'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_Z0p' % b] = [
                    boot0fit['pmean'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0p' % b] = [
                    boot0fit['psdev'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
                boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            tbl_print['logGBF'] = boot0fit['logGBF']
            tbl_print['Q'] = boot0fit['Q']
            print tabulate(tbl_print, headers='keys')
            print "tmin, E0%s, +-, chi2dof%s, Q%s, logGBF%s" % (
                nstates, nstates, nstates, nstates)
            for i in range(len(boot0fit['tmin'])):
                print '%s, %s, %s, %s, %s, %s' % (
                    str(boot0fit['tmin'][i]), str(boot0fit['pmean'][i]['E0']),
                    str(boot0fit['psdev'][i]['E0']),
                    boot0fit['chi2'][i] / boot0fit['dof'][i], boot0fit['Q'][i],
                    boot0fit['logGBF'][i])
        # submit boot0 to db
        if False:  #params['flags']['write']:
            corr_lst = np.array(
                [[barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],
                 [barp['meta_id']['PS'][i]
                  for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('baryon',
                                      nstates=nstates,
                                      basak=params['gA_fit']['basak'])
            for t in range(len(boot0fit['tmin'])):
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['tmin'][t]
                tmax = boot0fit['tmax'][t]
                result = c51.make_result(boot0fit, t)
                psql.submit_boot0('proton', corr_lst, fit_id, tmin, tmax,
                                  init_id, prior_id, result,
                                  params['flags']['update'])
        return {'nucleon_fit': boot0fit['rawoutput'][0]}
    return 0
예제 #12
0
파일: dM.py 프로젝트: erinaldi/c51scripts
def fit_proton(psql,params,gvboot0):
    # read data
    mq = params['gA_fit']['ml']
    basak = copy.deepcopy(params['gA_fit']['basak'])
    tag = params['gA_fit']['ens']['tag']
    stream = params['gA_fit']['ens']['stream']
    Nbs = params['gA_fit']['nbs']
    Mbs = params['gA_fit']['mbs']
    nstates = params['gA_fit']['nstates']
    tau = params['gA_fit']['tau']
    barp = params[tag]['proton'][mq]
    fhbp = params[tag]['gA'][mq]
    # make gvars
    spec = gvboot0[:len(gvboot0)/2]
    fh = gvboot0[len(gvboot0)/2:]
    T = len(fh)/(2*len(basak))
    # R(t)
    Rl = fh/spec
    # dmeff [R(t+tau) - R(t)] / tau
    #dM = Rl
    dM = (np.roll(Rl,-tau)-Rl)/float(tau) #This is needed to plot gA correctly in the effective plots, but will give wrong data to fit with.
    # plot data
    if params['flags']['plot_data']:
        SSl = spec[:len(spec)/2].reshape((len(basak),T))
        PSl = spec[len(spec)/2:].reshape((len(basak),T))
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS)), SS, '%s %s ss' %(basak[b],str(mq)))
            c51.scatter_plot(np.arange(len(PS)), PS, '%s %s ps' %(basak[b],str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(SS, 1, 'log')
            meff_ps = eff.effective_mass(PS, 1, 'log')
            xlim = [2, len(meff_ss)/3]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)), meff_ss, '%s %s ss effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)), meff_ps, '%s %s ps effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            #print stuff
            #print 'meff_ss'
            #for t in range(len(meff_ss)):
            #    print t, meff_ss[t].mean, meff_ss[t].sdev
            #print 'meff_ps'
            #for t in range(len(meff_ps)):
            #    print t, meff_ps[t].mean, meff_ps[t].sdev
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = eff.scaled_correlator(SS, E0, phase=1.0)
            scaled_ps = eff.scaled_correlator(PS, E0, phase=1.0)
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ss)), scaled_ss, '%s %s ss scaled correlator (take sqrt to get Z0_s)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ps)), scaled_ps, '%s %s ps scaled correlator (divide by Z0_s to get Z0_p)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            #print stuff
            #print 'scaled_ss'
            #for t in range(len(scaled_ss)):
            #    print t, scaled_ss[t].mean, scaled_ss[t].sdev
            #print 'scaled_ps'
            #for t in range(len(meff_ps)):
            #    print t, scaled_ps[t].mean, scaled_ps[t].sdev
            plt.show()
    if params['flags']['fit_twopt']:
        # data already concatenated previously
        # read priors
        prior = c51.baryon_priors(barp['priors'],basak,nstates)
        ## read trange
        trange = barp['trange']
        ## fit boot0
        boot0gv = spec
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T,nstates)
        boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.twopt_baryon_ss_ps,basak=params['gA_fit']['basak'])
        print boot0fit['rawoutput'][0]
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit,'E0','%s' %str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            blist = []
            for b in params['gA_fit']['basak']:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' %b] = [boot0fit['pmean'][t]['%s_Z0s'%b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0s' %b] = [boot0fit['psdev'][t]['%s_Z0s' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_Z0p' %b] = [boot0fit['pmean'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0p' %b] = [boot0fit['psdev'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2'])/np.array(boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array([[barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],[barp['meta_id']['PS'][i] for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('baryon',nstates=nstates,basak=params['gA_fit']['basak'])
            for t in range(len(boot0fit['tmin'])):
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['tmin'][t]
                tmax = boot0fit['tmax'][t]
                result = c51.make_result(boot0fit,t)
                psql.submit_boot0('proton',corr_lst,fit_id,tmin,tmax,init_id,prior_id,result,params['flags']['update'])
        return {'nucleon_fit': boot0fit['rawoutput'][0]}
    return 0
예제 #13
0
def gA_bs(psql, params):
    # read data
    mq = params['gA_fit']['ml']
    basak = copy.deepcopy(params['gA_fit']['basak'])
    tag = params['gA_fit']['ens']['tag']
    stream = params['gA_fit']['ens']['stream']
    Nbs = params['gA_fit']['nbs']
    Mbs = params['gA_fit']['mbs']
    nstates = params['gA_fit']['nstates']
    tau = params['gA_fit']['tau']
    barp = params[tag]['proton'][mq]
    fhbp = params[tag]['gA'][mq]
    print "fitting for gA mq %s, basak %s, ens %s%s, Nbs %s, Mbs %s" % (
        str(mq), str(basak), str(tag), str(stream), str(Nbs), str(Mbs))
    # read two point
    SSl = np.array([
        psql.data('dwhisq_corr_baryon', idx)
        for idx in [barp['meta_id']['SS'][i] for i in basak]
    ])
    PSl = np.array([
        psql.data('dwhisq_corr_baryon', idx)
        for idx in [barp['meta_id']['PS'][i] for i in basak]
    ])
    T = len(SSl[0, 0])
    # read fh correlator
    fhSSl = np.array([
        psql.data('dwhisq_fhcorr_baryon', idx)
        for idx in [fhbp['meta_id']['SS'][i] for i in basak]
    ])
    fhPSl = np.array([
        psql.data('dwhisq_fhcorr_baryon', idx)
        for idx in [fhbp['meta_id']['PS'][i] for i in basak]
    ])
    # check outliers
    if False:
        # corr [basak, config, time]
        temp = [SSl, PSl, fhSSl, fhPSl]
        ntemp = ['SSl', 'PSl', 'fhSSl', 'fhPSl']
        for j in range(len(temp)):
            print ntemp[j]
            corr = temp[j]
            for i in range(len(corr[0, 0])):
                corrt = corr[0, :, i]  #00 ss 30 ps
                minimum = np.amin(corrt)
                argmin = np.argmin(corrt) * 5 + 300
                median = np.median(corrt)
                maximum = np.amax(corrt)
                argmax = np.argmax(corrt) * 5 + 300
                print i, median - minimum, maximum - median, argmin, argmax
    # concatenate and make gvars to preserve correlations
    SS = SSl[0]
    PS = PSl[0]
    for i in range(len(SSl) - 1):  # loop over basak operators
        SS = np.concatenate((SS, SSl[i + 1]), axis=1)
        PS = np.concatenate((PS, PSl[i + 1]), axis=1)
    fhSS = fhSSl[0]
    fhPS = fhPSl[0]
    for i in range(len(fhSSl) - 1):
        fhSS = np.concatenate((fhSS, fhSSl[i + 1]), axis=1)
        fhPS = np.concatenate((fhPS, fhPSl[i + 1]), axis=1)
    boot0 = np.concatenate((SS, PS, fhSS, fhPS), axis=1)
    # make gvars
    gvboot0 = c51.make_gvars(boot0)
    spec = gvboot0[:len(gvboot0) / 2]
    fh = gvboot0[len(gvboot0) / 2:]
    # R(t)
    Rl = fh / spec
    # dmeff [R(t+tau) - R(t)] / tau
    dM = (np.roll(Rl, -tau) - Rl) / float(tau)
    # plot data
    if params['flags']['plot_data']:
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS[0])), c51.make_gvars(SS),
                             '%s %s ss' % (basak[b], str(mq)))
            c51.scatter_plot(np.arange(len(PS[0])), c51.make_gvars(PS),
                             '%s %s ps' % (basak[b], str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(c51.make_gvars(SS), 1, 'log')
            meff_ps = eff.effective_mass(c51.make_gvars(PS), 1, 'log')
            xlim = [2, len(meff_ss) / 3 - 2]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)),
                             meff_ss,
                             '%s %s ss effective mass' % (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)),
                             meff_ps,
                             '%s %s ps effective mass' % (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = eff.scaled_correlator(c51.make_gvars(SS),
                                              E0,
                                              phase=1.0)
            scaled_ps = eff.scaled_correlator(c51.make_gvars(PS),
                                              E0,
                                              phase=1.0)
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(
                np.arange(len(scaled_ss)),
                scaled_ss,
                '%s %s ss scaled correlator (take sqrt to get Z0_s)' %
                (basak[b], str(mq)),
                xlim=xlim,
                ylim=ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(
                np.arange(len(scaled_ps)),
                scaled_ps,
                '%s %s ps scaled correlator (divide by Z0_s to get Z0_p)' %
                (basak[b], str(mq)),
                xlim=xlim,
                ylim=ylim)
            plt.show()
    if params['flags']['fit_twopt']:
        # data already concatenated previously
        # read priors
        prior = c51.baryon_priors(barp['priors'], basak, nstates)
        ## read trange
        trange = barp['trange']
        ## fit boot0
        #boot0gv = spec
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T, nstates)
        #boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.twopt_baryon_ss_ps,basak=params['gA_fit']['basak'])
        # fit two point
        for b in range(len(basak)):
            SSspec = (spec[:len(spec) / 2])[b * T:(b + 1) * T]
            PSspec = (spec[len(spec) / 2:])[b * T:(b + 1) * T]
            boot0gv = np.concatenate((SSspec, PSspec))
            boot0fit = c51.fitscript_v2(trange,
                                        T,
                                        boot0gv,
                                        boot0p,
                                        fitfcn.twopt_baryon_ss_ps,
                                        basak=[basak[b]])
            boot0p = boot0fit['rawoutput'][0].p
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit, 'E0', '%s' % str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['E0'] = [
                boot0fit['pmean'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            tbl_print['dE0'] = [
                boot0fit['psdev'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            blist = []
            for b in params['gA_fit']['basak']:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' % b] = [
                    boot0fit['pmean'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0s' % b] = [
                    boot0fit['psdev'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_Z0p' % b] = [
                    boot0fit['pmean'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0p' % b] = [
                    boot0fit['psdev'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
                boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array(
                [[barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],
                 [barp['meta_id']['PS'][i]
                  for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('baryon',
                                      nstates=nstates,
                                      basak=params['gA_fit']['basak'])
            for t in range(len(boot0fit['tmin'])):
                init_id = psql.initid(
                    c51.baryon_initpriors(barp['priors'], basak, nstates))
                prior_id = psql.priorid(
                    c51.dict_of_tuple_to_gvar(
                        c51.baryon_priors(barp['priors'], basak, nstates)))
                tmin = boot0fit['tmin'][t]
                tmax = boot0fit['tmax'][t]
                result = c51.make_result(boot0fit, t)
                psql.submit_boot0('proton', corr_lst, fit_id, tmin, tmax,
                                  init_id, prior_id, result,
                                  params['flags']['update'])
    # plot fh correlator
    if params['flags']['plot_fhdata']:
        for b in range(len(basak)):
            # raw correlator dC_lambda/dlambda
            fhSS = fhSSl[b]
            fhPS = fhPSl[b]
            c51.scatter_plot(np.arange(len(fhSS[0])), c51.make_gvars(fhSS),
                             '%s %s fh ss' % (basak[b], str(mq)))
            c51.scatter_plot(np.arange(len(fhPS[0])), c51.make_gvars(fhPS),
                             '%s %s fh ps' % (basak[b], str(mq)))
            plt.show()
            # R(t)
            RSS = (Rl[:len(Rl) / 2])[b * len(Rl) / (2 * len(basak)):(b + 1) *
                                     len(Rl) / (2 * len(basak))]
            RPS = (Rl[len(Rl) / 2:])[b * len(Rl) / (2 * len(basak)):(b + 1) *
                                     len(Rl) / (2 * len(basak))]
            c51.scatter_plot(np.arange(len(RSS)), RSS,
                             '%s %s R(t) ss' % (basak[b], str(mq)))
            c51.scatter_plot(np.arange(len(RPS)), RPS,
                             '%s %s R(t) ps' % (basak[b], str(mq)))
            plt.show()
            # dmeff R(t+tau) - R(t)
            dMSS = (dM[:len(dM) / 2])[b * len(dM) / (2 * len(basak)):(b + 1) *
                                      len(dM) / (2 * len(basak))]
            dMPS = (dM[len(dM) / 2:])[b * len(dM) / (2 * len(basak)):(b + 1) *
                                      len(dM) / (2 * len(basak))]
            c51.scatter_plot(
                np.arange(len(dMSS)), dMSS, '%s %s [R(t+%s)-R(t)]/%s ss' %
                (basak[b], str(mq), str(tau), str(tau)))
            c51.scatter_plot(
                np.arange(len(dMPS)), dMPS, '%s %s [R(t+%s)-R(t)]/%s ps' %
                (basak[b], str(mq), str(tau), str(tau)))
            plt.show()
    # fit fh correlator
    if params['flags']['fit_gA']:
        # data concatenated previously
        # read priors
        prior = c51.fhbaryon_priors(barp['priors'], fhbp['priors'], basak,
                                    nstates)
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T, nstates, tau)
        # read trange
        trange = barp['trange']
        fhtrange = fhbp['trange']
        # fit two point
        for b in range(len(basak)):
            SSspec = (spec[:len(spec) / 2])[b * T:(b + 1) * T]
            PSspec = (spec[len(spec) / 2:])[b * T:(b + 1) * T]
            boot0gv = np.concatenate((SSspec, PSspec))
            boot0fit = c51.fitscript_v2(trange,
                                        T,
                                        boot0gv,
                                        boot0p,
                                        fitfcn.twopt_baryon_ss_ps,
                                        basak=[basak[b]])
            boot0p = boot0fit['rawoutput'][0].p
        # fit fhcorr
        for b in range(len(basak)):
            SSdM = (dM[:len(dM) / 2])[b * T:(b + 1) * T]
            PSdM = (dM[len(dM) / 2:])[b * T:(b + 1) * T]
            boot0gv = np.concatenate((SSdM, PSdM))
            boot0fit = c51.fitscript_v2(fhtrange,
                                        T,
                                        boot0gv,
                                        boot0p,
                                        fitfcn.fhbaryon_ss_ps,
                                        basak=[basak[b]])
            boot0p = boot0fit['rawoutput'][0].p
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit, 'E0', '%s' % str(mq))
            c51.stability_plot(boot0fit, 'gA00', '%s' % str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            #tbl_print['fhtmin'] = boot0fit['fhtmin']
            #tbl_print['fhtmax'] = boot0fit['fhtmax']
            tbl_print['E0'] = [
                boot0fit['pmean'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            tbl_print['dE0'] = [
                boot0fit['psdev'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            tbl_print['gA00'] = [
                boot0fit['pmean'][t]['gA00']
                for t in range(len(boot0fit['pmean']))
            ]
            tbl_print['dgA00'] = [
                boot0fit['psdev'][t]['gA00']
                for t in range(len(boot0fit['pmean']))
            ]
            blist = []
            for b in params['gA_fit']['basak']:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' % b] = [
                    boot0fit['pmean'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0s' % b] = [
                    boot0fit['psdev'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_Z0p' % b] = [
                    boot0fit['pmean'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0p' % b] = [
                    boot0fit['psdev'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
                boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array(
                [[fhbp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],
                 [fhbp['meta_id']['PS'][i]
                  for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('fhbaryon',
                                      nstates=nstates,
                                      tau=params['gA_fit']['tau'])
            for t in range(len(boot0fit['tmin'])):
                baryon_corr_lst = np.array([[
                    barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']
                ], [
                    barp['meta_id']['PS'][i] for i in params['gA_fit']['basak']
                ]]).flatten()
                baryon_fit_id = c51.select_fitid(
                    'baryon', nstates=nstates, basak=params['gA_fit']['basak'])
                baryon_tmin = trange['tmin'][0]
                baryon_tmax = trange['tmax'][0]
                baryon_init_id = psql.initid(
                    c51.baryon_initpriors(barp['priors'], basak, nstates))
                baryon_prior_id = psql.priorid(
                    c51.dict_of_tuple_to_gvar(
                        c51.baryon_priors(barp['priors'], basak, nstates)))
                baryon_id = psql.select_boot0("proton", baryon_corr_lst,
                                              baryon_fit_id, baryon_tmin,
                                              baryon_tmax, baryon_init_id,
                                              baryon_prior_id)
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['tmin'][t]
                tmax = boot0fit['tmax'][t]
                result = c51.make_result(boot0fit, t)
                psql.submit_fhboot0('fhproton', corr_lst, baryon_id, fit_id,
                                    tmin, tmax, init_id, prior_id, result,
                                    params['flags']['update'])
예제 #14
0
def gA_bs(psql, params):
    # read data
    mq = params['gA_fit']['ml']
    basak = copy.deepcopy(params['gA_fit']['basak'])
    tag = params['gA_fit']['ens']['tag']
    stream = params['gA_fit']['ens']['stream']
    Nbs = params['gA_fit']['nbs']
    Mbs = params['gA_fit']['mbs']
    nstates = params['gA_fit']['nstates']
    tau = params['gA_fit']['tau']
    barp = params[tag]['proton'][mq]
    fhbp = params[tag]['gA'][mq]
    print "fitting for gA mq %s, basak %s, ens %s%s, Nbs %s, Mbs %s" % (
        str(mq), str(basak), str(tag), str(stream), str(Nbs), str(Mbs))
    # read two point
    SSl = np.array([
        psql.data('dwhisq_corr_baryon', idx)
        for idx in [barp['meta_id']['SS'][i] for i in basak]
    ])
    PSl = np.array([
        psql.data('dwhisq_corr_baryon', idx)
        for idx in [barp['meta_id']['PS'][i] for i in basak]
    ])
    T = len(SSl[0, 0])
    # read fh correlator
    fhSSl = np.array([
        psql.data('dwhisq_fhcorr_baryon', idx)
        for idx in [fhbp['meta_id']['SS'][i] for i in basak]
    ])
    fhPSl = np.array([
        psql.data('dwhisq_fhcorr_baryon', idx)
        for idx in [fhbp['meta_id']['PS'][i] for i in basak]
    ])
    # concatenate and make gvars to preserve correlations
    SS = SSl[0]
    PS = PSl[0]
    for i in range(len(SSl) - 1):  # loop over basak operators
        SS = np.concatenate((SS, SSl[i + 1]), axis=1)
        PS = np.concatenate((PS, PSl[i + 1]), axis=1)
    fhSS = fhSSl[0]
    fhPS = fhPSl[0]
    for i in range(len(fhSSl) - 1):
        fhSS = np.concatenate((fhSS, fhSSl[i + 1]), axis=1)
        fhPS = np.concatenate((fhPS, fhPSl[i + 1]), axis=1)
    boot0 = np.concatenate((SS, PS, fhSS, fhPS), axis=1)
    # make gvars
    gvboot0 = c51.make_gvars(boot0)
    spec = gvboot0[:len(gvboot0) / 2]
    fh = gvboot0[len(gvboot0) / 2:]
    # R(t)
    Rl = fh / spec
    # dmeff [R(t+tau) - R(t)] / tau
    #dM = Rl
    dM = (np.roll(Rl, -tau) - Rl) / float(
        tau
    )  #This is needed to plot gA correctly in the effective plots, but will give wrong data to fit with.
    # plot data
    if params['flags']['plot_data']:
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS[0])), c51.make_gvars(SS),
                             '%s %s ss' % (basak[b], str(mq)))
            c51.scatter_plot(np.arange(len(PS[0])), c51.make_gvars(PS),
                             '%s %s ps' % (basak[b], str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(c51.make_gvars(SS), 1, 'log')
            meff_ps = eff.effective_mass(c51.make_gvars(PS), 1, 'log')
            xlim = [2, len(meff_ss) / 3 - 2]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)),
                             meff_ss,
                             '%s %s ss effective mass' % (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)),
                             meff_ps,
                             '%s %s ps effective mass' % (basak[b], str(mq)),
                             xlim=xlim,
                             ylim=ylim)
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = eff.scaled_correlator(c51.make_gvars(SS),
                                              E0,
                                              phase=1.0)
            scaled_ps = eff.scaled_correlator(c51.make_gvars(PS),
                                              E0,
                                              phase=1.0)
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(
                np.arange(len(scaled_ss)),
                scaled_ss,
                '%s %s ss scaled correlator (take sqrt to get Z0_s)' %
                (basak[b], str(mq)),
                xlim=xlim,
                ylim=ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(
                np.arange(len(scaled_ps)),
                scaled_ps,
                '%s %s ps scaled correlator (divide by Z0_s to get Z0_p)' %
                (basak[b], str(mq)),
                xlim=xlim,
                ylim=ylim)
            plt.show()
    if params['flags']['fit_twopt']:
        # data already concatenated previously
        # read priors
        prior = c51.baryon_priors(barp['priors'], basak, nstates)
        ## read trange
        trange = barp['trange']
        ## fit boot0
        boot0gv = spec
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T, nstates)
        boot0fit = c51.fitscript_v2(trange,
                                    T,
                                    boot0gv,
                                    boot0p,
                                    fitfcn.twopt_baryon_ss_ps,
                                    basak=params['gA_fit']['basak'])
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit, 'E0', '%s' % str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['E0'] = [
                boot0fit['pmean'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            tbl_print['dE0'] = [
                boot0fit['psdev'][t]['E0']
                for t in range(len(boot0fit['pmean']))
            ]
            blist = []
            for b in params['gA_fit']['basak']:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' % b] = [
                    boot0fit['pmean'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0s' % b] = [
                    boot0fit['psdev'][t]['%s_Z0s' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_Z0p' % b] = [
                    boot0fit['pmean'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
                tbl_print['%s_dZ0p' % b] = [
                    boot0fit['psdev'][t]['%s_Z0p' % b]
                    for t in range(len(boot0fit['pmean']))
                ]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
                boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array(
                [[barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],
                 [barp['meta_id']['PS'][i]
                  for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('baryon',
                                      nstates=nstates,
                                      basak=params['gA_fit']['basak'])
            for t in range(len(boot0fit['tmin'])):
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['tmin'][t]
                tmax = boot0fit['tmax'][t]
                result = c51.make_result(boot0fit, t)
                psql.submit_boot0('proton', corr_lst, fit_id, tmin, tmax,
                                  init_id, prior_id, result,
                                  params['flags']['update'])
    # plot fh correlator
    if params['flags']['plot_fhdata']:
        for b in range(len(basak)):
            # raw correlator dC_lambda/dlambda
            fhSS = fhSSl[b]
            fhPS = fhPSl[b]
            c51.scatter_plot(np.arange(len(fhSS[0])), c51.make_gvars(fhSS),
                             '%s %s fh ss' % (basak[b], str(mq)))
            c51.scatter_plot(np.arange(len(fhPS[0])), c51.make_gvars(fhPS),
                             '%s %s fh ps' % (basak[b], str(mq)))
            plt.show()
            # R(t)
            RSS = (Rl[:len(Rl) / 2])[b * len(Rl) / (2 * len(basak)):(b + 1) *
                                     len(Rl) / (2 * len(basak))]
            RPS = (Rl[len(Rl) / 2:])[b * len(Rl) / (2 * len(basak)):(b + 1) *
                                     len(Rl) / (2 * len(basak))]
            c51.scatter_plot(np.arange(len(RSS)), RSS,
                             '%s %s R(t) ss' % (basak[b], str(mq)))
            c51.scatter_plot(np.arange(len(RPS)), RPS,
                             '%s %s R(t) ps' % (basak[b], str(mq)))
            plt.show()
            # dmeff R(t+tau) - R(t)
            dMSS = (dM[:len(dM) / 2])[b * len(dM) / (2 * len(basak)):(b + 1) *
                                      len(dM) / (2 * len(basak))]
            dMPS = (dM[len(dM) / 2:])[b * len(dM) / (2 * len(basak)):(b + 1) *
                                      len(dM) / (2 * len(basak))]
            xlim = [2, 15]
            ylim = [0.5, 2]  #c51.find_yrange(dMSS, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(dMSS)),
                             dMSS,
                             '%s %s [R(t+%s)-R(t)]/%s ss' %
                             (basak[b], str(mq), str(tau), str(tau)),
                             xlim=xlim,
                             ylim=ylim)
            ylim = [0.5, 2]  #c51.find_yrange(dMPS, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(dMPS)),
                             dMPS,
                             '%s %s [R(t+%s)-R(t)]/%s ps' %
                             (basak[b], str(mq), str(tau), str(tau)),
                             xlim=xlim,
                             ylim=ylim)
            if True:
                for i in range(len(dMSS)):
                    print i, ',', dMSS[i].mean, ',', dMSS[i].sdev, ',', dMPS[
                        i].mean, ',', dMPS[i].sdev
            plt.show()
    # fit fh correlator
    if params['flags']['fit_gA']:
        # data concatenated previously
        # read priors
        prior = c51.fhbaryon_priors(barp['priors'], fhbp['priors'], basak,
                                    nstates)
        print prior
        # read trange
        trange = barp['trange']
        fhtrange = fhbp['trange']
        # fit boot0
        boot0gv = np.concatenate((spec, dM))
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T, nstates, tau)
        boot0fit = c51.fitscript_v3(trange,
                                    fhtrange,
                                    T,
                                    boot0gv,
                                    boot0p,
                                    fitfcn.baryon_dm_ss_ps,
                                    basak=params['gA_fit']['basak'])
        print boot0fit['rawoutput'][0]
        #print {k: boot0fit['p0'][0][k] for k in [bk for n in range(nstates) for bk in barp['priors'][n+1].keys()]}
        if False:  # plot R(t+1) - R(t)
            posterior = boot0fit['post'][0]
            x = np.arange(20)
            ssline, psline = fitfcn.baryon_rt_fitline(x, posterior)
            for i in range(len(ssline)):
                print x[i], ',', ssline[i].mean, ',', ssline[
                    i].sdev, ',', psline[i].mean, ',', psline[i].sdev
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit, 'E0', '%s' % str(mq))
            c51.stability_plot(boot0fit, 'gA00', '%s' % str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['fhtmin'] = boot0fit['fhtmin']
            tbl_print['fhtmax'] = boot0fit['fhtmax']
            #tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            #tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            tbl_print['gA00'] = [
                boot0fit['pmean'][t]['gA00']
                for t in range(len(boot0fit['pmean']))
            ]
            tbl_print['dgA00'] = [
                boot0fit['psdev'][t]['gA00']
                for t in range(len(boot0fit['pmean']))
            ]
            #blist = []
            #for b in params['gA_fit']['basak']:
            #    blist.append(b[2:])
            #    blist.append(b[:2])
            #blist = np.unique(blist)
            #for b in blist:
            #    tbl_print['%s_Z0s' %b] = [boot0fit['pmean'][t]['%s_Z0s'%b] for t in range(len(boot0fit['pmean']))]
            #    tbl_print['%s_dZ0s' %b] = [boot0fit['psdev'][t]['%s_Z0s' %b] for t in range(len(boot0fit['pmean']))]
            #    tbl_print['%s_Z0p' %b] = [boot0fit['pmean'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            #    tbl_print['%s_dZ0p' %b] = [boot0fit['psdev'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2']) / np.array(
                boot0fit['dof'])
            tbl_print['chi2'] = boot0fit['chi2']
            tbl_print['chi2f'] = boot0fit['chi2f']
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
        # submit boot0 to db
        if params['flags']['write']:
            corr_lst = np.array(
                [[fhbp['meta_id']['SS'][i] for i in params['gA_fit']['basak']],
                 [fhbp['meta_id']['PS'][i]
                  for i in params['gA_fit']['basak']]]).flatten()
            fit_id = c51.select_fitid('fhbaryon',
                                      nstates=nstates,
                                      tau=params['gA_fit']['tau'])
            for t in range(len(boot0fit['tmin'])):
                baryon_corr_lst = np.array([[
                    barp['meta_id']['SS'][i] for i in params['gA_fit']['basak']
                ], [
                    barp['meta_id']['PS'][i] for i in params['gA_fit']['basak']
                ]]).flatten()
                baryon_fit_id = c51.select_fitid(
                    'baryon', nstates=nstates, basak=params['gA_fit']['basak'])
                baryon_tmin = trange['tmin'][0]
                baryon_tmax = trange['tmax'][0]
                baryon_p0 = {
                    k: boot0fit['p0'][0][k]
                    for k in [
                        bk for n in range(nstates)
                        for bk in barp['priors'][n + 1].keys()
                    ]
                }
                baryon_init_id = psql.initid(baryon_p0)
                baryon_prior_id = psql.priorid(
                    c51.dict_of_tuple_to_gvar(
                        c51.baryon_priors(barp['priors'], basak, nstates)))
                baryon_id = psql.select_boot0("proton", baryon_corr_lst,
                                              baryon_fit_id, baryon_tmin,
                                              baryon_tmax, baryon_init_id,
                                              baryon_prior_id)
                init_id = psql.initid(boot0fit['p0'][t])
                prior_id = psql.priorid(boot0fit['prior'][t])
                tmin = boot0fit['fhtmin'][t]
                tmax = boot0fit['fhtmax'][t]
                result = c51.make_result(boot0fit, t)
                print tmin, tmax
                psql.submit_fhboot0('fhproton', corr_lst, baryon_id, fit_id,
                                    tmin, tmax, init_id, prior_id, result,
                                    params['flags']['update'])
        if params['flags']['csvformat']:
            for t in range(len(boot0fit['fhtmin'])):
                print nstates, ',', boot0fit['tmin'][t], ',', boot0fit['tmax'][
                    t], ',', boot0fit['fhtmin'][t], ',', boot0fit['fhtmax'][
                        t], ',', boot0fit['pmean'][t]['gA00'], ',', boot0fit[
                            'psdev'][t]['gA00'], ',', (
                                np.array(boot0fit['chi2']) /
                                np.array(boot0fit['dof']))[t], ',', (
                                    np.array(boot0fit['chi2']) /
                                    np.array(boot0fit['chi2f'])
                                )[t], ',', boot0fit['logGBF'][t]
예제 #15
0
def fit_proton(params,gvboot0):
    # read data
    ens = params['LHCP_params']['ens']
    nstates = params['LHCP_params']['nstates']
    barp = params[ens]['proton']
    basak = ['G1G1', 'G1G2', 'G1G3', 'G2G1', 'G2G2', 'G2G3', 'G3G1', 'G3G2', 'G3G3'] # src snk
    mq = 'unitary'
    # make gvars
    spec = gvboot0[:len(gvboot0)/2]
    fh = gvboot0[len(gvboot0)/2:]
    T = len(fh)/(2*len(basak))
    # R(t)
    Rl = fh/spec
    # dmeff [R(t+tau) - R(t)] / tau
    dM = Rl
    #dM = (np.roll(Rl,-tau)-Rl)/float(tau) #This is needed to plot gA correctly in the effective plots, but will give wrong data to fit with.
    # plot data
    if params['flags']['plot_data']:
        SSl = spec[:len(spec)/2].reshape((len(basak),T))
        PSl = spec[len(spec)/2:].reshape((len(basak),T))
        for b in range(len(basak)):
            SS = SSl[b]
            PS = PSl[b]
            # raw correlator
            c51.scatter_plot(np.arange(len(SS)), SS, '%s %s ss' %(basak[b],str(mq)))
            c51.scatter_plot(np.arange(len(PS)), PS, '%s %s ps' %(basak[b],str(mq)))
            plt.show()
            # effective mass
            eff = c51.effective_plots(T)
            meff_ss = eff.effective_mass(SS, 1, 'log')
            meff_ps = eff.effective_mass(PS, 1, 'log')
            xlim = [2, len(meff_ss)/3-2]
            ylim = c51.find_yrange(meff_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ss)), meff_ss, '%s %s ss effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(meff_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(meff_ps)), meff_ps, '%s %s ps effective mass' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            plt.show()
            # scaled correlator
            E0 = barp['priors'][1]['E0'][0]
            scaled_ss = eff.scaled_correlator(SS, E0, phase=1.0)
            scaled_ps = eff.scaled_correlator(PS, E0, phase=1.0)
            ylim = c51.find_yrange(scaled_ss, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ss)), scaled_ss, '%s %s ss scaled correlator (take sqrt to get Z0_s)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            ylim = c51.find_yrange(scaled_ps, xlim[0], xlim[1])
            c51.scatter_plot(np.arange(len(scaled_ps)), scaled_ps, '%s %s ps scaled correlator (divide by Z0_s to get Z0_p)' %(basak[b],str(mq)), xlim = xlim, ylim = ylim)
            plt.show()
    if params['flags']['fit_twopt']:
        # data already concatenated previously
        # read priors
        prior = c51.baryon_priors(barp['priors'],basak,nstates)
        ## read trange
        trange = barp['trange']
        ## fit boot0
        boot0gv = spec
        boot0p = c51.dict_of_tuple_to_gvar(prior)
        fitfcn = c51.fit_function(T,nstates)
        boot0fit = c51.fitscript_v2(trange,T,boot0gv,boot0p,fitfcn.asqtad_twopt_baryon_ss_ps,basak=basak)
        print boot0fit['rawoutput'][0]
        if params['flags']['stability_plot']:
            c51.stability_plot(boot0fit,'E0','%s' %str(mq))
            c51.stability_plot(boot0fit,'E1','%s' %str(mq))
            plt.show()
        if params['flags']['tabulate']:
            tbl_print = collections.OrderedDict()
            tbl_print['tmin'] = boot0fit['tmin']
            tbl_print['tmax'] = boot0fit['tmax']
            tbl_print['E0'] = [boot0fit['pmean'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            tbl_print['dE0'] = [boot0fit['psdev'][t]['E0'] for t in range(len(boot0fit['pmean']))]
            tbl_print['E1'] = [boot0fit['pmean'][t]['E1'] for t in range(len(boot0fit['pmean']))]
            tbl_print['dE1'] = [boot0fit['psdev'][t]['E1'] for t in range(len(boot0fit['pmean']))]
            blist = []
            for b in basak:
                blist.append(b[2:])
                blist.append(b[:2])
            blist = np.unique(blist)
            for b in blist:
                tbl_print['%s_Z0s' %b] = [boot0fit['pmean'][t]['%s_Z0s'%b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0s' %b] = [boot0fit['psdev'][t]['%s_Z0s' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_Z0p' %b] = [boot0fit['pmean'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
                tbl_print['%s_dZ0p' %b] = [boot0fit['psdev'][t]['%s_Z0p' %b] for t in range(len(boot0fit['pmean']))]
            tbl_print['chi2/dof'] = np.array(boot0fit['chi2'])/np.array(boot0fit['dof'])
            tbl_print['logGBF'] = boot0fit['logGBF']
            print tabulate(tbl_print, headers='keys')
    return 0