File size: 12,710 Bytes
dab7db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""Synthetic lab-report generator.

Produces realistic-looking lab reports (PDF, plus degraded "phone photo" PNGs) together
with ground-truth JSON. Everything is fictional: lab names, patients and values.

Variation that matters for robustness:
  * locale: English (US-style units) and French (Tunisia/France-style units: g/L glucose,
    G/L white cells, decimal commas)
  * three layouts (clean table, dotted lines, compact two-column header)
  * range formats: "12.0 - 16.0", "(12,0-16,0)", "< 2,00", "> 0,40", "[70 – 99]"
  * optional H/L markers next to abnormal values
  * value distribution: mostly normal, some low/high, a few critical
"""
from __future__ import annotations

import json
import random
from dataclasses import asdict, dataclass
from datetime import date, timedelta
from pathlib import Path

from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas

from ..catalog import load_catalog
from ..flagger import flag_result
from ..models import Result

FONT_DIR = Path("/usr/share/fonts/truetype/dejavu")
pdfmetrics.registerFont(TTFont("DejaVu", str(FONT_DIR / "DejaVuSans.ttf")))
pdfmetrics.registerFont(TTFont("DejaVu-Bold", str(FONT_DIR / "DejaVuSans-Bold.ttf")))

# Units as printed on reports, per locale.
LOCALE_UNITS = {
    "en": {
        "hemoglobin": "g/dL", "hematocrit": "%", "rbc": "10^6/µL", "mcv": "fL", "wbc": "10^3/µL",
        "platelets": "10^3/µL", "glucose": "mg/dL", "hba1c": "%", "creatinine": "mg/dL",
        "urea": "mg/dL", "sodium": "mmol/L", "potassium": "mmol/L", "calcium": "mg/dL",
        "cholesterol_total": "mg/dL", "ldl": "mg/dL", "hdl": "mg/dL", "triglycerides": "mg/dL",
        "alt": "U/L", "ast": "U/L", "tsh": "mIU/L", "ferritin": "ng/mL", "crp": "mg/L",
        "vitamin_d": "ng/mL",
    },
    "fr": {
        "hemoglobin": "g/dL", "hematocrit": "%", "rbc": "T/L", "mcv": "fL", "wbc": "G/L",
        "platelets": "G/L", "glucose": "g/L", "hba1c": "%", "creatinine": "mg/L",
        "urea": "g/L", "sodium": "mmol/L", "potassium": "mmol/L", "calcium": "mmol/L",
        "cholesterol_total": "g/L", "ldl": "g/L", "hdl": "g/L", "triglycerides": "g/L",
        "alt": "UI/L", "ast": "UI/L", "tsh": "µUI/mL", "ferritin": "ng/mL", "crp": "mg/L",
        "vitamin_d": "ng/mL",
    },
}

SECTIONS = {
    "blood_count": {"en": "COMPLETE BLOOD COUNT", "fr": "HÉMOGRAMME (NFS)"},
    "metabolic": {"en": "GLUCOSE METABOLISM", "fr": "MÉTABOLISME GLUCIDIQUE"},
    "kidney": {"en": "KIDNEY FUNCTION", "fr": "FONCTION RÉNALE"},
    "electrolytes": {"en": "ELECTROLYTES", "fr": "IONOGRAMME"},
    "lipids": {"en": "LIPID PANEL", "fr": "BILAN LIPIDIQUE"},
    "liver": {"en": "LIVER ENZYMES", "fr": "BILAN HÉPATIQUE"},
    "thyroid": {"en": "THYROID", "fr": "THYROÏDE"},
    "iron": {"en": "IRON STUDIES", "fr": "BILAN MARTIAL"},
    "inflammation": {"en": "INFLAMMATION", "fr": "INFLAMMATION"},
    "vitamins": {"en": "VITAMINS", "fr": "VITAMINES"},
}

LABS = ["Laboratoire Les Oliviers", "Medina Biolab", "Laboratoire du Sahel",
        "Cap Bon Diagnostics", "Laboratoire El Amen", "Nour Medical Lab"]
FIRST = ["Amira", "Youssef", "Salma", "Karim", "Ines", "Mehdi", "Rania", "Omar", "Leila", "Sami"]
LAST = ["Ben Salah", "Trabelsi", "Jaziri", "Haddad", "Mansour", "Gharbi", "Chaabane", "Ayari"]

HEADERS = {
    "en": {"title": "LABORATORY REPORT", "patient": "Patient", "date": "Collection date",
           "test": "Test", "result": "Result", "unit": "Unit", "ref": "Reference range",
           "footer": "Results to be interpreted by your physician."},
    "fr": {"title": "COMPTE RENDU D'ANALYSES", "patient": "Patient(e)", "date": "Date de prélèvement",
           "test": "Analyse", "result": "Résultat", "unit": "Unité", "ref": "Valeurs de référence",
           "footer": "Résultats à interpréter par votre médecin traitant."},
}


@dataclass
class TruthRow:
    test_key: str
    printed_name: str
    value: float
    value_str: str
    unit: str
    ref_low: float | None
    ref_high: float | None
    ref_str: str
    expected_flag: str


def _decimals(high: float) -> int:
    if high < 10:
        return 2
    if high < 100:
        return 1
    return 0


def _fmt(x: float, d: int, locale: str) -> str:
    s = f"{x:.{d}f}"
    return s.replace(".", ",") if locale == "fr" else s


def _sample_value(rng: random.Random, td, status: str) -> float:
    lo, hi = td.typical_range
    span = hi - lo if hi > lo else hi
    if status == "normal":
        return rng.uniform(lo + 0.05 * span, hi - 0.05 * span) if lo > 0 else rng.uniform(0.3 * hi, 0.95 * hi)
    if status == "high":
        cap = td.critical.get("high", hi * 2)
        return rng.uniform(hi * 1.05, min(hi * 1.6, cap * 0.97))
    if status == "low":
        floor = td.critical.get("low", 0.0)
        return rng.uniform(max(lo * 0.55, floor * 1.05), lo * 0.95)
    if status == "critical_high":
        return td.critical["high"] * rng.uniform(1.03, 1.15)
    if status == "critical_low":
        return td.critical["low"] * rng.uniform(0.85, 0.97)
    raise ValueError(status)


def _choose_status(rng: random.Random, td) -> str:
    one_sided_upper = td.typical_range[0] == 0
    options = [("normal", 0.68), ("high", 0.16)]
    if not one_sided_upper:
        options.append(("low", 0.12))
    if "high" in td.critical:
        options.append(("critical_high", 0.03))
    if "low" in td.critical:
        options.append(("critical_low", 0.03))
    r, acc = rng.random() * sum(w for _, w in options), 0.0
    for name, w in options:
        acc += w
        if r <= acc:
            return name
    return "normal"


def make_report(seed: int, locale: str | None = None) -> dict:
    rng = random.Random(seed)
    catalog = load_catalog()
    locale = locale or rng.choice(["en", "fr"])
    layout = rng.choice(["table", "dotted", "compact"])
    range_style = rng.choice(["dash", "paren", "bracket"])
    show_hl = rng.random() < 0.5

    keys = rng.sample(list(catalog), rng.randint(6, 14))
    order = list(SECTIONS)
    keys.sort(key=lambda k: (order.index(catalog[k].category), k))

    rows: list[TruthRow] = []
    for k in keys:
        td = catalog[k]
        unit = LOCALE_UNITS[locale][k]
        factor = td.to_canonical(1.0, unit)
        assert factor, (k, unit)
        lo_c, hi_c = td.typical_range
        lo_r, hi_r = lo_c / factor, hi_c / factor
        d = _decimals(hi_r)
        value = round(_sample_value(rng, td, _choose_status(rng, td)) / factor, d)

        if lo_c == 0:  # upper bound only
            ref_low, ref_high = None, round(hi_r, d)
            ref_str = f"< {_fmt(ref_high, d, locale)}"
        elif td.key == "hdl":  # lower bound only, as printed on real reports
            ref_low, ref_high = round(lo_r, d), None
            ref_str = f"> {_fmt(ref_low, d, locale)}"
        else:
            ref_low, ref_high = round(lo_r, d), round(hi_r, d)
            a, b = _fmt(ref_low, d, locale), _fmt(ref_high, d, locale)
            ref_str = {"dash": f"{a} - {b}", "paren": f"({a}-{b})", "bracket": f"[{a} – {b}]"}[range_style]

        name = rng.choice(td.aliases[locale][: max(1, min(3, len(td.aliases[locale])))])
        if name.isascii() and len(name.replace("-", "")) <= 6 and " " not in name:
            name = name.upper()  # abbreviations: MCV, HDL-C, SGOT
        elif rng.random() < 0.3:
            name = name.upper()
        else:
            name = name[:1].upper() + name[1:]

        truth = Result(test_key=k, raw_name=name, value=value, unit=unit, ref_low=ref_low, ref_high=ref_high)
        flag = flag_result(truth).flag.value
        value_str = _fmt(value, d, locale)
        rows.append(TruthRow(k, name, value, value_str, unit, ref_low, ref_high, ref_str, flag))

    patient = f"{rng.choice(FIRST)} {rng.choice(LAST)}"
    day = date(2026, 1, 1) + timedelta(days=rng.randint(0, 290))
    return {
        "id": f"rpt_{seed:05d}", "seed": seed, "locale": locale, "layout": layout,
        "show_hl": show_hl, "lab": rng.choice(LABS), "patient": patient,
        "date": day.isoformat(), "rows": [asdict(r) for r in rows],
    }


def render_pdf(report: dict, path: Path) -> None:
    catalog = load_catalog()
    loc = report["locale"]
    h = HEADERS[loc]
    c = canvas.Canvas(str(path), pagesize=A4)
    W, H = A4
    x0, y = 18 * mm, H - 20 * mm

    c.setFont("DejaVu-Bold", 14)
    c.drawString(x0, y, report["lab"])
    c.setFont("DejaVu", 8)
    c.drawString(x0, y - 11, "Fictional laboratory - synthetic report generated by LabLens")
    y -= 30
    c.setFont("DejaVu-Bold", 12)
    c.drawString(x0, y, h["title"])
    y -= 18
    c.setFont("DejaVu", 10)
    if report["layout"] == "compact":
        c.drawString(x0, y, f"{h['patient']}: {report['patient']}")
        c.drawString(x0 + 95 * mm, y, f"{h['date']}: {report['date']}")
        y -= 20
    else:
        c.drawString(x0, y, f"{h['patient']}: {report['patient']}")
        c.drawString(x0, y - 14, f"{h['date']}: {report['date']}")
        y -= 34

    cols = [x0, x0 + 72 * mm, x0 + 102 * mm, x0 + 130 * mm]
    c.setFont("DejaVu-Bold", 9.5)
    for cx, label in zip(cols, [h["test"], h["result"], h["unit"], h["ref"]]):
        c.drawString(cx, y, label)
    c.setLineWidth(0.6)
    c.line(x0, y - 4, W - 18 * mm, y - 4)
    y -= 18

    current = None
    for r in report["rows"]:
        cat = catalog[r["test_key"]].category
        if cat != current:
            current = cat
            y -= 4
            c.setFont("DejaVu-Bold", 9)
            c.setFillColor(colors.HexColor("#1f4e79"))
            c.drawString(x0, y, SECTIONS[cat][loc])
            c.setFillColor(colors.black)
            y -= 14
        c.setFont("DejaVu", 9.5)
        c.drawString(cols[0], y, r["printed_name"])
        abnormal = r["expected_flag"] not in ("normal", "unknown")
        val = r["value_str"]
        if report["show_hl"] and abnormal:
            val += " H" if "high" in r["expected_flag"] else " L"
            c.setFont("DejaVu-Bold", 9.5)
        c.drawString(cols[1], y, val)
        c.setFont("DejaVu", 9.5)
        c.drawString(cols[2], y, r["unit"])
        c.drawString(cols[3], y, r["ref_str"])
        if report["layout"] == "dotted":
            c.setDash(1, 2)
            c.setLineWidth(0.3)
            c.line(x0, y - 3.5, W - 18 * mm, y - 3.5)
            c.setDash()
        y -= 15
        if y < 30 * mm:
            c.showPage()
            y = H - 25 * mm

    c.setFont("DejaVu", 8)
    c.drawString(x0, 15 * mm, h["footer"])
    c.save()


def degrade_to_photo(pdf_path: Path, png_path: Path, rng: random.Random) -> None:
    """Rasterize page 1 and add mild photo-like noise: rotation, blur, contrast, grain."""
    import subprocess

    from PIL import Image, ImageEnhance, ImageFilter

    stem = png_path.with_suffix("")
    subprocess.run(["pdftoppm", "-png", "-r", "150", "-f", "1", "-l", "1", "-singlefile",
                    str(pdf_path), str(stem)], check=True)
    img = Image.open(f"{stem}.png").convert("L")
    img = img.rotate(rng.uniform(-1.5, 1.5), expand=True, fillcolor=245)
    img = img.filter(ImageFilter.GaussianBlur(rng.uniform(0.2, 0.7)))
    img = ImageEnhance.Contrast(img).enhance(rng.uniform(0.75, 1.0))
    img = ImageEnhance.Brightness(img).enhance(rng.uniform(0.9, 1.05))
    noise = Image.effect_noise(img.size, rng.uniform(6, 14))
    img = Image.blend(img, noise, 0.06)
    img.save(png_path)


def generate(out_dir: Path, n: int, start_seed: int = 0, photos: bool = True) -> list[dict]:
    out_dir.mkdir(parents=True, exist_ok=True)
    reports = []
    for i in range(n):
        seed = start_seed + i
        rep = make_report(seed)
        pdf = out_dir / f"{rep['id']}.pdf"
        render_pdf(rep, pdf)
        rep["pdf"] = pdf.name
        if photos:
            png = out_dir / f"{rep['id']}.png"
            degrade_to_photo(pdf, png, random.Random(seed + 10_000))
            rep["image"] = png.name
        (out_dir / f"{rep['id']}.json").write_text(json.dumps(rep, ensure_ascii=False, indent=2), encoding="utf-8")
        reports.append(rep)
    return reports


if __name__ == "__main__":
    import argparse

    ap = argparse.ArgumentParser(description="Generate synthetic lab reports with ground truth.")
    ap.add_argument("--out", default="benchmark/reports")
    ap.add_argument("-n", type=int, default=100)
    ap.add_argument("--seed", type=int, default=0)
    ap.add_argument("--no-photos", action="store_true")
    a = ap.parse_args()
    reps = generate(Path(a.out), a.n, a.seed, photos=not a.no_photos)
    print(f"wrote {len(reps)} reports to {a.out}")