def sigma_clip(time, flux, unc_flux, clip_at=6): """Perform sigma-clipping on the lightcurve. Inputs ------ time, flux, unc_flux: array_like clip_at: float (optional) how many sigma to clip at. Defaults to 6. Outputs ------- clipped_time, clipped_flux, clipped_unc: arrays to_keep: boolean mask of locations that were kept """ # Compute statistics on the lightcurve med, stdev = utils.stats(flux, unc_flux) # Sigma-clip the lightcurve outliers = abs(flux-med)>(stdev*clip_at) to_clip = np.where(outliers==True)[0] to_keep = np.where(outliers==False)[0] logging.debug("Sigma-clipping") logging.debug(to_clip) clipped_time = np.delete(time, to_clip) clipped_flux = np.delete(flux, to_clip) clipped_unc = np.delete(unc_flux, to_clip) # Return clipped lightcurve return clipped_time, clipped_flux, clipped_unc, to_keep
def prep_lc(time, flux, unc_flux, clip_at=3): """Trim, sigma-clip, and calculate stats on a lc. Inputs ------ time, flux, unc_flux: array_like clip_at: float (optional) How many sigma to clip at. Defaults to 6. Set to None for no sigma clipping Outputs ------- clean_time, clean_flux, clean_unc: arrays """ # Trim the lightcurve, remove bad values t_time, t_flux, t_unc, t_kept = trim(time, flux, unc_flux) # Run sigma-clipping if desired, repeat 2X if clip_at is not None: c_time, c_flux, c_unc, c_kept = smooth_and_clip(t_time, t_flux, t_unc, clip_at=clip_at) else: c_time, c_flux, c_unc, c_kept = t_time, t_flux, t_unc, t_kept all_kept = t_kept[c_kept] # Calculate statistics on lightcurve c_med, c_stdev = utils.stats(c_flux, c_unc) # Return cleaned lightcurve and statistics return c_time, c_flux, c_unc, c_med, c_stdev, all_kept
def fit_sine(time, flux, unc, period): """Fit a simple sine model fixed to the best-fit period.""" def _sine_model(t, amp, yoffset, tshift): return amp * np.sin(2 * np.pi * t / period + tshift) + yoffset # Phase by the period, then extend the arrays # To fit two cycles instead of one phased_time = utils.phase(time, period) fit_time = np.append(phased_time, phased_time + period) fit_flux = np.append(flux, flux) fit_unc = np.append(unc, unc) # initial amplitude and yoffset are stdev and median, respectively p0 = np.append(utils.stats(flux, unc), 0.0) popt, pcov = opt.curve_fit(_sine_model, fit_time, fit_flux, sigma=fit_unc, p0=p0) perr = np.sqrt(np.diag(pcov)) logging.debug("amplitude, yoffset, tshift") logging.debug(popt) logging.debug(perr) return phased_time, _sine_model(phased_time, *popt)
def sigma_clip(time, flux, unc_flux, clip_at=6): """Perform sigma-clipping on the lightcurve. Inputs ------ time, flux, unc_flux: array_like clip_at: float (optional) how many sigma to clip at. Defaults to 6. Outputs ------- clipped_time, clipped_flux, clipped_unc: arrays to_keep: boolean mask of locations that were kept """ # Compute statistics on the lightcurve med, stdev = utils.stats(flux, unc_flux) # Sigma-clip the lightcurve outliers = abs(flux - med) > (stdev * clip_at) to_clip = np.where(outliers == True)[0] to_keep = np.where(outliers == False)[0] logging.debug("Sigma-clipping") logging.debug(to_clip) clipped_time = np.delete(time, to_clip) clipped_flux = np.delete(flux, to_clip) clipped_unc = np.delete(unc_flux, to_clip) # Return clipped lightcurve return clipped_time, clipped_flux, clipped_unc, to_keep