"""Target issue family: a target rejected day after day ("Degraded target"); visibility.py compares each sensor with its own usual level, so a target almost always rejected looks normal to it. For a few days only, Target obscured (Obstruction family). 1. Degraded day: at least SHARE of the sensor's measurements of the UTC day rejected; a day with fewer than MIN_HOURS of measurements (at the station's cadence) does not count. 2. Run: degraded days with at most BRIDGE other days in a row between them, and SHARE of rejections over the run. WEEK days or more: Degraded target; MIN_DAYS or more: Target obscured. The episode runs from the first rejected measurement of the run to the last, greyed. The cause (dirt, vegetation, a parked vehicle) is seen on the images. """ from datetime import timedelta from episodes import cadence, episode SHARE = 0.25 # wf-improve Target issue, mod. 1, S1 (WILDSTRUBEL R3 rejected from 04:00 to 16:00 every day, 30 to 45 %) MIN_HOURS = 3 # wf-improve NEBRASKA, batch 01 (one measurement every 4 h: as hours, not as instants) BRIDGE = 1 # wf-improve Obstruction, mod. 1, S1 and Target issue, mod. 1, S1 (problem targets cut by a better day) WEEK = 7 # wf-improve NEBRASKA, batch 01 (lost references, 25 days; parked vehicles, 2 to 5 days, are not one) MIN_DAYS = 2 # shorter than WEEK: a target hidden for a few days, Target obscured (Obstruction); NORDEND: isolated days def rejected(m): return m["error_value"] is not None and m["error_threshold"] is not None and m["error_value"] > m["error_threshold"] def _runs(days, min_instants): """{day: [rejected, measurements, first, last]} -> runs of degraded days; at most BRIDGE other days in a row between two degraded days belong to the run; a day that does not count is skipped.""" runs, cur, gap = [], [], [] for d in sorted(days): rej, n = days[d][:2] if n < min_instants: continue if rej / n >= SHARE: cur += gap + [days[d]] gap = [] elif cur: gap.append(days[d]) if len(gap) > BRIDGE: runs.append(cur) cur, gap = [], [] return runs + ([cur] if cur else []) def detect(measurements): min_instants = timedelta(hours=MIN_HOURS) / cadence(measurements) days = {} # sensor -> day -> [rejected, measurements, first, last] for s, ms in measurements.items(): for m in ms: c = days.setdefault(s, {}).setdefault(m["t"].date(), [0, 0, m["t"], m["t"]]) c[0], c[1], c[3] = c[0] + rejected(m), c[1] + 1, m["t"] out = [] for s, ds in sorted(days.items()): for run in _runs(ds, min_instants): share = sum(c[0] for c in run) / sum(c[1] for c in run) if len(run) < MIN_DAYS or share < SHARE: continue bad = [m["t"] for m in measurements[s] if run[0][2] <= m["t"] <= run[-1][3] and rejected(m)] n = sum(c[0] / c[1] >= SHARE for c in run) seen = f"{s} was rejected in {round(100 * share)} % of the measurements, on {n} of {len(run)} days." out.append(episode(bad[0], bad[-1], "Target issue", "Degraded target", seen + " Ignore its curve in this period " "and look at the images of the target.", [s], True) if len(run) >= WEEK else episode(bad[0], bad[-1], "Obstruction", "Target obscured", seen + " Something hid it for a few " "days: ignore its curve in this period.", [s], True)) return out