File size: 7,441 Bytes
04c01e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Turn a chest CT into model input.

Modes:
  npz       a cube already preprocessed for this model (key ``ct``); centre-cropped to the input size
  totalseg  NIfTI; lungs segmented with TotalSegmentator, then the exact preprocessing used in training
  fast      NIfTI; same preprocessing, but the lungs are found by HU threshold instead of TotalSegmentator
  center    NIfTI; same preprocessing, but the cube is centred on the middle of the volume

NIfTI preprocessing: reorient to LPS, resample to the model's spacing (trilinear), pad with air, crop a cube
around the lungs, store depth-first as raw HU int16, then centre-crop to the model's input size.
``totalseg`` needs the ``TotalSegmentator`` package and a GPU for reasonable speed; the other modes need
``monai`` and ``nibabel`` only.
"""
from __future__ import annotations

import os
import tempfile
from typing import Optional, Sequence, Tuple

import numpy as np
import torch

AIR_HU = -1024.0
LUNG_LABELS = (10, 11, 12, 13, 14)  # TotalSegmentator lung lobes
MODES = ("npz", "totalseg", "fast", "center")


def crop_starts(shape: Sequence[int], roi: Sequence[int], lo: Sequence[int], hi: Sequence[int]) -> Tuple[int, ...]:
    """Start per axis of a crop of size ``roi`` around the box ``lo``..``hi``, kept inside the volume."""
    starts = []
    for size, r, a, b in zip(shape, roi, lo, hi):
        if size <= r:
            starts.append(0)
            continue
        a, b = int(a), int(b)
        extent = b - a + 1
        start = (a + b) // 2 - r // 2 if extent >= r else a - (r - extent) // 2
        starts.append(int(np.clip(start, 0, size - r)))
    return tuple(starts)


def hu_lung_box(vol: np.ndarray) -> Optional[Tuple[np.ndarray, np.ndarray]]:
    """Bounding box of the lungs found by HU threshold, or None.

    The body is the largest connected region above -500 HU (this drops the table), filled slice by slice
    along the last (I-S) axis; the lungs are air below -400 HU strictly inside it, keeping the two
    largest connected regions.
    """
    from scipy import ndimage
    lab, n = ndimage.label(vol > -500)
    if n == 0:
        return None
    sizes = ndimage.sum_labels(np.ones_like(lab, dtype=np.int8), lab, index=np.arange(1, n + 1))
    body = lab == (int(np.argmax(sizes)) + 1)
    filled = np.zeros_like(body)
    for z in range(body.shape[2]):
        filled[:, :, z] = ndimage.binary_fill_holes(body[:, :, z])
    air = filled & ~body & (vol < -400)
    lab, n = ndimage.label(air)
    if n == 0:
        return None
    sizes = ndimage.sum_labels(np.ones_like(lab, dtype=np.int8), lab, index=np.arange(1, n + 1))
    keep = np.argsort(sizes)[::-1][:2] + 1
    lungs = np.isin(lab, keep)
    if lungs.sum() < 10000:
        return None
    coords = np.argwhere(lungs)
    return coords.min(0), coords.max(0)


def run_totalsegmentator(nifti_path: str, out_path: str, device: str = "gpu") -> str:
    """Segment the scan with TotalSegmentator (multi-label output) and return the mask path."""
    from totalsegmentator.python_api import totalsegmentator
    totalsegmentator(nifti_path, out_path, ml=True, device=device, quiet=True)
    return out_path


def preprocess_nifti(nifti_path: str, spacing_mm: float, cube_size: int, mode: str = "fast",
                     totalseg_mask: Optional[str] = None, device: str = "gpu", return_volume: bool = False):
    """NIfTI to a raw-HU int16 cube [cube, cube, cube] in the model's layout (LPS, depth-first).

    With return_volume, also returns the resampled volume (depth-first) and the crop start per axis.
    """
    from monai.transforms import (Compose, EnsureChannelFirstd, LoadImaged, Orientationd, Spacingd,
                                  SpatialPadd)
    if mode not in ("totalseg", "fast", "center"):
        raise ValueError(f"mode must be totalseg, fast or center for NIfTI input, got {mode!r}")
    tmp = None
    if mode == "totalseg" and totalseg_mask is None:
        tmp = tempfile.mkdtemp()
        totalseg_mask = run_totalsegmentator(nifti_path, os.path.join(tmp, "seg.nii.gz"), device)
    keys = ("ct", "seg") if mode == "totalseg" else ("ct",)
    roi = (cube_size,) * 3
    steps = [LoadImaged(keys=keys, reader="NibabelReader"), EnsureChannelFirstd(keys=keys),
             Orientationd(keys=keys, axcodes="LPS", labels=(("L", "R"), ("P", "A"), ("I", "S"))),
             Spacingd(keys=("ct",), pixdim=(spacing_mm,) * 3, mode="trilinear", align_corners=True)]
    if mode == "totalseg":
        steps.append(Spacingd(keys=("seg",), pixdim=(spacing_mm,) * 3, mode="nearest", align_corners=True))
    steps.append(SpatialPadd(keys=("ct",), spatial_size=roi, constant_values=AIR_HU))
    if mode == "totalseg":
        steps.append(SpatialPadd(keys=("seg",), spatial_size=roi, constant_values=0.0))
    paths = {"ct": nifti_path, **({"seg": totalseg_mask} if mode == "totalseg" else {})}
    d = Compose(steps)(paths)
    ct = np.asarray(d["ct"])[0]
    if mode == "totalseg":
        lungs = np.isin(np.asarray(d["seg"])[0], LUNG_LABELS)
        if not lungs.any():
            raise ValueError(f"{nifti_path}: TotalSegmentator found no lungs")
        coords = np.argwhere(lungs)
        starts = crop_starts(ct.shape, roi, coords.min(0), coords.max(0))
    elif mode == "fast":
        box = hu_lung_box(ct)
        if box is None:
            c = tuple(x // 2 for x in ct.shape)
            box = (c, c)
        starts = crop_starts(ct.shape, roi, box[0], box[1])
    else:
        c = tuple(s // 2 for s in ct.shape)
        starts = crop_starts(ct.shape, roi, c, c)
    full = ct
    ct = ct[tuple(slice(s, s + r) for s, r in zip(starts, roi))]
    ct = np.clip(np.round(ct), np.iinfo(np.int16).min, np.iinfo(np.int16).max)
    if tmp is not None:
        for f in os.listdir(tmp):
            os.remove(os.path.join(tmp, f))
        os.rmdir(tmp)
    cube = np.ascontiguousarray(np.transpose(ct, (2, 0, 1))).astype(np.int16)
    if return_volume:
        return cube, np.transpose(full, (2, 0, 1)), (starts[2], starts[0], starts[1])
    return cube


def center_crop(volume: np.ndarray, size: int) -> np.ndarray:
    off = [(s - size) // 2 for s in volume.shape[-3:]]
    if min(off) < 0:
        raise ValueError(f"volume {volume.shape[-3:]} is smaller than {size}^3")
    return volume[..., off[0]:off[0] + size, off[1]:off[1] + size, off[2]:off[2] + size]


def load_volume(path: str, config: dict, mode: Optional[str] = None, totalseg_mask: Optional[str] = None,
                device: str = "gpu") -> torch.Tensor:
    """Read an npz cube or a NIfTI scan and return a [1, 1, S, S, S] raw-HU tensor for the model.

    ``config`` is the model config (a dict or the loaded config object; its ``input`` block sets spacing, cube and
    crop size). ``mode``
    defaults to ``npz`` for .npz files and ``fast`` for NIfTI.
    """
    inp = config["input"] if isinstance(config, dict) else config.input
    if mode is None:
        mode = "npz" if path.endswith(".npz") else "fast"
    if mode not in MODES:
        raise ValueError(f"mode must be one of {MODES}, got {mode!r}")
    if mode == "npz":
        with np.load(path) as d:
            cube = d["ct"]
        cube = cube[0] if cube.ndim == 4 else cube
    else:
        cube = preprocess_nifti(path, inp["spacing_mm"], inp["cube_size"], mode, totalseg_mask, device)
    cube = center_crop(cube, inp["crop_size"])
    return torch.from_numpy(np.ascontiguousarray(cube)).unsqueeze(0).unsqueeze(0)