Skip to content

Commit

Permalink
bug fixing binning function
Browse files Browse the repository at this point in the history
  • Loading branch information
hposborn committed Nov 21, 2024
1 parent 6bd7a03 commit 5455d45
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 66 deletions.
6 changes: 3 additions & 3 deletions MonoTools/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4065,10 +4065,10 @@ def plot(self, interactive=False, n_samp=None, overwrite=False, interp=True, new
bin_flux=self.lc.bin_flux_flat[self.lc_regions[key]['bin_ix']]
elif plot_flat and self.use_GP:
flux=self.lc.flux[self.lc_regions[key]['ix']]-self.gp_to_plot['gp_pred'][self.lc_regions[key]['ix']]
bin_flux=tools.bin_lc_given_new_x(np.column_stack((self.lc.time[self.lc_regions[key]['ix']],
bin_flux=tools.old_bin_lc_given_new_x(np.column_stack((self.lc.time[self.lc_regions[key]['ix']],
(self.lc.flux-self.gp_to_plot['gp_pred'])[self.lc_regions[key]['ix']],
self.lc.flux_err[self.lc_regions[key]['ix']])),
self.lc.bin_time[self.lc_regions[key]['bin_ix']])[:,1]
self.lc.flux_err[self.lc_regions[key]['ix']])),
self.lc.bin_time[self.lc_regions[key]['bin_ix']])[:,1]
else:
flux=self.lc.flux[self.lc_regions[key]['ix']]
bin_flux=self.lc.bin_flux[self.lc_regions[key]['bin_ix']]
Expand Down
26 changes: 18 additions & 8 deletions MonoTools/lightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,16 @@ def flatten(self, timeseries=['flux'], knot_dist=1.25, maxiter=10, sigmaclip = 3
self.timeseries+=[its+'_flat']

elif flattype=='polystep':
splines={}
flats={}
for its in timeseries:
timearr=self.bin_time[:] if 'bin_' in its else self.time[:]

if its+'_flat' not in self.timeseries:
setattr(self,its+'_flat',np.zeros(len(getattr(self,its))))

splines[its]=np.zeros(len(getattr(self,its)))
flats[its]=np.zeros(len(getattr(self,its)))
#if its+'_flat' not in self.timeseries:
# setattr(self,its+'_spline',np.zeros(len(getattr(self,its))))
# setattr(self,its+'_flat',np.zeros(len(getattr(self,its))))

if hasattr(self,its+'err'):
uselc=np.column_stack((timearr,getattr(self,its)[:],getattr(self,its+'_err')[:]))
else:
Expand Down Expand Up @@ -469,15 +473,19 @@ def flatten(self, timeseries=['flux'], knot_dist=1.25, maxiter=10, sigmaclip = 3
#now for each step centre we perform the flattening:
#actual flattening
for stepcent in stepcentres:
win,box = tools.formwindow(uselc,stepcent, knot_dist, stepsize,0.8*knot_dist) #should return window around box not including box
win,box = tools.form_window(uselc,stepcent, knot_dist, stepsize,0.8*knot_dist) #should return window around box not including box
newbox=box[uselc[:,4].astype(bool)] # Excluding from our box any points which are actually part of the "reflection"
#Checking that we have points in the box where the window is not entirely junk/masked
if np.sum(newbox)>0 and np.sum(win&uselc[:,3].astype(bool))>0:
#Forming the polynomial fit from the window around the box:
baseline = tools.dopolyfit(uselc[win,:3],mask=uselc[win,3].astype(bool),
baseline = tools.do_polyfit(uselc[win,:3],mask=uselc[win,3].astype(bool),
stepcent=stepcent,d=polydegree,ni=maxiter,sigclip=sigmaclip)
getattr(self, its+'_flat')[newbox] = getattr(self,its)[newbox] - np.polyval(baseline,timearr[newbox]-stepcent)
self.timeseries+=[its+'_flat']
splines[its][newbox] = np.polyval(baseline,timearr[newbox]-stepcent)
flats[its][newbox] = getattr(self,its)[newbox] - splines[its][newbox]
print(stepcent,baseline,np.ptp(splines[its][newbox]))
setattr(self,its+'_spline',splines[its])
setattr(self,its+'_flat',flats[its])
self.timeseries+=[its+'_flat',its+'_spline']

def OOTbin(self,near_transit_mask,use_flat=False,binsize=1/48):
"""Out-Of-Transit binning of the lightcurve (to speed up computation)
Expand Down Expand Up @@ -534,6 +542,7 @@ def bin(self,timeseries=['flux'],binsize=1/48,split_gap_size=0.8,use_masked=True
self.sort_timeseries()

if np.any(['_flat' in its and its not in self.timeseries for its in timeseries]):
print("Flattening "+its+" to bin")
self.flatten(timeseries=list(np.unique([t.replace('_flat','') for t in timeseries])))

#setattr(self, 'bin_cadence',binlc['flux'][:,0])
Expand Down Expand Up @@ -648,6 +657,7 @@ def plot(self, plot_rows=1, timeseries=['flux'], jump_thresh=10, ylim=None, xlim
import seaborn as sns
sns.set_palette('viridis')
if 'flux_flat' in timeseries and not hasattr(self,'flux_flat'):
print("Flattening", hasattr(self,'flux_flat'))
self.flatten()

if plot_ephem is not None:
Expand Down
Loading

0 comments on commit 5455d45

Please sign in to comment.