From b2fd0d8a67831961368781435c719099b87752bc Mon Sep 17 00:00:00 2001 From: Youssef Assis Date: Tue, 29 Sep 2026 20:18:21 +0200 Subject: [PATCH] [FEAT] Predict any scan and export detections predict.py takes input= besides the test patients of the split. Input scans are skull-stripped and normalized like the training data (skull_strip, the training's normalization). For every case it writes the probability map and the detections: center and radius in mm, size and probability, as CSV and as .fcsv markups for 3D Slicer. remove_skull.py and predict.py share skull_strip and normalize_volume. --- README.md | 8 +- configs/predict.yaml | 7 +- deepaneseg/data/io.py | 25 +++--- deepaneseg/inference/prediction.py | 77 +++++++++++------- deepaneseg/volume/selection.py | 9 +++ scripts/predict.py | 81 ++++++++++++++----- scripts/remove_skull.py | 5 +- tests/test_predict_scans.py | 122 +++++++++++++++++++++++++++++ tests/test_volume.py | 7 ++ 9 files changed, 274 insertions(+), 67 deletions(-) create mode 100644 tests/test_predict_scans.py diff --git a/README.md b/README.md index 7c70d10..38a926e 100644 --- a/README.md +++ b/README.md @@ -77,10 +77,16 @@ uv run python scripts/preprocess_data.py data_dir=Data_dir # split=[0.7 uv run python scripts/train.py data_dir=Data_dir name=my_training ``` -**4. Testing:** predictions are written to `Data_dir/0Work/my_training/prediction/test`. +**4. Testing:** predictions of the test patients are written to `Data_dir/0Work/my_training/prediction/test`: a +probability map per patient, and its detections (center and radius in mm, size, probability) as CSV and as `.fcsv` +markups for 3D Slicer. ``` uv run python scripts/predict.py train_dir=Data_dir/0Work/my_training ``` +Any other scan, or folder of scans, can be predicted the same way (skull-stripped first, like the training data): +``` +uv run python scripts/predict.py train_dir=Data_dir/0Work/my_training input=scan.nii.gz output_dir=results +``` # Differences from the published code The Kappa metric/loss follows Cohen's definition and the focal loss is computed per voxel (Lin et al.), so diff --git a/configs/predict.yaml b/configs/predict.yaml index 4c94d67..422bc3a 100644 --- a/configs/predict.yaml +++ b/configs/predict.yaml @@ -1,7 +1,12 @@ -# Predicts the test patients with a trained model; data and model settings are read from the training's config +# Predicts with a trained model; data and model settings are read from the training's config train_dir: ??? # training directory written by train.py +input: null # NIfTI scan or folder of scans; null: the test patients of the training's split +output_dir: null # null: /prediction/test, or /prediction/scans for input checkpoint: last_checkpoint.pytorch volume: noskull volume # key, in each patient's config.json, of the volume to predict on +skull_strip: true # skull-strip input scans as remove_skull.py does, to match volume above +threshold: 0.5 # detections: connected components above this probability +min_size: null # detections smaller than this, in voxels, are ignored; null keeps all margin: 8 # voxels discarded at each patch border when stitching predictions batch_size: null # null: the training's validation_batch_size diff --git a/deepaneseg/data/io.py b/deepaneseg/data/io.py index 18e2de4..c1b12b6 100644 --- a/deepaneseg/data/io.py +++ b/deepaneseg/data/io.py @@ -40,6 +40,19 @@ def save_nii_to_file(fname, vol, vox2met): ni.save(ni.Nifti1Image(vol, vox2met), fname) +def normalize_volume(vol, normalize): + """ + normalize: None or 'None' (unchanged), 'Linear' (rescaled to [0, 1]) or 'Normal' (zero mean, unit variance) + """ + if normalize == "Linear": + return (vol - np.min(vol)) / (np.max(vol) - np.min(vol)) + if normalize == "Normal": + return (vol - np.mean(vol)) / np.std(vol) + if normalize in (None, "None"): + return vol + raise ValueError(f"Unsupported normalization '{normalize}', expected Linear, Normal or None") + + def read_points_from_csv(fname): """ returns a list of points, read from a csv file. Coordinates are stored in columns named 'x', 'y' and 'z' @@ -88,17 +101,7 @@ def read_patient_data(vol_file, ane_file, normalize=None, vessel_file=None): else: s = None - # should we use scikit.learn.normalize (or transform)? - if normalize == "Linear": - m = np.min(d.ravel()) - M = np.max(d.ravel()) - d = (d - m) / (M - m) - elif normalize == "Normal": - m = np.mean(d.ravel()) - std = np.std(d.ravel()) - d = (d - m) / std - - return {"data": d, "affine": a, "aneurysms": s, "vessel": seg} + return {"data": normalize_volume(d, normalize), "affine": a, "aneurysms": s, "vessel": seg} def read_patient_data_from_config(cfg_name, volume="init volume", normalize=None, vessel=None): diff --git a/deepaneseg/inference/prediction.py b/deepaneseg/inference/prediction.py index 81360b1..e92a2c0 100644 --- a/deepaneseg/inference/prediction.py +++ b/deepaneseg/inference/prediction.py @@ -1,8 +1,10 @@ import os import math -import nibabel as nib import numpy as np +import pandas as pd +import skimage.measure as skme +import skimage.morphology as skmo import SimpleITK as sitk from tqdm import tqdm @@ -88,57 +90,72 @@ def patch_wise_prediction(device, model, data, patch_shape, margin=0, batch_size return output[tuple(slice(0, n) for n in image_shape)] -def predict_patient(pat_dict, device, model, patch_size, patch_shape, output_dir, margin=0, batch_size=1): +@torch.no_grad() +def predict_volume(data, affine, device, model, patch_size, patch_shape, margin=0, batch_size=1, name=""): """ - Predicts one patient and writes the (sigmoid) prediction, on the patient's voxel grid, to - /.nii.gz. - :param pat_dict: patient data, as returned by data.io.read_patient_data_base (including the 'dir' key) + Predicts a (normalized) volume and returns the (sigmoid) prediction on the volume's own voxel grid. :param patch_size: size in mm of a patch, and patch_shape its size in voxels (as used for training) """ - os.makedirs(output_dir, exist_ok=True) - name = os.path.basename(pat_dict["dir"]) - affine, data = pat_dict["affine"], pat_dict["data"] - print(f"Processing {name}") - spacing = np.linalg.norm(affine[:3, :3], axis=0) work_spacing = np.asarray(patch_size, float) / np.asarray(patch_shape) work = resample_to_spacing(data, spacing, work_spacing) prediction = patch_wise_prediction( device, model, work, patch_shape, margin=margin, batch_size=batch_size, patient_name=name ) - prediction = resample_to_grid(prediction, work_spacing, data.shape, spacing) - - nib.Nifti1Image(prediction, affine).to_filename(os.path.join(output_dir, name + ".nii.gz")) + return resample_to_grid(prediction, work_spacing, data.shape, spacing) -@torch.no_grad() -def predict_patients(pat_db, device, model, patch_size, patch_shape, output_dir=".", margin=0, batch_size=1): - for p in pat_db: - predict_patient(p, device, model, patch_size, patch_shape, output_dir, margin=margin, batch_size=batch_size) +def find_detections(prediction, affine, threshold=0.5, min_size=None): + """ + Connected components of the prediction above threshold, as a DataFrame with one row per detection: + center (x, y, z) and radius (half the largest extent) in mm, size in voxels and maximum probability. + min_size: components smaller than this, in voxels, are ignored + """ + labels = skme.label(prediction >= threshold) + if min_size is not None: + labels = skme.label(skmo.remove_small_objects(labels, min_size=min_size)) + rows = [] + for region in skme.regionprops(labels, intensity_image=prediction): + points = region.coords @ affine[:3, :3].T + affine[:3, 3] + x, y, z = points.mean(axis=0) + radius = np.max(points.max(axis=0) - points.min(axis=0)) / 2 + rows.append( + {"x": x, "y": y, "z": z, "radius": radius, "voxels": int(region.area), "probability": region.intensity_max} + ) + return pd.DataFrame(rows, columns=["x", "y", "z", "radius", "voxels", "probability"]) + + +def save_detections(detections, csv_path): + """Writes the detections to csv_path and, for 3D Slicer, as markups to the matching .fcsv file.""" + detections.assign(type="Detection").to_csv(csv_path) + dio.csv2fcsv(csv_path) @torch.no_grad() def get_patient_prediction( - pat_path, device, model, patch_size, patch_shape=(48, 48, 48), margin=8, batch_size=10, normalization="Normal" + pat_path, + device, + model, + patch_size, + patch_shape=(48, 48, 48), + margin=8, + batch_size=10, + normalization="Normal", ): """Predicts one patient directory; returns the prediction (on the patient's grid), its affine and the ground-truth aneurysm spheres.""" pat_dict = dio.read_patient_data_base([pat_path], volume="init volume", normalize=normalization)[0] - affine, data = pat_dict["affine"], pat_dict["data"] - - spacing = np.linalg.norm(affine[:3, :3], axis=0) - work_spacing = np.asarray(patch_size, float) / np.asarray(patch_shape) - work = resample_to_spacing(data, spacing, work_spacing) - prediction = patch_wise_prediction( + affine = pat_dict["affine"] + prediction = predict_volume( + pat_dict["data"], + affine, device, model, - work, + patch_size, patch_shape, - margin=margin, - batch_size=batch_size, - patient_name=os.path.basename(pat_dict["dir"]), + margin, + batch_size, + name=os.path.basename(pat_dict["dir"]), ) - prediction = resample_to_grid(prediction, work_spacing, data.shape, spacing) - spheres = dio.points_to_spheres(pat_dict["aneurysms"]) return prediction, affine, spheres diff --git a/deepaneseg/volume/selection.py b/deepaneseg/volume/selection.py index caa7bf7..d1ebf4d 100644 --- a/deepaneseg/volume/selection.py +++ b/deepaneseg/volume/selection.py @@ -132,6 +132,15 @@ def remove_skull_mask(vol, percent=60): return mask +def skull_strip(vol): + """Skull-stripped volume, rescaled by its maximum (as written to 'noskull volume' by remove_skull.py).""" + vol = np.asarray(vol, dtype=np.float32) + vol = remove_skull_mask(vol) * vol + if np.max(vol) <= 0: + raise ValueError(f"skull stripping left no brain voxel in a volume of shape {vol.shape}") + return vol / np.max(vol) + + def connected_components_to_spheres(vol, vox2met): """ Takes a binary volume and returns a sphere for each connected components diff --git a/scripts/predict.py b/scripts/predict.py index 0ea7fe5..30d952f 100755 --- a/scripts/predict.py +++ b/scripts/predict.py @@ -1,11 +1,15 @@ #!/usr/bin/env python3 -"""Predict the test patients with a trained model; configuration in configs/predict.yaml. +"""Predict with a trained model; configuration in configs/predict.yaml. -Example: python scripts/predict.py train_dir=/data/0Work/exp1 +Examples: + python scripts/predict.py train_dir=/data/0Work/exp1 # test patients of the split + python scripts/predict.py train_dir=/data/0Work/exp1 input=scan.nii.gz # any scan, or a folder of scans Data and model settings come from the training's resolved configuration (/.hydra/config.yaml). -Predicted volumes are written to /prediction/test. +For each case, writes the probability map .nii.gz and the detections _detections.csv (and .fcsv, +for 3D Slicer). """ +import glob import os import hydra @@ -13,35 +17,72 @@ from omegaconf import OmegaConf import deepaneseg.data.io as dio -from deepaneseg.inference.prediction import predict_patients +from deepaneseg.inference.prediction import find_detections, predict_volume, save_detections from deepaneseg.utils import get_logger, load_model +from deepaneseg.volume.selection import skull_strip + +logger = get_logger("Prediction") + + +def scan_name(path): + return os.path.basename(path).removesuffix(".gz").removesuffix(".nii") + + +def input_scans(path): + if os.path.isdir(path): + files = sorted(glob.glob(os.path.join(path, "*.nii")) + glob.glob(os.path.join(path, "*.nii.gz"))) + if not files: + raise FileNotFoundError(f"No .nii or .nii.gz file in {path}") + return files + if not os.path.isfile(path): + raise FileNotFoundError(f"No such scan or folder: {path}") + return [path] + + +def cases(cfg, normalize): + """Yields (name, normalized volume, affine) for the input scans or the test patients.""" + if cfg.input is not None: + for path in input_scans(cfg.input): + data, affine = dio.read_nii_from_file(path) + if cfg.skull_strip: + data = skull_strip(data) + yield scan_name(path), dio.normalize_volume(data, normalize), affine + return + train_cfg = OmegaConf.load(os.path.join(cfg.train_dir, ".hydra", "config.yaml")) + _, _, test_list = dio.read_split(train_cfg.data.split_file) + logger.info(f"{len(test_list)} patients for testing, '{cfg.volume}' with '{normalize}' normalization") + for patient in dio.read_patient_data_base(test_list, volume=cfg.volume, normalize=normalize): + yield os.path.basename(patient["dir"]), patient["data"], patient["affine"] @hydra.main(version_base="1.3", config_path="../configs", config_name="predict") def main(cfg): - logger = get_logger("Prediction") train_cfg = OmegaConf.load(os.path.join(cfg.train_dir, ".hydra", "config.yaml")) - - _, _, test_list = dio.read_split(train_cfg.data.split_file) - logger.info(f"{len(test_list)} Patients for testing") - test_db = dio.read_patient_data_base(test_list, volume=cfg.volume, normalize=train_cfg.data.normalize) - logger.info(f"'{cfg.volume}' successfully loaded with '{train_cfg.data.normalize}' normalization") + default_output = os.path.join(cfg.train_dir, "prediction", "test" if cfg.input is None else "scans") + output_dir = cfg.output_dir or default_output + os.makedirs(output_dir, exist_ok=True) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = load_model(train_cfg.model, os.path.join(cfg.train_dir, cfg.checkpoint)).to(device) model.eval() logger.info(f"Model '{cfg.checkpoint}' loaded on '{device}' in evaluation mode") - predict_patients( - test_db, - device=device, - model=model, - patch_size=list(train_cfg.data.patch_size), - patch_shape=list(train_cfg.data.patch_shape), - output_dir=os.path.join(cfg.train_dir, "prediction", "test"), - margin=cfg.margin, - batch_size=cfg.batch_size or train_cfg.validation_batch_size, - ) + for name, data, affine in cases(cfg, train_cfg.data.normalize): + prediction = predict_volume( + data, + affine, + device, + model, + patch_size=list(train_cfg.data.patch_size), + patch_shape=list(train_cfg.data.patch_shape), + margin=cfg.margin, + batch_size=cfg.batch_size or train_cfg.validation_batch_size, + name=name, + ) + dio.save_nii_to_file(os.path.join(output_dir, name + ".nii.gz"), prediction, affine) + detections = find_detections(prediction, affine, threshold=cfg.threshold, min_size=cfg.min_size) + save_detections(detections, os.path.join(output_dir, name + "_detections.csv")) + logger.info(f"{name}: {len(detections)} detections written to {output_dir}") if __name__ == "__main__": diff --git a/scripts/remove_skull.py b/scripts/remove_skull.py index a87aa68..2a04a68 100644 --- a/scripts/remove_skull.py +++ b/scripts/remove_skull.py @@ -21,10 +21,7 @@ def remove_skull(patient_dir, output_name): with open(config_file) as f: config = json.load(f) ni_vol = ni.load(os.path.join(patient_dir, config["init volume"])) - vol = np.asarray(ni_vol.dataobj).astype(np.float32) - - vol = vs.remove_skull_mask(vol) * vol - vol /= np.max(vol) + vol = vs.skull_strip(np.asarray(ni_vol.dataobj)) ni.Nifti1Image(vol, ni_vol.affine).to_filename(os.path.join(patient_dir, output_name)) config["noskull volume"] = output_name diff --git a/tests/test_predict_scans.py b/tests/test_predict_scans.py new file mode 100644 index 0000000..a6e7e5c --- /dev/null +++ b/tests/test_predict_scans.py @@ -0,0 +1,122 @@ +import json +import subprocess +import sys +from pathlib import Path + +import nibabel as ni +import numpy as np +import pandas as pd +import pytest +import torch +from hydra import compose, initialize_config_dir +from omegaconf import OmegaConf + +from deepaneseg.inference.prediction import find_detections, save_detections +from deepaneseg.utils import get_model + +ROOT = Path(__file__).parents[1] +AFFINE = np.diag([0.5, 0.5, 0.5, 1.0]) +AFFINE[:3, 3] = [-10, 20, 5] + + +def test_find_detections_reports_each_component_in_mm(): + prediction = np.zeros((40, 40, 40)) + prediction[10:14, 10:14, 10:14] = 0.9 # 64 voxels, 2 mm wide + prediction[30, 30, 30] = 0.7 # single voxel + prediction[20, 5, 5] = 0.3 # below threshold + + detections = find_detections(prediction, AFFINE, threshold=0.5) + + assert len(detections) == 2 + big = detections.sort_values("voxels").iloc[-1] + np.testing.assert_allclose(big[["x", "y", "z"]].to_numpy(float), [-10 + 5.75, 20 + 5.75, 5 + 5.75]) + assert (big["voxels"], big["radius"], big["probability"]) == (64, pytest.approx(0.75), pytest.approx(0.9)) + assert len(find_detections(prediction, AFFINE, threshold=0.5, min_size=2)) == 1 + + +def test_save_detections_writes_csv_and_slicer_markups(tmp_path): + detections = find_detections(np.pad(np.ones((2, 2, 2)), 3), AFFINE) + + save_detections(detections, str(tmp_path / "scan_detections.csv")) + + assert len(pd.read_csv(tmp_path / "scan_detections.csv")) == 1 + markups = (tmp_path / "scan_detections.fcsv").read_text().splitlines() + assert markups[0].startswith("# Markups fiducial file") and len(markups) == 4 # 3 header lines + 1 point + + +@pytest.fixture +def trained(tmp_path): + """A tiny untrained model with its saved training configuration.""" + with initialize_config_dir(config_dir=str(ROOT / "configs"), version_base="1.3"): + cfg = compose( + "train", + [ + f"data_dir={tmp_path}", + "name=exp", + "model.f_maps=4", + "data.patch_shape=[16,16,16]", + "data.patch_size=[16,16,16]", + ], + ) + train_dir = tmp_path / "0Work" / "exp" + (train_dir / ".hydra").mkdir(parents=True) + OmegaConf.save(cfg, train_dir / ".hydra" / "config.yaml") + torch.save({"model_state_dict": get_model(cfg.model).state_dict()}, train_dir / "last_checkpoint.pytorch") + return train_dir + + +def predict(*args): + return subprocess.run( + [sys.executable, str(ROOT / "scripts" / "predict.py"), *map(str, args)], capture_output=True, text=True + ) + + +def write_scan(path, shape=(20, 18, 22)): + ni.save(ni.Nifti1Image(np.random.default_rng(0).random(shape).astype(np.float32), AFFINE), path) + + +def test_predict_a_single_scan(trained, tmp_path): + write_scan(tmp_path / "scan.nii.gz") + + result = predict(f"train_dir={trained}", f"input={tmp_path / 'scan.nii.gz'}", "skull_strip=false", "margin=2") + + assert result.returncode == 0, result.stderr[-2000:] + output = trained / "prediction" / "scans" + prediction = ni.load(output / "scan.nii.gz") + assert prediction.shape == (20, 18, 22) and np.allclose(prediction.affine, AFFINE) + assert (output / "scan_detections.csv").exists() and (output / "scan_detections.fcsv").exists() + + +def test_predict_a_folder_of_scans_into_a_chosen_folder(trained, tmp_path): + (tmp_path / "scans").mkdir() + write_scan(tmp_path / "scans" / "a.nii.gz") + write_scan(tmp_path / "scans" / "b.nii") + + result = predict( + f"train_dir={trained}", + f"input={tmp_path / 'scans'}", + f"output_dir={tmp_path / 'out'}", + "skull_strip=false", + "margin=2", + ) + + assert result.returncode == 0, result.stderr[-2000:] + assert sorted(p.name for p in (tmp_path / "out").glob("*.nii.gz")) == ["a.nii.gz", "b.nii.gz"] + + +def test_predict_the_test_split_also_writes_detections(trained, tmp_path): + patient = tmp_path / "P0001" + patient.mkdir() + write_scan(patient / "noskull.nii.gz") + (patient / "config.json").write_text( + json.dumps({"init volume": "noskull.nii.gz", "noskull volume": "noskull.nii.gz"}) + ) + (tmp_path / "0Work" / "split_pats.json").write_text( + json.dumps({"training list": [], "validation list": [], "testing list": [str(patient)]}) + ) + + result = predict(f"train_dir={trained}", "margin=2") + + assert result.returncode == 0, result.stderr[-2000:] + output = trained / "prediction" / "test" + assert (output / "P0001.nii.gz").exists() and (output / "P0001_detections.csv").exists() diff --git a/tests/test_volume.py b/tests/test_volume.py index acd732d..568e327 100644 --- a/tests/test_volume.py +++ b/tests/test_volume.py @@ -104,3 +104,10 @@ def test_fill_between_edges_fills_each_line_inclusively(): assert np.flatnonzero(mask[:, 1, 0]).tolist() == [5] assert mask[:, 2, 1].all() assert mask.sum() == 5 + 1 + 10 # untouched lines stay empty + + +def test_skull_strip_refuses_a_volume_it_would_empty(): + from deepaneseg.volume.selection import skull_strip + + with pytest.raises(ValueError, match="skull stripping"): + skull_strip(np.random.default_rng(0).random((20, 20, 20)))