diff --git a/scripts/displacement.py b/scripts/displacement.py index 16cbb50..6712e3e 100644 --- a/scripts/displacement.py +++ b/scripts/displacement.py @@ -4,27 +4,25 @@ 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; or the day after the jump at the level of the day before (the jump ended a dip). Otherwise - "Lasting shift", or "Jump, not back yet" with less than BACK_H of hindsight. After a jump that never comes back, its - new level becomes the level before. -3. Jumps of all the targets less than GAP apart make one episode; a target that jumps and comes back at least REPEAT - times, each within BACK_H of the previous one, makes an episode of its own, from its first jump to its last return. -4. "Sudden jump, back to normal" is greyed and its cause is not established here: categories.py gives it the family of - an overlapping episode (rain, something passing...), otherwise it is to be checked on the images. A shift is never - greyed (maybe a real movement); its episode ends at its last jump. + 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 common import episode, noise, targets +from common 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) -GAP = timedelta(hours=1) BACK_H = timedelta(hours=24) # E-002: plateaus up to 11.5 h -REPEAT = 5 # CLARIDEN, batch 01: PR_BG flips 28 times in 2.5 days, the other targets 3 at most 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): @@ -39,52 +37,14 @@ def _jumps(t, v): 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)) - if j < len(v) and t[j] - t[i] <= BACK_H: - state = "back" - elif abs(statistics.median(x for x, u in zip(v, t) if t[i] <= u < t[i] + BACK_H) - - statistics.median([x for x, u in zip(v, t) if t[i] - BACK_H <= u < t[i]] or [base])) < tol: - state, j = "back", i + 1 # the day after at the level of the day before: the jump ended a dip - else: - state = "lasting" if t[-1] - t[i] >= BACK_H else "recent" - out.append((i, j - 1, state)) + 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 _repeats(found): - """Jumps back of the same target, each within BACK_H of the previous one: REPEAT or more -> [(start, end, target)].""" - out = [] - for name in {f[3] for f in found}: - chain = [] # (start, end); corr_x and corr_y together: counted by instant - for s, e in sorted({(s, e) for s, e, state, n in found if n == name and state == "back"}) + [(None, None)]: - if chain and s is not None and s <= max(c[1] for c in chain) + BACK_H: - chain.append((s, e)) - continue - if len({c[0] for c in chain}) >= REPEAT: - out.append((chain[0][0], max(c[1] for c in chain), name)) - chain = [(s, e)] - return out - - -def _episode(start, end, states): - """{target: {states}} -> the episode of a group of jumps.""" - n = len(states) - lasting = sum("lasting" in st for st in states.values()) - who = targets(n).capitalize() - if lasting: - some = "stayed shifted" if lasting == n else f"{lasting} of them stayed shifted" - label, text = "Lasting shift", f"{who} shifted; {some}." - elif any("recent" in st for st in states.values()): - label, text = "Jump, not back yet", f"{who} shifted and {'has' if n == 1 else 'have'} not come back yet." - else: - return episode(start, end, "Displacement", JUMP_BACK, f"{who} jumped and came back. The cause is not established: " - "look at the images.", states, True) - return episode(start, end, "Displacement", label, text + " Check whether the structure moved: look at the images.", - states, False) - - def jumps(measurements): - found = [] # (start, end, state, target) + found = set() # (start, end, state, target); corr_x and corr_y together for name, ms in measurements.items(): if is_reference(ms): continue @@ -92,22 +52,8 @@ def jumps(measurements): 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] if state == "back" else t[a], state, name) for a, b, state in _jumps(t, v)] - alone = _repeats(found) - out = [_episode(a, b, {name: {"back"}}) for a, b, name in alone] - groups = [] # a shift only counts by its instant: its end is often the end of the data - for s, e, state, name in sorted(found): - if state == "back" and any(name == n and a <= s <= b for a, b, n in alone): - continue - if not groups or s > groups[-1]["until"] + GAP: - groups.append({"start": s, "end": e, "until": s, "states": {}}) - g = groups[-1] - g["end"], g["until"], g["last"] = max(g["end"], e), max(g["until"], e if state == "back" else s), s - g["states"].setdefault(name, set()).add(state) - for g in groups: - shift = any(st != {"back"} for st in g["states"].values()) - out.append(_episode(g["start"], g["last"] if shift else g["end"], g["states"])) - return out + 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):