A person nothing names is held for review; a scan may misread one valid code as another

This commit is contained in:
Roberto Treviño
2026-11-27 08:46:05 -06:00
parent 999f0fb926
commit 4ff4a3a7d8
2 changed files with 40 additions and 15 deletions
+32 -12
View File
@@ -6,6 +6,7 @@ two SQL Server tables, the institute's forms and what outsourced labs send back;
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.
Medical notes (the colposcopy clinic's, oncology's) and death notes are the institute's forms.
"""
import json
@@ -23,6 +24,7 @@ FIELDS = {
"er", "pr", "her2", "ki67_pct", "size_mm", "stage"),
"breast_treatment": ("start_date", "kind"),
"clinical_note": ("note_date", "specialty", "icd10"),
"death": ("death_date",),
} # fmt: skip
BOOLS = {"hpv", "hpv_16", "hpv_18"}
NUMBERS = {"ki67_pct", "size_mm"}
@@ -57,17 +59,27 @@ def test_a_site_becomes_silver_and_review_gets_the_rest(platform, site, tmp_path
# Persons: nobody is merged with somebody else, except the two women of one name, joined
# by a pathology report (it shows no birth date) and held (torn) until a reviewer says
# whose report it is.
# A person nothing names (a scan whose name line wasn't read) is held as well: there is
# nobody to join her to.
members: dict[str, set] = {}
held, person_of = set(), {}
for record, person, why in q("select _record, person_id, _held from bronze.n__site__persons"):
held, nameless, named, person_of = set(), set(), set(), {}
for record, person, why, *identifiers in q(
"select _record, person_id, _held, full_name, birth_date, record_number "
"from bronze.n__site__persons"
):
members.setdefault(person, set()).add(of[record])
person_of[record] = person
if why:
if any(identifiers):
named.add(person)
if why and "no name" in why:
nameless.add(person)
elif why:
held.add(person)
assert nameless == set(members) - named, nameless
torn = {p for p, who in members.items() if len(who) > 1}
assert len(torn) == 1 and torn == held, (torn, held)
assert members[next(iter(torn))] == set(site.meta["namesakes"])
patient = {p: next(iter(who)) for p, who in members.items() if p not in torn}
patient = {p: next(iter(who)) for p, who in members.items() if p not in torn | nameless}
# Every woman split into several persons (a misspelled form, say) is in the review
# queue, joined by candidates.
@@ -88,9 +100,11 @@ def test_a_site_becomes_silver_and_review_gets_the_rest(platform, site, tmp_path
frontier -= reached
assert persons <= reached, (x, persons)
# Every value accepted from a document is what the document says; and most of what the
# documents say is read (a scan's columns can come back out of order, and a value its
# pattern doesn't find is left out, never guessed).
# Every value accepted from a typed document is what the document says; and most of what
# the documents say is read (a scan's columns can come back out of order, and a value its
# pattern doesn't find is left out, never guessed). A scan can misread one valid code as
# 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")
@@ -108,22 +122,28 @@ def test_a_site_becomes_silver_and_review_gets_the_rest(platform, site, tmp_path
got = next((v for v in values if v is not None), None)
mine.setdefault(record, {})[field] = value(field, got)
assert mine, entity
stated = read = 0
stated = read = scanned = 0
missed: Counter = Counter()
wrong = []
for record, got in mine.items():
t = at[place[record]]
if not t.get("flipped"):
truly = {f: v for f, v in said(t, entity).items() if v is not None}
assert got == {f: value(f, truly.get(f)) for f in got}, (t["where"], got)
differ = {f: (v, value(f, truly.get(f))) for f, v in got.items()
if v != value(f, truly.get(f))} # fmt: skip
assert t.get("scanned") or not differ, (t["where"], differ)
wrong += [(t["where"], differ)] if differ else []
scanned += len(got) if t.get("scanned") else 0
stated, read = stated + len(truly), read + len(got)
missed.update(set(truly) - set(got))
assert read >= 0.95 * stated, (entity, read, stated, missed)
assert len(wrong) <= 0.01 * scanned, (entity, wrong)
# A row a rule held because of a value (a scan misread) is held because it is wrong:
# never loaded, and a reviewer sees it.
misread = set()
for record, entity, raw in q("select _record, study, _raw from bronze.staged__held"):
if record in place and person_of.get(record) not in torn:
if record in place and person_of.get(record) not in torn | nameless:
t, got = at[place[record]], json.loads(raw)
truly = said(t, entity)
assert {f: value(f, got.get(f)) for f in truly} != {
@@ -140,7 +160,7 @@ def test_a_site_becomes_silver_and_review_gets_the_rest(platform, site, tmp_path
for r in truth:
record = record_of[r["where"]]
got = accepted[entity].get(record)
if got is None or person_of[record] in torn or record in misread:
if got is None or person_of[record] in torn | nameless or record in misread:
continue
at_key = (person_of[record], *(got.get(k) for k in key))
merged.setdefault(at_key, {}).update(got)
@@ -150,6 +170,6 @@ def test_a_site_becomes_silver_and_review_gets_the_rest(platform, site, tmp_path
for k, row in merged.items() if k not in disputed} # fmt: skip
rows = q(f"select person_id, {', '.join(fields)} from silver.{entity}")
got = {(p, *(value(f, v) for f, v in zip(fields, values, strict=True)))
for p, *values in rows if p not in torn} # fmt: skip
for p, *values in rows if p not in torn | nameless} # fmt: skip
assert got == expected, (entity, sorted(got - expected, key=str)[:4],
sorted(expected - got, key=str)[:4]) # fmt: skip