|
a |
|
b/src/preprocess_b0.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 |
# Function to preprocess the dwi cases |
|
|
11 |
def process_trainingdata(dwib0_arr): |
|
|
12 |
count = 0 |
|
|
13 |
for b0 in dwib0_arr: |
|
|
14 |
img = nib.load(b0) |
|
|
15 |
imgF32 = img.get_fdata().astype(np.float32) |
|
|
16 |
''' Intensity based segmentation of MR images is hampered by radio frerquency field |
|
|
17 |
inhomogeneity causing intensity variation. The intensity range is typically |
|
|
18 |
scaled between the highest and lowest signal in the Image. Intensity values |
|
|
19 |
of the same tissue can vary between scans. The pixel value in images must be |
|
|
20 |
scaled prior to providing the images as input to CNN. The data is projected in to |
|
|
21 |
a predefined range [0,1] ''' |
|
|
22 |
p = np.percentile(imgF32, 99) |
|
|
23 |
imgF32_sagittal = imgF32 / p # sagittal view |
|
|
24 |
imgF32_sagittal[ imgF32_sagittal < 0 ] = sys.float_info.epsilon |
|
|
25 |
imgF32_sagittal[ imgF32_sagittal > 1 ] = 1 |
|
|
26 |
imgF32_coronal = np.swapaxes(imgF32_sagittal,0,1) # coronal view |
|
|
27 |
imgF32_axial = np.swapaxes(imgF32_sagittal,0,2) # Axial view |
|
|
28 |
|
|
|
29 |
# dwi volume data is written to the binary file |
|
|
30 |
imgF32_sagittal.tofile(sagittal_f_handle) |
|
|
31 |
imgF32_coronal.tofile(coronal_f_handle) |
|
|
32 |
imgF32_axial.tofile(axial_f_handle) |
|
|
33 |
|
|
|
34 |
print('Case ' + str(count) + ' done') |
|
|
35 |
count = count + 1 |
|
|
36 |
|
|
|
37 |
# Closing the binary file |
|
|
38 |
sagittal_f_handle.close() |
|
|
39 |
axial_f_handle.close() |
|
|
40 |
coronal_f_handle.close() |
|
|
41 |
|
|
|
42 |
# parser module for input arguments |
|
|
43 |
SUFFIX_TXT = "txt" |
|
|
44 |
parser = argparse.ArgumentParser() |
|
|
45 |
parser.add_argument('-i', action='store', dest='b0', type=str, |
|
|
46 |
help="txt file containing list of /path/to/b0, one path in each line") |
|
|
47 |
args = parser.parse_args() |
|
|
48 |
|
|
|
49 |
try: |
|
|
50 |
args = parser.parse_args() |
|
|
51 |
if len(sys.argv) == 1: |
|
|
52 |
parser.print_help() |
|
|
53 |
parser.error('too few arguments') |
|
|
54 |
sys.exit(0) |
|
|
55 |
|
|
|
56 |
except SystemExit: |
|
|
57 |
sys.exit(0) |
|
|
58 |
|
|
|
59 |
if args.b0: |
|
|
60 |
f = pathlib.Path(args.b0) |
|
|
61 |
if f.exists(): |
|
|
62 |
print ("File exist") |
|
|
63 |
filename = args.b0 |
|
|
64 |
else: |
|
|
65 |
print ("File not found") |
|
|
66 |
sys.exit(1) |
|
|
67 |
|
|
|
68 |
# Input caselist.txt |
|
|
69 |
if filename.endswith(SUFFIX_TXT): |
|
|
70 |
with open(filename) as f: |
|
|
71 |
dwib0_arr = f.read().splitlines() |
|
|
72 |
|
|
|
73 |
storage = path.dirname(dwib0_arr[0]) |
|
|
74 |
# dwi cases will be written to the below binary files |
|
|
75 |
sagittal_bin_file = storage + '/sagittal-binary-dwi' |
|
|
76 |
coronal_bin_file = storage + '/coronal-binary-dwi' |
|
|
77 |
axial_bin_file = storage + '/axial-binary-dwi' |
|
|
78 |
|
|
|
79 |
# The above binary files will be converted to 3D numpy array |
|
|
80 |
sagittal_trainingdata = storage + '/sagittal-traindata-dwi.npy' |
|
|
81 |
coronal_trainingdata = storage + '/coronal-traindata-dwi.npy' |
|
|
82 |
axial_trainingdata = storage + '/axial-traindata-dwi.npy' |
|
|
83 |
|
|
|
84 |
# Open the binary file for writing |
|
|
85 |
sagittal_f_handle = open(sagittal_bin_file, 'wb') |
|
|
86 |
coronal_f_handle = open(coronal_bin_file, 'wb') |
|
|
87 |
axial_f_handle = open(axial_bin_file, 'wb') |
|
|
88 |
|
|
|
89 |
process_trainingdata(dwib0_arr) |
|
|
90 |
x_dim=len(dwib0_arr)*256 |
|
|
91 |
y_dim=256 |
|
|
92 |
z_dim=256 |
|
|
93 |
|
|
|
94 |
# Open the binary file and convert it to 3D numpy array |
|
|
95 |
merge_sagittal = np.memmap(sagittal_bin_file, dtype=np.float32, mode='r+', shape=(x_dim, y_dim, z_dim)) |
|
|
96 |
print("Saving sagittal training data to disk") |
|
|
97 |
np.save(sagittal_trainingdata, merge_sagittal) |
|
|
98 |
os.unlink(sagittal_bin_file) |
|
|
99 |
|
|
|
100 |
merge_coronal = np.memmap(coronal_bin_file, dtype=np.float32, mode='r+', shape=(x_dim, y_dim, z_dim)) |
|
|
101 |
print("Saving coronal training data to disk") |
|
|
102 |
np.save(coronal_trainingdata, merge_coronal) |
|
|
103 |
os.unlink(coronal_bin_file) |
|
|
104 |
|
|
|
105 |
merge_axial = np.memmap(axial_bin_file, dtype=np.float32, mode='r+', shape=(x_dim, y_dim, z_dim)) |
|
|
106 |
print("Saving axial training data to disk") |
|
|
107 |
np.save(axial_trainingdata, merge_axial) |
|
|
108 |
os.unlink(axial_bin_file) |