|
a |
|
b/src/registration.py |
|
|
1 |
#!/usr/bin/env python |
|
|
2 |
import os |
|
|
3 |
from multiprocessing import Process, Manager, Value, Pool |
|
|
4 |
import multiprocessing as mp |
|
|
5 |
import subprocess |
|
|
6 |
import argparse |
|
|
7 |
import pathlib |
|
|
8 |
import sys |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
def ANTS_rigid_body_trans(b0_nii, result, mask_file, reference): |
|
|
12 |
|
|
|
13 |
print("Performing ants rigid body transformation...") |
|
|
14 |
input_file = b0_nii |
|
|
15 |
case_name = os.path.basename(input_file) |
|
|
16 |
output_name = case_name[:len(case_name) - (len(SUFFIX_NIFTI_GZ) + 1)] + '-' |
|
|
17 |
output_file = os.path.join(os.path.dirname(input_file), output_name) |
|
|
18 |
|
|
|
19 |
trans_matrix = "antsRegistrationSyNQuick.sh -d 3 -f " + reference + " -m " + input_file + " -t r -o " + output_file |
|
|
20 |
output1 = subprocess.check_output(trans_matrix, shell=True) |
|
|
21 |
|
|
|
22 |
omat_name = case_name[:len(case_name) - (len(SUFFIX_NIFTI_GZ) + 1)] + '-0GenericAffine.mat' |
|
|
23 |
omat_file = os.path.join(os.path.dirname(input_file), omat_name) |
|
|
24 |
|
|
|
25 |
output_name = case_name[:len(case_name) - (len(SUFFIX_NIFTI_GZ) + 1)] + '-Warped.nii.gz' |
|
|
26 |
transformed_file = os.path.join(os.path.dirname(input_file), output_name) |
|
|
27 |
|
|
|
28 |
result.append((transformed_file, omat_file, mask_file)) |
|
|
29 |
|
|
|
30 |
SUFFIX_TXT = "txt" |
|
|
31 |
SUFFIX_NIFTI_GZ = "nii.gz" |
|
|
32 |
parser = argparse.ArgumentParser() |
|
|
33 |
parser.add_argument('-b0', action='store', dest='b0', type=str, |
|
|
34 |
help="txt file containing list of /path/to/b0, one path in each line") |
|
|
35 |
parser.add_argument('-mask', action='store', dest='mask', type=str, |
|
|
36 |
help="txt file containing list of /path/to/mask, one path in each line") |
|
|
37 |
parser.add_argument('-ref', action='store', dest='ref', type=str, |
|
|
38 |
help="reference b0 file for registration") |
|
|
39 |
|
|
|
40 |
args = parser.parse_args() |
|
|
41 |
reference = str(args.ref) |
|
|
42 |
|
|
|
43 |
try: |
|
|
44 |
args = parser.parse_args() |
|
|
45 |
if len(sys.argv) == 1: |
|
|
46 |
parser.print_help() |
|
|
47 |
parser.error('too few arguments') |
|
|
48 |
sys.exit(0) |
|
|
49 |
|
|
|
50 |
except SystemExit: |
|
|
51 |
sys.exit(0) |
|
|
52 |
|
|
|
53 |
if args.b0: |
|
|
54 |
f = pathlib.Path(args.b0) |
|
|
55 |
if f.exists(): |
|
|
56 |
print ("File exist") |
|
|
57 |
filename = args.b0 |
|
|
58 |
else: |
|
|
59 |
print ("File not found") |
|
|
60 |
sys.exit(1) |
|
|
61 |
|
|
|
62 |
# Input caselist.txt |
|
|
63 |
if filename.endswith(SUFFIX_TXT): |
|
|
64 |
with open(filename) as f: |
|
|
65 |
target_list = f.read().splitlines() |
|
|
66 |
|
|
|
67 |
if args.mask: |
|
|
68 |
f = pathlib.Path(args.mask) |
|
|
69 |
if f.exists(): |
|
|
70 |
print ("File exist") |
|
|
71 |
filename = args.mask |
|
|
72 |
else: |
|
|
73 |
print ("File not found") |
|
|
74 |
sys.exit(1) |
|
|
75 |
|
|
|
76 |
# Input caselist.txt |
|
|
77 |
if filename.endswith(SUFFIX_TXT): |
|
|
78 |
with open(filename) as f: |
|
|
79 |
mask_list = f.read().splitlines() |
|
|
80 |
|
|
|
81 |
with Manager() as manager: |
|
|
82 |
result = manager.list() |
|
|
83 |
ants_jobs = [] |
|
|
84 |
for i in range(0, len(target_list)): |
|
|
85 |
p_ants = mp.Process(target=ANTS_rigid_body_trans, args=(target_list[i], |
|
|
86 |
result, mask_list[i], reference)) |
|
|
87 |
ants_jobs.append(p_ants) |
|
|
88 |
p_ants.start() |
|
|
89 |
|
|
|
90 |
for process in ants_jobs: |
|
|
91 |
process.join() |
|
|
92 |
|
|
|
93 |
result = list(result) |
|
|
94 |
|
|
|
95 |
transformed_cases = [] |
|
|
96 |
omat_list = [] |
|
|
97 |
masks_new_list = [] |
|
|
98 |
|
|
|
99 |
for subject_ANTS in result: |
|
|
100 |
transformed_cases.append(subject_ANTS[0]) |
|
|
101 |
omat_list.append(subject_ANTS[1]) |
|
|
102 |
masks_new_list.append(subject_ANTS[2]) |
|
|
103 |
|
|
|
104 |
# Apply the same tranformation to the mask file |
|
|
105 |
for i in range(0, len(transformed_cases)): |
|
|
106 |
|
|
|
107 |
input_file = transformed_cases[i] |
|
|
108 |
case_name = os.path.basename(input_file) |
|
|
109 |
output_name = case_name[:len(case_name) - (len(SUFFIX_NIFTI_GZ) + 1)] + '-mask.nii.gz' |
|
|
110 |
output_file = os.path.join(os.path.dirname(masks_new_list[i]), output_name) |
|
|
111 |
apply_mask_trans = "antsApplyTransforms -d 3 -i " + masks_new_list[i] + " -r " + input_file + " -o " \ |
|
|
112 |
+ output_file + " --transform [" + omat_list[i] + "]" |
|
|
113 |
|
|
|
114 |
output2 = subprocess.check_output(apply_mask_trans, shell=True) |