-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepareDataNNUNet.py
63 lines (44 loc) · 2.11 KB
/
prepareDataNNUNet.py
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
# Prepare data for 2D nnUNet
import numpy as np
import os
import nibabel as nib
root_dir = '/vol/biomedic3/kc2322/'
data_dir = os.path.join(root_dir, 'data/MSDPancreas/MSDPancreas/imagesTrMIRTK')
img_dir = os.path.join(root_dir, 'data/MSDPancreas/MSDPancreas/nnUNet/imagesTr')
lab_dir = os.path.join(root_dir, 'data/MSDPancreas/MSDPancreas/nnUNet/labelsTr')
def prep2DData():
# list the sub-folders in the data directory
folders = os.listdir(data_dir)
for f in folders:
if f.startswith("pancreas"):
try:
print(f)
# open label and image
img_nii = nib.load(os.path.join(data_dir, f, f + "_warp.nii.gz"))
gt_nii = nib.load(os.path.join(data_dir, f, f + "_label_warp.nii.gz"))
gt = gt_nii.get_fdata()
# flatten label and find extent
gt_flat = np.sum(gt, axis=(0, 1))
indicies = np.where(gt_flat > 0)[0]
# Find where extent of pancreas slice is greatest
x = np.argmax(gt_flat)
# save the slices around the middle (along wih labels)
for i in range(3):
slice_idx = x - i
gt_slice = gt_nii.slicer[:, :, slice_idx:slice_idx + 1]
img_slice = img_nii.slicer[:, :, slice_idx:slice_idx + 1]
nib.save(gt_slice, os.path.join(lab_dir, f + "{0}.nii.gz".format(slice_idx)))
nib.save(img_slice, os.path.join(img_dir, f + "{0}_0000.nii.gz".format(slice_idx)))
for i in range(1, 3):
slice_idx = x + i
gt_slice = gt_nii.slicer[:, :, slice_idx:slice_idx + 1]
img_slice = img_nii.slicer[:, :, slice_idx:slice_idx + 1]
nib.save(gt_slice, os.path.join(lab_dir, f + "{0}.nii.gz".format(slice_idx)))
nib.save(img_slice, os.path.join(img_dir, f + "{0}_0000.nii.gz".format(slice_idx)))
except:
print(f + " failed")
continue
def main():
prep2DData()
if __name__ == "__main__":
main()