Removed (their benchmark contribution, each alone): the cold rule with COLD, which needed the station temperature and camera pose (Thermal found -64 at CLARIDEN and -19 at BREITHORN, 27 and 4 instants; Totals unchanged), so categories.py no longer reads them; PARTIAL days (Thermal found -1, correct -1); DAY_MATCH, every day of a cycle gets its episodes (Thermal found +8, correct -2); MIN_WARM_HOURS (none). The 24 h sinusoid by projection instead of least squares (same figures). The event sentences of the thermal varieties are now the episodes' texts. Benchmark Totals (from the previous commit): Thermal displacement found 74 -> 80 %, correct 75 -> 72 %; the other families unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
8.7 KiB
Python
160 lines
8.7 KiB
Python
"""Thermal displacement family: the daily wave of the displacements (expansion by day, contraction in the evening).
|
|
|
|
Per target (references excluded) and per axis:
|
|
1. Each UTC day measured at least MIN_HOURS hours: the mean of each hour minus the median of the day.
|
|
2. Profile: the median, hour by hour, over the days. The axis has a cycle if its days look like the profile of the
|
|
other days (median correlation at least REPRO).
|
|
3. The station has a cycle if at least SHARE of the targets have an axis with one and, for the corrected curves, a 24 h
|
|
sinusoid explains at least R2_MIN of an axis of SHARE of the targets.
|
|
4. Gap of each hour from the night level (median of the NIGHT flattest consecutive hours of the profile), as a share of
|
|
the profile's amplitude, median over the axes with a cycle; there is a wave if the largest gap (the peak) reaches
|
|
WARM. "Expansion" from where the gap rises above EDGE to the peak, "contraction" from the peak until the gap falls
|
|
under RETURN, after midnight if needed, at most to the next start: the cooling tail is slow (wf-improve Thermal
|
|
displacement, mod. 1, S1; wf-improve NORDEND 2026-11-05, S1). Two episodes for every day of the axes with a cycle.
|
|
The corrected curves first (the structure, "Thermal ..."); without a cycle there, the raw positions x, y ("Camera
|
|
thermal ...": the camera moves with its mount and the compensation removes it, D-074; the whole image, every sensor);
|
|
without one either, the camera slides ("Camera thermal movement", wf-improve MICHIGAN, mod. 1): instants where the raw
|
|
positions of SHARE of the sensors and the corrected curves of SHARE of the targets move away on the same side by
|
|
SLIDE_K noises from their median of the day before; slides less than a day apart make one episode.
|
|
"""
|
|
import math
|
|
import statistics
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from common import episode, noise, runs
|
|
from data import is_reference
|
|
|
|
MIN_HOURS = 20 # hours of a day of the profile
|
|
REPRO = 0.6 # pure noise ≈ 0
|
|
SHARE = 0.5 # NORDEND of October is just under (9/19)
|
|
R2_MIN = 0.3 # PS des Coudres 14 axes out of 22 above, CLARIDEN 0 out of 48 (least squares then; now the projection)
|
|
NIGHT = 8 # flattest consecutive hours of the profile
|
|
WARM = 0.3 # a wave: the peak at WARM times the amplitude from the night level
|
|
EDGE = 0.1 # start of the wave (wf-improve Thermal displacement, mod. 1, S1)
|
|
RETURN = 0.05 # end of the wave (wf-improve NORDEND 2026-11-05, S1)
|
|
SLIDE_K = 3 # common slide, in noises (wf-improve MICHIGAN, mod. 1: at 2, Displacement instants of BREITHORN taken)
|
|
W = 2 * math.pi / 86400 # angular frequency of a 24 h cycle, rad/s
|
|
DAY = timedelta(days=1)
|
|
TEXTS = {"Thermal expansion": "The structure expands in the day heat.",
|
|
"Thermal contraction": "The structure contracts as it cools down.",
|
|
"Camera thermal expansion": "The camera support expands in the day heat; the curves are corrected for it.",
|
|
"Camera thermal contraction": "The camera support contracts as it cools down; the curves are corrected for it."}
|
|
|
|
|
|
def _corr(a, b):
|
|
ma, mb = statistics.mean(a), statistics.mean(b)
|
|
num = sum((x - ma) * (y - mb) for x, y in zip(a, b))
|
|
den = (sum((x - ma) ** 2 for x in a) * sum((y - mb) ** 2 for y in b)) ** 0.5
|
|
return num / den if den else 0.0
|
|
|
|
|
|
def _axis(ms, col):
|
|
"""-> (24 h profile, {day: correlation with the profile of the other days}) or None."""
|
|
days = {}
|
|
for m in ms:
|
|
if m[col] is not None:
|
|
days.setdefault(m["t"].date(), {}).setdefault(m["t"].hour, []).append(m[col])
|
|
full = {}
|
|
for d, hs in days.items():
|
|
if len(hs) >= MIN_HOURS:
|
|
med = statistics.median(v for vs in hs.values() for v in vs)
|
|
full[d] = {h: statistics.mean(vs) - med for h, vs in hs.items()}
|
|
if len(full) < 2 or any(all(h not in q for q in full.values()) for h in range(24)):
|
|
return None
|
|
profile = [statistics.median(q[h] for q in full.values() if h in q) for h in range(24)]
|
|
likeness = {}
|
|
for d, p in full.items():
|
|
others = [q for e, q in full.items() if e != d]
|
|
hs = [h for h in p if all(h in q for q in others)]
|
|
if len(hs) >= 3:
|
|
likeness[d] = _corr([p[h] for h in hs], [statistics.median(q[h] for q in others) for h in hs])
|
|
return profile, likeness
|
|
|
|
|
|
def _sine_r2(ms, col):
|
|
"""Share of the curve explained by a 24 h sinusoid (its projection on the sine and cosine of the day: the
|
|
measurements cover the hours evenly), or None if too short."""
|
|
pts = [(m["t"].timestamp(), m[col]) for m in ms if m[col] is not None]
|
|
if len(pts) <= 48:
|
|
return None
|
|
mean = statistics.fmean(y for _, y in pts)
|
|
b = 2 * statistics.fmean((y - mean) * math.sin(W * t) for t, y in pts)
|
|
c = 2 * statistics.fmean((y - mean) * math.cos(W * t) for t, y in pts)
|
|
sst = sum((y - mean) ** 2 for _, y in pts)
|
|
sse = sum((y - mean - b * math.sin(W * t) - c * math.cos(W * t)) ** 2 for t, y in pts)
|
|
return 1 - sse / sst if sst else None
|
|
|
|
|
|
def _wave(measurements, cols, name, camera):
|
|
targets = {s: ms for s, ms in measurements.items() if not is_reference(ms)}
|
|
axes = [(s, *a) for s, ms in targets.items() for col in cols if (a := _axis(ms, col))] # (target, profile, likeness)
|
|
concerned = [a for a in axes if a[2] and statistics.median(a[2].values()) >= REPRO]
|
|
if not axes or len({a[0] for a in concerned}) < SHARE * len({a[0] for a in axes}):
|
|
return []
|
|
if not camera: # the sinusoid must see it too
|
|
r2 = {s: [r for col in cols if (r := _sine_r2(ms, col)) is not None] for s, ms in targets.items()}
|
|
r2 = {s: v for s, v in r2.items() if v}
|
|
if not r2 or sum(max(v) >= R2_MIN for v in r2.values()) < SHARE * len(r2):
|
|
return []
|
|
|
|
def gap(p, h): # from the night level, as a share of the amplitude
|
|
start = min(range(24), key=lambda i: max(p[(i + k) % 24] for k in range(NIGHT)) - min(p[(i + k) % 24] for k in range(NIGHT)))
|
|
return abs(p[h] - statistics.median(p[(start + k) % 24] for k in range(NIGHT))) / (max(p) - min(p))
|
|
e = [statistics.median(gap(a[1], h) for a in concerned) for h in range(24)]
|
|
peak, rest = max(range(24), key=e.__getitem__), min(range(24), key=e.__getitem__)
|
|
if e[peak] < WARM:
|
|
return []
|
|
a = peak
|
|
while (a - 1) % 24 != rest and e[(a - 1) % 24] >= EDGE:
|
|
a -= 1
|
|
b = peak + 1
|
|
while b < a + 24 and e[b % 24] >= RETURN:
|
|
b += 1
|
|
last = max(m["t"] for ms in measurements.values() for m in ms)
|
|
sensors = measurements if camera else {a[0] for a in concerned}
|
|
out = []
|
|
for d in sorted({d for a in concerned for d in a[2]}):
|
|
midnight = datetime(d.year, d.month, d.day, tzinfo=timezone.utc)
|
|
for label, u, v in ((f"{name} expansion", a, peak), (f"{name} contraction", peak, b)):
|
|
start, end = midnight + timedelta(hours=u), min(midnight + timedelta(hours=v), last)
|
|
if end > start:
|
|
out.append(episode(start, end, "Thermal displacement", label,
|
|
TEXTS[label] + " This daily wave is normal: nothing to do.", sensors, False))
|
|
return out
|
|
|
|
|
|
def _signs(ms, col):
|
|
"""{instant: +1 or -1} where the value moves away by at least SLIDE_K noises from its median of the day before."""
|
|
pts = [(m["t"], m[col]) for m in ms if m[col] is not None]
|
|
n, out, j = noise([v for _, v in pts]), {}, 0
|
|
for i, (t, v) in enumerate(pts):
|
|
while pts[j][0] < t - DAY:
|
|
j += 1
|
|
before = [w for _, w in pts[j:i]]
|
|
if n and len(before) >= 3 and abs(v - (med := statistics.median(before))) >= SLIDE_K * n:
|
|
out[t] = 1 if v > med else -1
|
|
return out
|
|
|
|
|
|
def _common(series):
|
|
"""[{instant: sign}] of a group of sensors -> instants where at least SHARE of them move away on the same side."""
|
|
count = {}
|
|
for sg in series:
|
|
for t, x in sg.items():
|
|
count[(t, x)] = count.get((t, x), 0) + 1
|
|
return {t for (t, x), c in count.items() if c >= SHARE * len(series)}
|
|
|
|
|
|
def slides(measurements):
|
|
targets = [ms for ms in measurements.values() if not is_reference(ms)]
|
|
raw = set().union(*(_common([_signs(ms, c) for ms in measurements.values()]) for c in ("x", "y")))
|
|
corr = set().union(*(_common([_signs(ms, c) for ms in targets]) for c in ("corr_x", "corr_y")))
|
|
return [episode(r[0], r[-1], "Thermal displacement", "Camera thermal movement", "The camera moved with a change of "
|
|
"temperature and all the curves moved with it; this is not a movement of the structure, nothing to "
|
|
"do.", measurements, False) for r in runs(sorted(raw & corr), DAY)]
|
|
|
|
|
|
def detect(measurements):
|
|
return (_wave(measurements, ("corr_x", "corr_y"), "Thermal", False)
|
|
or _wave(measurements, ("x", "y"), "Camera thermal", True) or slides(measurements))
|