def get_q_s_prob(q,s,eventname,eventinfo,data,parms_names,isspitz,use_color,I_minus_L,ntheta,outputlc=False,finmod_opt=False,date_mod=[]): alpha,delta,oblats,oblongs,obgammas = eventinfo parmfit,namefit,parmflx,nameflx,parmref = parms_names tbegin,talert,t0par,t0base,mjd = parmref thetas = np.linspace(0,360,ntheta+1) binlcs,binfull = [],[] bincau = [] use_fsfb = True start = time.clock() ############################ ### fake data generation ### ############################ print '----- Generating binary lcs... ------' for i in range(ntheta): itheta = thetas[i] parmbfit = np.hstack([parmfit,[itheta,q,s]]) parmbflx = parmflx iobbinlc,iobfull = [],[] 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],use_fsfb,caustic=bincau) bincau = [xcau,ycau] iobbinlc.append([date,ibinlc[:,2],ferr]) iobfull.append([date,ibinlc[:,0],ibinlc[:,1],ibinlc[:,2],ferr]) ### find the fine input model ### if finmod_opt == True: ibinlc_mod,xcau,ycau = genlc.getbinlc(alpha,delta,oblats[iob],oblongs[iob],date_mod,parmbfit,parmbflx[iob],obgammas[iob],t0par,isspitz[iob],use_fsfb,caustic=bincau) np.savetxt('output/models/finmod-%s-%.5f-%.2f-%03d-%d.dat'%(eventname,q,s,itheta,iob+1),np.vstack([date_mod,ibinlc_mod.T]).T,fmt='%f') binlcs.append(iobbinlc) binfull.append(iobfull) np.savetxt('output/caustics/caustic-%s-%.5f-%.2f.dat'%(eventname,q,s),np.array(bincau).T,fmt='%f') print '------ Binary lcs generated: ',time.clock()-start,' s ------' ################################## ### employ sfitpy to fake data ### ################################## if talert > max(data[0][0]): ## obj event print '--- This is an obj event ---' trials = sfitpy.massfit_obj(q,s,thetas,binlcs,binfull,eventinfo,parms_names,isspitz,use_color,I_minus_L,outputlc) else: ## subj event print '--- This is a subj event ---' trials = sfitpy.massfit_sub(q,s,thetas,binlcs,binfull,eventinfo,parms_names,isspitz,use_color,I_minus_L,outputlc) return trials
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
def main(): ## Event Information ## eventname = 'ob151346' alpha = (17. + 59/60. + 13.67/3600.)*15 delta =-(28. + 56/60. + 11.8/3600.) fs,fb = 0.07 Gamma = 0.60 ## OGLE site ## qlong = -70.702 qlat = -29.0083 ## planet/binary parameters ## t0 = 7190.74 u0 = 0.18 te = 25.12 rho = 0.001 theta = 80. # in degrees q = 0.0014 s = 1.116 Ibase = 19.4027 fb_over_fs = -0.4134 # ## Binary Parameters for ob150914 ## # t0 = 7172.14 # u0 = 0.0243 # te = 36.8418 # rho = 0.0047 # theta = 3.81532/np.pi*180 # theta = 180-theta ## Bozza system # q = 0.369 # s = 1.4718 # ## Bozza gave Ibase and Fb/Fs ## # Ibase = 14.6422 # fb_over_fs = 18.445 # ## binary event ob150060 ## # t0 = 7148.109 # u0 = 0.019 # te = 71.8 # rho = 0.00479 # theta = 3.593/np.pi*180 # q = 1.10 # s = 3.40 # Ibase = 16.6885 # fb_over_fs = 0.3337 # ## Planetary Event OB150051 ## # t0 = 7083.100 # u0 = 0.221 # te = 10.95 # rho = 0.045 # theta = 5.351*180/np.pi #Han # q = 5.94e-3 # s = 0.939 # Ibase = 16.629 # fb_over_fs = -0.0216 ########### fb_plus_fs = 10**(0.4*(18-Ibase)) fs = fb_plus_fs/(1+fb_over_fs) fb = fb_over_fs*fs print fs,fb ########### fig = plt.figure(figsize=(8.5,11)) ax1 = plt.subplot(311) ax2 = plt.subplot(312) ax3 = plt.subplot(313) linestr = ['-','--','-.'] # colors = ['k','#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','0.5'] # pies = [-0.1,0,0.1] pieNs = [0.0193] pieEs = [-0.2949] colors = cm.rainbow(np.linspace(0,1,len(pieEs)*len(pieNs))) nmod = 0 # ogle = np.loadtxt('data/ob150966.dat') # dates = ogle[:,0]-2450000 dates = np.linspace(7100,7250,15000) for pieE in pieEs: for pieN in pieNs: piex = pieN piey = pieE parmfit = [t0,u0,te,rho,piex,piey,theta,q,s] parmflx = [fs,fb] t0par = t0 lcg,xcau,ycau = genlc.getbinlc(alpha,delta,qlat,qlong,dates,parmfit,parmflx,Gamma,t0par,False,True) lcs,xcau,ycau = genlc.getbinlc(alpha,delta,qlat,qlong,dates,parmfit,parmflx,Gamma,t0par,True,True) magg = 18-2.5*np.log10(lcg[:,2]) mags = 18-2.5*np.log10(lcs[:,2]) if nmod == 0: ax1.plot(dates,magg,color='b',linestyle='--',label='OGLE',lw=2) # np.savetxt('ob150966-Bozza.mod',np.vstack([dates,magg]).T,fmt='%f') ax2.plot(dates,magg,color='b',linestyle='--',label='OGLE',lw=2) ax3.plot(xcau,ycau,marker='.',markersize=2,linestyle='none',color='k') ax3.plot(lcg[:,0],lcg[:,1],color='b') ax1.plot(dates,mags,color=colors[nmod],label='Spitzer,(%.1f,%.1f)'%(pieE,pieN)) ax2.plot(dates,mags,color=colors[nmod],label='Spitzer,(%.1f,%.1f)'%(pieE,pieN)) ax3.plot(lcs[:,0],lcs[:,1],color=colors[nmod]) nmod += 1 ax3.axis('equal') plt.suptitle(eventname,fontsize=14,fontweight='bold') ax1.legend(loc=0,fontsize=10) ax1.invert_yaxis() ax2.invert_yaxis() ax2.axis([7143,7156,15.3,11.7]) ax1.yaxis.set_minor_locator(AutoMinorLocator(5)) ax1.xaxis.set_minor_locator(AutoMinorLocator(5)) ax2.yaxis.set_minor_locator(AutoMinorLocator(5)) ax2.xaxis.set_minor_locator(AutoMinorLocator(5)) ax3.yaxis.set_minor_locator(AutoMinorLocator(5)) ax3.xaxis.set_minor_locator(AutoMinorLocator(5)) plt.show()