From 3546269eafcd265a1af9b5f8aa150bb7fd9e71f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Trevi=C3=B1o?= Date: Fri, 27 Nov 2026 14:02:08 -0600 Subject: [PATCH] Review queues a scan read only in part, and an undated name one typo from a person --- models/graph.py | 14 ++++++++++++-- tests/test_ingest.py | 24 ++++++------------------ tests/test_silver.py | 9 +++------ 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/models/graph.py b/models/graph.py index 1977634..1d88c31 100644 --- a/models/graph.py +++ b/models/graph.py @@ -340,6 +340,8 @@ def run_link(p: str, node: Link): known = [f for f in ("birth_date", "sex") if f in fields] orphan = set(whole.unique_id[whole[facts].isna().all(axis=1)]) if facts else set() + # no birth date to tell her apart (a pathology scan: a name, misread or not, and a sex) + undated = set(whole.unique_id[whole["birth_date"].isna()]) if "birth_date" in fields else set() lone = set() # persons whose identifier is carried by one record only for f in ids: carried = persons.dropna(subset=[f]).groupby(["cluster_id", f]).size() @@ -349,15 +351,23 @@ def run_link(p: str, node: Link): """(the person to settle, a possible owner), or None when the pair is implausible: they contradict each other, or one typo apart is all they share (with sequential record numbers every patient has such neighbours) unless one side has no - demographics and the other's identifier stands on a single record.""" + demographics and the other's identifier stands on a single record. Names one typo + apart are rare: an undated side (a misread pathology scan) is always queued; so is the + very same identifier on a record that says nothing else (a scan read only in part).""" a, b = r.unique_id_l, r.unique_id_r sides = [(getattr(r, f"{f}_l"), getattr(r, f"{f}_r")) for f in known] if frozenset((a, b)) in decided or any( pd.notna(x) and pd.notna(y) and x != y for x, y in sides ): return None - if not reasons[int(r.match_key)].endswith("typo apart"): + reason = reasons[int(r.match_key)] + if not reason.endswith("typo apart"): return a, b + field = reason.removesuffix(" one typo apart") + if getattr(r, f"{field}_l") == getattr(r, f"{field}_r"): # the very same identifier + return (a, b) if a in undated or b not in undated else (b, a) + if field == "full_name" and (a in undated) != (b in undated): + return (a, b) if a in undated else (b, a) # names are rarely one typo apart if a in orphan and b in lone: return a, b if b in orphan and a in lone: diff --git a/tests/test_ingest.py b/tests/test_ingest.py index fb4d0fd..19e8bd2 100644 --- a/tests/test_ingest.py +++ b/tests/test_ingest.py @@ -27,15 +27,8 @@ def graph(tmp: Path, hospital_tables: dict[str, list[str]], site_url: str) -> di return { "nodes": nodes + [ - table("archivos", site_url, "ArchivosUni", schema="dbo"), - table("subrogados", site_url, "ResultadosSubrogados", schema="dbo"), - {"id": "archivos_text", "kind": "read", "inputs": ["archivos"], "column": "archivo"}, - { - "id": "subrogados_text", - "kind": "read", - "inputs": ["subrogados"], - "column": "archivo", - }, + table("resultados", site_url, "ResultadoSubrogado", schema="dbo"), + {"id": "text", "kind": "read", "inputs": ["resultados"], "column": "archivo"}, ] } @@ -58,13 +51,8 @@ def test_a_site_reaches_bronze_once_and_its_documents_are_read(platform, site, t by_place = {} for r in truth: by_place.setdefault(r["where"].split(":")[0], []).append(r) - documents = len(by_place["ArchivosUni"]) + len(by_place["ResultadosSubrogados"]) - expected = { - "archivos": len(by_place["ArchivosUni"]), - "subrogados": len(by_place["ResultadosSubrogados"]), - "archivos_text": len(by_place["ArchivosUni"]), - "subrogados_text": len(by_place["ResultadosSubrogados"]), - } + documents = len(truth) + expected = {"resultados": documents, "text": documents} for name, names in tables.items(): with sqlite3.connect(tmp_path / f"hospital_{name}.db") as con: for t in names: @@ -87,8 +75,8 @@ def test_a_site_reaches_bronze_once_and_its_documents_are_read(platform, site, t assert query(corrected) == [("64000",)] space = re.compile(r"\s+") - for table, node in (("ArchivosUni", "archivos"), ("ResultadosSubrogados", "subrogados")): - read = query(f"select id, text, ocr, error from bronze.n__site__{node}_text") + for table in ("ResultadoSubrogado",): + read = query("select id, text, ocr, error from bronze.n__site__text") assert [error for *_, error in read if error is not None] == [] by_id = {i: (space.sub(" ", txt), ocr) for i, txt, ocr, _ in read} for r in by_place[table]: diff --git a/tests/test_silver.py b/tests/test_silver.py index 24e9dbc..33d1cb1 100644 --- a/tests/test_silver.py +++ b/tests/test_silver.py @@ -2,7 +2,7 @@ can trust, and everything the platform can't decide alone is put in front of a reviewer. The source hides its answers the way the real one does (synthetic.site): one PDF per study in -two SQL Server tables, the institute's forms and what outsourced labs send back; an HPV result +one SQL Server table, the institute's forms beside what outsourced labs send back; an HPV result is a document of its own on its cytology's order; pathology comes scanned, the name surname first and no birth date; some forms say "MA." for MarĂ­a or misspell the name and leave the employee number out; some HPV reports were re-issued the other way; two women share a name. @@ -106,11 +106,8 @@ def test_a_site_becomes_silver_and_review_gets_the_rest(platform, site, tmp_path # another ("IIA" as "IA"), which nothing in the text betrays: at most 1% of a scan's # values, each listed. at = {r["where"]: r for r in truth} - place = { - r: f"ArchivosUni:{i}" for i, r in q("select id, _record from bronze.n__site__archivos") - } - place |= {r: f"ResultadosSubrogados:{i}" - for i, r in q("select id, _record from bronze.n__site__subrogados")} # fmt: skip + place = {r: f"ResultadoSubrogado:{i}" + for i, r in q("select id, _record from bronze.n__site__resultados")} # fmt: skip record_of = {w: r for r, w in place.items()} accepted: dict[str, dict[str, dict]] = {} # study -> record -> fields (from silver's long rows) for entity in FIELDS: