| from __future__ import annotations |
|
|
| import argparse |
| from pathlib import Path |
|
|
| import numpy as np |
| import pandas as pd |
|
|
|
|
| def _make_case(path: Path, index: int, shape: tuple[int, int, int], rng: np.random.Generator) -> dict[str, float | str]: |
| zz, yy, xx = np.indices(shape) |
| label = float(index % 2) |
| center = np.array(shape, dtype=np.float32) / 2.0 + rng.normal(0, 1.2, size=3) |
| radius = 3.0 + (index % 3) + label |
| dist = np.sqrt(((zz - center[0]) ** 2) + ((yy - center[1]) ** 2) + ((xx - center[2]) ** 2)) |
| mask = (dist <= radius).astype(np.float32) |
|
|
| ct = rng.normal(-760, 95, size=shape).astype(np.float32) |
| ct += mask * (520 + 180 * label) |
| pet = rng.gamma(shape=1.4, scale=0.45, size=shape).astype(np.float32) |
| pet += mask * (0.8 + 1.0 * label) |
|
|
| path.parent.mkdir(parents=True, exist_ok=True) |
| np.savez_compressed(path, ct=ct, pet=pet, mask=mask) |
| return { |
| "ct_path": path.name, |
| "pet_path": path.name, |
| "mask_path": path.name, |
| "label": label, |
| } |
|
|
|
|
| def _write_cohort( |
| root: Path, |
| cohort: str, |
| manifest_name: str, |
| n: int, |
| shape: tuple[int, int, int], |
| clinical_cols: tuple[str, ...], |
| include_pet: bool, |
| include_label: bool, |
| include_survival: bool, |
| rng: np.random.Generator, |
| ) -> pd.DataFrame: |
| cohort_dir = root / cohort |
| rows = [] |
| for i in range(n): |
| case = _make_case(cohort_dir / f"sample_{i:03d}.npz", i, shape, rng) |
| row: dict[str, float | str] = { |
| "patient_id": f"{cohort}_p{i // 2:03d}", |
| "lesion_id": f"{cohort}_l{i:03d}", |
| "ct_path": f"{cohort}/{case['ct_path']}", |
| "mask_path": f"{cohort}/{case['mask_path']}", |
| } |
| if include_pet and i % 5 != 0: |
| row["pet_path"] = f"{cohort}/{case['pet_path']}" |
| else: |
| row["pet_path"] = "" |
| if include_label: |
| row["label"] = case["label"] |
| else: |
| row["label"] = np.nan |
| if "age" in clinical_cols: |
| row["age"] = 50 + (i % 18) |
| if "sex" in clinical_cols: |
| row["sex"] = float(i % 2) |
| if "smoking_status" in clinical_cols: |
| row["smoking_status"] = np.nan if i % 7 == 0 else float((i + 1) % 3) |
| if "stage" in clinical_cols: |
| row["stage"] = np.nan if i % 6 == 0 else float(1 + (i % 4)) |
| if include_survival: |
| row["time"] = float(i % 6) |
| row["event"] = float(i % 3 != 0) |
| rows.append(row) |
| frame = pd.DataFrame(rows) |
| manifest = cohort_dir / manifest_name |
| frame.to_csv(manifest, index=False) |
| return frame |
|
|
|
|
| def create_synthetic_plan_data(output_dir: str | Path = "processed/synthetic_plan", n: int = 16) -> Path: |
| root = Path(output_dir) |
| root.mkdir(parents=True, exist_ok=True) |
| rng = np.random.default_rng(1701) |
| shape = (16, 16, 16) |
|
|
| _write_cohort(root, "lidc", "seg_manifest.csv", n, shape, (), False, False, False, rng) |
| _write_cohort(root, "lidc_cls", "cls_manifest.csv", n, shape, (), False, True, False, rng) |
| lung = _write_cohort( |
| root, |
| "lung_pet_ct_dx", |
| "manifest.csv", |
| n, |
| shape, |
| ("age", "sex", "smoking_status"), |
| True, |
| True, |
| False, |
| rng, |
| ) |
| nsclc = _write_cohort( |
| root, |
| "nsclc_radiomics", |
| "manifest.csv", |
| n, |
| shape, |
| ("age", "sex", "stage"), |
| False, |
| True, |
| True, |
| rng, |
| ) |
|
|
| full = pd.concat([lung, nsclc], ignore_index=True, sort=False) |
| for col in ("age", "sex", "smoking_status", "stage", "time", "event"): |
| if col not in full.columns: |
| full[col] = np.nan |
| (root / "full").mkdir(exist_ok=True) |
| full.to_csv(root / "full" / "manifest.csv", index=False) |
| return root |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser(description="Create synthetic data for the full reproduction plan.") |
| parser.add_argument("--out-dir", default="processed/synthetic_plan") |
| parser.add_argument("--n", type=int, default=16) |
| args = parser.parse_args() |
| root = create_synthetic_plan_data(args.out_dir, n=args.n) |
| print(root) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|