def _bulk_detrend(self, detrend_kwargs, to_plot=False): """Smooth the rapid variations in the lightcurve and remove bulk trends. inputs ------ alpha: float "bass enhancement" for supersmoother. """ if detrend_kwargs is None: detrend_kwargs = dict() detrend_kwargs["kind"] = detrend_kwargs.get("kind", "supersmoother") detrend_kwargs["phaser"] = detrend_kwargs.get("phaser", None) logging.debug("Removing bulk trend...") det_out = detrend.simple_detrend(self.time, self.flux, self.unc_flux, to_plot=to_plot, **detrend_kwargs) self.det_flux, self.det_unc, self.bulk_trend = det_out logging.debug("len detrended t %d f %d u %d", len(self.time), len(self.det_flux),len(self.det_unc)) if to_plot==True: fig = plt.gcf() fig.suptitle("{}; {} ({})".format(self.name,detrend_kwargs["kind"], detrend_kwargs["phaser"]),fontsize="x-large") plt.savefig("{0}plot_outputs/{1}_detrend.png".format(base_path, self.name))
def smooth_and_clip(time, flux, unc_flux, clip_at=3, to_plot=False): """Smooth the lightcurve, then clip based on residuals.""" if to_plot: plt.figure(figsize=(8,4)) ax = plt.subplot(111) ax.plot(time,flux,'k.',label="orig") # Simple sigma clipping first to get rid of really big outliers ct, cf, cu, to_keep = sigma_clip(time, flux, unc_flux, clip_at=clip_at) logging.debug("c len t %d f %d u %d tk %d", len(ct), len(cf), len(cu), len(to_keep)) if to_plot: ax.plot(ct, cf, '.',label="-1") # Smooth with supersmoother without much bass enhancement for i in range(3): det_out = detrend.simple_detrend(ct, cf, cu, phaser=0) detrended_flux, detrended_unc, bulk_trend = det_out # Take the difference, and find the standard deviation of the residuals # logging.debug("flux, bulk trend, diff") # logging.debug(cf[:5]) # logging.debug(bulk_trend[:5]) f_diff = cf - bulk_trend # logging.debug(f_diff[:5]) diff_std = np.zeros(len(f_diff)) diff_std[ct<=2102] = np.std(f_diff[ct<=2102]) diff_std[ct>2102] = np.std(f_diff[ct>2102]) # logging.debug("std %f %f",diff_std[0], diff_std[-1]) if to_plot: ax.plot(ct, bulk_trend) logging.debug("%d len tk %d diff %d", i, len(to_keep), len(f_diff)) # Clip outliers based on residuals this time to_keep = to_keep[abs(f_diff)<=(diff_std*clip_at)] ct = time[to_keep] cf = flux[to_keep] cu = unc_flux[to_keep] if to_plot: ax.plot(ct, cf, '.',label=str(i)) if to_plot: ax.legend() clip_time = time[to_keep] clip_flux = flux[to_keep] clip_unc_flux = unc_flux[to_keep] return clip_time, clip_flux, clip_unc_flux, to_keep
def smooth_and_clip(time, flux, unc_flux, clip_at=3, to_plot=False): """Smooth the lightcurve, then clip based on residuals.""" if to_plot: plt.figure(figsize=(8, 4)) ax = plt.subplot(111) ax.plot(time, flux, 'k.', label="orig") # Simple sigma clipping first to get rid of really big outliers ct, cf, cu, to_keep = sigma_clip(time, flux, unc_flux, clip_at=clip_at) logging.debug("c len t %d f %d u %d tk %d", len(ct), len(cf), len(cu), len(to_keep)) if to_plot: ax.plot(ct, cf, '.', label="-1") # Smooth with supersmoother without much bass enhancement for i in range(3): det_out = detrend.simple_detrend(ct, cf, cu, phaser=0) detrended_flux, detrended_unc, bulk_trend = det_out # Take the difference, and find the standard deviation of the residuals # logging.debug("flux, bulk trend, diff") # logging.debug(cf[:5]) # logging.debug(bulk_trend[:5]) f_diff = cf - bulk_trend # logging.debug(f_diff[:5]) diff_std = np.zeros(len(f_diff)) diff_std[ct <= 2102] = np.std(f_diff[ct <= 2102]) diff_std[ct > 2102] = np.std(f_diff[ct > 2102]) # logging.debug("std %f %f",diff_std[0], diff_std[-1]) if to_plot: ax.plot(ct, bulk_trend) logging.debug("%d len tk %d diff %d", i, len(to_keep), len(f_diff)) # Clip outliers based on residuals this time to_keep = to_keep[abs(f_diff) <= (diff_std * clip_at)] ct = time[to_keep] cf = flux[to_keep] cu = unc_flux[to_keep] if to_plot: ax.plot(ct, cf, '.', label=str(i)) if to_plot: ax.legend() clip_time = time[to_keep] clip_flux = flux[to_keep] clip_unc_flux = unc_flux[to_keep] return clip_time, clip_flux, clip_unc_flux, to_keep