コード例 #1
0
ファイル: splot.py プロジェクト: zhu-wei-pku/SFITPY
def main():
    eventname = 'ob150448'
#    eventname = raw_input('Please give the event name (eg, ob140124): ')
    alpha,delta,datfiles,oblats,oblongs,obgammas,isspitz,parmref,parmfit,parmflx,errfacs,namefit,nameflx,use_color,I_minus_L = getparm.getEventInfo(eventname)
    data = getdat.getalldat(datfiles,errfacs,isspitz,parmref)
    tbegin,talert,t0par,t0base,mjd = parmref
    parms_names = [parmfit,namefit,parmflx,nameflx,parmref]
    parmfree,namefree,parmfix,namefix = sfitpy.initialization(parms_names,isspitz)
    ## generating fake data ##
    q,s,theta = 1e-6,10,0.
    parmbfit = np.hstack([parmfit,[theta,q,s]])
    parmbflx = parmflx
#    data_fake = []
#    for iob in range(len(data)):
#        date,flux,ferr,mag,merr = data[iob]
#        ibinlc,xcau,ycau = genlc.getbinlc(alpha,delta,oblats[iob],oblongs[iob],date,parmbfit,parmbflx[iob],obgammas[iob],t0par,isspitz[iob],True)
#        imag = 18-2.5*np.log10(ibinlc[:,2])
#        ## save the fake data sets ##
#        np.savetxt('fake-data-%d.dat'%(iob+1),np.vstack([date,imag,merr,np.ones_like(date),np.ones_like(date)]).T,fmt='%f')
#        data_fake.append([date,ibinlc[:,2],ferr,imag,merr])
#    data = data_fake
#   ## change (pien,piee) ##
#    parmfree[namefree=='pien'] = 0.2
#    parmfree[namefree=='piee'] = 1.
#    print parmfree
    ### Downhill simplex method ###
#    parmfit,parmflx,chi2s,dof = sfitpy.downhill(sfitpy.lnprob_func,namefree,parmfree,namefix,parmfix,data,[alpha,delta,oblats,oblongs,obgammas],parmref,isspitz,use_color=use_color,I_minus_L=I_minus_L)
    ### MCMC to minimize the chisq ###
#    parmfit,parmflx,chi2s,dof = sfitpy.mcmc(sfitpy.lnprob_func,eventname,namefree,parmfree,namefix,parmfix,data,[alpha,delta,oblats,oblongs,obgammas],parmref,isspitz,use_color=use_color,I_minus_L=I_minus_L)
#### use getchi2 instead of lnprob_func: TBD ###
    ### Downhill simplex method ###
    parmfit,parmflx,chi2s,dof = sfitpy.downhill(sfitpy.getchi2,namefree,parmfree,namefix,parmfix,data,[alpha,delta,oblats,oblongs,obgammas],parmref,isspitz,use_color=use_color,I_minus_L=I_minus_L)
    ### MCMC to minimize the chisq ###
#    parmfit,parmflx,chi2s,dof = sfitpy.mcmc(sfitpy.getchi2,eventname,namefree,parmfree,namefix,parmfix,data,[alpha,delta,oblats,oblongs,obgammas],parmref,isspitz,use_color=use_color,I_minus_L=I_minus_L)
#### Find the model according to the given parameters ###
    fmod,mmod = [],[] ## model in flux and magnitude
    trajmod = []
    fmod_fin,mmod_fin = [],[]
    trajmod_fin = []
#    parmfit,parmflx = sfitpy.getparmfit(parmfree,parmfix,namefree,namefix)
    te = parmfit[namefit=='te']
    npts = (7300-6500)*10000./te
    npts = min([npts,100000])
    tmod = np.linspace(6500,7300,int(npts))
    for iob in range(len(data)):
        ## find model ##
        date,flux,ferr = data[iob][:3]
        itraj,ifmod = genlc.getslc(alpha,delta,oblats[iob],oblongs[iob],date,parmfit,parmflx[iob],obgammas[iob],t0par,isspitz[iob],False,True)
        immod = 18.-2.5*np.log10(ifmod)
        fmod.append(ifmod)
        mmod.append(immod)
        trajmod.append(itraj)
        ## find a finer light curve ##
        date = tmod
        itraj,ifmod = genlc.getslc(alpha,delta,oblats[iob],oblongs[iob],date,parmfit,parmflx[iob],obgammas[iob],t0par,isspitz[iob],False,True)
        immod = 18.-2.5*np.log10(ifmod)
        fmod_fin.append(ifmod)
        mmod_fin.append(immod)
        trajmod_fin.append(itraj)
