"""The Points page: the point labels of labeling/points/, one boolean per measurement (valid or not) on a range, set by the user. Their format, their files and their check are labeling/scripts/points.py; the measurements are read by tools/scripts/data.py, as everywhere. A station of data/ or a run of the emulator (@), each its own points. """ import threading import station from station import Refusal, data import points # noqa: E402 (labeling/scripts, on the path through station) def refused(f): def wrapped(*a): try: return f(*a) except (ValueError, FileNotFoundError) as e: raise Refusal(str(e)) from e return wrapped def summary(n): p = points.read(n) s, start, end = points.parts(n) return {"name": n, "station": s, "start": start, "end": end, "points": len(p), "invalid": sum(not v for v in p.values())} @refused def listing(st=""): return [summary(n) for n in points.datasets(st or None)] def measured(st, a, b): """-> {sensor: [t]}: the points of [a, b), every measurement with a relative position, the rejected ones included (labeling/scripts/points.py).""" return {s: [m["t"] for m in ms if a <= m["t"] < b and m["relative_x"] is not None and m["relative_y"] is not None] for s, ms in data.load(station.folder(st)).items()} @refused def create(body): st, start, end = body.get("station", ""), body.get("start", ""), body.get("end", "") n = points.name(st, start, end) points.create(n, measured(st, *station.bounds(start, end))) return summary(n) # the plots of the page, in their order: rx, ry, the recognition error and its threshold, the image quality COLUMNS = ("relative_x", "relative_y", "error_value", "error_threshold", "peak", "flux", "sharpness", "fwhm", "dominance", "snr") @refused def dataset(n): """-> {name, station, start, end, sensors: {sensor: {t, valid, columns: {column: [value]}}}}: the COLUMNS of each point, None where the data no longer have it (a pumping or a run again); a column empty for the sensor left out.""" p, st = points.read(n), points.parts(n)[0] now = {(s, points.time(m["t"])): m for s, ms in data.load(station.folder(st)).items() for m in ms} out = {} for (s, t), v in sorted(p.items()): m, o = now.get((s, t), {}), out.setdefault(s, {"t": [], "valid": [], "columns": {c: [] for c in COLUMNS}}) o["t"].append(t[:19].replace("T", " ")) # as Plotly shows it, UTC o["valid"].append(v) for c in COLUMNS: o["columns"][c].append(m.get(c)) for o in out.values(): o["columns"] = {c: vs for c, vs in o["columns"].items() if any(x is not None for x in vs)} return {**summary(n), "sensors": out} LOCK = threading.Lock() # the requests run in threads: two marks never read and write the file at the same time @refused def mark(n, body): """{sensor, times (as the page shows them), valid} -> the summary.""" times = [t.replace(" ", "T") + "Z" for t in body.get("times", [])] with LOCK: points.mark(n, body.get("sensor", ""), times, bool(body.get("valid"))) return summary(n)