"""Search the early KIC 6691030 light curve near its reported candidate period.""" import csv import gzip import json import math from pathlib import Path STAR = "KIC 6691030" TRAINING_FILE = Path("observations/6691030-train.csv.gz") PERIOD_MIN = 4.60 PERIOD_MAX = 4.95 PERIOD_STEP = 0.0002 PHASE_BINS = 512 MIN_DURATION_HOURS = 0.75 MAX_DURATION_HOURS = 8.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 bin_phases(rows, period, origin): 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 return sums, counts def best_window(sums, counts, total_flux, total_count, period): doubled_sums = sums + sums doubled_counts = counts + counts prefix_sums = [0.0] prefix_counts = [0] for flux, count in zip(doubled_sums, doubled_counts): prefix_sums.append(prefix_sums[-1] + flux) prefix_counts.append(prefix_counts[-1] + count) best = None minimum_bins = max(2, math.floor(MIN_DURATION_HOURS / 24 / period * PHASE_BINS)) maximum_bins = math.ceil(MAX_DURATION_HOURS / 24 / period * PHASE_BINS) for width in range(minimum_bins, maximum_bins + 1): duration_hours = width / PHASE_BINS * period * 24 for start in range(PHASE_BINS): inside_count = prefix_counts[start + width] - prefix_counts[start] if inside_count < 2: continue inside_flux = prefix_sums[start + width] - prefix_sums[start] outside_count = total_count - inside_count inside_mean = inside_flux / inside_count outside_mean = (total_flux - inside_flux) / outside_count depth = outside_mean - inside_mean score = depth * math.sqrt(inside_count * outside_count / total_count) if best is None or score > best[0]: best = (score, start, width, duration_hours, depth) return best def search(rows): origin = rows[0][0] total_flux = sum(flux for _, flux in rows) total_count = len(rows) steps = round((PERIOD_MAX - PERIOD_MIN) / PERIOD_STEP) best = None for step in range(steps + 1): period = PERIOD_MIN + step * PERIOD_STEP sums, counts = bin_phases(rows, period, origin) window = best_window(sums, counts, total_flux, total_count, period) if window is not None and (best is None or window[0] > best[0]): best = (*window, period) if best is None: raise RuntimeError("No candidate transit window was found.") _, start, width, duration_hours, _, _ = best period = best[-1] phase = ((start + width / 2) / PHASE_BINS) % 1.0 transit = origin + phase * period midpoint = (rows[0][0] + rows[-1][0]) / 2 epoch = transit + round((midpoint - transit) / period) * period return { "star": STAR, "period_days": round(period, 9), "epoch_bkjd": round(epoch, 8), "duration_hours": round(duration_hours, 4), } if __name__ == "__main__": print(json.dumps(search(read_training())))