Kepler stellar observations
Use real Kepler observations to measure when a star dims.

| 1 | """Search the early window for a repeating dip, with the noise of the folded curve beside it. |
| 2 | |
| 3 | python3 routes/galaxy-21-0342e2d9c9/search.py early.csv.gz |
| 4 | |
| 5 | For each trial period the curve is folded into 100 phase bins of median flux. The |
| 6 | statistic is the deepest 3% of phase below the median of those bins, divided by the |
| 7 | robust scatter of the bins themselves: a dip that is not several times its own bin |
| 8 | noise is not evidence of anything, however deep the raw curve looks elsewhere. |
| 9 | """ |
| 10 | |
| 11 | import csv |
| 12 | import gzip |
| 13 | import sys |
| 14 | |
| 15 | import numpy as np |
| 16 | |
| 17 | NB = 100 |
| 18 | WIDTH = 3 |
| 19 | |
| 20 | |
| 21 | def load(path): |
| 22 | t, f = [], [] |
| 23 | with gzip.open(path, "rt", newline="") as handle: |
| 24 | for row in csv.DictReader(handle): |
| 25 | t.append(float(row["time_bkjd"])) |
| 26 | f.append(int(row["relative_flux_ppm"])) |
| 27 | return np.array(t), np.array(f, float) |
| 28 | |
| 29 | |
| 30 | def folded(t, f, period): |
| 31 | """Median curve over phase, plus the deepest short dip in it against the bin scatter.""" |
| 32 | phase = (t / period) % 1.0 |
| 33 | index = np.minimum((phase * NB).astype(int), NB - 1) |
| 34 | med = np.full(NB, np.nan) |
| 35 | for i in range(NB): |
| 36 | part = index == i |
| 37 | if part.sum() >= 5: |
| 38 | med[i] = np.median(f[part]) |
| 39 | if np.isnan(med).sum() > NB // 3: |
| 40 | return 0.0, 0.0, 0.0 |
| 41 | base = np.nanmedian(med) |
| 42 | scatter = 1.4826 * np.nanmedian(np.abs(med - base)) |
| 43 | best = 0.0 |
| 44 | for i in range(NB): |
| 45 | vals = med[[(i + k) % NB for k in range(WIDTH)]] |
| 46 | if not np.isnan(vals).any(): |
| 47 | best = max(best, base - np.median(vals)) |
| 48 | return best, scatter, best / scatter if scatter > 0 else 0.0 |
| 49 | |
| 50 | |
| 51 | def main(): |
| 52 | t, f = load(sys.argv[1]) |
| 53 | split = (t.min() + t.max()) / 2 |
| 54 | first = t < split |
| 55 | rows = [] |
| 56 | for period in np.exp(np.linspace(np.log(0.5), np.log(30), 20000)): |
| 57 | dip, scatter, ratio = folded(t, f, period) |
| 58 | rows.append((ratio, dip, scatter, period)) |
| 59 | rows.sort(reverse=True) |
| 60 | print("period dip_ppm bin_scatter_ppm dip/scatter dip_first_half dip_second_half") |
| 61 | shown = [] |
| 62 | for ratio, dip, scatter, period in rows: |
| 63 | if all(abs(period - s) / s > 0.02 for s in shown): |
| 64 | shown.append(period) |
| 65 | a = folded(t[first], f[first], period)[0] |
| 66 | b = folded(t[~first], f[~first], period)[0] |
| 67 | print("%8.5f %8.1f %11.1f %13.2f %15.1f %16.1f" % (period, dip, scatter, ratio, a, b)) |
| 68 | if len(shown) == 10: |
| 69 | break |
| 70 | |
| 71 | |
| 72 | if __name__ == "__main__": |
| 73 | main() |