From b31371a241559410f4f806190c9ad8d401412de8 Mon Sep 17 00:00:00 2001 From: maxime Date: Tue, 17 Nov 2026 10:12:14 +0100 Subject: [PATCH] Validate labels: the user agrees with all the labels of a finished analysis, each gets review (the date) A change of the labels afterwards (an edit in the page, the analyst after a message) removes it. Co-Authored-By: Claude Opus 5.5 --- app/server/analysis.py | 27 +++++++++++++++++++++++++-- app/server/check.py | 36 +++++++++++++++++++++++++++++++++++- app/server/server.py | 5 +++++ app/web/src/pages/Data.jsx | 8 +++++++- 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/app/server/analysis.py b/app/server/analysis.py index a011fc9..a625c40 100644 --- a/app/server/analysis.py +++ b/app/server/analysis.py @@ -4,7 +4,8 @@ by `claude -p`, non-interactive, on a range chosen in the page, with the user's labeling/auto/analyses/__/ its labels (labels.jsonl) and a diagnostic report (report.md, its figures next to it); at the end, the server submits its labels to labeling/auto/.jsonl, marked with their analysis (field `analysis`). Then the user can write to it: the same session resumes (`claude -p --resume`), and its labels, if they -change, replace those it had submitted. +change, replace those it had submitted. "Validate labels": the user agrees with all the labels of the analysis, each gets +`review`, the date (labeling/README.md); labels the analyst changes afterwards are submitted again without it. On a finished analysis, "Optimize" launches `/optimize` in Optimizer (optimizer/, the skill that, from the analysis, improves the detectors on its range, its labels as truth, then brings the analyst's scripts into the reading tools, tools/); one optimization at a time, no analysis or message meanwhile; the summary of the session @@ -186,11 +187,33 @@ def state(name): analyses[name] = {"state": "done" if (out / "report.md").exists() else "interrupted", "lines": lines, "submitted": None, "station": request["station"], "request": request} a, r = analyses[name], out / "report.md" - return {"name": name, "state": a["state"], "lines": a["lines"], "submitted": a["submitted"], + labels = [e for e in station.read(a["station"], "auto") if e.get("analysis") == name] + validated = max(e["review"] for e in labels) if labels and all(e.get("review") for e in labels) else None + return {"name": name, "state": a["state"], "lines": a["lines"], "submitted": a["submitted"], "validated": validated, "report": int(r.stat().st_mtime) if r.exists() else None, "dialogue": (out / "work" / "session").exists(), "request": a["request"], "optimization": optimization(name)} +def validate(name): + """The "Validate labels" button: the user agrees with all the labels of the finished analysis -> their number.""" + a = state(name) + if a["state"] != "done": + raise station.Refusal("only a finished analysis can be validated") + today = datetime.now(timezone.utc).strftime("%Y-%m-%d") + with lock: + f = station.label_file(a["request"]["station"], "auto") # the other lines stay as they are + lines, n = f.read_text(encoding="utf-8").split("\n") if f.exists() else [], 0 + for i, l in enumerate(lines): + if l.strip() and (e := json.loads(l)).get("analysis") == name: + lines[i], n = json.dumps(e | {"review": today}, ensure_ascii=False), n + 1 + if not n: + raise station.Refusal(f"no label of this analysis in labeling/auto/{f.name}") + temp = f.with_name(f.name + ".tmp") + temp.write_text("\n".join(lines), encoding="utf-8") + os.replace(temp, f) + return {"validated": n} + + def list_analyses(): """The history of the analyses (the sidebar), the most recent first: those of the server with their state, the others read on disk (done with a report, otherwise interrupted: an analysis stops with the server).""" diff --git a/app/server/check.py b/app/server/check.py index 2f9a8d6..9863042 100644 --- a/app/server/check.py +++ b/app/server/check.py @@ -260,7 +260,41 @@ def check_analysis(): analysis.analyses.clear() -CHECKS = [check_server, check_station, check_optimization, check_analysis] +def check_validation(): + # "Validate labels" (analysis.validate): review on all the labels of the analysis, the others untouched; an edit in + # the page removes it from one label, and the analysis is no longer validated + import analysis + before = station.LABELS, analysis.FOLDER + station.LABELS = temp_folder() + analysis.FOLDER = station.LABELS / "auto" / "analyses" + try: + for name, report in (("NORDEND_a", True), ("NORDEND_b", False)): + (analysis.FOLDER / name).mkdir(parents=True) + (analysis.FOLDER / name / "request.json").write_text(json.dumps({"station": "NORDEND"})) + if report: + (analysis.FOLDER / name / "report.md").write_text("# report") + mine = [{"id": f"NORDEND-{i}", "start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility", + "description": f"fog {i}", "analysis": "NORDEND_a"} for i in (1, 2)] + other = {"id": "NORDEND-3", "start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility", + "description": "rain"} + (station.LABELS / "auto" / "NORDEND.jsonl").write_text("".join(json.dumps(e) + "\n" for e in mine + [other])) + assert analysis.state("NORDEND_a")["validated"] is None + assert "finished" in refuses(analysis.validate, "NORDEND_b") # interrupted, no report + assert analysis.validate("NORDEND_a") == {"validated": 2} + labels = station.read("NORDEND", "auto") + assert [bool(e.get("review")) for e in labels] == [True, True, False], labels + assert analysis.state("NORDEND_a")["validated"] == labels[0]["review"] + station.update({"station": "NORDEND", "before": labels[0], "family": "Visibility", "description": "fog again"}) + assert analysis.state("NORDEND_a")["validated"] is None + (station.LABELS / "auto" / "NORDEND.jsonl").write_text(json.dumps(other) + "\n") + assert "no label" in refuses(analysis.validate, "NORDEND_a") + finally: + shutil.rmtree(station.LABELS) + station.LABELS, analysis.FOLDER = before + analysis.analyses.clear() + + +CHECKS = [check_server, check_station, check_optimization, check_analysis, check_validation] if __name__ == "__main__": name = sys.argv[1] if len(sys.argv) > 1 else None diff --git a/app/server/server.py b/app/server/server.py index 90643cb..9cae80c 100644 --- a/app/server/server.py +++ b/app/server/server.py @@ -147,6 +147,11 @@ def analysis_report(name: str, dark: bool = False): return HTMLResponse(analysis.report(name, dark)) +@app.post("/api/analyses/{name}/validate") +def validate_analysis(name: str): + return analysis.validate(name) + + @app.post("/api/analyses/{name}/optimize") def optimize_analysis(name: str): return analysis.optimize(name) diff --git a/app/web/src/pages/Data.jsx b/app/web/src/pages/Data.jsx index c9aae1a..6377fad 100644 --- a/app/web/src/pages/Data.jsx +++ b/app/web/src/pages/Data.jsx @@ -539,6 +539,9 @@ function Analysis({ name, dark, done, close }) { async function optimize() { if (await call(`/api/analyses/${name}/optimize`, "POST") != null) setTurn(t => t + 1); } + async function validate() { + if (await call(`/api/analyses/${name}/validate`, "POST") != null) { setTurn(t => t + 1); done(); } + } if (!a) return ; const d = a.request, o = a.optimization, busy = a.state === "running", optimizing = o?.state === "running"; const end = new Date(Date.parse(d.end.slice(0, 10)) + 864e5).toISOString().slice(0, 10); @@ -552,6 +555,9 @@ function Analysis({ name, dark, done, close }) { {busy && } + {a.validated ? labels validated {a.validated} + : a.state === "done" && + } {a.state === "done" && } @@ -560,7 +566,7 @@ function Analysis({ name, dark, done, close }) { {d.hints && Hints: {d.hints}} {a.state === "interrupted" && Stopped with the server before the end.{a.dialogue ? " Its session is kept: write “continue” below to resume it where it left off." : ""}} - {a.submitted !== null && a.state === "done" && {a.submitted} label(s) of this analysis in labeling/auto/{d.station}.jsonl, to review.} + {a.submitted !== null && a.state === "done" && {a.submitted} label(s) of this analysis in labeling/auto/{d.station}.jsonl{a.validated ? ", validated" : ", to validate"}.} {a.report && } {a.dialogue && (