Download lablens/synth/generator.py from Imene-ee/LabLens: direct link, hf CLI and curl.
- Browser
- Download file 12.7 kB
-
https://huggingface.co/spaces/Imene-ee/LabLens/resolve/main/lablens/synth/generator.py
- Command line
-
hf download hf://spaces/Imene-ee/LabLens/lablens/synth/generator.py
-
curl -L -o generator.py https://huggingface.co/spaces/Imene-ee/LabLens/resolve/main/lablens/synth/generator.py
12.7 kB
| """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."}, | |
| } | |
| 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}") | |