<jupyter_start><jupyter_text>How to find eclipsing binaries and transits and (optionally) mask them. Warning: this code is not yet stable.You may want to mask transits if you're trying to measure a star's rotation period. The transits can interfere with the rotation period measurement. Not shown in this tutorial is a smoothing step. To find transits using BLS, it's best if the stellar variability is removed first. People often use a median filter for this.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

import starspot as ss<jupyter_output><empty_output><jupyter_text>Simulate some Basic data.<jupyter_code>N = 1000
x = np.linspace(0, 100, N)
err = 1e-3
y = np.random.randn(N)*err + 1
yerr = np.ones_like(y)*err

t0, dur_hours, porb = 12, 24, 20
dur_days = dur_hours/24.

# Create 'eclipses' or 'transits'
mask = ((x - (t0 - .5*dur_days)) % porb) < dur_days
y[mask] -= 1e-2

plt.plot(x, y, ".");
plt.xlabel("Time [days]")
plt.ylabel("Normalized Flux");<jupyter_output><empty_output><jupyter_text>Plot the folded light curve.<jupyter_code>true_phase = ss.calc_phase(porb, x-t0)
plt.plot(true_phase, y, "k.")
plt.plot(true_phase+1, y, "k.")
plt.xlim(.5, 1.5);
plt.xlabel("Phase")
plt.ylabel("Normalized Flux");<jupyter_output><empty_output><jupyter_text>Now find the period and epoch of the transits. This is just a wrapper to the astropy.timeseries BLS algorithm.<jupyter_code>period_grid = np.linspace(2, 20, 100)  # The array of periods to search over for BLS.
duration_grid = np.linspace(.5, 1.5, 10)  # The array of durations (in days) to search over for BLS.
                                          # (The longest duration must be shorter than the shortest period)
transit_masks, t0s, durs, porbs = ss.find_and_mask_transits(x, y, yerr,  # The light curve
                                                            period_grid, duration_grid,  # The period & duration grids for BLS
                                                            nplanets=1,  # The number of companions to look for.
                                                            plot=True)  # Option to plot the BLS periodogram.<jupyter_output><empty_output><jupyter_text>Compare the results to the true values.<jupyter_code>print(t0, dur_days, porb)
print(t0s[0], durs[0], porbs[0])<jupyter_output>12 1.0 20
12.075000000000001 1.05 19.9667221297837<jupyter_text>Plot the light curve, folded on the detected period, over the truth.<jupyter_code>phase = ss.calc_phase(porbs[0], x-t0s[0])
plt.plot(true_phase, y, "C0.", zorder=0)
plt.plot(true_phase+1, y, "C0.", zorder=0)
plt.plot(phase, y, "k.")
plt.plot(phase+1, y, "k.")
plt.xlim(.5, 1.5);
plt.xlabel("Phase")
plt.ylabel("Normalized Flux");<jupyter_output><empty_output><jupyter_text>Mask the transits and plot the resulting light curve.<jupyter_code>plt.plot(x, y, "C1.")
plt.plot(x[~transit_masks[0]], y[~transit_masks[0]], "C0.")<jupyter_output><empty_output><jupyter_text>Or<jupyter_code>mask = ss.transit_mask(x, t0s[0], durs[0]+.1, porbs[0])
plt.plot(x, y, "C1.")
plt.plot(x[mask], y[mask], "C0.")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1270096463022508, 'fraction_numerical': 0.03569131832797428, 'mean_word_length': 3.8232558139534882, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '165887', 'license': 'MIT', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1102, 'n_words': 302, 'path': 'docs/source/_static/notebooks/Eclipses_and_transits.ipynb', 'repo_name': 'RuthAngus/starspot', 'size': '3110'}