Skip to content

src/core/lcprep.js ​

Preparing long space light curves (TESS, Kepler, K2) for transit fitting: quality masks, per-sector stitching, transit-aware detrending, outlier clipping, per-transit cut-outs, the injection test that picks the detrending window, the alias scan of a planet seen in a few transits, and lightkurve's CDPP noise metric.

Ported from the owner's TOI scripts (EXOTIC examples/toi-3planet/toi1873_extract.py: transit_mask, duration_t14, clip_outliers, optimize_window, bin_series, the post-gap trim, stage_alias_scan), from wotan's time-windowed biweight (Hippke et al. 2019, flatten(method='biweight')) and from lightkurve (quality bitmasks, flatten, estimate_cdpp). Checked against those codes in test/lcprep.test.mjs (reference: test/make_lcprep_reference.py). No DOM: runs in Node, in the worker and on the page.

Import: import { … } from './src/core/lcprep.js' — no DOM, runs in Node and the browser.

QUALITY_BITMASKS ​

js
const QUALITY_BITMASKS

lightkurve's quality bitmasks (lightkurve 2.6 utils.py: TessQualityFlags, KeplerQualityFlags, also used for K2). 'default': cadences that are definitely bad; 'hard': also cosmic rays in the aperture or collateral pixels, stray light (TESS), rolling band and possible thruster firings (Kepler / K2); 'hardest': any flag. EXOTIC's TESS scripts download with 'hard'.

qualityBitmask ​

js
function qualityBitmask(mission, preset = 'default')

The bitmask of a preset ('none' | 'default' | 'hard' | 'hardest') for a mission ('TESS', 'Kepler', 'K2'); a number passes through.

K2_THRUSTER_BITS ​

js
const K2_THRUSTER_BITS

K2 thruster-firing bits: ThrusterFiring (1048576) and PossibleThrusterFiring (524288).

nanMedian ​

js
function nanMedian(v)

No description in the source.

nanStd ​

js
function nanStd(v)

numpy nanstd (ddof 0).

interp ​

