|
a |
|
b/src/preprocess_mask.py |
|
|
1 |
#!/usr/bin/env python |
|
|
2 |
import os |
|
|
3 |
from os import path |
|
|
4 |
import sys |
|
|
5 |
import numpy as np |
|
|
6 |
import nibabel as nib |
|
|
7 |
import argparse |
|
|
8 |
import pathlib |
|
|
9 |
|
|
|
10 |
def process_trainingdata(mask_arr): |
|
|
11 |
count = 0 |
|
|
12 |
for b0_mask in mask_arr: |
|
|
13 |
img = nib.load(b0_mask) |
|
|
14 |
imgU8_sagittal = img.get_fdata().astype(np.uint8) # sagittal view |
|
|
15 |
imgU8_sagittal[ imgU8_sagittal < 0 ] = 0 |
|
|
16 |
imgU8_sagittal[ imgU8_sagittal > 1 ] = 1 |
|
|
17 |
imgU8_coronal = np.swapaxes(imgU8_sagittal,0,1) # coronal view |
|
|
18 |
imgU8_axial = np.swapaxes(imgU8_sagittal,0,2) # Axial view |
|
|
19 |
|
|
|
20 |
# dwi mask volume data is written to the binary file |
|
|
21 |
imgU8_sagittal.tofile(sagittal_f_handle) |
|
|
22 |
imgU8_coronal.tofile(coronal_f_handle) |
|
|
23 |
imgU8_axial.tofile(axial_f_handle) |
|
|
24 |
|
|
|
25 |
print('Case ' + str(count) + ' done') |
|
|
26 |
count = count + 1 |
|
|
27 |
|
|
|
28 |
# Closing the binary file |
|
|
29 |
sagittal_f_handle.close() |
|
|
30 |
axial_f_handle.close() |
|
|
31 |
coronal_f_handle.close() |
|
|
32 |
|
|
|
33 |
# parser module for input arguments |
|
|
34 |
SUFFIX_TXT = "txt" |
|
|
35 |
parser = argparse.ArgumentParser() |
|
|
36 |
parser.add_argument('-i', action='store', dest='mask', type=str, |
|
|
37 |
help="txt file containing list of /path/to/mask, one path in each line") |
|
|
38 |
args = parser.parse_args() |
|
|
39 |
|
|
|
40 |
try: |
|
|
41 |
args = parser.parse_args() |
|
|
42 |
if len(sys.argv) == 1: |
|
|
43 |
parser.print_help() |
|
|
44 |
parser.error('too few arguments') |
|
|
45 |
sys.exit(0) |
|
|
46 |
|
|
|
47 |
except SystemExit: |
|
|
48 |
sys.exit(0) |
|
|
49 |
|
|
|
50 |
if args.mask: |
|
|
51 |
f = pathlib.Path(args.mask) |
|
|
52 |
if f.exists(): |
|
|
53 |
print ("File exist") |
|
|
54 |
filename = args.mask |
|
|
55 |
else: |
|
|
56 |
print ("File not found") |
|
|
57 |
sys.exit(1) |
|
|
58 |
|
|
|
59 |
# Input caselist.txt |
|
|
60 |
if filename.endswith(SUFFIX_TXT): |
|
|
61 |
with open(filename) as f: |
|
|
62 |
mask_arr = f.read().splitlines() |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
storage = path.dirname(mask_arr[0]) |
|
|
66 |
|
|
|
67 |
# dwi cases mask will be written to the below binary files |
|
|
68 |
sagittal_bin_file = storage + '/sagittal-binary-mask' |
|
|
69 |
coronal_bin_file = storage + '/coronal-binary-mask' |
|
|
70 |
axial_bin_file = storage + '/axial-binary-mask' |
|
|
71 |
|
|
|
72 |
# The above binary files will be converted to 3D numpy array |
|
|
73 |
sagittal_trainingdata = storage + '/sagittal-traindata-mask.npy' |
|
|
74 |
coronal_trainingdata = storage + '/coronal-traindata-mask.npy' |
|
|
75 |
axial_trainingdata = storage + '/axial-traindata-mask.npy' |
|
|
76 |
|
|
|
77 |
# Open the binary file for writing |
|
|
78 |
sagittal_f_handle = open(sagittal_bin_file, 'wb') |
|
|
79 |
coronal_f_handle = open(coronal_bin_file, 'wb') |
|
|
80 |
axial_f_handle = open(axial_bin_file, 'wb') |
|
|
81 |
|
|
|
82 |
process_trainingdata(mask_arr) |
|
|
83 |
|
|
|
84 |
x_dim=len(mask_arr)*256 |
|
|
85 |
y_dim=256 |
|
|
86 |
z_dim=256 |
|
|
87 |
|
|
|
88 |
# Open the binary file and convert it to 3D numpy array |
|
|
89 |
merge_sagittal = np.memmap(sagittal_bin_file, dtype=np.uint8, mode='r+', shape=(x_dim, y_dim, z_dim)) |
|
|
90 |
print("Saving sagittal training data mask to disk") |
|
|
91 |
np.save(sagittal_trainingdata, merge_sagittal) |
|
|
92 |
os.unlink(sagittal_bin_file) |
|
|
93 |
|
|
|
94 |
merge_coronal = np.memmap(coronal_bin_file, dtype=np.uint8, mode='r+', shape=(x_dim, y_dim, z_dim)) |
|
|
95 |
print("Saving coronal training data mask to disk") |
|
|
96 |
np.save(coronal_trainingdata, merge_coronal) |
|
|
97 |
os.unlink(coronal_bin_file) |
|
|
98 |
|
|
|
99 |
merge_axial = np.memmap(axial_bin_file, dtype=np.uint8, mode='r+', shape=(x_dim, y_dim, z_dim)) |
|
|
100 |
print("Saving axial training data mask to disk") |
|
|
101 |
np.save(axial_trainingdata, merge_axial) |
|
|
102 |
os.unlink(axial_bin_file) |