Files
aimsight-detectors/scripts/target_issue.py
T

76 lines
4.0 KiB
Python
Raw Normal View History

"""Target issue family: a target rejected day after day while the others are seen ("Degraded target"). visibility.py
compares each sensor with its own usual level, so a target almost always rejected looks normal to it.
1. Calm instant for a sensor: at most CALM of the other sensors rejected (weather and fog, which touch everyone, are set
aside).
2. Degraded day: at least SHARE of the sensor's calm instants of the UTC day rejected; a day with fewer than MIN_HOURS of
calm instants (at the station's cadence) does not count.
3. Problem target: degraded days from a first to a last at least WEEK days apart, at most BRIDGE non-degraded days in a
row between them, and SHARE of rejections over these days; the episode runs from its first rejected measurement to
its last, greyed. The cause (dirt, vegetation, setting) is only seen on the images.
"""
from datetime import timedelta
from common import cadence, episode
CALM = 0.25 # share of the other sensors rejected, at most, at a calm instant
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, calm, 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, calm = days[d][:2]
if calm < min_instants:
continue
if rej / calm >= 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)
by_t = {}
for s, ms in measurements.items():
for m in ms:
by_t.setdefault(m["t"], {})[s] = rejected(m)
days = {} # sensor -> day -> [rejected, calm, first measurement, last]
for t in sorted(by_t):
for s, r in by_t[t].items():
others = [v for k, v in by_t[t].items() if k != s]
c = days.setdefault(s, {}).setdefault(t.date(), [0, 0, t, t])
c[3] = t
if others and sum(others) / len(others) <= CALM:
c[0] += r
c[1] += 1
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 when the other targets were seen, on " \
f"{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