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
+94
-42
No files matched your search
+40
-32
@@ -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
|
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
|
`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
|
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
|
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
|
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
|
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)
|
log.write(line)
|
||||||
a["lines"] += digest(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 "
|
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()
|
threading.Thread(target=work, args=(name, out, prompt, session.read_text()), daemon=True).start()
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -148,27 +150,34 @@ def digest(line):
|
|||||||
return out
|
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):
|
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
|
"""The labels of the analysis into labeling/auto/<STATION>.jsonl, with their id, their review and the name of the
|
||||||
analyst changed them since the last submission, they replace those it had submitted -> their number."""
|
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"
|
labels, copy = out / "labels.jsonl", out / "work" / "submitted.jsonl"
|
||||||
text = labels.read_text(encoding="utf-8") if labels.exists() else ""
|
text = labels.read_text(encoding="utf-8") if labels.exists() else ""
|
||||||
new = []
|
new = []
|
||||||
for l in text.split("\n"):
|
for e in _labels(out):
|
||||||
if l.strip():
|
label = {k: e[k] for k in (*station.CORE, "review") if k in e}
|
||||||
e = json.loads(l)
|
station.checked(label)
|
||||||
label = {k: e[k] for k in ("start", "end", "family", "description") if k in e}
|
new.append(label | {"analysis": name})
|
||||||
station.checked(label)
|
|
||||||
new.append(label | {"analysis": name})
|
|
||||||
if copy.exists() and copy.read_text(encoding="utf-8") == text:
|
if copy.exists() and copy.read_text(encoding="utf-8") == text:
|
||||||
return len(new)
|
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:
|
with lock:
|
||||||
f = station.label_file(st, "auto") # the other lines stay as they are
|
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 [])
|
lines = [l for l in (f.read_text(encoding="utf-8").split("\n") if f.exists() else []) if l.strip()]
|
||||||
if l.strip() and json.loads(l).get("analysis") != name]
|
kept = [l for l in lines if json.loads(l).get("analysis") != name]
|
||||||
station.ids.assign(st, [json.loads(l) for l in kept] + new)
|
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 = 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")
|
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)
|
os.replace(temp, f)
|
||||||
@@ -187,31 +196,30 @@ def state(name):
|
|||||||
analyses[name] = {"state": "done" if (out / "report.md").exists() else "interrupted", "lines": lines,
|
analyses[name] = {"state": "done" if (out / "report.md").exists() else "interrupted", "lines": lines,
|
||||||
"submitted": None, "station": request["station"], "request": request}
|
"submitted": None, "station": request["station"], "request": request}
|
||||||
a, r = analyses[name], out / "report.md"
|
a, r = analyses[name], out / "report.md"
|
||||||
labels = [e for e in station.read(a["station"], "auto") if e.get("analysis") == name]
|
return {"name": name, "state": a["state"], "lines": a["lines"], "submitted": a["submitted"],
|
||||||
validated = max(e["review"] for e in labels) if labels and all(e.get("review") for e in labels) else None
|
"validated": _validated(name),
|
||||||
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,
|
"report": int(r.stat().st_mtime) if r.exists() else None,
|
||||||
"dialogue": (out / "work" / "session").exists(), "request": a["request"], "optimization": optimization(name)}
|
"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):
|
def validate(name):
|
||||||
"""The "Validate labels" button: the user agrees with all the labels of the finished analysis -> their number."""
|
"""The "Validate labels" button: the user agrees with all the labels of the finished analysis -> their number."""
|
||||||
a = state(name)
|
a = state(name)
|
||||||
if a["state"] != "done":
|
if a["state"] != "done":
|
||||||
raise station.Refusal("only a finished analysis can be validated")
|
raise station.Refusal("only a finished analysis can be validated")
|
||||||
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
out, today = FOLDER / name, datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
||||||
with lock:
|
labels = _labels(out)
|
||||||
f = station.label_file(a["request"]["station"], "auto") # the other lines stay as they are
|
if not labels:
|
||||||
lines, n = f.read_text(encoding="utf-8").split("\n") if f.exists() else [], 0
|
raise station.Refusal("no label in this analysis")
|
||||||
for i, l in enumerate(lines):
|
station.write(out / "labels.jsonl", [e | {"review": e.get("review") or today} for e in labels])
|
||||||
if l.strip() and (e := json.loads(l)).get("analysis") == name:
|
analyses[name]["submitted"] = submit(a["request"]["station"], name, out)
|
||||||
lines[i], n = json.dumps(e | {"review": today}, ensure_ascii=False), n + 1
|
return {"validated": len(labels)}
|
||||||
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():
|
def list_analyses():
|
||||||
@@ -222,7 +230,7 @@ def list_analyses():
|
|||||||
name = d.parent.name
|
name = d.parent.name
|
||||||
a = analyses.get(name) or {"state": "done" if (d.parent / "report.md").exists() else "interrupted",
|
a = analyses.get(name) or {"state": "done" if (d.parent / "report.md").exists() else "interrupted",
|
||||||
"request": json.loads(d.read_text(encoding="utf-8"))}
|
"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
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+24
-10
@@ -261,32 +261,46 @@ def check_analysis():
|
|||||||
|
|
||||||
|
|
||||||
def check_validation():
|
def check_validation():
|
||||||
# "Validate labels" (analysis.validate): review on all the labels of the analysis, the others untouched; an edit in
|
# "Validate labels" (analysis.validate): review on all the labels of the analysis, in its labels.jsonl (which says
|
||||||
# the page removes it from one label, and the analysis is no longer validated
|
# 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
|
import analysis
|
||||||
before = station.LABELS, analysis.FOLDER
|
before = station.LABELS, analysis.FOLDER
|
||||||
station.LABELS = temp_folder()
|
station.LABELS = temp_folder()
|
||||||
analysis.FOLDER = station.LABELS / "auto" / "analyses"
|
analysis.FOLDER = station.LABELS / "auto" / "analyses"
|
||||||
try:
|
try:
|
||||||
for name, report in (("NORDEND_a", True), ("NORDEND_b", False)):
|
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"}))
|
(analysis.FOLDER / name / "request.json").write_text(json.dumps({"station": "NORDEND"}))
|
||||||
if report:
|
if report:
|
||||||
(analysis.FOLDER / name / "report.md").write_text("# 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",
|
mine = [{"start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility",
|
||||||
"description": f"fog {i}", "analysis": "NORDEND_a"} for i in (1, 2)]
|
"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",
|
other = {"id": "NORDEND-3", "start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility",
|
||||||
"description": "rain"}
|
"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 analysis.state("NORDEND_a")["validated"] is None
|
||||||
assert "finished" in refuses(analysis.validate, "NORDEND_b") # interrupted, no report
|
assert "finished" in refuses(analysis.validate, "NORDEND_b") # interrupted, no report
|
||||||
assert analysis.validate("NORDEND_a") == {"validated": 2}
|
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")
|
labels = station.read("NORDEND", "auto")
|
||||||
assert [bool(e.get("review")) for e in labels] == [True, True, False], labels
|
assert {e["id"]: bool(e.get("review")) for e in labels} == \
|
||||||
assert analysis.state("NORDEND_a")["validated"] == labels[0]["review"]
|
{"NORDEND-1": True, "NORDEND-2": True, "NORDEND-3": False}, labels # the ids kept
|
||||||
station.update({"station": "NORDEND", "before": labels[0], "family": "Visibility", "description": "fog again"})
|
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
|
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")
|
assert "no label" in refuses(analysis.validate, "NORDEND_a")
|
||||||
finally:
|
finally:
|
||||||
shutil.rmtree(station.LABELS)
|
shutil.rmtree(station.LABELS)
|
||||||
|
|||||||
@@ -284,6 +284,20 @@ def label_file(station, source):
|
|||||||
return LABELS / source / f"{station}.jsonl"
|
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):
|
def read(station, source):
|
||||||
f = label_file(station, source)
|
f = label_file(station, source)
|
||||||
# split and not splitlines: the latter would also cut on the Unicode separators the text may contain
|
# 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 = f.with_name(f.name + ".tmp")
|
||||||
temp.write_text("\n".join(lines), encoding="utf-8")
|
temp.write_text("\n".join(lines), encoding="utf-8")
|
||||||
os.replace(temp, f)
|
os.replace(temp, f)
|
||||||
|
if before.get("analysis"):
|
||||||
|
analysis_label(before, label)
|
||||||
return label
|
return label
|
||||||
raise Refusal("this label is no longer in the file: reload the page")
|
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