"""Refine a training-only candidate ephemeris for KIC 5689121.""" import csv import gzip import json import math from pathlib import Path from statistics import median STAR = "KIC 5689121" TRAINING_FILE = Path("observations/5689121-train.csv.gz") PERIOD_MIN = 13.65 PERIOD_MAX = 13.90 PERIOD_STEP = 0.00025 PHASE_BINS = 256 MIN_DURATION_HOURS = 1.0 MAX_DURATION_HOURS = 4.0 def read_training(): with gzip.open(TRAINING_FILE, "rt", newline="") as handle: reader = csv.reader(handle) next(reader) return [(float(time), int(flux)) for time, flux in reader] def training_snr(rows, period, epoch, duration_hours): width = duration_hours / 24 events = {} for time, flux in rows: cycle = math.floor((time - epoch) / period + 0.5) distance = abs(time - epoch - cycle * period) if distance <= width / 2: events.setdefault(cycle, ([], []))[0].append(flux) elif width <= distance <= 2 * width: events.setdefault(cycle, ([], []))[1].append(flux) depths = [median(outside) - median(inside) for inside, outside in events.values() if len(inside) >= 2 and len(outside) >= 4] if not depths: return float("-inf") depth = median(depths) scatter = max(40.0, 1.4826 * median(abs(value - depth) for value in depths)) return depth * math.sqrt(len(depths)) / scatter def coarse_candidate(rows): origin = rows[0][0] midpoint = (rows[0][0] + rows[-1][0]) / 2 total_flux = sum(flux for _, flux in rows) total_count = len(rows) best = None period_count = round((PERIOD_MAX - PERIOD_MIN) / PERIOD_STEP) for period_index in range(period_count + 1): period = PERIOD_MIN + period_index * PERIOD_STEP sums = [0.0] * PHASE_BINS counts = [0] * PHASE_BINS for time, flux in rows: phase = ((time - origin) / period) % 1.0 index = min(PHASE_BINS - 1, int(phase * PHASE_BINS)) sums[index] += flux counts[index] += 1 extended_sums = sums + sums extended_counts = counts + counts sum_prefix = [0.0] count_prefix = [0] for flux, count in zip(extended_sums, extended_counts): sum_prefix.append(sum_prefix[-1] + flux) count_prefix.append(count_prefix[-1] + count) min_width = max(1, math.floor(MIN_DURATION_HOURS / 24 / period * PHASE_BINS)) max_width = math.ceil(MAX_DURATION_HOURS / 24 / period * PHASE_BINS) for width in range(min_width, max_width + 1): duration = width * period * 24 / PHASE_BINS if duration < MIN_DURATION_HOURS: continue for start in range(PHASE_BINS): inside_count = count_prefix[start + width] - count_prefix[start] if inside_count < 2: continue inside_sum = sum_prefix[start + width] - sum_prefix[start] outside_count = total_count - inside_count depth = (total_flux - inside_sum) / outside_count - inside_sum / inside_count score = depth * math.sqrt(inside_count * outside_count / total_count) if best is None or score > best[0]: phase_center = ((start + width / 2) / PHASE_BINS) % 1.0 transit = origin + phase_center * period epoch = transit + round((midpoint - transit) / period) * period best = (score, period, epoch, duration) if best is None: raise RuntimeError("No candidate window found in the period range.") return best[1:] def search(rows): period, epoch, duration = coarse_candidate(rows) for period_step, epoch_step in ((0.00001, 0.002), (0.000002, 0.0004)): candidates = ( (period + period_offset * period_step, epoch + epoch_offset * epoch_step) for period_offset in range(-5, 6) for epoch_offset in range(-5, 6) ) period, epoch = max( candidates, key=lambda candidate: training_snr(rows, candidate[0], candidate[1], duration), ) return { "star": STAR, "period_days": round(period, 9), "epoch_bkjd": round(epoch, 8), "duration_hours": duration, } if __name__ == "__main__": print(json.dumps(search(read_training())))