js
function interp(x, xp, fp, out = new Float64Array(x.length)

Linear interpolation of (xp, fp) (xp increasing) at x, constant beyond the ends (numpy interp).

rng ​

js
function rng(seed = 1)

A small seeded generator (mulberry32), so that injection tests repeat.

biweightLocation ​

js
function biweightLocation(s, n = s.length, cval = 5, tmp = null)
text
Tukey's biweight location of the values `s` (sorted, length n), starting at the median with the median absolute
deviation as scale, iterated until a step is smaller than 1e-6 MAD (wotan location_iter, cval = 5).

biweightTrend ​

js
function biweightTrend(t, f, opts = {})

Time-windowed sliding biweight trend (wotan flatten, method 'biweight'). For every point the location of the unmasked points with time in [t - w/2, t + w/2) of its segment; segments are split where consecutive times differ by more than breakTolerance (default w/2, as wotan). Points whose window holds no unmasked point get NaN, and so do points within edgeCutoff of a segment's ends.

  • param t times (any order; NaNs allowed), f fluxes
  • param opts { window (days, required), mask: truthy = excluded from the trend estimate (wotan's convention, e.g. in-transit points), breakTolerance, edgeCutoff = 0, cval = 5, step: evaluate about every step days and interpolate between (wotan's step; much faster for long windows, used by the window scan) }
  • returns Float64Array trend, aligned with t

flatten ​

js
function flatten(t, f, opts = {})

flux / trend with the biweight trend: { flat, trend } (wotan flatten(..., return_trend=True)).

durationT14 ​

js
function durationT14(per, rprs, ars, inc = 90, ecc = 0, omega = 0)

Total transit duration T14 in days (EXOTIC toi1873_extract.py duration_t14): P/π asin(√((1+k)² − b²) / (a/R★ sin i)), with the eccentric correction √(1−e²)/(1+e sin ω) on the speed and on b. NaN when the planet does not transit.

phaseDistance ​

js
function phaseDistance(t, per, tmid)

Time from the nearest predicted transit, |((t − T0 + P/2) mod P) − P/2| with a floored modulo (numpy %).

transitMask ​

js
function transitMask(t, planets, pad = 1.5)

True where t lies within pad·T14/2 of a transit of any planet (EXOTIC transit_mask). planets: [{ per, tmid, t14 }].

  • returns Uint8Array (1 = in transit)

clipOutliers ​

js
function clipOutliers(t, f, tmask = null, sigma = 4, dtHr = 0.5)

EXOTIC's sigma clip against a smooth trend that never clips in-transit points (toi1873_extract.py clip_outliers): a Savitzky-Golay smooth (order 2, window 1 + 2·max(7, ⌊dtHr/24/cadence⌋) points) of the finite points in time order, interpolated to every time; points more than sigma standard deviations (numpy, ddof 0) from it are dropped, unless tmask marks them as in transit.

  • returns Uint8Array keep (1 = keep)

trimAfterGaps ​

js
function trimAfterGaps(t, good, minutes = 30, gapDays = 1 / 24)
text
Cadences to drop after gaps (thermal settling; EXOTIC's post_gap_trim): the first 2·ntrim good points of the
series and the ntrim points after every gap longer than `gapDays`, ntrim = max(1, ⌊minutes / (1440 · cadence)⌋).
  • param good Uint8Array of usable points; returns Uint8Array trim (1 = drop)

binSeries ​

js
function binSeries(t, f, m, chan = {}, nb = 2)

Average groups of nb cadences on a uniform time grid (EXOTIC bin_series): a downlink gap never merges cadences across it, and a bin is in transit when any member was. chan: { name: array } binned the same way.

  • returns { t, f, m, chan }

optimizeWindow ​

js
function optimizeWindow(t, f, tmask, t14max, opts = {})

Choose the biweight window by synthetic-transit recovery (EXOTIC optimize_window): box transits of depth depth and duration t14max are injected (one at a time) at epochs away from the real transits; each candidate window detrends with the real transits masked and the injected one NOT masked (the damage a trend does to an unknown transit); the recovered fraction is (median out of the box − median in the box) / depth, and the score is recovered / (noise / √(t14max / cadence)), noise being the standard deviation of the flattened out-of-transit flux.

  • param opts { grid: windows in days, depth = 1500e-6, epochs: injection times (else drawn with a seeded generator as EXOTIC draws them), nInject = 12, seed = 0, breakTolerance (EXOTIC: gap tolerance, 1 h), step (biweightTrend's; the page uses 10 min, EXOTIC none) }
  • returns { window, scan: { [window]: { depth_recovered, noise_ppm, score } }, epochs }

stitch ​

js
function stitch(parts)

Join several light curves (sectors, quarters, campaigns) into one: each is divided by its own median (lightkurve LightCurveCollection.stitch, EXOTIC's per-sector normalisation), the points are put in time order, and a point less than 1 s after the previous one is dropped (the same photons twice: a sector in 20 s and 2 min). parts: [{ name, t, f, e?, columns?: { name: array } }].

  • returns { t, f, e, segment (index of the part per point), names, medians, dropped }

transitWindows ​

js
function transitWindows(t, f, planets, opts = {})

The window around every predicted transit of every planet that the data cover (EXOTIC stage_fit): points with |t − Tc| < halfWidth (default 2.5 T14, EXOTIC's fit_window_durations); a window is kept when it holds at least max(10, round(40 · 2 / cadence_minutes)) points (EXOTIC's 40 points at 2 min, scaled for coarser data) and points on both sides of Tc, and at least one inside ±T14/2 (unless keepEmpty). Another planet transiting inside the window is reported (overlaps), and with divideOthers its model transit is divided out of fOut (EXOTIC's fix for blended windows; needs rprs, ars, inc). planets: [{ name, per, tmid, t14, rprs?, ars?, inc?, ecc?, omega?, ld? }].

  • returns { windows: [{ planet, epoch, tc, idx: [indices], n, overlaps: [names], coverage }], fOut }

aliasScan ​

js
function aliasScan(t, cad, epochs, opts = {})
text
Which aliases P = (span of the observed transits) / N of a planet seen in a few transits survive the coverage
(EXOTIC stage_alias_scan): every transit an alias predicts inside the data is "covered" when the cadences within
±T14/2 of it add up (each counting its own sector's cadence) to more than half of T14, and "missed" when it is
covered but more than 0.1 d from every observed transit. An alias with a missed transit is rejected.
  • param t times of the data, cad per-point cadence (days), epochs observed mid-transit times
  • param opts { rprs, arsFor: P => a/R★ (Kepler's law with the star), Nmax = 25, inc = 90 }
  • returns { baseline, rows: [{ N, per, t14_hr, covered, missed, allowed }], minimumAllowed }

cadencePerPoint ​

js
function cadencePerPoint(t, segment)

Per-point cadence: the median spacing of each segment's own points (EXOTIC: per sector), so an FFI sector counts its 30 min.

sigmaClipKeep ​

js
function sigmaClipKeep(v, sigma = 5, maxiters = 5)

astropy sigma_clip(data, sigma, maxiters=5, cenfunc='median', stdfunc='std') as lightkurve's remove_outliers uses it: returns Uint8Array keep.

savgolFlatten ​

js
function savgolFlatten(t, f, opts = {})

lightkurve LightCurve.flatten: a Savitzky-Golay trend (window window cadences, order 2) per stretch between gaps of more than breakTolerance × the median spacing, iterated niters times with points beyond sigma standard deviations set aside and the trend interpolated across them. Returns { flat, trend }.

cdpp ​

js
function cdpp(t, f, opts = {})

lightkurve estimate_cdpp: flatten (Savitzky-Golay, savgolWindow cadences), remove outliers beyond sigma (astropy sigma_clip), normalise to ppm, take the running mean over transitDuration cadences and return its standard deviation (ppm). Times and fluxes of good cadences only (NaNs are ignored).

MIT licensed · Kyle A. Pearson