"""Displacement family: sudden jumps of the corrected displacements (corr_x, corr_y, px) of each target, and slow drift (end of file). Global references are excluded, rejected measurements (empty corr) skipped. 1. Jump: a measurement at least K noises (median step of the series) away from the level before (median of the BASE previous measurements). 2. Back to normal: within BACK_H, an hour of measurements in a row (BASE at most, one at least) within half the jump of the level before; greyed, its cause not established here (categories.py joins it to an overlapping Visibility or Obstruction episode, otherwise it is to be checked on the images). Otherwise "Lasting shift", or "Jump, not back yet" with less than BACK_H of hindsight: never greyed (maybe a real movement), the episode is the jump's instant; the new level becomes the level before. """ import statistics from datetime import timedelta from episodes import episode, noise from data import is_reference K = 20 # E-001: at 15, two healthy targets are greyed (17 and 18 noises); the trains of E-005 are visibility's BASE = 4 # measurements of the level before (1 h at 15 min); the return is judged on an hour (wf-improve MICHIGAN, mod. 1) BACK_H = timedelta(hours=24) # E-002: plateaus up to 11.5 h JUMP_BACK = "Sudden jump, back to normal" CHECK = " Check whether the structure moved: look at the images." LABELS = {"back": (JUMP_BACK, "A target jumped and came back within a day. The cause is not established: look at the images."), "lasting": ("Lasting shift", "A target shifted and stayed shifted for more than a day." + CHECK), "recent": ("Jump, not back yet", "A target shifted and has not come back yet." + CHECK)} def _jumps(t, v): """A series (instants, values) -> [(start index, end index, state)], state "back", "lasting" or "recent".""" n = noise(v) back_n = max(1, min(BASE, round(timedelta(hours=1) / statistics.median(b - a for a, b in zip(t, t[1:]))))) out, i = [], BASE while n and i < len(v): base = statistics.median(v[i - BASE:i]) if abs(v[i] - base) < K * n: i += 1 continue tol = abs(v[i] - base) / 2 j = next((k for k in range(i + 1, len(v)) if all(abs(x - base) < tol for x in v[k:k + back_n])), len(v)) state = "back" if j < len(v) and t[j] - t[i] <= BACK_H else "lasting" if t[-1] - t[i] >= BACK_H else "recent" out.append((i, j - 1 if state == "back" else i, state)) i = (j if j < len(v) else i) + BASE return out def jumps(measurements): found = set() # (start, end, state, target); corr_x and corr_y together for name, ms in measurements.items(): if is_reference(ms): continue for axis in ("corr_x", "corr_y"): kept = [m for m in ms if m[axis] is not None] if len(kept) > BASE: t, v = [m["t"] for m in kept], [m[axis] for m in kept] found |= {(t[a], t[b], state, name) for a, b, state in _jumps(t, v)} return [episode(a, b, "Displacement", *LABELS[state], [name], state == "back") for a, b, state, name in sorted(found)] def detect(measurements): return jumps(measurements) + drift(measurements) # --- Slow drift: a target that moves little by little, day after day (the trend of the Displacements tab, D-055). # Per target and per axis (corr_x, corr_y): the level of each UTC day measured at least HOURS of the hours the station # measures (median of the day), at least DAYS days; the amplitude of a day: its 9th minus its 1st decile. # 1. Over the whole window: Theil-Sen slope × duration at least SIZE median daily amplitudes, |Kendall tau| ≥ STEADY, and # no step from one day to the next larger than half the drift (that is a jump). # 2. Otherwise, pieces: runs of at least DAYS kept days whose last level minus the first is at least PIECE amplitudes, with # the same tau and step conditions; taken from the largest to the smallest without overlapping (wf-improve # Displacement, mod. 1, S1: rows of CLARIDEN, 3.8 to 8.3 amplitudes; thermal cycles of JUNGFRAU, 3.1 at most). # One episode per drift, not greyed: the largest of the two axes over the whole window, otherwise each piece. HOURS = 20 # out of 24, as a share of the hours the station measures (wf-improve MICHIGAN, mod. 1) DAYS = 5 # D-055 SIZE = 1.0 # D-055 STEADY = 0.6 # D-055 PIECE = 3.5 # wf-improve Displacement, mod. 1, S1 def _decile(vs, q): vs = sorted(vs) return vs[min(len(vs) - 1, int(q * len(vs)))] def _days(ms, col): """-> (kept days, their levels, median daily amplitude, [(first instant, last) of each day]).""" days = {} for m in ms: if m[col] is not None: days.setdefault(m["t"].date(), []).append(m) slots = len({m["t"].hour for dm in days.values() for m in dm}) # hours the station measures (6 at a 4 h step) ds = sorted(d for d, dm in days.items() if len({m["t"].hour for m in dm}) >= HOURS / 24 * slots) vals = [[m[col] for m in days[d]] for d in ds] ampl = statistics.median(_decile(v, 0.9) - _decile(v, 0.1) for v in vals) if ds else 0 return ds, [statistics.median(v) for v in vals], ampl, [(days[d][0]["t"], days[d][-1]["t"]) for d in ds] def _whole(ds, lv, ampl, _): """-> [(drift, 0, last index)] if the levels drift over the whole window, else [].""" pairs = [(i, j) for i in range(len(ds)) for j in range(i + 1, len(ds))] slope = statistics.median((lv[j] - lv[i]) / (ds[j] - ds[i]).days for i, j in pairs) tau = sum((lv[j] > lv[i]) - (lv[j] < lv[i]) for i, j in pairs) / len(pairs) d = slope * (ds[-1] - ds[0]).days step = max(abs(b - a) for a, b in zip(lv, lv[1:])) return [(d, 0, len(ds) - 1)] if abs(d) >= SIZE * ampl and abs(tau) >= STEADY and step <= abs(d) / 2 else [] def _pieces(ds, lv, ampl, _): """-> [(drift, first index, last index)]: the drift pieces, without overlapping.""" pieces = [] for i in range(len(ds)): signs = step = 0 for j in range(i + 1, len(ds)): signs += sum((lv[j] > lv[k]) - (lv[j] < lv[k]) for k in range(i, j)) # Kendall tau, step by step step = max(step, abs(lv[j] - lv[j - 1])) n, d = j - i + 1, lv[j] - lv[i] if n >= DAYS and d and abs(d) >= PIECE * ampl and step <= abs(d) / 2 \ and signs * (1 if d > 0 else -1) >= STEADY * n * (n - 1) / 2: pieces.append((d, i, j)) taken = [] for d, i, j in sorted(pieces, key=lambda x: (-abs(x[0]), x[2] - x[1])): if all(j <= a or i >= b for _, a, b in taken): taken.append((d, i, j)) return taken def drift(measurements): out = [] for s, ms in sorted(measurements.items()): if is_reference(ms): continue axes = [a for col in ("corr_x", "corr_y") if len((a := _days(ms, col))[0]) >= DAYS] whole = [(w, a) for a in axes for w in _whole(*a)] found = [max(whole, key=lambda x: abs(x[0][0]))] if whole else [(p, a) for a in axes for p in _pieces(*a)] out += [episode(a[3][i][0], a[3][j][1], "Displacement", "Slow drift", f"{s} drifts slowly and steadily: {d:+.2f} " f"px in {j - i + 1} days. Keep watching it.", [s], False) for (d, i, j), a in found] return out