#    fmod = np.array(fmod)
#    mmod = np.array(mmod)
    trajmod = np.array(trajmod)
    trajmod_fin = np.array(trajmod_fin)
    print 'Total chisq = ',chi2s[0]
    print ['%.2f/%d'%(chi2s[i+1],dof[i]) for i in range(len(dof))]
コード例 #2
0
ファイル: sensitivity.py プロジェクト: zhu-wei-pku/SFITPY
def main():
    eventname = 'ob140939'
### find event information, input data, program setup ###
    alpha,delta,datfiles,oblat,oblong,obgamma = getparm.getEventInfo(eventname)
    parmref,parmfit,parmflx,efacs = getparm.getFitSetting(eventname)
    t0par = parmref[0]
    t0base= parmref[1]
    HMJD  = parmref[2]
    nobs  = len(datfiles)
    parmfit[0] += t0base
    print 'Total number of obs: ',nobs
    print 'Parameters (ref): ',parmref
    print 'Parameters (sfit): ',parmfit
    print 'Parameters (flux): ',parmflx
    print 'Errfacs: ',efacs
#    isspit = np.zeros(nobs).astype(bool)
    isspit = np.zeros(nobs-1).astype(bool)
    isspit = np.hstack([isspit,True])
    linestrs = ['-','--']
    date,flux,ferr,magr,merr = getdat.getdat1(nobs,datfiles,efacs,isspit,HMJD)
#############################################################
### use more intensive time sequence ###
    date_mod = np.linspace(6800,6900,1000)
#### choose s and q for sensitivity mapping ###
#    plot_opt = False
#    finmod_opt = False
#    ntheta = 300
#    qList = np.logspace(np.log10(1e-4),np.log10(1e-2),50)
#    sList = np.logspace(np.log10(0.1),np.log10(10),50)
#    ### five parallel programs ###
#    ncore = 6
#    each = 50/ncore
##    qList = qList[:each]
##    qList = qList[each:2*each]
##    qList = qList[2*each:3*each]
##    qList = qList[3*each:4*each]
##    qList = qList[4*each:5*each]
#    qList = qList[5*each:]
#################################################
### for test purpose ###
    plot_opt = True
    finmod_opt = True
    ntheta = 6
    qList = [1e-2]
    sList = [1.1]
########################
    for q in qList:
        trials = []
        for s in sList:
            theta = np.linspace(0,360,ntheta+1)
            xtrajs,ytrajs,binlcs = [],[],[]
            binlcs_mod = []
            use_fsfb = True
            start = time.clock()
            ############################
            ### fake data generation ###
            ############################
            for i in range(ntheta):
                itheta = theta[i]
                parmbfit = np.hstack([parmfit,[itheta,q,s]])
                parmbflx = parmflx
                iobxtraj,iobytraj,iobbinlc = [],[],[]
                iobbinlc_mod = []
                for iob in range(nobs):
                    ibinlc,xcau,ycau = genlc.getbinlc(alpha,delta,oblat[iob],oblong[iob],date[iob],parmbfit,parmbflx[iob],obgamma[iob],t0par,isspit[iob],use_fsfb)
                    iobxtraj.append(ibinlc[:,0])
                    iobytraj.append(ibinlc[:,1])
                    iobbinlc.append(ibinlc[:,2])
                    ### find the fine input model ###
                    if finmod_opt == True:
                        ibinlc_mod,xcau,ycau = genlc.getbinlc(alpha,delta,oblat[iob],oblong[iob],date_mod,parmbfit,parmbflx[iob],obgamma[iob],t0par,isspit[iob],use_fsfb)
                        iobbinlc_mod.append(ibinlc_mod[:,2])
                xtrajs.append(iobxtraj)
                ytrajs.append(iobytraj)
                binlcs.append(iobbinlc)
                binlcs_mod.append(iobbinlc_mod)
            xtrajs = np.array(xtrajs)
            ytrajs = np.array(ytrajs)
            binlcs = np.array(binlcs)
            print 'binary lcs generated: ',time.clock()-start
