File size: 5,452 Bytes
6cc6072
 
 
aad86f7
6cc6072
 
 
 
 
 
ae43c93
6cc6072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aad86f7
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
import nibabel as nib
import numpy as np
import torch
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.patches import Patch
from monai.data import MetaTensor
from monai.transforms import Orientation, EnsureType
from monai.inferers import sliding_window_inference
from sklearn.preprocessing import MinMaxScaler

SW_ROI = [128, 128, 128]
SW_BATCH_SIZE = 2

region_cmap = ListedColormap(["none", "yellow", "green", "red"])


# ---------------------------------------------------------------------------
# Preprocessing (matches training pipeline exactly)
# ---------------------------------------------------------------------------

def normalize(x: np.ndarray) -> np.ndarray:
    scaler = MinMaxScaler(feature_range=(0, 1))
    normalized_1D_array = scaler.fit_transform(x.reshape(-1, x.shape[-1]))
    return normalized_1D_array.reshape(x.shape)


def reorient_to_ras(x: MetaTensor) -> MetaTensor:
    return Orientation(axcodes="RAS")(x)


def convert_metatensor_to_numpy(x: MetaTensor) -> np.ndarray:
    return EnsureType(data_type="numpy", track_meta=False)(x)


def crop_brats2021_volume(x: np.ndarray) -> np.ndarray:
    return x[:, 56:184, 56:184, 13:141]


def preprocess_single_modality(fp):
    nifti_data = nib.load(fp)
    data = nifti_data.get_fdata()
    affine = nifti_data.affine

    data = normalize(x=data)
    data = data[np.newaxis, ...]  # (240,240,155) -> (1,240,240,155)

    data = MetaTensor(x=data, affine=affine)
    data = reorient_to_ras(data)
    data = convert_metatensor_to_numpy(data)
    data = crop_brats2021_volume(data)  # -> (1, 128, 128, 128)
    return data


def preprocess_volume(t1_path, t1ce_path, t2_path, flair_path):
    # channel order matches training: Flair, T1w, T1gd, T2w
    flair = preprocess_single_modality(flair_path).swapaxes(1, 3)
    t1w = preprocess_single_modality(t1_path).swapaxes(1, 3)
    t1gd = preprocess_single_modality(t1ce_path).swapaxes(1, 3)
    t2w = preprocess_single_modality(t2_path).swapaxes(1, 3)

    volume = np.concatenate((flair, t1w, t1gd, t2w), axis=0).astype(np.float32)
    return volume, None


# ---------------------------------------------------------------------------
# Inference
# ---------------------------------------------------------------------------

def run_inference(model, volume, device):
    tensor = torch.from_numpy(volume).unsqueeze(0).to(device)  # (1, 4, D, H, W)

    with torch.no_grad():
        output = sliding_window_inference(
            inputs=tensor,
            roi_size=SW_ROI,
            sw_batch_size=SW_BATCH_SIZE,
            predictor=model,
            overlap=0.5,
        )
        pred = (torch.sigmoid(output) > 0.5).float()

    return pred[0].cpu().numpy()  # (3, D, H, W) -- channel order: TC, WT, ET


# ---------------------------------------------------------------------------
# Visualization helpers
# ---------------------------------------------------------------------------

def combine_regions(vol_3ch):
    tc, wt, et = vol_3ch[0], vol_3ch[1], vol_3ch[2]
    combined = np.zeros_like(wt)
    combined[wt > 0] = 1   # whole tumor / edema region -> yellow
    combined[tc > 0] = 2   # tumor core -> green
    combined[et > 0] = 3   # enhancing tumor -> red
    return combined


def find_best_slice(pred):
    """Pick the D-axis slice with the most total tumor voxels (TC+WT+ET),
    so small regions like TC/ET are actually visible instead of relying
    on the raw geometric middle slice."""
    combined_mask = (pred[0] > 0) | (pred[1] > 0) | (pred[2] > 0)
    voxels_per_slice = combined_mask.sum(axis=(1, 2))
    return int(voxels_per_slice.argmax())


def make_slice_figure(volume, pred, save_path="preview.png"):
    """3-panel region breakdown: WT, TC, ET overlaid on the FLAIR slice
    (no standalone plain-FLAIR panel)."""
    mid = find_best_slice(pred)
    flair_slice = volume[0][mid]  # channel 0 = FLAIR, used only as anatomical background

    pred_tc, pred_wt, pred_et = pred[0][mid], pred[1][mid], pred[2][mid]

    rows = [
        ("Whole Tumor (WT)", pred_wt),
        ("Tumor Core (TC)", pred_tc),
        ("Enhancing Tumor (ET)", pred_et),
    ]

    fig, axes = plt.subplots(1, 3, figsize=(15, 5))

    for i, (region_name, region_slice) in enumerate(rows):
        axes[i].imshow(flair_slice, cmap="gray")
        axes[i].imshow(region_slice, cmap="Reds", alpha=0.5)
        axes[i].set_title(region_name, fontsize=13)
        axes[i].axis("off")

    plt.tight_layout()
    plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white")
    plt.close(fig)
    return save_path


def make_combined_figure(volume, pred, save_path="combined.png"):
    mid = find_best_slice(pred)
    flair_slice = volume[0][mid]
    pred_combined = combine_regions(pred)[mid]

    fig, ax = plt.subplots(figsize=(7, 7))
    ax.imshow(flair_slice, cmap="gray")
    ax.imshow(pred_combined, cmap=region_cmap, alpha=0.6, vmin=0, vmax=3)
    ax.set_title("Combined Prediction", fontsize=14)
    ax.axis("off")

    legend_elements = [
        Patch(facecolor="yellow", label="Whole Tumor (WT)"),
        Patch(facecolor="green", label="Tumor Core (TC)"),
        Patch(facecolor="red", label="Enhancing Tumor (ET)"),
    ]
    ax.legend(handles=legend_elements, loc="lower right", fontsize=9, framealpha=0.8)

    plt.tight_layout()
    plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white")
    plt.close(fig)
    return save_path