def choose_initial_k2sff(filename): """ Select which aperture from the K2SFF file to use by picking the light curve with the highest periodogram peak <35 days. (BESTAPER frequently includes a long-term trend that shouldn't dominate.)""" peak_power = np.zeros(21) peak_periods = np.zeros(21) with fits.open(filename) as hdu: for ext in np.arange(2, 21, 1): table = hdu[ext].data t = table["T"][table["MOVING"] == 0] f = table["FCOR"][table["MOVING"] == 0] ls_out = prot.run_ls(t, f, np.ones_like(f), 0.1, prot_lims=[0.1, 35], run_bootstrap=False) fund_period, fund_power, periods_to_test, periodogram, _, _ = ls_out peak_periods[ext] = periods_to_test[np.argmax(periodogram)] peak_power[ext] = max(periodogram) best_ext = np.arange(2, 21, 1)[np.argmax(peak_power)] return best_ext
def _run_fit(self, use_lc, prot_lims=[0.1,70]): """Run a fit on a single lc, either "raw" or "detrended" or a array/list of [time, flux, and unc] """ if use_lc=="raw": logging.debug("fitting raw lc") tt, ff, uu = self.time, self.flux, self.unc_flux elif (use_lc=="detrended") or (use_lc=="det"): logging.debug("fitting detrended lc") tt, ff, uu = self.time, self.det_flux, self.det_unc else: logging.debug("fitting other lc") tt, ff, uu = use_lc # Test the periodogram and pick the best period and power ls_out = prot.run_ls(tt, ff, uu, threshold=self.power_threshold, prot_lims=prot_lims, run_bootstrap=True) # fund_prot, fund_power, periods_to_test, periodogram = ls_out[:4] return ls_out
def choose_initial_k2sff(filename): """ Select which aperture from the K2SFF file to use by picking the light curve with the highest periodogram peak <35 days. (BESTAPER frequently includes a long-term trend that shouldn't dominate.)""" peak_power = np.zeros(21) peak_periods = np.zeros(21) with fits.open(filename) as hdu: for ext in np.arange(2,21,1): table = hdu[ext].data t = table["T"][table["MOVING"]==0] f = table["FCOR"][table["MOVING"]==0] ls_out = prot.run_ls(t,f,np.ones_like(f),0.1,prot_lims=[0.1,35], run_bootstrap=False) fund_period, fund_power, periods_to_test, periodogram, _, _ = ls_out peak_periods[ext] = periods_to_test[np.argmax(periodogram)] peak_power[ext] = max(periodogram) best_ext = np.arange(2,21,1)[np.argmax(peak_power)] return best_ext
def run_one(t, f, epic=None): """Run a lomb-scargle analysis on one light curve. Inputs: ------- t: array of epochs/time points f: array of fluxes corresponding to time points in t epic: object identifier Returns: -------- fund_period, fund_power: floats period and power corresponding to the highest periodogram peak sig_period, sig_power: floats period and power corresponding to the highest periodogram peak that is a) higher than the bootstrap significance theshold, and b) higher than N(=100) nearby points as selected with argrelextrema sigma: float bootstrap significance threshold """ logging.info(epic) ylims = np.percentile(f, [0.5, 99.5]) fig = plt.figure(figsize=(8, 10)) base_grid = gridspec.GridSpec(2, 1, height_ratios=[2, 3]) if epic is not None: plt.suptitle("EPIC {0}".format(epic), fontsize="x-large") top_grid = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=base_grid[0]) # Just plot the light curve ax = plt.subplot(top_grid[0]) ax.plot(t, f, 'k.') ax.set_ylim(ylims) # Run the lomb-scargle periodogram on the light curve ls_out = prot.run_ls(t, f, np.ones_like(f), 0.1, prot_lims=[0.1, 70], run_bootstrap=True) # unpack lomb-scargle results fund_period, fund_power, periods_to_test, periodogram, aliases, sigmas = ls_out logging.info("Prot={0:.3f} Power={1:.3f}".format(fund_period, fund_power)) # Find all peaks in the periodogram peak_locs = argrelextrema(periodogram, np.greater, order=100) print(len(peak_locs[0]), periods_to_test[np.argmax(peak_locs[0])]) # Only keep significant peaks (use bootstrap significance levels) sig_locs = peak_locs[0][periodogram[peak_locs[0]] > sigmas[0]] sig_periods = periods_to_test[sig_locs] sig_powers = periodogram[sig_locs] # Plot the periodogram ax = plt.subplot(top_grid[1]) ax.plot(periods_to_test, periodogram, 'k-') ax.axvline(fund_period, color="r", linestyle=":", linewidth=2) ax.axhline(sigmas[0], color="grey", linestyle="-.", linewidth=2) ax.set_xscale("log") ax.set_xlim(0.1, 70) # Mark significant peaks (if any) on the periodogram num_sig = len(sig_locs) if num_sig > 0: plt.plot(sig_periods, sig_powers * 1.1, 'kv') # What's the most powerful of the significant peaks? most_significant = np.argmax(sig_powers) most_sig_period = sig_periods[most_significant] most_sig_power = sig_powers[most_significant] if num_sig > 1: trim_periods = np.delete(sig_periods, most_significant) trim_powers = np.delete(sig_powers, most_significant) second_significant = np.argmax(trim_powers) sec_period = trim_periods[second_significant] sec_power = trim_powers[second_significant] else: sec_period, sec_power = -9999, -9999 else: most_sig_period, most_sig_power = -9999, -9999 sec_period, sec_power = -9999, -9999 # Count the number of significant periods besides the first # A likely harmonic doesn't count against this extra_sig = 0 # Record type of potential harmonic, if applicable harm_type = "-" if num_sig > 1: # Compare the two most significant periods period_ratio = sec_period / most_sig_period power_ratio = sec_power / most_sig_power # Is the secondary peak a 1/2 or 2x harmonic? if abs(period_ratio - 0.5) <= 0.05: harm_type = "half" extra_sig = num_sig - 2 elif abs(period_ratio - 2.0) <= 0.05: harm_type = "dbl" extra_sig = num_sig - 2 else: extra_sig = num_sig - 1 if (harm_type != "-") and (power_ratio > 0.5): harm_type = harm_type + "-maybe" # plot phase-folded periods num_cols = np.int(np.ceil((len(sig_periods) + 1) / 2)) bottom_grid = gridspec.GridSpecFromSubplotSpec(2, num_cols, subplot_spec=base_grid[1]) # Plot the phase-folded light curve corresponding to the max # peak in the periodogram ax = plt.subplot(bottom_grid[0, 0]) phased_t = t % fund_period / fund_period ax.plot(phased_t, f, 'r.') ax.set_ylim(ylims) ax.set_xlim(0, 1) ax.set_title(r"P$_0$={0:.2f}".format(fund_period)) # Now plot the phase-folded light curves for all other significant peaks row = 0 for i, per in enumerate(sig_periods[np.argsort(sig_powers)]): if (i + 1) == num_cols: row = 1 ax = plt.subplot(bottom_grid[row, i + 1 - num_cols]) phased_t = t % per / per ax.plot(phased_t, f, 'k.') ax.set_ylim(ylims) ax.set_xlim(0, 1) ax.set_title("P={0:.2f}".format(per)) ax.tick_params(labelleft=False) plt.subplots_adjust(hspace=0.25) return (fund_period, fund_power, most_sig_period, most_sig_power, sec_period, sec_power, sigmas[0], extra_sig, harm_type)
def run_one(t,f,epic=None,secondary_file=None): """Run a lomb-scargle analysis on one light curve. Inputs: ------- t: array of epochs/time points f: array of fluxes corresponding to time points in t epic: object identifier Returns: -------- fund_period, fund_power: floats period and power corresponding to the highest periodogram peak sig_period, sig_power: floats period and power corresponding to the highest periodogram peak that is a) higher than the bootstrap significance theshold, and b) higher than N(=100) nearby points as selected with argrelextrema sigma: float bootstrap significance threshold """ logging.info(epic) ylims = np.percentile(f,[0.5,99.5]) fig = plt.figure(figsize=(8,10)) base_grid = gridspec.GridSpec(2,1,height_ratios=[2,3]) if epic is not None: plt.suptitle("EPIC {0}".format(epic),fontsize="x-large") top_grid = gridspec.GridSpecFromSubplotSpec(2,1,subplot_spec=base_grid[0]) # Just plot the light curve ax = plt.subplot(top_grid[0]) ax.plot(t,f,'k.') ax.set_ylim(ylims) # Run the lomb-scargle periodogram on the light curve ls_out = prot.run_ls(t,f,np.ones_like(f),0.1,prot_lims=[0.1,70], run_bootstrap=True) # unpack lomb-scargle results fund_period, fund_power, periods_to_test, periodogram, aliases, sigmas = ls_out logging.info("Prot={0:.3f} Power={1:.3f}".format(fund_period,fund_power)) # Find all peaks in the periodogram peak_locs = argrelextrema(periodogram,np.greater,order=100) print(len(peak_locs[0]),periods_to_test[np.argmax(peak_locs[0])]) # Only keep significant peaks (use bootstrap significance levels) sig_locs = peak_locs[0][periodogram[peak_locs[0]]>sigmas[0]] sig_periods = periods_to_test[sig_locs] sig_powers = periodogram[sig_locs] # Plot the periodogram ax = plt.subplot(top_grid[1]) ax.plot(periods_to_test,periodogram,'k-') ax.axvline(fund_period,color="r",linestyle=":",linewidth=2) ax.axhline(sigmas[0],color="grey",linestyle="-.",linewidth=2) ax.set_xscale("log") ax.set_xlim(0.1,70) # Mark significant peaks (if any) on the periodogram num_sig = len(sig_locs) if num_sig>0: plt.plot(sig_periods,sig_powers*1.1,'kv') # What's the most powerful of the significant peaks? most_significant = np.argmax(sig_powers) most_sig_period = sig_periods[most_significant] most_sig_power = sig_powers[most_significant] # If there are two or more, also record the second one if num_sig>1: trim_periods = np.delete(sig_periods, most_significant) trim_powers = np.delete(sig_powers, most_significant) second_significant = np.argmax(trim_powers) sec_period = trim_periods[second_significant] sec_power = trim_powers[second_significant] else: sec_period, sec_power = -9999, -9999 # Record all secondary peaks, just in case if secondary_file is not None: for i in range(num_sig): secondary_file.write("{0},{1:.4f},{2:.4f},{3:.4f}\n".format(epic, sig_periods[i],sig_powers[i],sigmas[0])) else: most_sig_period, most_sig_power = -9999,-9999 sec_period, sec_power = -9999, -9999 # Count the number of significant periods besides the first # A likely harmonic doesn't count against this extra_sig = 0 # Record type of potential harmonic, if applicable harm_type = "-" if num_sig>1: # Compare the two most significant periods period_ratio = sec_period / most_sig_period power_ratio = sec_power / most_sig_power # Is the secondary peak a 1/2 or 2x harmonic? if abs(period_ratio-0.5)<=0.05: harm_type = "half" extra_sig = num_sig - 2 elif abs(period_ratio-2.0)<=0.05: harm_type = "dbl" extra_sig = num_sig - 2 else: extra_sig = num_sig-1 if (harm_type!="-") and (power_ratio>0.5): harm_type = harm_type+"-maybe" # plot phase-folded periods num_cols = np.int(np.ceil((len(sig_periods)+1) / 2)) bottom_grid = gridspec.GridSpecFromSubplotSpec(2,num_cols, subplot_spec=base_grid[1]) # Plot the phase-folded light curve corresponding to the max # peak in the periodogram ax = plt.subplot(bottom_grid[0,0]) phased_t = t % fund_period / fund_period ax.plot(phased_t,f,'r.') ax.set_ylim(ylims) ax.set_xlim(0,1) ax.set_title(r"P$_0$={0:.2f}".format(fund_period)) # Now plot the phase-folded light curves for all other significant peaks row = 0 for i,per in enumerate(sig_periods[np.argsort(sig_powers)]): if (i+1)==num_cols: row = 1 ax = plt.subplot(bottom_grid[row,i+1-num_cols]) phased_t = t % per / per ax.plot(phased_t,f,'k.') ax.set_ylim(ylims) ax.set_xlim(0,1) ax.set_title("P={0:.2f}".format(per)) ax.tick_params(labelleft=False) plt.subplots_adjust(hspace=0.25) return (fund_period, fund_power, most_sig_period, most_sig_power, sec_period, sec_power, sigmas[0], extra_sig, harm_type)
pg_savepath = join(pgs_savepath, f'{ticid}PG-{ap_type}-M{gmag}.pdf') pg_fits_savepath = f'{pgs_fits_savepath}/{ticid}PG-{ap_type}-M{gmag}.fits' # Get J-K temp proxy j_mag = targets['J'][targets[tic_col] == ticid][0] k_mag = targets['K'][targets[tic_col] == ticid][0] j_k = j_mag - k_mag # Update progress bar, import Light Curve, and calculate periodogram pbar.set_description(f'{ticid}-{ap_type}') lc = open_lc(lc_path).get_lightcurve('FLUX').remove_outliers(sigma=outlier_sigma) # Run LombScarlge Periodogram pbar.set_postfix(Status='Running LS') fund_period, fund_power, periods_to_test, periodogram, aliases, sigmas = prot.run_ls(lc.time, lc.flux, lc.flux_err, threshold, prot_lims=prot_lims, run_bootstrap=True) pbar.set_postfix(Status='Saving PG FITS') # Meta info to add to the FITS file to easily access it meta_info = {'TICID':ticid, 'aperture':ap_type, 'gmag':gmag, 'jkmag':j_k, 'sig99p':sigmas[0]} with catch_warnings(): simplefilter('ignore', VerifyWarning) pg_table = QTable(data=(periods_to_test * u.day, periodogram), names=('period', 'power'), meta=meta_info) pg_table.write(pg_fits_savepath, overwrite=True) pbar.set_postfix(Status=f'Found period={fund_period:.2f} days') # Find local maxima in data; most credible periods peaks = argrelmax(periodogram)[0]
def plot_lcs(axes, EPIC): lc_file = "/home/stephanie/data/c5_k2sc/hlsp_k2sc_k2_llc_{0}-c05_kepler_v1_lc.fits".format( EPIC) t, f, w = k2sc_io(lc_file) i = np.where(res["EPIC"] == EPIC)[0] if len(i > 0): i = i[0] ylims = np.percentile(f[np.isfinite(f)], [0.05, 99.05]) color1 = cmap(0.35) #plt.cm.inferno(0.5) color2 = cmap(0.85) #plt.cm.inferno(0.75) color3 = cmap(0.6) label_fontsize = "small" # Periodogram ls_out = prot.run_ls(t, f, np.ones_like(f), 0.1, prot_lims=[0.1, 70], run_bootstrap=False) periods, pgram = ls_out[2], ls_out[3] if res[i]["num_sig"] >= 2: peak_locs = np.where(peaks["EPIC"] == EPIC)[0] if len(peak_locs) > 2: sort_locs = peak_locs[np.argsort(peaks["power"][peak_locs])] # print(peaks["period"][sort_locs], peaks["power"][sort_locs]) third_period = peaks["period"][sort_locs[-3]] third_power = peaks["power"][sort_locs[-3]] else: third_period, third_power = None, None axes[0].plot(periods, pgram, 'k-') axes[0].set_xlim(0.1, 70) axes[0].set_xscale("log") axes[0].plot(res[i]["sig_period"], res[i]["sig_power"] * 1.1, 'v', mfc=color1, mec="none", ms=11) axes[0].plot(res[i]["sec_period"], res[i]["sec_power"] * 1.1, 'v', mfc=color2, mec="none", ms=11) if third_period is not None: axes[0].plot(third_period, third_power * 1.1, 'v', mfc=color3, mec="none", ms=11) if res[i]["sig_power"] > 0: ymax = res[i]["sig_power"] * 1.15 else: ymax = max(pgram) axes[0].set_ylim((0, ymax)) axes[0].axhline(res[i]["threshold"], color='grey', ls="-.", label="threshold {0:2f}".format(float( res[i]["threshold"]))) #axes[0].set_xlabel("Period (d)",y=0.95) #axes[0].tick_params(labelbottom=False,labeltop=True) axes[0].set_xticklabels(["", "0.1", "1", "10"]) axes[0].set_ylabel("Power", fontsize=label_fontsize) axes[0].set_xlabel("Period (d)", fontsize=label_fontsize) # Full light curve axes[1].plot(t, f, 'k.') axes[1].set_ylim(ylims) axes[1].set_xlim(t[0], t[-1]) axes[1].set_ylabel("Light curve", fontsize=label_fontsize) axes[1].set_yticklabels([]) # axes[1].tick_params(labelbottom=False) axes[1].set_xlabel("Time (d)", fontsize=label_fontsize) # White noise axes[2].plot(t, w, 'k.') axes[2].set_ylim(np.percentile(w, [0.5, 99.5])) axes[2].set_xlim(t[0], t[-1]) axes[2].set_ylabel("White noise", fontsize=label_fontsize) axes[2].set_yticklabels([]) # axes[2].tick_params(labelbottom=False) axes[2].set_xlabel("Time (d)", fontsize=label_fontsize) # Time-dependent trend axes[3].plot(t, f - w, 'k.') axes[3].set_ylabel("Time-dep", fontsize=label_fontsize) axes[3].set_xlim(t[0], t[-1]) axes[3].set_yticklabels([]) # axes[3].tick_params(labelbottom=False) axes[3].set_xlabel("Time (d)", fontsize=label_fontsize) # Phase folded 1 if res[i]["sig_period"] > 0: plot_phased(axes[4], t, f, res[i]["sig_period"], res[i]["sig_power"], color=color1) axes[4].set_ylim(ylims) axes[4].set_ylabel("P1", fontsize=label_fontsize) if res[i]["sig_period"] >= 2: repeats = np.arange(t[0], t[-1], res[i]["sig_period"]) for r in repeats: axes[1].axvline(r, ls="--", color=color1, lw=2) # axes[4].tick_params(labelbottom=False) axes[4].set_xlabel("Phase", fontsize=label_fontsize) else: axes[4].set_axis_off() # Phase folded 2 if res[i]["sec_period"] > 0: plot_phased(axes[5], t, f, res[i]["sec_period"], res[i]["sec_power"], color=color2) axes[5].set_ylim(ylims) axes[5].set_ylabel("P2", fontsize=label_fontsize) # print(res[i]["sec_period"]) if res[i]["sec_period"] >= 2: repeats = np.arange(t[0], t[-1], res[i]["sec_period"]) # print(repeats) for r in repeats: # print(r) axes[1].axvline(r, ls=":", color=color2, lw=2) axes[5].set_xlabel("Phase", fontsize=label_fontsize) else: # axes[5].set_yticks([]) # axes[5].set_yticklabels([]) axes[5].set_axis_off() if third_period is not None: plot_phased(axes[6], t, f, third_period, third_power, color=color3) axes[6].set_ylim(ylims) axes[6].set_ylabel("P3", fontsize=label_fontsize) axes[6].set_xlim(0, 1) axes[6].set_xlabel("Phase", fontsize=label_fontsize) # # if third_period]>=2: # repeats = np.arange(t[0],t[-1],third_period) # # print(repeats) # for r in repeats: # # print(r) # axes[1].axvline(r,ls="-.",color=color3,lw=2) else: # axes[6].set_yticks([]) # axes[6].set_yticklabels([]) axes[6].set_axis_off() plt.subplots_adjust(hspace=0.6)