Validation in the analysis: labels.jsonl carries review and says whether the analysis is validated (also in the sidebar list); Validate writes it there then submits; a page edit or removal is mirrored there; a resubmission keeps the ids of unchanged labels
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
effcf8fdcb
commit
ce0200a97a
3 files changed
+92
-40
No files matched your search
+38
-30
@@ -5,7 +5,8 @@ labeling/auto/analyses/<STATION>_<start>_<end>/ its labels (labels.jsonl) and a
|
||||
next to it); at the end, the server submits its labels to labeling/auto/<STATION>.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. "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.
|
||||
`review`, the date (labeling/README.md), in labels.jsonl, then submitted; the analysis is validated when all the labels of
|
||||
its labels.jsonl have it. A label changed afterwards, by the analyst or in the page, loses 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
|
||||
@@ -122,7 +123,8 @@ def message(name, body):
|
||||
log.write(line)
|
||||
a["lines"] += digest(line)
|
||||
prompt = (f"{text}\n\n(If you change your labels, rewrite {out.relative_to(station.ROOT)}/labels.jsonl in full, the "
|
||||
"server will submit them again; if the diagnosis changes, update report.md.)")
|
||||
"server will submit them again; a label you change loses its review field; if the diagnosis changes, update "
|
||||
"report.md.)")
|
||||
threading.Thread(target=work, args=(name, out, prompt, session.read_text()), daemon=True).start()
|
||||
return {}
|
||||
|
||||
@@ -148,27 +150,34 @@ def digest(line):
|
||||
return out
|
||||
|
||||
|
||||
def _labels(out):
|
||||
"""The labels of the analysis, its labels.jsonl."""
|
||||
f = out / "labels.jsonl"
|
||||
return [json.loads(l) for l in f.read_text(encoding="utf-8").split("\n") if l.strip()] if f.exists() else []
|
||||
|
||||
|
||||
def submit(st, name, out):
|
||||
"""The labels of the analysis into labeling/auto/<STATION>.jsonl, with their id and the name of the analysis; if the
|
||||
analyst changed them since the last submission, they replace those it had submitted -> their number."""
|
||||
"""The labels of the analysis into labeling/auto/<STATION>.jsonl, with their id, their review and the name of the
|
||||
analysis; if they changed since the last submission, they replace those it had submitted, an unchanged label keeping
|
||||
its id -> their number."""
|
||||
labels, copy = out / "labels.jsonl", out / "work" / "submitted.jsonl"
|
||||
text = labels.read_text(encoding="utf-8") if labels.exists() else ""
|
||||
new = []
|
||||
for l in text.split("\n"):
|
||||
if l.strip():
|
||||
e = json.loads(l)
|
||||
label = {k: e[k] for k in ("start", "end", "family", "description") if k in e}
|
||||
for e in _labels(out):
|
||||
label = {k: e[k] for k in (*station.CORE, "review") if k in e}
|
||||
station.checked(label)
|
||||
new.append(label | {"analysis": name})
|
||||
if copy.exists() and copy.read_text(encoding="utf-8") == text:
|
||||
return len(new)
|
||||
# ponytail: removed then put back, the labels of the analysis lose the edits made in the page; keeping them would
|
||||
# require matching them one by one
|
||||
with lock:
|
||||
f = station.label_file(st, "auto") # the other lines stay as they are
|
||||
kept = [l for l in (f.read_text(encoding="utf-8").split("\n") if f.exists() else [])
|
||||
if l.strip() and json.loads(l).get("analysis") != name]
|
||||
station.ids.assign(st, [json.loads(l) for l in kept] + new)
|
||||
lines = [l for l in (f.read_text(encoding="utf-8").split("\n") if f.exists() else []) if l.strip()]
|
||||
kept = [l for l in lines if json.loads(l).get("analysis") != name]
|
||||
ids = {station.core(e): e["id"] for l in lines if (e := json.loads(l)).get("analysis") == name and "id" in e}
|
||||
for e in new:
|
||||
if station.core(e) in ids:
|
||||
e["id"] = ids.pop(station.core(e))
|
||||
station.ids.assign(st, [json.loads(l) for l in kept] + new, removed=ids.values())
|
||||
temp = f.with_name(f.name + ".tmp")
|
||||
temp.write_text("".join(l + "\n" for l in kept + [json.dumps(e, ensure_ascii=False) for e in new]), encoding="utf-8")
|
||||
os.replace(temp, f)
|
||||
@@ -187,31 +196,30 @@ 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"
|
||||
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,
|
||||
return {"name": name, "state": a["state"], "lines": a["lines"], "submitted": a["submitted"],
|
||||
"validated": _validated(name),
|
||||
"report": int(r.stat().st_mtime) if r.exists() else None,
|
||||
"dialogue": (out / "work" / "session").exists(), "request": a["request"], "optimization": optimization(name)}
|
||||
|
||||
|
||||
def _validated(name):
|
||||
"""The date the user validated the labels of the analysis (the last), or None if one of its labels.jsonl is not."""
|
||||
its = _labels(FOLDER / name)
|
||||
return max(e["review"] for e in its) if its and all(e.get("review") for e in its) else None
|
||||
|
||||
|
||||
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}
|
||||
out, today = FOLDER / name, datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
||||
labels = _labels(out)
|
||||
if not labels:
|
||||
raise station.Refusal("no label in this analysis")
|
||||
station.write(out / "labels.jsonl", [e | {"review": e.get("review") or today} for e in labels])
|
||||
analyses[name]["submitted"] = submit(a["request"]["station"], name, out)
|
||||
return {"validated": len(labels)}
|
||||
|
||||
|
||||
def list_analyses():
|
||||
@@ -222,7 +230,7 @@ def list_analyses():
|
||||
name = d.parent.name
|
||||
a = analyses.get(name) or {"state": "done" if (d.parent / "report.md").exists() else "interrupted",
|
||||
"request": json.loads(d.read_text(encoding="utf-8"))}
|
||||
out.append({"name": name, "state": a["state"]} | a["request"])
|
||||
out.append({"name": name, "state": a["state"], "validated": _validated(name)} | a["request"])
|
||||
return out
|
||||
|
||||
|
||||
|
||||
+24
-10
@@ -261,32 +261,46 @@ def 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
|
||||
# "Validate labels" (analysis.validate): review on all the labels of the analysis, in its labels.jsonl (which says
|
||||
# whether it is validated) and submitted with their id; the others untouched; an edit in the page removes it from one
|
||||
# label, there too, 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 / "work").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)]
|
||||
mine = [{"start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility",
|
||||
"description": f"fog {i}"} 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]))
|
||||
station.write(analysis.FOLDER / "NORDEND_a" / "labels.jsonl", mine)
|
||||
station.write(station.LABELS / "auto" / "NORDEND.jsonl",
|
||||
[{"id": f"NORDEND-{i}"} | e | {"analysis": "NORDEND_a"} for i, e in enumerate(mine, 1)] + [other])
|
||||
analysis.state("NORDEND_a")
|
||||
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}
|
||||
theirs = analysis._labels(analysis.FOLDER / "NORDEND_a")
|
||||
assert all(e.get("review") for e in theirs), theirs
|
||||
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 {e["id"]: bool(e.get("review")) for e in labels} == \
|
||||
{"NORDEND-1": True, "NORDEND-2": True, "NORDEND-3": False}, labels # the ids kept
|
||||
assert analysis.state("NORDEND_a")["validated"] == theirs[0]["review"]
|
||||
assert {a["name"]: a["validated"] for a in analysis.list_analyses()} == \
|
||||
{"NORDEND_a": theirs[0]["review"], "NORDEND_b": None} # the sidebar
|
||||
edited = next(e for e in labels if e["id"] == "NORDEND-1")
|
||||
station.update({"station": "NORDEND", "before": edited, "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 [e["description"] for e in analysis._labels(analysis.FOLDER / "NORDEND_a")] == ["fog again", "fog 2"]
|
||||
assert analysis.validate("NORDEND_a") == {"validated": 2} # validated again, the edit and the ids kept
|
||||
assert sorted((e["id"], e["description"]) for e in station.read("NORDEND", "auto")) == \
|
||||
[("NORDEND-1", "fog again"), ("NORDEND-2", "fog 2"), ("NORDEND-3", "rain")]
|
||||
station.write(analysis.FOLDER / "NORDEND_a" / "labels.jsonl", [])
|
||||
assert "no label" in refuses(analysis.validate, "NORDEND_a")
|
||||
finally:
|
||||
shutil.rmtree(station.LABELS)
|
||||
|
||||
@@ -284,6 +284,20 @@ def label_file(station, source):
|
||||
return LABELS / source / f"{station}.jsonl"
|
||||
|
||||
|
||||
CORE = ("start", "end", "family", "description") # what a label says; the rest (id, review, analysis) is about it
|
||||
|
||||
|
||||
def core(label):
|
||||
return tuple(label.get(k) for k in CORE)
|
||||
|
||||
|
||||
def write(f, labels):
|
||||
"""Writes the labels next to the file then replaces it: an interruption does not damage it."""
|
||||
temp = f.with_name(f.name + ".tmp")
|
||||
temp.write_text("".join(json.dumps(e, ensure_ascii=False) + "\n" for e in labels), encoding="utf-8")
|
||||
os.replace(temp, f)
|
||||
|
||||
|
||||
def read(station, source):
|
||||
f = label_file(station, source)
|
||||
# split and not splitlines: the latter would also cut on the Unicode separators the text may contain
|
||||
@@ -348,5 +362,21 @@ def replace(station, source, before, label):
|
||||
temp = f.with_name(f.name + ".tmp")
|
||||
temp.write_text("\n".join(lines), encoding="utf-8")
|
||||
os.replace(temp, f)
|
||||
if before.get("analysis"):
|
||||
analysis_label(before, label)
|
||||
return label
|
||||
raise Refusal("this label is no longer in the file: reload the page")
|
||||
|
||||
|
||||
def analysis_label(before, label):
|
||||
"""A label of an analysis edited or removed in the page: the same in the labels.jsonl of the analysis, which says
|
||||
whether it is validated (its review); a resubmission then keeps the edit."""
|
||||
f = LABELS / "auto" / "analyses" / before["analysis"] / "labels.jsonl"
|
||||
if not f.exists():
|
||||
return
|
||||
labels = [json.loads(l) for l in f.read_text(encoding="utf-8").split("\n") if l.strip()]
|
||||
for i, e in enumerate(labels):
|
||||
if core(e) == core(before):
|
||||
labels[i:i + 1] = [] if label is None else [{k: label[k] for k in (*CORE, "review") if k in label}]
|
||||
write(f, labels)
|
||||
return
|
||||
Reference in new issue
Block a user