Skip to content

Commit 6567477

Browse files
committed
quick change to fitting module
1 parent 0d63d89 commit 6567477

1 file changed

Lines changed: 19 additions & 26 deletions

File tree

‎MonoTools/fit.py‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ def make_phase(self,time,tcens,per,fit_ttv_polynomial=True,max_ttv_amp=0.66,idea
621621
#exp_transtimes = tcens[0]+(tcens[-1]-tcens[0])*np.hstack([0,ideal_pratio_spans[:,1]])/ideal_pratio_spans[0,0]
622622
linfit=np.polyfit(exp_transn,tcens,len(tcens))#
623623
poly_tcens=np.polyval(linfit,exp_transn)
624-
if np.max(tcens-poly_tcens)<max_ttv_amp and np.max(np.polyval(linfit,time))<max_ttv_amp:
625-
print("TTVs appear too large - "+str(max_ttv_amp)+" day threshold exceeded.")
624+
if np.max(tcens-poly_tcens)>max_ttv_amp:
625+
# and np.max(np.polyval(linfit,time))<max_ttv_amp:
626626
return self.make_phase(time,tcens,per,fit_ttv_polynomial=False,ideal_pratio_spans=ideal_pratio_spans)# Turning off fit_ttv_polynomial and returning "normal" linear fit
627627
else:
628628
#This gives the polynomial-derived t0s, which we can then find minimum distances to:
@@ -993,7 +993,6 @@ def init_starpars(self,Rstar=None,Teff=None,logg=None,FeH=0.0,rhostar=None,Mstar
993993
rho_MR=[Mstar[0]/self.Rstar[0]**3]
994994
rho_MR+=[(Mstar[0]+Mstar[1])/(self.Rstar[0]-abs(self.Rstar[1]))**3/rho_MR[0]-1.0,
995995
1.0-(Mstar[0]-abs(Mstar[2]))/(self.Rstar[0]+self.Rstar[2])**3/rho_MR[0]]
996-
print(rho_MR)
997996
#Weighted sums of two avenues to density:
998997
rhostar=[rho_logg[0]*(rho_MR[1]+rho_MR[2])/(rho_logg[1]+rho_logg[2]+rho_MR[1]+rho_MR[2])+
999998
rho_MR[0]*(rho_logg[1]+rho_logg[2])/(rho_logg[1]+rho_logg[2]+rho_MR[1]+rho_MR[2])]
@@ -1159,7 +1158,7 @@ def init_transit_indices(self):
11591158
#p=np.max(self.planets[pl]['period_aliases'])
11601159
t0s= self.init_soln['t0_'+pl] if hasattr(self,'init_soln') else self.planets[pl]['tcens']
11611160
#print(t0s)
1162-
phase=self.make_phase(self.lc.time,t0s,per=None,ideal_pratio_spans=self.planets[pl]['ideal_pratio_span'])
1161+
phase=self.make_phase(self.lc.time,t0s,per=None,ideal_pratio_spans=self.planets[pl]['p_ratios'][:,:,0])
11631162
elif pl in self.duos:
11641163
t0= self.init_soln['t0_'+pl] if hasattr(self,'init_soln') else np.max(self.planets[pl]['tcens'])
11651164
p=abs(self.init_soln['t0_2_'+pl]-self.init_soln['t0_'+pl]) if hasattr(self,'init_soln') else abs(np.max(self.planets[pl]['tcens'])-np.min(self.planets[pl]['tcens']))
@@ -1170,7 +1169,6 @@ def init_transit_indices(self):
11701169
dur = self.init_soln['tdur_'+pl] if hasattr(self,'init_soln') else self.planets[pl]['tdur']
11711170
self.lc.near_trans[pl] = abs(phase)<self.cut_distance*dur
11721171
self.lc.near_trans['all'] += self.lc.near_trans[pl][:]
1173-
print(phase[abs(self.lc.time-2677.94)<0.2],np.sum(self.lc.near_trans[pl][:]))
11741172
self.lc.in_trans[pl] = abs(phase)<self.mask_distance*dur
11751173
self.lc.in_trans['all'] += self.lc.in_trans[pl][:]
11761174

@@ -1562,7 +1560,6 @@ def init_interpolated_v_prior(self):
15621560
interp_locs={'kipping':"kip", 'vaneylen':"vve",'flat':"flat",'apogee':'apo','bernmodel_both':'both__bernmodel','bernmodel_sing':'singles__bernmodel','bernmodel_mult':'multis__bernmodel'}
15631561

15641562
interp_locs['auto']='both__bernmodel' if len(self.planets)==1 else 'multis__bernmodel' #1 transiting planet may not mean only one, so assuming both where Npl=1.
1565-
print(interp_locs[self.ecc_prior.lower()],self.ecc_prior,interp_locs,interp_locs[self.ecc_prior.lower()][:4],["bern","auto"])
15661563
if self.ecc_prior.lower() not in ["bernmodel_sing","bernmodel_mult","bernmodel_both","auto"]:
15671564
f_emarg=gzip.open(os.path.join(MonoData_tablepath,
15681565
"emarg_array_"+interp_locs[self.ecc_prior.lower()]+".txt.gz"), "rb")
@@ -1788,7 +1785,6 @@ def init_pymc_fast(self, start=None, ld_mult=1.75):
17881785
mu=np.nanmedian(trans_ld_dists,axis=0),
17891786
sigma=np.clip(ld_mult*np.nanstd(trans_ld_dists,axis=0),0.1,1.0), shape=2,
17901787
lower=0.0, upper=1.0, initval=np.clip(np.nanmedian(trans_ld_dists,axis=0),0,1))
1791-
print(q_star[mis].shape)
17921788
u_star[mis] = pm.Deterministic("u_star_"+mis, pm.math.stack([2*pm.math.sqrt(q_star[mis][0])*q_star[mis][1],
17931789
pm.math.sqrt(q_star[mis][0])*(1 - 2*q_star[mis][1])]))
17941790
else:
@@ -2164,8 +2160,8 @@ def assess_period_from_posterior(self,**kwargs):
21642160
#Period from:
21652161
# - Uniform per-gap (in ^per_index)
21662162
ind_min = np.power(self.planets[pl]['per_gaps']['gap_ends']/self.planets[pl]['per_gaps']['gap_starts'],self.per_index)
2167-
print((1-ind_min[None,None,:]).shape,np.random.random(sample_shapes)[:,:,None].shape,ind_min[None,None,:].shape,self.planets[pl]['per_gaps']['gap_starts'][None,None,:].shape)
2168-
print(((((1-ind_min[None,None,:])*np.random.random(sample_shapes)[:,:,None]+ind_min[None,None,:])**(1/self.per_index))*self.planets[pl]['per_gaps']['gap_starts'][None,None,:]).shape)
2163+
# print((1-ind_min[None,None,:]).shape,np.random.random(sample_shapes)[:,:,None].shape,ind_min[None,None,:].shape,self.planets[pl]['per_gaps']['gap_starts'][None,None,:].shape)
2164+
# print(((((1-ind_min[None,None,:])*np.random.random(sample_shapes)[:,:,None]+ind_min[None,None,:])**(1/self.per_index))*self.planets[pl]['per_gaps']['gap_starts'][None,None,:]).shape)
21692165
self.trace.posterior['per_'+pl] = (('chain','draw','per_'+pl+'_dim_0'), (((1-ind_min[None,None,:])*np.random.random(sample_shapes)[:,:,None]+ind_min[None,None,:])**(1/self.per_index))*self.planets[pl]['per_gaps']['gap_starts'][None,None,:])
21702166

21712167
self.trace.posterior['av_t0_'+pl] = (('chain','draw'),self.trace.posterior['t0_'+pl].values)
@@ -2423,7 +2419,7 @@ def init_gp_to_plot(self, n_samp=150, max_gp_len=12000, interp=True, overwrite=F
24232419
#min_dist_to_lc=np.hstack([np.min(abs(self.lc.time[(self.lc.time>timechunks[tc])&(self.lc.time<=timechunks[tc+1]),None]-self.model_time[None,(self.model_time>timechunks[tc])&(self.model_time<=timechunks[tc+1])]),axis=1) for tc in range(nchunks)])
24242420
min_dist_to_lc=np.hstack([np.min(abs(t[(t>timechunks[tc])&(t<=timechunks[tc+1]),None]-self.model_lcs[unqcad]['time'][None,(self.model_lcs[unqcad]['time']>timechunks[tc])&(self.model_lcs[unqcad]['time']<=timechunks[tc+1])]),axis=1) for tc in range(nchunks)])
24252421
else:
2426-
print([(self.lc.time>timechunks[tc])&(self.lc.time<=timechunks[tc+1]).sum() for tc in range(nchunks)])
2422+
#print([(self.lc.time>timechunks[tc])&(self.lc.time<=timechunks[tc+1]).sum() for tc in range(nchunks)])
24272423
print("No time to intertpolate GP")
24282424
#scaling stdev -> 0.1day duration -> making artificially larger away from parts of lc
24292425
sd=np.nanmedian(abs(np.diff(self.lc.flux[self.cad_indexes[unqcad]])))/np.sqrt(0.1/self.texp_dict[unqcad])*(np.clip(86400/1800*min_dist_to_lc,1.0,25)**0.33)
@@ -4067,18 +4063,18 @@ def plot(self, interactive=False, n_samp=None, overwrite=False, interp=True, new
40674063
else:
40684064
# print("flux",len(self.lc.flux_flat),"mask",len(self.lc.mask),np.sum(self.lc.mask),"phasebool",len(phasebool),np.sum(phasebool),
40694065
# "cad_index",len(self.cad_indexes[unqcad]),np.sum(self.cad_indexes[unqcad]),"transit model",len(np.sum([self.trans_to_plot[unqcad][opl]['med'] for opl in self.planets if opl!=pl],axis=0)))
4070-
print("phase",self.lc.phase[pl][self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4071-
"masked_flux",len(self.lc.flux_flat[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]]),
4072-
"cad_mask",len(self.cad_masks[unqcad]),self.cad_masks[unqcad].sum(),
4073-
"phasebool",phasebool[pl][self.cad_indexes[unqcad]].shape,phasebool[pl][self.cad_indexes[unqcad]].sum(),
4074-
"transmod",self.trans_to_plot[unqcad][pl]['med'][self.cad_masks[unqcad]&phasebool[pl][self.cad_indexes[unqcad]]].shape,
4075-
"othpls",othpls[unqcad],othpls[unqcad].shape)
4076-
for unqcad in self.unique_cads:
4077-
print(self.lc.phase[pl][self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4078-
self.lc.flux_flat[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4079-
self.lc.flux_err[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4080-
othpls[unqcad].shape,
4081-
self.trans_to_plot[unqcad][pl]['med'][self.cad_masks[unqcad]&phasebool[pl][self.cad_indexes[unqcad]]].shape)
4066+
# print("phase",self.lc.phase[pl][self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4067+
# "masked_flux",len(self.lc.flux_flat[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]]),
4068+
# "cad_mask",len(self.cad_masks[unqcad]),self.cad_masks[unqcad].sum(),
4069+
# "phasebool",phasebool[pl][self.cad_indexes[unqcad]].shape,phasebool[pl][self.cad_indexes[unqcad]].sum(),
4070+
# "transmod",self.trans_to_plot[unqcad][pl]['med'][self.cad_masks[unqcad]&phasebool[pl][self.cad_indexes[unqcad]]].shape,
4071+
# "othpls",othpls[unqcad],othpls[unqcad].shape)
4072+
# for unqcad in self.unique_cads:
4073+
# print(self.lc.phase[pl][self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4074+
# self.lc.flux_flat[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4075+
# self.lc.flux_err[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]].shape,
4076+
# othpls[unqcad].shape,
4077+
# self.trans_to_plot[unqcad][pl]['med'][self.cad_masks[unqcad]&phasebool[pl][self.cad_indexes[unqcad]]].shape)
40824078
phaselc[pl]=np.vstack([np.column_stack((self.lc.phase[pl][self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]],
40834079
self.lc.flux_flat[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]] - othpls[unqcad],
40844080
self.lc.flux_err[self.lc.mask&phasebool[pl]&self.cad_indexes[unqcad]],
@@ -4189,7 +4185,6 @@ def plot(self, interactive=False, n_samp=None, overwrite=False, interp=True, new
41894185
(phaselc[pl][:,1]-phaselc[pl][:,3]-phaselc[pl][:,4])[np.argsort(phaselc[pl][:,0])],
41904186
phaselc[pl][np.argsort(phaselc[pl][:,0]),2])),binsize[pl])
41914187
nrtrns_resid=np.nanstd(bin_resids[:,1])
4192-
print(nrtrns_resid)
41934188
f_trans_resids[pl].errorbar(bin_resids[:,0],bin_resids[:,1],yerr=bin_resids[:,2],fmt=".",color='C2',
41944189
alpha=0.75, markersize=5, rasterized=raster)
41954190

@@ -4487,7 +4482,6 @@ def plot_corner(self,corner_vars=None,use_marg=True,truths=None):
44874482
#print(samples.shape,samples.columns)
44884483
#assert samples.shape[1]<50
44894484

4490-
print(corner_vars)
44914485
if use_marg:
44924486
fig = corner.corner(self.trace.posterior,var_names=corner_vars)#,truths=truths)
44934487
else:
@@ -4535,7 +4529,6 @@ def plot_corner(self,corner_vars=None,use_marg=True,truths=None):
45354529
# samples.loc[sampl_loc,'log_prob'] = ext['logprob_marg_'+dpl][:,n_per]
45364530
# n_pos+=1
45374531
# weight_samps = np.exp(samples["log_prob"])
4538-
print(samples)
45394532
fig = corner.corner(samples)#[[col for col in samples.columns if col!='log_prob']],weights=weight_samps);
45404533

45414534
fig.savefig(self.savenames[0]+'_corner.pdf')#,dpi=400,rasterized=True)
@@ -4902,7 +4895,7 @@ def plot_cheops_or(self,ordf,or_niter=3):
49024895
plt.plot(tdur_mult+sd_t0+tdur[1],dip,'--',c='C0',lw=2.5,alpha=0.6)
49034896

49044897
start_times=np.linspace(pred_t0+(row['Ph_early']-1)*p[0], pred_t0+(row['Ph_late']-1)*p[0], or_niter)
4905-
print(p,dep,tdur,t0,pred_t0,sd_t0,tdur,tdur_mult,start_times)
4898+
#print(p,dep,tdur,t0,pred_t0,sd_t0,tdur,tdur_mult,start_times)
49064899
for start in start_times:
49074900
plt.fill_between([start, start+row['T_visit']/86400],[-1.25*dep,-1.25*dep],[0.25*dep,0.25*dep],alpha=0.2,color='C4')
49084901

0 commit comments

Comments
 (0)