DosePET / tools /README.md
eltzanis's picture
DosePET v1.0 — initial public release
f487fa5
|
Raw History Blame Contribute Delete
10.6 kB
# Dose conversion tools
`convert_dose_images.py` converts the published total-dose images of the DosePET dataset into:
- **dose-rate maps**, in mGy/h (or mGy/s, Gy/s), at scan start by default, and
- **dose conversion factor maps**, in **Gy per decay**,
and can reproduce the shipped per-organ tables from the images and masks so you
can verify the release yourself.
---
## 1. The one thing to know
Each voxel of `dose_image.nii.gz` is the **total absorbed dose in Gy**,
integrated from the start of the scan to infinity assuming **physical decay
only** (the activity distribution is frozen in space; no biological clearance,
no redistribution).
Because the only time dependence is a single exponential shared by every voxel,
both conversions are a multiplication of the whole image by **one scalar**:
| quantity | formula | units |
|---|---|---|
| dose rate at scan start | `Ddot = D * ln2 / T_half` | Gy/s |
| dose rate at scan start | `Ddot = D * 3.6e6 * ln2 / T_half` | mGy/h |
| dose conversion factor | `DCF = D / A_tilde` | Gy per decay |
where `T_half` is the isotope half-life of that case and `A_tilde` is that
case's cumulated activity in Bq·s (1 Bq·s = 1 decay). Both are recorded per
case inside the dataset — the script finds them for you.
Two optional time shifts follow from the same relation:
```
Ddot(tau) = Ddot(0) * exp(-lambda*tau) tau seconds after the scan
Ddot(t_inj) = Ddot(0) * ICF back to injection time
lambda = ln2 / T_half
```
Full derivation and the assumptions to cite: see `../dose_conversion_plan.md`.
---
## 2. Requirements
```bash
pip install numpy nibabel SimpleITK
```
`nibabel` is needed for the image modes, `SimpleITK` for the mask-based modes
(`organs`, `check`). Python 3.8+.
---
## 3. Quick start
Set a shell variable for the dataset so the examples stay short:
```bash
DATA="/media/et/Seagate Basic/PET_CT_Dose_Data/dataset_ready_for_publication"
```
### 3.1 Dose-rate maps in mGy/h (the common case)
```bash
python3 convert_dose_images.py rate \
--root "$DATA/FDG" \
--out ~/dose_rate_FDG
```
Every case gets `dose_rate_at_scan_mGy_per_h.nii.gz` plus a `.json` sidecar
recording the scale factor, half-life, cumulated activity, and the source path.
Try it on a couple of cases first:
```bash
python3 convert_dose_images.py rate --root "$DATA/FDG" --out ~/trial --limit 2 --verify
```
`--verify` reloads each output and confirms its maximum equals
`max(dose) x k`.
### 3.2 Other units
```bash
python3 convert_dose_images.py rate --root "$DATA/PSMA" --out ~/rate_s --unit mGy_per_s
python3 convert_dose_images.py rate --root "$DATA/PSMA" --out ~/rate_gys --unit Gy_per_s
```
Choices: `mGy_per_h` (default), `mGy_per_min`, `mGy_per_s`, `Gy_per_s`.
### 3.3 A different time point (opt-in)
The default is always **scan start**. To ask for something else:
```bash
# dose rate one hour after the start of the scan
python3 convert_dose_images.py rate --root "$DATA/FDG" --out ~/rate_1h --at +3600
# dose rate back-extrapolated to injection time
python3 convert_dose_images.py rate --root "$DATA/FDG" --out ~/rate_inj --at injection
```
`--at injection` uses the stored `injection_correction_factor`. Four FDG cases
(`0718`, `1022`, `1195`, `1289`) do not have one, because the acquisition time
is missing in the source metadata; they are skipped with an explicit message
and everything else proceeds:
```
[1/1] SKIP 0718: injection_correction_factor unavailable (acquisition time missing in the source metadata)
```
### 3.4 Dose conversion factor maps, Gy per decay
```bash
python3 convert_dose_images.py dcf --root "$DATA/PSMA" --out ~/dcf_PSMA
```
Produces `dose_conversion_factor_Gy_per_decay.nii.gz` per case.
> **Read this before using DCF.** `A_tilde` is the total cumulated activity in
> the **imaged field of view**. So a DCF voxel means *Gy delivered to that voxel
> per decay occurring anywhere in the FOV, with this patient's own tracer
> distribution as the source*. It is a FOV-normalised S-value. It is **not**
> dose per decay in a specific source organ, and must not be compared directly
> with organ-to-organ S-values from MIRD/OLINDA without that caveat.
### 3.5 Per-organ tables from the images and masks
```bash
# one case -> one CSV
python3 convert_dose_images.py organs \
--case "$DATA/FDG/1597" \
--out ~/organ_table_1597.csv
# a whole subset -> one organ_table.csv per case, mirroring the dataset layout
python3 convert_dose_images.py organs --root "$DATA/PSMA" --out ~/organ_tables_PSMA
```
The output uses **exactly the column names and order of the published
`dose_report.csv`**, alphabetical by organ, with the same line endings, so you
can diff the two directly:
```bash
diff <(cut -d, -f1 ~/organ_table_1597.csv) \
<(cut -d, -f1 "$DATA/FDG/1597/dose_report.csv") # same organs, same order
```
One column is written **empty**: `mass_g`, which needs the CT, and the CT is
not part of the release.
### 3.6 Verify the release against the images
```bash
python3 convert_dose_images.py check --root "$DATA/FDG" --json-out ~/check_FDG.json
```
`check` recomputes every reproducible column from `dose_image.nii.gz` +
`segmentation/*.nii.gz` and compares it against the shipped `dose_report.csv`,
printing an explicit PASS/FAIL per case:
```
[1/2] PASS 0001 117 organs, 1283 values, worst 0.499 x tolerance
[2/2] PASS 0002 117 organs, 1284 values, worst 0.496 x tolerance
2/2 cases PASS, 234 organ rows, 2567 values compared
```
"worst 0.499 x tolerance" is the largest deviation found, expressed as a
fraction of that column's own allowance (one quantisation step of the stored
uint16 dose image, carried through the column's scale factor). Anything below
1.0 is agreement. `num_voxels` must match **exactly** and `volume_cm3` to 1e-9
relative — those two catch a report row paired with the wrong mask.
To check a single case, point `--root` at that case directory:
```bash
python3 convert_dose_images.py check --root "$DATA/FDG/0718"
```
---
## 4. Output modes, size and precision
### `--mode header` (default, recommended)
The dose image is `uint16` with a `scl_slope` in the NIfTI header. Since the
conversion is a pure scalar, the script copies the integer payload **byte for
byte** and multiplies only `scl_slope`:
```
scl_slope_out = scl_slope_in * k
```
- **No second quantisation**: the output is exactly as precise as the source.
Verified: the uint16 payload of the output is bit-identical to the input's.
- The file stays the same size as the source.
- Residual error is only the float32 rounding of `scl_slope` itself, about
1e-8 relative — three orders of magnitude below the 1.5e-5 relative
quantisation already present in the published image.
### `--mode float32`
Decodes with the slope applied, multiplies, writes float32. Use this if your
software ignores `scl_slope`. Files are roughly 35% larger.
### Disk space — please read
Converted maps are the same size as the originals, so converting a whole subset
**duplicates it** (hundreds of GB for FDG). Unless you genuinely need the files
on disk, apply the scalar on the fly instead:
```python
import json, math, numpy as np, nibabel as nib
case = "/path/to/dataset/FDG/1597"
cfg = json.load(open(f"{case}/dosimetry_config.json"))
meta = json.load(open(f"{case}/metadata.json"))
T_half = cfg.get("half_life_seconds") or meta["isotope"]["half_life_seconds"]
A_tilde = cfg["cumulated_from_scan"]
D = np.asanyarray(nib.load(f"{case}/dose_image.nii.gz").dataobj) # Gy
rate_mGy_per_h = D * 3.6e6 * math.log(2) / T_half # at scan start
dcf_Gy_per_dec = D / A_tilde # Gy per decay
```
And if you only need **organ-level** numbers, you do not need to convert
anything at all: `dose_report.csv` already ships
`mean_dose_rate_at_scan_mGy_per_s`, `mean_dose_rate_at_scan_mGy_per_h` and
`dcf_Gy_per_Bqs`.
---
## 5. Where the constants come from
Per case, the script resolves the half-life in this order and records which one
it used in the sidecar JSON:
```
dosimetry_config.json : half_life_seconds
-> metadata.json : isotope.half_life_seconds
-> metadata.json : dicom_half_life_seconds
-> built-in isotope table (fallback, flagged)
```
Cumulated activity comes from `dosimetry_config.json : cumulated_from_scan`,
the same value used to scale the Monte Carlo output.
---
## 6. Output layout
```
<--out>/
0001/
dose_rate_at_scan_mGy_per_h.nii.gz
dose_rate_at_scan_mGy_per_h.json
0002/
...
```
For PSMA the nested `<patient>/<scan>/` structure is mirrored. Case discovery
is by searching for `dose_image.nii.gz` at any depth, so the flat FDG layout and
the nested PSMA layout both work with no extra flags.
Each sidecar JSON records the quantity, units, time point, the scale factor
applied, the formula, the decay-only assumption, the source path, every case
constant with its origin, and the resulting `scl_slope`.
---
## 7. Assumptions and caveats
1. **Physical decay only.** No biological clearance, no redistribution. All rates and DCFs inherit this.
2. **Time zero is scan start** — the integration window is `scan -> infinity`
in all cases.
3. **`Gy per decay` is FOV-normalised**, not a source-organ S-value (§3.4).
4. **Apply `scl_slope`.** The published images, and the header-mode outputs, are
uint16 with a scale factor in the header. nibabel and SimpleITK do this
automatically; some other toolkits do not.
5. **Four FDG cases have no injection correction factor**: `0718`, `1022`,
`1195`, `1289`. Unaffected for scan-time rates and DCF; only `--at injection`
is unavailable for them.
---
## 8. Safety
- `--out` is mandatory and the script exits if it resolves inside the dataset:
```
REFUSING to write inside the dataset root.
```
- All dataset files are opened read-only. Nothing is written, renamed or
deleted inside the release.
- A case missing a required constant is skipped with a message, never silently
defaulted.
---
## 9. Command reference
```
convert_dose_images.py rate --root DIR --out DIR [--unit U] [--at scan|injection|+N]
[--mode header|float32] [--verify] [--limit N]
convert_dose_images.py dcf --root DIR --out DIR [--mode header|float32]
[--verify] [--limit N]
convert_dose_images.py organs (--case DIR | --root DIR) --out PATH [--limit N]
convert_dose_images.py check --root DIR [--json-out FILE] [--limit N]
```
`python3 convert_dose_images.py <command> --help` prints the same detail.