"""Search the early window for a repeating dip, with the noise of the folded curve beside it. python3 routes/galaxy-21-0342e2d9c9/search.py early.csv.gz For each trial period the curve is folded into 100 phase bins of median flux. The statistic is the deepest 3% of phase below the median of those bins, divided by the robust scatter of the bins themselves: a dip that is not several times its own bin noise is not evidence of anything, however deep the raw curve looks elsewhere. """ import csv import gzip import sys import numpy as np NB = 100 WIDTH = 3 def load(path): t, f = [], [] with gzip.open(path, "rt", newline="") as handle: for row in csv.DictReader(handle): t.append(float(row["time_bkjd"])) f.append(int(row["relative_flux_ppm"])) return np.array(t), np.array(f, float) def folded(t, f, period): """Median curve over phase, plus the deepest short dip in it against the bin scatter.""" phase = (t / period) % 1.0 index = np.minimum((phase * NB).astype(int), NB - 1) med = np.full(NB, np.nan) for i in range(NB): part = index == i if part.sum() >= 5: med[i] = np.median(f[part]) if np.isnan(med).sum() > NB // 3: return 0.0, 0.0, 0.0 base = np.nanmedian(med) scatter = 1.4826 * np.nanmedian(np.abs(med - base)) best = 0.0 for i in range(NB): vals = med[[(i + k) % NB for k in range(WIDTH)]] if not np.isnan(vals).any(): best = max(best, base - np.median(vals)) return best, scatter, best / scatter if scatter > 0 else 0.0 def main(): t, f = load(sys.argv[1]) split = (t.min() + t.max()) / 2 first = t < split rows = [] for period in np.exp(np.linspace(np.log(0.5), np.log(30), 20000)): dip, scatter, ratio = folded(t, f, period) rows.append((ratio, dip, scatter, period)) rows.sort(reverse=True) print("period dip_ppm bin_scatter_ppm dip/scatter dip_first_half dip_second_half") shown = [] for ratio, dip, scatter, period in rows: if all(abs(period - s) / s > 0.02 for s in shown): shown.append(period) a = folded(t[first], f[first], period)[0] b = folded(t[~first], f[~first], period)[0] print("%8.5f %8.1f %11.1f %13.2f %15.1f %16.1f" % (period, dip, scatter, ratio, a, b)) if len(shown) == 10: break if __name__ == "__main__": main()