-
Notifications
You must be signed in to change notification settings - Fork 0
/
animaAtlasBasedBrainExtraction.py
132 lines (103 loc) · 5.61 KB
/
animaAtlasBasedBrainExtraction.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
# Warning: works only on unix-like systems, not windows where "python animaAtlasBasedBrainExtraction.py ..." has to be run
import sys
import argparse
if sys.version_info[0] > 2:
import configparser as ConfParser
else:
import ConfigParser as ConfParser
import glob
import os
from shutil import copyfile
from subprocess import call, check_output
configFilePath = os.path.expanduser("~") + "/.anima/config.txt"
if not os.path.exists(configFilePath):
print('Please create a configuration file for Anima python scripts. Refer to the README')
quit()
configParser = ConfParser.RawConfigParser()
configParser.read(configFilePath)
animaDir = configParser.get("anima-scripts", 'anima')
animaExtraDataDir = configParser.get("anima-scripts", 'extra-data-root')
animaPyramidalBMRegistration = os.path.join(animaDir, "animaPyramidalBMRegistration")
animaDenseSVFBMRegistration = os.path.join(animaDir, "animaDenseSVFBMRegistration")
animaTransformSerieXmlGenerator = os.path.join(animaDir, "animaTransformSerieXmlGenerator")
animaApplyTransformSerie = os.path.join(animaDir, "animaApplyTransformSerie")
animaConvertImage = os.path.join(animaDir, "animaConvertImage")
animaMaskImage = os.path.join(animaDir, "animaMaskImage")
# Argument parsing
parser = argparse.ArgumentParser(
description="Computes the brain mask of images given in input by registering a known atlas on it. Their output is prefix_brainMask.nrrd and prefix_masked.nrrd")
parser.add_argument('-S', '--second-step', action='store_true',
help="Perform second step of atlas based cropping (might crop part of the external part of the brain)")
parser.add_argument('-i', '--input', type=str, required=True, help='DWI file to process')
args = parser.parse_args()
numImages = len(sys.argv) - 1
atlasImage = animaExtraDataDir + "icc_atlas/Reference_T1.nrrd"
atlasImageMasked = animaExtraDataDir + "icc_atlas/Reference_T1_masked.nrrd"
iccImage = animaExtraDataDir + "icc_atlas/BrainMask.nrrd"
brainImage = args.input
print("Brain masking image: " + brainImage)
# Get floating image prefix
brainImagePrefix = os.path.splitext(brainImage)[0]
if os.path.splitext(brainImage)[1] == '.gz':
brainImagePrefix = os.path.splitext(brainImagePrefix)[0]
# Decide on whether to use large image setting or small image setting
command = [animaConvertImage, "-i", brainImage, "-I"]
convert_output = check_output(command, universal_newlines=True)
size_info = convert_output.split('\n')[1].split('[')[1].split(']')[0]
large_image = False
for i in range(0, 3):
size_tmp = int(size_info.split(', ')[i])
if size_tmp >= 350:
large_image = True
break
pyramidOptions = ["-p", "4", "-l", "1"]
if large_image:
pyramidOptions = ["-p", "5", "-l", "2"]
# Rough mask with whole brain
command = [animaPyramidalBMRegistration, "-m", atlasImage, "-r", brainImage, "-o", brainImagePrefix + "_rig.nrrd",
"-O", brainImagePrefix + "_rig_tr.txt", "--sp", "3"] + pyramidOptions
call(command)
command = [animaPyramidalBMRegistration, "-m", atlasImage, "-r", brainImage, "-o", brainImagePrefix + "_aff.nrrd",
"-O", brainImagePrefix + "_aff_tr.txt", "-i", brainImagePrefix + "_rig_tr.txt", "--sp", "3", "--ot",
"2"] + pyramidOptions
call(command)
command = [animaDenseSVFBMRegistration, "-r", brainImage, "-m", brainImagePrefix + "_aff.nrrd", "-o",
brainImagePrefix + "_nl.nrrd", "-O", brainImagePrefix + "_nl_tr.nrrd", "--sr", "1"] + pyramidOptions
call(command)
command = [animaTransformSerieXmlGenerator, "-i", brainImagePrefix + "_aff_tr.txt", "-i",
brainImagePrefix + "_nl_tr.nrrd", "-o", brainImagePrefix + "_nl_tr.xml"]
call(command)
command = [animaApplyTransformSerie, "-i", iccImage, "-t", brainImagePrefix + "_nl_tr.xml", "-g", brainImage, "-o",
brainImagePrefix + "_rough_brainMask.nrrd", "-n", "nearest"]
call(command)
command = [animaMaskImage, "-i", brainImage, "-m", brainImagePrefix + "_rough_brainMask.nrrd", "-o",
brainImagePrefix + "_rough_masked.nrrd"]
call(command)
brainImageRoughMasked = brainImagePrefix + "_rough_masked.nrrd"
if args.second_step is True:
print("Fine mask with masked brain...")
# Fine mask with masked brain
command = [animaPyramidalBMRegistration, "-m", atlasImageMasked, "-r", brainImageRoughMasked, "-o",
brainImagePrefix + "_rig.nrrd", "-O", brainImagePrefix + "_rig_tr.txt", "--sp", "3"] + pyramidOptions
call(command)
command = [animaPyramidalBMRegistration, "-m", atlasImageMasked, "-r", brainImageRoughMasked, "-o",
brainImagePrefix + "_aff.nrrd", "-O", brainImagePrefix + "_aff_tr.txt", "-i",
brainImagePrefix + "_rig_tr.txt", "--sp", "3", "--ot", "2"] + pyramidOptions
call(command)
command = [animaDenseSVFBMRegistration, "-r", brainImageRoughMasked, "-m", brainImagePrefix + "_aff.nrrd", "-o",
brainImagePrefix + "_nl.nrrd", "-O", brainImagePrefix + "_nl_tr.nrrd", "--sr", "1"] + pyramidOptions
call(command)
command = [animaApplyTransformSerie, "-i", iccImage, "-t", brainImagePrefix + "_nl_tr.xml", "-g", brainImage, "-o",
brainImagePrefix + "_brainMask.nrrd", "-n", "nearest"]
call(command)
command = [animaMaskImage, "-i", brainImage, "-m", brainImagePrefix + "_brainMask.nrrd", "-o",
brainImagePrefix + "_masked.nrrd"]
call(command)
else:
copyfile(brainImageRoughMasked,brainImagePrefix + "_masked.nrrd")
copyfile(brainImagePrefix + "_rough_brainMask.nrrd",brainImagePrefix + "_brainMask.nrrd")
map(os.remove, glob.glob("*_rig*"))
map(os.remove, glob.glob("*_aff*"))
map(os.remove, glob.glob("*_nl*"))
map(os.remove, glob.glob("*_rough*"))