Files
WebApi/webapi/fhir_cacu.py
T

71 lines
3.1 KiB
Python

"""CaCu's FHIR operations on a patient: `$eligibility` (gold.cacu_eligibility) and, off
unless CACU_NCI_REFERENCE=1, the experimental `$risk-series` (the models' and the NCI's)
and `$reference-risk` (disabled: its series are `$risk-series`')."""
import os
from datetime import date
from fastapi import APIRouter, HTTPException, Query
from fastapi.responses import Response
from core import fhir as f
from core import models
from core.cacu import parameters, reference, risk_series
from core.domains import Domain
from persistance import fhir as reads
from webapi.fhir import _OK, _cutoff, _risks, fhir
router = APIRouter(prefix="/cacu/fhir/Patient/{id}", tags=["fhir"])
NCI_REFERENCE = "CACU_NCI_REFERENCE"
CUTOFF = Query(None, description="Only what was reported by this day")
SAMPLE = Query(None, description="The cytology's sample day (default: her latest)")
def _person(id: str) -> dict:
(found,) = reads.rows("cacu", "person", id=id) or [None]
if found is None:
raise HTTPException(404, f"no Patient/{id}")
return found
def _reference_on():
if os.environ.get(NCI_REFERENCE) != "1":
raise HTTPException(404, "the NCI reference comparison is not enabled")
@router.get("/$eligibility", responses=_OK, response_class=Response)
def cacu_eligibility(
id: str,
scenario: str | None = Query(None, description="One CaCu scenario (default: every one)"),
cutoff: date | None = Query(None, description="Only index dates up to this day"),
) -> Response:
"""Her indexed candidates of each CaCu cohort: eligible, or every reason she is not."""
if scenario is not None and scenario not in f.CODE_SYSTEMS["cacu-eligibility-scenario"][1]:
raise HTTPException(422, f"no CaCu scenario {scenario}")
_person(id)
day = _cutoff(Domain.CACU, cutoff)
found = reads.rows("cacu", "eligibility", id=id, cutoff=day.isoformat(), scenario=scenario)
return fhir(parameters.eligibility(found, id, day))
@router.get("/$risk-series", responses=_OK, response_class=Response)
def cacu_risk_series(id: str, cutoff: date | None = CUTOFF, sample: date | None = SAMPLE):
"""EXPERIMENTAL. The risk series her record can draw: the models' and the NCI tables',
by outcome, with their published intervals."""
_reference_on()
person, day, one = _person(id), _cutoff(Domain.CACU, cutoff), {"persons": [id]}
facts = {"birth_date": person["birth_date"], "master": reads.rows("cacu", "reference_master"),
**{k: reads.rows("cacu", f"reference_{k}", **one)
for k in ("cytologies", "biopsies", "events", "requirements")}} # fmt: skip
resolution = reference.resolve(facts, day, sample)
risks, _ = _risks(Domain.CACU, [id], day)
found = risk_series.series(resolution, risks, day, models.title)
return fhir(parameters.risk_series(found, id, day))
@router.get("/$reference-risk", responses=_OK, response_class=Response)
def cacu_reference_risk(id: str, cutoff: date | None = CUTOFF, sample: date | None = SAMPLE):
"""Disabled: the NCI series, the master table's included, are in `$risk-series`."""
_reference_on()
raise HTTPException(503, "$reference-risk is disabled; the NCI series are in $risk-series")