########################################################
            ################################
            ### employ sfit to fake data ###
            ################################
########################################################
            sinlcs,chi2all = [],[]
            parmfit0 = parmfit
            parmflx0 = parmflx
            for i in range(ntheta):
                itheta = theta[i]
########################################
############# SFITPY MODULE ############
########################################
### if sfit.getchi2 is used, the plotted
### light curve will not be right, beca-
### -use fs, fb are still the input vals
########################################
######## Downhill minimization only ###
                iparmfit,iparmflx,chi2 = sfit.downhill(sfit.lnprob_func,parmfit,parmflx,[date,binlcs[i],ferr],nobs,[alpha,delta,oblat,oblong,obgamma],t0par,isspit)
#                iparmfit,iparmflx,chi2 = sfit.downhill(sfit.getchi2,parmfit,parmflx,[date,binlcs[i],ferr],nobs,[alpha,delta,oblat,oblong,obgamma],t0par,isspit)
######## MCMC minimization only ###
#                iparmfit,iparmflx,chi2 = sfit.mcmcfit(sfit.getchi2,parmfit,parmflx,[date,binlcs[i],ferr],nobs,[alpha,delta,oblat,oblong,obgamma],t0par,isspit)
######## Downhill + MCMC ####
#                iparmfit,iparmflx,chi2 = sfit.downhill(sfit.getchi2,parmfit,parmflx,[date,binlcs[i],ferr],nobs,[alpha,delta,oblat,oblong,obgamma],t0par,isspit)
#                if chi2[0] > 200:
#                    iparmfit,iparmflx,chi2 = sfit.mcmcfit(sfit.getchi2,iparmfit,iparmflx,[date,binlcs[i],ferr],nobs,[alpha,delta,oblat,oblong,obgamma],t0par,isspit)
########################################
                if chi2[0] < 100:
                    parmfit0 = iparmfit
                    parmflx0 = iparmflx
                ### find best-fit model ###
                isinlc = []
                for iob in range(nobs):
                    itraj,ifmod = genlc.getslc(alpha,delta,oblat[iob],oblong[iob],date_mod,iparmfit,iparmflx[iob],obgamma[iob],t0par,isspit[iob],False,True)
                    isinlc.append(ifmod)
                ###########################
                temp = np.hstack([q,s,theta[i],chi2.reshape(-1),iparmfit,iparmflx.reshape(-1)])
                trials.append(temp)
                sinlcs.append(isinlc)
                chi2all.append(chi2[0])
                print '##########',q,s,theta[i],chi2[0],'############'
            sinlcs = np.array(sinlcs)
############################################################
            ###############################
            ### fake data demonstration ###
            ###############################
            if plot_opt == True:
                color1 = ['#99d8c9','#66c2a4','#41ae76','#238b45','#005824'] #green
                color2 = ['#fdbb84','#fc8d59','#ef6548','#d7301f','#990000'] #red
                color3 = ['#a6bddb','#74a9cf','#3690c0','#0570b0','#034e7b'] #blue
                color4 = ['#bdbdbd','#969696','#737373','#525252','#252525'] #black
                colors = np.array([color1,color2,color3,color4]).reshape(-1)
                fig1 = plt.figure(figsize=(8.5,11))
                nax1 = ntheta/2
                axs = []
                fig2 = plt.figure(figsize=(8.6,8))
                ax0 = fig2.add_subplot(111) # axis to plot the trajectories
                ax0.plot(xcau,ycau,linestyle='none',color='k',marker='o',markersize=1)
                ax0.axis('equal')
                for i in range(ntheta):
                    itheta = theta[i]
                    axi = fig1.add_subplot(nax1,2,i+1)
                    axs.append(axi)
                    for iob in range(nobs):
                        ### plot fake data ###
