# iTrialSpace **A Synthetic Imaging Clinical-Trial Engine for lung-CT AI evaluation.** *Specify the cohort you need — then manufacture anatomically grounded synthetic CTs to match, and evaluate detectors and vision-language models under controlled, one-variable-at-a-time conditions.*
> ⚠️ **License pending.** The license is being finalized — see [`LICENSE`](LICENSE). Please do > not redistribute until it is confirmed. **Research use only — not for clinical use.** --- ## What it is iTrialSpace turns medical-imaging AI evaluation from *"find more data"* into *"specify the data you need."* It harmonizes **13,140 nodule profiles** from **7 public lung-CT datasets**, plans virtual clinical-trial cohorts across **13 trial modes**, inserts donor nodule masks into real host anatomy, synthesizes CT volumes with a ControlNet-conditioned diffusion model (**NodMAISI**), and evaluates **vision-language models (VLMs)** under systematically controlled conditions. **Why it exists.** Public lung-screening datasets are each narrow in their own way — some are enriched for heavy smokers, some lack malignancy labels, some contain only large cancers — so AI models end up tested on data that under-represents where they fail. iTrialSpace lets you declare a cohort by its *statistical properties* (prevalence, size distribution, lobe distribution, demographics) and generates synthetic CTs to match, enabling controlled experiments that fixed retrospective datasets cannot support. **What this repository is.** A clean, installable, **code-only** monorepo. Source CT *images* and trained model *weights* are not redistributed here (they carry their own licenses and are large); the derived synthetic data is released separately as a dataset (see [Data & release](#7-data--release)). --- ## Track — how to read this README A guided path from zero to a full evaluation. Each row is a section below; follow them in order, or jump to what you need. | # | Section | What you'll get | |---|---------|-----------------| | 1 | [Highlights](#1-highlights) | The capabilities at a glance | | 2 | [Architecture](#2-architecture) | The five-stage engine and how the pieces connect | | 3 | [Installation](#3-installation) | Environment, the package, and the extras matrix | | 4 | [Configuration](#4-configuration) | The one env var that makes it run anywhere | | 5 | [Quickstart](#5-quickstart) | Install → configure → smoke-test in five commands | | 6 | [The end-to-end pipeline](#6-the-end-to-end-pipeline) | index → trials → masks → synthesis → evaluation | | 7 | [Data & release](#7-data--release) | The 7 datasets, what is shared, how to assemble it | | 8 | [Trial modes](#8-trial-modes) | The 13 cohort designs you can synthesize | | 9 | [VLM evaluation (end to end)](#9-vlm-evaluation-end-to-end) | Build → run → analyze, locally or on SLURM | | 10 | [Reproducibility](#10-reproducibility) | Frozen splits, the reporting convention, checksums | | 11 | [Apps: NoduleMap & Retriever](#11-apps-nodulemap--retriever) | Explore and search the nodule space | | 12 | [Command reference](#12-command-reference) | Console scripts and module entry points | | 13 | [Project layout](#13-project-layout) | Where everything lives | | 14 | [Citation, contributing, license](#14-citation-contributing-license) | How to cite and contribute | --- ## 1. Highlights - **Cohort-by-specification.** Plan trials by prevalence, size, lobe, and demographics — not by whatever a retrospective dataset happens to contain. - **Anatomically grounded synthesis.** Donor nodule masks are pasted into real host lung anatomy and rendered by **NodMAISI**, a ControlNet-conditioned diffusion model. - **13 trial modes**, from controlled-prevalence and size/location sensitivity curves to digital twins of real patients. - **Zero-shot VLM evaluation** — BiomedCLIP, LLaVA-Med, and MedGemma across 3 clinical tasks × 4 spatial-guidance conditions, on synthetic and real CT. - **Portable by construction.** All paths resolve from a single env var; the same code runs on a laptop, a workstation, or an HPC cluster. A CI portability gate enforces it. - **Reproducible reporting.** Frozen, checksummed evaluation splits and a documented reporting convention keep every published number tied to an exact, verifiable case set. --- ## 2. Architecture ``` itrialspace → trials → mask_inserter → synthesis → evaluation (nodule index + (cohort (donor mask (NodMAISI (VLM query + features) manifests) → host anatomy) CT volumes) zero-shot) │ ├─ nodulemap — interactive embedding-graph explorer └─ retriever — faceted search, similarity & donor matching + CT viewer ``` One installable package, `itrialspace`, with sub-packages for each stage. Configuration, I/O, and path resolution are centralized so nothing hardcodes a machine-specific path. Full component map: [blinded_doc/architecture.md](blinded_doc/architecture.md). --- ## 3. Installation ```bash # 1) Environment (conda/mamba) conda env create -f environment.yml && conda activate itrialspace # 2) Install the package with the extras you need pip install -e ".[all]" # or a subset — see the matrix ``` **Extras matrix** | Extra | Installs | For | |-------|----------|-----| | *(base)* | pandas, numpy, pyyaml | Index, query, trial planning | | `imaging` | nibabel, scipy, SimpleITK | Mask insertion, NIfTI handling | | `web` | fastapi, streamlit, faiss, umap | NoduleMap + Retriever UIs | | `synthesis` | monai, (torch) | NodMAISI CT synthesis ¹ | | `vlm` | transformers, accelerate, open_clip | VLM evaluation ¹ | | `dev` | pytest, ruff, black, pre-commit | Development | | `all` | imaging + web + vlm + dev | Everything bundled | ¹ PyTorch is platform-specific — install it per [blinded_doc/installation.md](blinded_doc/installation.md). A `docker/Dockerfile` is provided for a containerized setup. --- ## 4. Configuration Everything is driven by environment variables (loaded from a gitignored `.env`); nothing is hardcoded. ```bash cp .env.example .env # then edit: # ITRIALSPACE_DATA_DIR → the dataset root (raw_ct/ masks/ profiles/ …) # ITRIALSPACE_OUTPUT_DIR → where generated artifacts go (default: = data dir) # HF_TOKEN → HuggingFace token for gated VLMs (kept local; never committed) ``` Any config YAML may reference these with `${VAR}` interpolation. `its config` prints the resolved paths. Details: [blinded_doc/configuration.md](blinded_doc/configuration.md). --- ## 5. Quickstart ```bash conda activate itrialspace pip install -e ".[all]" cp .env.example .env # set ITRIALSPACE_DATA_DIR (+ HF_TOKEN for VLMs) pytest -m "not full_volume" # unit tests — no external data needed its config # verify resolved paths its index stats # build the nodule index + print stats (needs profiles/) ``` A guided first cohort is in [blinded_doc/walkthrough_mode1.md](blinded_doc/walkthrough_mode1.md). --- ## 6. The end-to-end pipeline Five stages take you from a raw nodule index to evaluated models. Each stage is a CLI command and a documented module. | Stage | What it does | Entry point | |-------|--------------|-------------| | **1. Index** | Harmonize 7 datasets into one nodule index with reinsertion features | `its index` | | **2. Trials** | Plan cohort **manifests** for the 13 trial modes (donor → host, location, scale) | `its-trials` | | **3. Mask insertion** | Paste donor nodule masks into host organ/lobe masks (nodule = label 23) | `its-insert` | | **4. Synthesis** | Render CT volumes from the combined masks with **NodMAISI** | `its` (synthesis) | | **5. Evaluation** | Score **VLMs** (zero-shot) under controlled conditions | `its-vlm` | Full walkthrough: [blinded_doc/pipeline.md](blinded_doc/pipeline.md) and [blinded_doc/running_the_pipeline.md](blinded_doc/running_the_pipeline.md). HPC orchestration scripts are under [`infra/slurm/`](infra/slurm/). --- ## 7. Data & release iTrialSpace harmonizes **7 public lung-CT datasets** (13,140 nodule profiles): | Dataset | Nodule profiles | Dataset | Nodule profiles | |---------|----------------:|---------|----------------:| | LUNA25 | 6,163 | LUNA16 | 1,186 | | DLCS24 | 2,487 | LNDbv4 | 768 | | IMDCT | 2,032 | NSCLCR | 421 | | LUNGx | 83 | **Total** | **13,140** | **What the release ships** (derived artifacts): nodule `profiles/`, `masks/`, cohort `manifests/`, combined `inserted_masks/`, the **synthetic CT volumes** (`generated_cts/` — **44,174** volumes across all 13 modes), and the **VLM evaluation dataset** (`vlm_dataset/` — **42,858** synthetic and **13,087** real evaluation cases). **What it does not ship.** The real CT **images** are not redistributed — download them from each provider under their own license and place them under `raw_ct/{DATASET}/`. The data contract, counts, ground-truth definitions, and assembly instructions are in [blinded_doc/dataset_card.md](blinded_doc/dataset_card.md) and [blinded_doc/data.md](blinded_doc/data.md). --- ## 8. Trial modes Thirteen cohort designs, each producing a manifest you can synthesize and evaluate: | # | Mode | Question it answers | |---|------|---------------------| | 1 | `controlled_prevalence` | How does performance change with cancer prevalence? | | 2 | `size_detection_curve` | Detection sensitivity vs. nodule size (FROC) | | 3 | `location_sensitivity` | Detection vs. anatomical lobe | | 4 | `demographic_stratification` | Performance across demographic strata | | 5 | `counterfactual` | The same patient, with vs. without a nodule | | 6 | `cross_dataset` | Generalization across acquisition sources | | 7 | `bootstrap_confidence` | Confidence intervals via resampled cohorts | | 8 | `algorithm_comparison` | Standardized head-to-head model comparison | | 9 | `screening_simulation` | Multi-round screening with prevalence decay | | 10 | `multi_nodule_realism` | Impact of companion nodules in context | | 11 | `digital_twin_isolation` | Each native nodule isolated within its host anatomy | | 12 | `digital_twin_complete` | All native nodules of a scan, reconstructed | | 13 | `digital_twin_cross` | Donor nodules placed in cross-patient host anatomy | Details and per-mode parameters: [blinded_doc/trial_modes.md](blinded_doc/trial_modes.md). --- ## 9. VLM evaluation (end to end) Evaluate **BiomedCLIP**, **LLaVA-Med**, and **MedGemma** on three zero-shot tasks — **presence** (binary), **lobe** (5-class), **size** (4-class) — under four image **conditions** (`plain`, `bbox`, `contour`, `bbox_contour`), on synthetic and real CT. The whole workflow is **three steps**, identical for a small demo and the full dataset (only the paths/env change): ```bash # 1) BUILD the 2-D eval dataset (slices + overlays + ground truth) python -m itrialspace.evaluation.vlm_eval.build_dataset --manifest … --output-dir … --overlays # (real CTs: itrialspace.evaluation.vlm_eval.build_real_dataset) # 2) RUN a model across all conditions × tasks python -m itrialspace.evaluation.vlm_eval.runners.run_conditions \ --model biomedclip --run-all-conditions --dataset-csv …/eval_dataset.csv --output-dir … # 3) ANALYZE → tables, figures, statistics, report.md python -m itrialspace.evaluation.vlm_eval.eval_analysis --results … --out analysis/ ``` On a cluster, the same three steps are env-driven SLURM jobs — `build_dataset.sub → run_vlm.sub → analyze.sub` — see [infra/slurm/vlm_eval/README.md](infra/slurm/vlm_eval/README.md). The **analysis suite** (`eval_analysis`) auto-discovers whatever models/conditions/tasks are present and emits accuracy and Δ-vs-baseline tables, per-mode / per-dataset / per-size / per-lobe breakdowns, confusion matrices, per-class accuracy, **bootstrap confidence intervals**, **McNemar significance**, publication-style figures, and an assembled `report.md`. - User guide: [blinded_doc/vlm_eval.md](blinded_doc/vlm_eval.md) - Architecture & module reference: [blinded_doc/vlm_eval_implementation.md](blinded_doc/vlm_eval_implementation.md) - Headline results: [blinded_doc/vlm_results.md](blinded_doc/vlm_results.md) --- ## 10. Reproducibility - **Frozen splits.** Each result is reported on a checksummed case list (`vlm_dataset/splits/`): `release_v1` (the headline set) and `release_v1_full` (cases scored under all four conditions). `splits.json` records counts, SHA-256, and provenance — regenerate and the hash must match. - **One reporting N.** Accuracy is always computed on the cases scored in *every* (model × condition × task), keyed by a stable canonical slice id (not a positional index), so all reported numbers are directly comparable. - **Determinism & audit trails.** Every synthesis case carries an audit record; see [blinded_doc/reproducibility.md](blinded_doc/reproducibility.md). - **Quality gates.** CI runs lint (`ruff`/`black`), unit tests, and a portability check that fails on any hardcoded machine-specific path. --- ## 11. Apps: NoduleMap & Retriever - **NoduleMap** (`its-nodulemap`) — an interactive embedding-graph explorer of the nodule space. See [blinded_doc/nodulemap.md](blinded_doc/nodulemap.md). - **Retriever** (`its-retriever`) — faceted search, similarity and donor matching, with an integrated CT viewer. See [blinded_doc/retriever.md](blinded_doc/retriever.md). --- ## 12. Command reference | Command | Purpose | |---------|---------| | `its` | Umbrella CLI — index, query, config, synthesis subcommands | | `its-trials` | Generate cohort manifests for the 13 trial modes | | `its-insert` | Insert donor nodule masks into host anatomy from a manifest | | `its-vlm` | VLM evaluation utilities | | `its-nodulemap` | Launch the NoduleMap server | | `its-retriever` | Launch the Retriever API + UI | VLM modules: `…vlm_eval.build_dataset` · `…build_real_dataset` · `…runners.run_conditions` · `…make_split` · `…eval_analysis` · `…report`. --- ## 13. Project layout ``` itrialspace/ ├── src/itrialspace/ # the package │ ├── core/ io/ index/ query/ site/ # nodule index, query, path resolution │ ├── config/ # env-driven settings + ${VAR} interpolation │ ├── trials/ # the 13 trial-mode cohort planners │ ├── mask_inserter/ # donor mask → host anatomy │ ├── synthesis/ # NodMAISI CT synthesis (code; weights external) │ ├── nodulemap/ retriever/ # the two apps │ └── evaluation/ │ └── vlm_eval/ # build → run → analyze (incl. eval_analysis suite) ├── configs/ # example, env-interpolated configs ├── infra/slurm/ # cluster job scripts (env-driven) ├── blinded_doc/ # full documentation set (blinded) ├── tests/ docker/ └── pyproject.toml environment.yml .env.example ``` --- ## 14. Citation, contributing, license - **Citation** — withheld for double-blind review. (Cite each underlying CT dataset you download into `raw_ct/` per its own terms.) - **Contribute** via [`CONTRIBUTING.md`](CONTRIBUTING.md) — all PRs run through CI (lint + tests + portability gate). - **License** — see [`LICENSE`](LICENSE) (being finalized). Research use only; not a medical device and not for clinical use.
iTrialSpace — specify the cohort, synthesize the data, evaluate with control.