"""Readable, self-contained HTML screening report with provenance.""" from __future__ import annotations import hashlib, html, json, datetime from . import __version__ DISCLAIMER = ("Educational screening prototype for a university assignment. It is NOT a clinical " "enrollment decision tool and must not be used to include or exclude real patients.") _COL = {"included": "#1b7350", "not excluded": "#1b7350", "not included": "#b42318", "excluded": "#b42318", "not enough information": "#8a5a00", "not applicable": "#555", "invalid output": "#b42318"} def provenance(note, criteria, settings): blob = json.dumps({"note": note, "criteria": criteria}, sort_keys=True).encode() return {"generated_utc": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds"), "app_version": __version__, "input_sha256": hashlib.sha256(blob).hexdigest(), **settings} def html_report(trial_name, sentences, results, verdict, prov): e = html.escape rows = [] for i, r in enumerate(results, 1): ev = "
".join(f"[{sid}] {e(t)}" for sid, t in zip(r["evidence"], r["evidence_text"])) or "none cited" rows.append(f"{i}{e(r['criterion_type'])}{e(r['criterion_text'])}" f"{e(r['label'])}" f"{ev}{e(r['explanation'])}") note = "".join(f"
  • {e(s)}
  • " for i, s in sentences) missing = [r for r in results if r["label"] == "not enough information"] miss = "".join(f"
  • {e(r['criterion_text'])}
  • " for r in missing) or "
  • None
  • " return f"""Eligibility screening report

    Eligibility screening report

    Notice: {e(DISCLAIMER)}

    Trial

    {e(trial_name or 'Unnamed trial')}

    Preliminary outcome (rule-based aggregation)

    {e(verdict['verdict'])} — {verdict['n_criteria']} criteria, {verdict['n_blocking']} blocking, {verdict['n_unknown']} undetermined.
    Rule: {e(verdict['rule'])} This aggregation is not a ground-truth label; the dataset only labels individual criteria.

    Missing information

    Criterion-level results

    {''.join(rows)}
    #TypeCriterionPredictionEvidence (sentence ID)Explanation

    Patient record (sentence IDs)

      {note}

    Provenance

    {e(json.dumps(prov, indent=1))}
    """