Create util.py
Browse files
util.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, torch, nibabel as nib, numpy as np
|
| 2 |
+
from glob import glob
|
| 3 |
+
from torch.utils.data import DataLoader
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# function that takes four slices from 4 modalities and stack them
|
| 7 |
+
def stack_slices(t1c, t2f, t1n, t2w):
|
| 8 |
+
stacked_slices = np.stack((t1c, t2f, t1n, t2w), axis=0)
|
| 9 |
+
return stacked_slices
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# function that takes the segmentation mask and turn it into a 4 channel mask
|
| 13 |
+
def convert_to_multichannel(mask):
|
| 14 |
+
"""
|
| 15 |
+
Convert labels to multi channels based on brats classes:
|
| 16 |
+
The provided segmentation labels have values of:
|
| 17 |
+
|
| 18 |
+
1 for NCR (necrotic)
|
| 19 |
+
2 for ED (edema)
|
| 20 |
+
3 for ET (enhancing tumor)
|
| 21 |
+
0 for background.
|
| 22 |
+
The possible classes are TC (Tumor core == NCR and ET), WT (Whole tumor)
|
| 23 |
+
, ET (Enhancing tumor) and background.
|
| 24 |
+
|
| 25 |
+
"""
|
| 26 |
+
results = []
|
| 27 |
+
# merge label 1 and label 3 to construct TC
|
| 28 |
+
results.append( np.logical_or(mask == 1, mask == 3) )
|
| 29 |
+
# merge labels 1, 2 and 3 to construct WT
|
| 30 |
+
results.append( mask != 0 )
|
| 31 |
+
# merge label 3 to keep ET
|
| 32 |
+
results.append( mask == 3 )
|
| 33 |
+
# merge label 0 to keep background
|
| 34 |
+
results.append( mask == 0 )
|
| 35 |
+
|
| 36 |
+
return np.stack(results, axis=0).astype(np.uint8)
|
| 37 |
+
class MyIterableDataset(torch.utils.data.IterableDataset):
|
| 38 |
+
def __init__(self, images_t1c, images_t2f, images_t1n, images_t2w, segs):
|
| 39 |
+
self.images_t1c = images_t1c
|
| 40 |
+
self.images_t2f = images_t2f
|
| 41 |
+
self.images_t1n = images_t1n
|
| 42 |
+
self.images_t2w = images_t2w
|
| 43 |
+
self.segs = segs
|
| 44 |
+
|
| 45 |
+
def stream(self):
|
| 46 |
+
for i in range(self.start, self.end):
|
| 47 |
+
t1c = nib.load(self.images_t1c[i]).get_fdata()
|
| 48 |
+
t2f = nib.load(self.images_t2f[i]).get_fdata()
|
| 49 |
+
t1n = nib.load(self.images_t1n[i]).get_fdata()
|
| 50 |
+
t2w = nib.load(self.images_t2w[i]).get_fdata()
|
| 51 |
+
seg = nib.load(self.segs[i]).get_fdata()
|
| 52 |
+
for j in range(t1c.shape[2]):
|
| 53 |
+
if np.sum(t1c[:,:,j]) != 0:
|
| 54 |
+
yield stack_slices(t1c[:,:,j], t2f[:,:,j], t1n[:,:,j], t2w[:,:,j]), convert_to_multichannel(seg[:,:,j])
|
| 55 |
+
else:
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
def __iter__(self):
|
| 59 |
+
worker_info = torch.utils.data.get_worker_info()
|
| 60 |
+
if worker_info is None:
|
| 61 |
+
self.start = 0
|
| 62 |
+
self.end = len(self.images_t1c)
|
| 63 |
+
else:
|
| 64 |
+
per_worker = int(np.ceil(len(self.images_t1c) / float(worker_info.num_workers)))
|
| 65 |
+
self.worker_id = worker_info.id
|
| 66 |
+
self.start = self.worker_id * per_worker
|
| 67 |
+
self.end = min(self.start + per_worker, len(self.images_t1c))
|
| 68 |
+
return self.stream()
|
| 69 |
+
|
| 70 |
+
def get_MyIterableDataset(folder_name):
|
| 71 |
+
|
| 72 |
+
# check if the folder exists
|
| 73 |
+
if not os.path.exists(folder_name):
|
| 74 |
+
raise FileNotFoundError(f"Folder {folder_name} not found,current working directory: {os.getcwd()}")
|
| 75 |
+
|
| 76 |
+
images_t1c = sorted(glob(os.path.join(folder_name, "*/*-t1c.nii.gz")))
|
| 77 |
+
images_t2f = sorted(glob(os.path.join(folder_name, "*/*-t2f.nii.gz")))
|
| 78 |
+
images_t1n = sorted(glob(os.path.join(folder_name, "*/*-t1n.nii.gz")))
|
| 79 |
+
images_t2w = sorted(glob(os.path.join(folder_name, "*/*-t2w.nii.gz")))
|
| 80 |
+
|
| 81 |
+
segs = sorted(glob(os.path.join(folder_name, "*/*seg.nii.gz")))
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
number_of_scans = len(images_t1c)
|
| 85 |
+
print(f"Number of scans: {number_of_scans}")
|
| 86 |
+
number_of_slices = nib.load(images_t1c[0]).get_fdata().shape[2]
|
| 87 |
+
|
| 88 |
+
# do a train test split
|
| 89 |
+
train_size = int(0.8 * number_of_scans)
|
| 90 |
+
train_images_t1c = images_t1c[:train_size]
|
| 91 |
+
train_images_t2f = images_t2f[:train_size]
|
| 92 |
+
train_images_t1n = images_t1n[:train_size]
|
| 93 |
+
train_images_t2w = images_t2w[:train_size]
|
| 94 |
+
train_segs = segs[:train_size]
|
| 95 |
+
|
| 96 |
+
test_images_t1c = images_t1c[train_size:]
|
| 97 |
+
test_images_t2f = images_t2f[train_size:]
|
| 98 |
+
test_images_t1n = images_t1n[train_size:]
|
| 99 |
+
test_images_t2w = images_t2w[train_size:]
|
| 100 |
+
test_segs = segs[train_size:]
|
| 101 |
+
|
| 102 |
+
train_dataset = MyIterableDataset(train_images_t1c, train_images_t2f, train_images_t1n, train_images_t2w, train_segs)
|
| 103 |
+
test_dataset = MyIterableDataset(test_images_t1c, test_images_t2f, test_images_t1n, test_images_t2w, test_segs)
|
| 104 |
+
|
| 105 |
+
return train_dataset, test_dataset
|