#                        iflux,iferr = binlcs[i][iob],ferr[iob] #do not convert to OGLE system
                        iflux,iferr = getdat.convert_to_ogle_dat(binlcs[i][iob],ferr[iob],parmflx[iob],parmflx[0]) #convert to OGLE system
                        axi.errorbar(date[iob],iflux,yerr=iferr,color=colors[i],linestyle='none',marker='o')
                        if finmod_opt == True:
#                            ifmod = binlcs_mod[i][iob] #do not convert to OGLE system
                            ifmod = getdat.convert_to_ogle_mod(binlcs_mod[i][iob],parmflx[iob],parmflx[0]) #convert to OGLE system
                            axi.plot(date_mod,ifmod,color=colors[i],linestyle='--')
                        ### plot best-fit smod ###
#                        ifmod = sinlcs[i][iob] #do not convert to OGLE system
                        ifmod = getdat.convert_to_ogle_mod(sinlcs[i][iob],parmflx[iob],parmflx[0]) #convert to OGLE system
                        axi.plot(date_mod,ifmod,color=colors[i])
                        ### plot trajectory ###
                        ax0.plot(xtrajs[i][iob],ytrajs[i][iob],color=colors[i],linestyle=linestrs[iob])
                    axi.set_xlim(6800,6900)
                    x1,x2,y1,y2 = axs[i].axis()
                    xpos = x1+(x2-x1)/10.
                    ypos = y2-(y2-y1)/10.
                    axi.text(xpos,ypos,r'$\theta=%3.1f^\circ, \chi^2=%3.1f$'%(itheta,chi2all[i]),va='center')
                    axi.invert_yaxis()
                plt.show()
        ### save parameters and chi2 ###
        np.savetxt('output/grid-%s-%e.dat'%(eventname,q),np.array(trials),fmt='%f')
    return
コード例 #3
0
def main(eventname):
    alpha,delta,datfiles,oblats,oblongs,obgammas,isspitz,parmref,parmfit,parmflx,errfacs,namefit,nameflx,use_color,I_minus_L = getparm.getEventInfo(eventname)
    data = getdat.getalldat(datfiles,errfacs,isspitz,parmref)
    tbegin,talert,t0par,t0base,mjd = parmref
### use more intensive time sequence ###
    date_mod = np.linspace(6700,7000,2000)
##### choose s and q for sensitivity mapping ###
#    ## for ob0124 ##
#    ntheta = 300
#    qList = np.logspace(np.log10(1e-5),np.log10(1e-2),20)
#    sList = np.logspace(-0.6,0.6,30)
#    nprocesses = 25
#    ## for ob0939 ##
#    ntheta = 300
#    qList = np.logspace(np.log10(1e-4),np.log10(1e-2),20)
#    sList = np.logspace(-0.4,0.4,100)
#    nprocesses = 6
#    ### pool.apply_async to parallelize ###
#    pool = mp.Pool(nprocesses)
#    for q in qList:
#        for s in sList:
#            pool.apply_async(get_q_s_prob,args=(q,s,eventname,[alpha,delta,oblats,oblongs,obgammas],data,[parmfit,namefit,parmflx,nameflx,parmref],isspitz,use_color,I_minus_L,ntheta),callback=mycallback)
#    pool.close()
#    pool.join()
#################################################
#### for test purpose ###
    plot_opt = True
    finmod_opt = True
    ntheta = 5
    qList = [7e-4]
    sList = [0.94]
#######################
    for q in qList:
        for s in sList:
            trials = get_q_s_prob(q,s,eventname,[alpha,delta,oblats,oblongs,obgammas],data,[parmfit,namefit,parmflx,nameflx,parmref],isspitz,use_color,I_minus_L,ntheta,outputlc=True,finmod_opt=True,date_mod=date_mod)
            mycallback(trials)
    return