Automático drafts whole pipelines (layouts, joins, codes) and suggests review answers

This commit is contained in:
Roberto Treviño
2026-12-03 10:42:18 -06:00
parent 1a1ea5c7a9
commit a2a9eb7af3
11 changed files with 814 additions and 42 deletions
+73
View File
@@ -0,0 +1,73 @@
"""What Automático checks without asking the model: documents grouped by the words their
layout prints, a proposed pattern kept only when it captures what the model read, a
column's date format, and the site's codes as a ConceptMap the platform accepts."""
from core import concept_map
from core.drafting import codes_schema, layout_schema, table_schema
from models.draft import fields
from models.draft.documents import layouts
FORM = (
"SOLICITUD DE CITOLOGÍA CERVICAL\nNombre: {name}\nFecha de recepción: {date}\n"
"Calidad de la muestra: SATISFACTORIO\nResultado: {result}\nObservaciones: ninguna"
)
MAMMOGRAM = (
"REPORTE DE MASTOGRAFÍA BILATERAL\nPaciente: {name}\nFecha del estudio: {date}\n"
"Hallazgos: densidad mamaria tipo B, sin masas\nBI-RADS: 2"
)
def test_documents_of_one_layout_group_together_whoever_they_are_about():
texts = [
FORM.format(name="ANA LÓPEZ RUIZ", date="02/03/2025", result="NEGATIVO"),
MAMMOGRAM.format(name="LUISA ORTIZ MENA", date="11/04/2025"),
FORM.format(name="ROSA DÍAZ PAZ", date="15/06/2025", result="LSIL"),
MAMMOGRAM.format(name="MARTA SOLÍS GIL", date="01/07/2025"),
FORM.format(name="EVA RUIZ CANO", date="20/08/2025", result="ASC-US"),
]
found = layouts(texts)
assert found[0] == found[2] == found[4] != found[1] == found[3]
def test_a_pattern_is_kept_only_if_it_captures_the_values_read():
texts = [
FORM.format(name="ANA LÓPEZ RUIZ", date="02/03/2025", result="NEGATIVO"),
FORM.format(name="ROSA DÍAZ PAZ", date="15/06/2025", result="LSIL"),
]
read = [
(texts[0], {"sample_date": "2025-03-02", "result": "NILM"}),
(texts[1], {"sample_date": "2025-06-15", "result": "LSIL"}),
]
codes = fields.Codes("sitio")
entity = "cervical_cytology"
date = r"recepci[oó]n:\s*(\d{2}/\d{2}/\d{4})"
assert fields.checks(entity, "sample_date", date, "%d/%m/%Y", read, codes)
assert not fields.checks(entity, "sample_date", date, "%m/%d/%Y", read, codes) # 15th month
assert not fields.checks(entity, "sample_date", r"(\d{2}/\d{2})", None, read, codes)
assert not fields.checks(entity, "sample_date", r"recepci[oó]n:", None, read, codes) # no group
assert not fields.checks(entity, "result", r"resultado:(\s*)", None, read, codes) # nothing
# A coded field: what the site writes becomes its ConceptMap, unless it is the code.
assert fields.checks(entity, "result", r"resultado:\s*(\S+)", None, read, codes)
assert codes.table() == {(entity, "result"): {"negativo": "NILM"}}
# A pattern taking text that means two things is the wrong text.
assert not fields.checks(entity, "result", r"(calidad)", None, read, fields.Codes("x"))
found = codes.concept_map()
assert concept_map.problems(found) == []
assert concept_map.table([found], "result") == {"negativo": "NILM"}
def test_a_columns_dates_get_the_format_every_value_parses_with():
assert fields.date_format(["02/03/2025", "15/06/2025"]) == "%d/%m/%Y"
assert fields.date_format(["2025-03-02", None]) == "%Y-%m-%d"
assert fields.date_format(["mañana"]) is None
def test_answers_are_held_to_the_platforms_terms():
layout = layout_schema()["properties"]["entity"]["enum"]
assert "cervical_cytology" in layout and "none" in layout and "identity" not in layout
codes = codes_schema(["NEGATIVO", "BAJO GRADO"], ["NILM", "LSIL"])
one = codes["$defs"]["Code"]["properties"]
assert one["raw"]["enum"] == ["NEGATIVO", "BAJO GRADO"]
assert {"enum": ["NILM", "LSIL"], "type": "string"} in one["code"]["anyOf"]
table = table_schema(["num", "nombre"])
assert table["$defs"]["Who"]["properties"]["column"]["enum"] == ["num", "nombre"]
+42
View File
@@ -0,0 +1,42 @@
"""Automático's judgements that need no model: a layout whose title the text layer
garbles is still marked by a line it prints, and a report that leaves its sample's date to
the order it answers is paired with that order by the column both carry."""
from core.pipeline import Join, Route
from models.draft.documents import printed_line
from models.draft.joins import Kind, joined, partner
HPV = (
"No.Proceso {n}\nPaciente {name}\nDDDDEEEETTTTEEEECCCCCCCCIIIIOOOONNNN\n"
"ESPECIMEN: CEPILLADO CERVICOVAGINAL\nVVVVPPPPHHHH . . . . . NNNNEEEEGGGG"
)
FORM = "SOLICITUD DE CITOLOGÍA CERVICAL\nNombre: {name}\nFecha de recepción: 0{n}/03/2025"
def row(record: str, text: str, order) -> dict:
return {"_record": record, "_file": f"{record:0>64}", "text": text, "format": "pdf",
"orden_servicio_id": order, "tipo": "resultado"} # fmt: skip
def test_a_layout_is_marked_by_a_line_only_it_prints():
hpv = [row(f"h{n}", HPV.format(n=n, name=f"PACIENTE {n}"), n) for n in range(5)]
forms = [row(f"c{n}", FORM.format(n=n, name=f"PACIENTE {n}"), n) for n in range(5)]
assert printed_line(hpv, forms) == "ESPECIMEN: CEPILLADO CERVICOVAGINAL"
assert printed_line(forms, forms[:1] + hpv) is None # what it prints, another prints too
def test_a_report_is_read_with_the_order_the_same_column_pairs_it_with():
reports = Kind("cervical_cytology", "VPH", "cervicovaginal", [],
[row(f"h{n}", HPV.format(n=n, name="X"), n) for n in range(4)]) # fmt: skip
orders = Kind("cervical_cytology", "Citología", "citología", [],
[row(f"c{n}", FORM.format(n=n, name="X"), n) for n in range(1, 6)]) # fmt: skip
# `tipo` and `format` pair every document with every other: they pair nothing.
assert partner(reports, [reports, orders]) == (orders, "orden_servicio_id")
orders.route = Route(id="citologia", label="Citología", inputs=["texto"])
node = Join(id="vph_con_citologia", inputs=["vph", "citologia"],
on=[("orden_servicio_id", "orden_servicio_id")], how="left") # fmt: skip
found = dict((r["_record"], t) for t, r in joined(node, reports.everyone, orders))
assert found["h1"].startswith("SOLICITUD DE CITOLOGÍA") and "Proceso 1" in found["h1"]
assert found["h0"].startswith("No.Proceso 0") # no order: its own text alone
lone = Kind("cervical_cytology", "VPH", "x", [], reports.everyone[:1])
assert partner(lone, [lone, orders]) is None