91 lines
4.5 KiB
Python
91 lines
4.5 KiB
Python
"""Phase 4.5: a CaMa population from Synthea (tests/fixtures/synthea/modules/
|
|
breast_screening.json) comes in through the FHIR door, by the API: every mammogram, biopsy
|
|
and first-course treatment lands in silver, each biopsy's results (sent as several reports of
|
|
the same day) as one biopsy, and gold's CaMa timeline carries the stage and the treatments the
|
|
survival model reads."""
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
|
|
from test_conectar import ok, wait_for
|
|
|
|
from conftest import ROOT
|
|
|
|
SYNTHEA = os.environ.get("SYNTHEA_JAR", "/opt/synthea/synthea.jar")
|
|
MODULES = ROOT / "tests" / "fixtures" / "synthea" / "modules"
|
|
DOOR = ROOT / "tests" / "fixtures" / "pipelines" / "fhir_door_cama.json"
|
|
STUDY = "https://isssteleon.org/fhir/CodeSystem/study"
|
|
FIELD = "https://isssteleon.org/fhir/CodeSystem/catalog-field"
|
|
WOMEN = 300
|
|
|
|
|
|
def population(out: Path, women: int = WOMEN, seed: int = 7) -> list[dict]:
|
|
"""Synthea's FHIR R4 Bundles for `women` women of 40 to 89 (fixed seeds), with only the
|
|
resources the door reads."""
|
|
subprocess.run(
|
|
["java", "-jar", SYNTHEA, "-p", str(women), "-s", str(seed), "-cs", str(seed),
|
|
"-g", "F", "-a", "40-89", "-d", str(MODULES), "-m", "breast_screening",
|
|
"--exporter.baseDirectory", str(out), "--exporter.fhir.export", "true",
|
|
"--exporter.hospital.fhir.export", "false",
|
|
"--exporter.practitioner.fhir.export", "false", "--exporter.years_of_history", "8",
|
|
"--exporter.fhir.included_resources", "Patient,Encounter,DiagnosticReport,Observation"],
|
|
check=True, capture_output=True, cwd=out.parent,
|
|
) # fmt: skip
|
|
return [json.loads(p.read_text()) for p in sorted((out / "fhir").glob("*.json"))]
|
|
|
|
|
|
def _sent(bundles: list[dict]) -> Counter:
|
|
"""What the bundles hold: mammograms, biopsies (one per patient and day), treatments, and
|
|
the treatments of patients with a mammogram or a biopsy (CaMa's timeline is theirs: the
|
|
export window can cut a study off and keep the treatment after it)."""
|
|
found: Counter = Counter()
|
|
biopsies = set()
|
|
for b in bundles:
|
|
studied = False
|
|
treated = 0
|
|
for e in b["entry"]:
|
|
r = e["resource"]
|
|
codes = [c for c in (r.get("code") or {}).get("coding", []) if c["system"] == STUDY]
|
|
codes += [c for t in r.get("type", []) for c in t.get("coding", [])
|
|
if c["system"] == STUDY] # fmt: skip
|
|
for c in codes:
|
|
if c["code"] == "breast_biopsy":
|
|
biopsies.add((r["subject"]["reference"], r["effectiveDateTime"][:10]))
|
|
else:
|
|
found[c["code"]] += 1
|
|
studied |= c["code"] in ("mammography", "breast_biopsy")
|
|
treated += c["code"] == "breast_treatment"
|
|
if r["resourceType"] == "Patient" and r.get("deceasedDateTime"):
|
|
found["death"] += 1
|
|
found["timeline_treatment"] += treated if studied else 0
|
|
found["breast_biopsy"] = len(biopsies)
|
|
return found
|
|
|
|
|
|
def test_a_synthea_cama_population_comes_in_through_the_fhir_door(api, platform, tmp_path):
|
|
bundles = population(tmp_path / "synthea")
|
|
sent = _sent(bundles)
|
|
assert sent["breast_biopsy"] > 50 and sent["breast_treatment"] > 20 and sent["death"] > 0
|
|
|
|
receiving = DOOR.read_text().replace("{sender}", "synthea")
|
|
ok(api.put("/ingest/pipelines/synthea", json=json.loads(receiving)))
|
|
batch = {"resourceType": "Bundle", "type": "batch",
|
|
"entry": [e for b in bundles for e in b["entry"]]} # fmt: skip
|
|
answer = api.post("/cama/fhir?sender=synthea", json=batch)
|
|
assert answer.status_code == 202, answer.text[:500]
|
|
wait_for(api, answer.headers["location"].rsplit("/", 1)[1], 1800)
|
|
|
|
for study in ("mammography", "breast_biopsy", "breast_treatment"):
|
|
assert ok(api.get("/review/held", params={"entity": study})) == [], study
|
|
assert platform.count(f"silver.{study}") == sent[study], study
|
|
q = platform.query
|
|
# Each invasive biopsy holds all of its results; gold's timeline has them and the course.
|
|
assert q("select count(*) from silver.breast_biopsy where histology = 'invasive' and "
|
|
"(stage is null or her2 is null or grade is null)")[0][0] == 0 # fmt: skip
|
|
kinds = dict(q("select type, count(*) from gold.cama_events where type in "
|
|
"('stage', 'her2', 'treatment', 'death') group by type")) # fmt: skip
|
|
assert kinds.get("stage", 0) > 0 and kinds.get("treatment", 0) == sent["timeline_treatment"]
|