<jupyter_start><jupyter_text>Example: Transit maskIf transits have already been discovered, it is best practice to mask them while detrending. This way, the in-transit data points can not influence the detrending. The current version only supports the ``cosine`` and ``lowess`` methods. Additional methods are planned for future releases. We begin our example by downloading a TESS dataset:<jupyter_code>import numpy as np
from astropy.io import fits

def load_file(filename):
    """Loads a TESS *spoc* FITS file and returns TIME, PDCSAP_FLUX"""
    hdu = fits.open(filename)
    time = hdu[1].data['TIME']
    flux = hdu[1].data['PDCSAP_FLUX']
    flux[flux == 0] = np.nan
    return time, flux

path = 'https://archive.stsci.edu/hlsps/tess-data-alerts/'
filename = 'hlsp_tess-data-alerts_tess_phot_00207081058-s01_tess_v1_lc.fits'
time, flux = load_file(path + filename)<jupyter_output><empty_output><jupyter_text>We detrend these data blindly (without masking) to prepare for a transit search:<jupyter_code>from wotan import flatten
flatten_lc1, trend_lc1 = flatten(
    time,
    flux,
    method='cosine',
    window_length=0.4,
    return_trend=True,
    robust=True
    )<jupyter_output>Iteration: 1 Rejected outliers (total): 176
Iteration: 2 Rejected outliers (total): 148
Converged.
Iteration: 1 Rejected outliers (total): 179
Iteration: 2 Rejected outliers (total): 156
Iteration: 3 Rejected outliers (total): 154
Converged.<jupyter_text>We plot the result:<jupyter_code>import matplotlib.pyplot as plt
from matplotlib import rcParams; rcParams["figure.dpi"] = 150
plt.scatter(time, flux, s=1, color='black')
plt.plot(time, trend_lc1, color='blue', linewidth=2)
plt.xlabel('Time (days)')
plt.ylabel('Raw flux')
plt.show();
plt.close()
plt.scatter(time, flatten_lc1, s=1, color='black')
plt.xlabel('Time (days)')
plt.ylabel('Detrended flux');<jupyter_output><empty_output><jupyter_text>There appear to be 2 transits in the data. We use the ``transitleastsquares`` package to get the ephemeris:<jupyter_code>from transitleastsquares import transitleastsquares
model = transitleastsquares(time, flatten_lc1)
results = model.power(n_transits_min=1)
print('Period (days)', format(results.period, '.5f'))
print('Duration (days)', format(results.duration, '.5f'))
print('T0 (days)', results.T0)<jupyter_output>0%|          | 0/2838 periods | 00:00<?<jupyter_text>These parameters look sensible. We can visually verify that the in-transit points have been determined correctly. For this, we use the feature ``transit_mask`` in ``wotan``:<jupyter_code>from wotan import transit_mask
mask = transit_mask(
    time=time,
    period=results.period,
    duration=results.duration,
    T0=results.T0)

plt.scatter(time[~mask], flux[~mask], s=1, color='black')
plt.scatter(time[mask], flux[mask], s=1, color='orange')
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>That looks good. We can now use this mask to refine our detrending with the knowledge of the in-transit points. More precisely, we instruct the detrender to ignore these points, by setting their weight to zero.<jupyter_code>flatten_lc2, trend_lc2 = flatten(
    time,
    flux,
    method='cosine',
    window_length=0.4,
    return_trend=True,
    robust=True,
    mask=mask
    )<jupyter_output>Iteration: 1 Rejected outliers (total): 150
Converged.
Iteration: 1 Rejected outliers (total): 153
Converged.<jupyter_text>Let's plot both trends to compare the difference of the blind detrending (blue) and the masked one (red):<jupyter_code>plt.scatter(time, flux, s=1, color='black')
plt.plot(time, trend_lc1, color='blue', linewidth=2)
plt.plot(time, trend_lc2, color='red', linewidth=2, linestyle='dashed')
plt.xlabel('Time (days)')
plt.ylabel('Raw flux')
plt.show()
plt.close();<jupyter_output><empty_output><jupyter_text>When zooming into the transit, we can see the effect more clearly:<jupyter_code>plt.scatter(time, flux, s=1, color='black')
plt.plot(time, trend_lc1, color='blue', linewidth=2)
plt.plot(time, trend_lc2, color='red', linewidth=2, linestyle='dashed')
plt.xlabel('Time (days)')
plt.ylabel('Raw flux')
plt.xlim(results.T0 - 1, results.T0 + 1)
plt.show()
plt.close();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12651894210150108, 'fraction_numerical': 0.0228734810578985, 'mean_word_length': 5.973421926910299, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '512756', 'license': 'MIT', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1441, 'n_words': 391, 'path': 'examples/transit mask.ipynb', 'repo_name': 'danielrios12/Wotan---filtro-de-dados', 'size': '4197'}