-
Notifications
You must be signed in to change notification settings - Fork 520
/
dynamic_slicer.py
217 lines (185 loc) · 8.26 KB
/
dynamic_slicer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Experimental Slicer wrapper - Work in progress."""
import os
import warnings
import xml.dom.minidom
from .base import (
CommandLine,
CommandLineInputSpec,
DynamicTraitedSpec,
traits,
Undefined,
File,
isdefined,
)
class SlicerCommandLineInputSpec(DynamicTraitedSpec, CommandLineInputSpec):
module = traits.Str(desc="name of the Slicer command line module you want to use")
class SlicerCommandLine(CommandLine):
"""Experimental Slicer wrapper. Work in progress."""
_cmd = "Slicer3"
input_spec = SlicerCommandLineInputSpec
output_spec = DynamicTraitedSpec
def _grab_xml(self, module):
cmd = CommandLine(
command="Slicer3", resource_monitor=False, args="--launch %s --xml" % module
)
ret = cmd.run()
if ret.runtime.returncode == 0:
return xml.dom.minidom.parseString(ret.runtime.stdout)
else:
raise Exception(cmd.cmdline + " failed:\n%s" % ret.runtime.stderr)
def _outputs(self):
base = super(SlicerCommandLine, self)._outputs()
undefined_output_traits = {}
for key in [
node.getElementsByTagName("name")[0].firstChild.nodeValue
for node in self._outputs_nodes
]:
base.add_trait(key, File(exists=True))
undefined_output_traits[key] = Undefined
base.trait_set(trait_change_notify=False, **undefined_output_traits)
return base
def __init__(self, module, **inputs):
warnings.warn("slicer is Not fully implemented", RuntimeWarning)
super(SlicerCommandLine, self).__init__(
command="Slicer3 --launch %s " % module, name=module, **inputs
)
dom = self._grab_xml(module)
self._outputs_filenames = {}
self._outputs_nodes = []
undefined_traits = {}
for paramGroup in dom.getElementsByTagName("parameters"):
for param in paramGroup.childNodes:
if param.nodeName in ["label", "description", "#text", "#comment"]:
continue
traitsParams = {}
name = param.getElementsByTagName("name")[0].firstChild.nodeValue
longFlagNode = param.getElementsByTagName("longflag")
if longFlagNode:
traitsParams["argstr"] = (
"--" + longFlagNode[0].firstChild.nodeValue + " "
)
else:
traitsParams["argstr"] = "--" + name + " "
argsDict = {
"file": "%s",
"integer": "%d",
"double": "%f",
"float": "%f",
"image": "%s",
"transform": "%s",
"boolean": "",
"string-enumeration": "%s",
"string": "%s",
}
if param.nodeName.endswith("-vector"):
traitsParams["argstr"] += argsDict[param.nodeName[:-7]]
else:
traitsParams["argstr"] += argsDict[param.nodeName]
index = param.getElementsByTagName("index")
if index:
traitsParams["position"] = index[0].firstChild.nodeValue
desc = param.getElementsByTagName("description")
if index:
traitsParams["desc"] = desc[0].firstChild.nodeValue
name = param.getElementsByTagName("name")[0].firstChild.nodeValue
typesDict = {
"integer": traits.Int,
"double": traits.Float,
"float": traits.Float,
"image": File,
"transform": File,
"boolean": traits.Bool,
"string": traits.Str,
"file": File,
}
if param.nodeName == "string-enumeration":
type = traits.Enum
values = [
el.firstChild.nodeValue
for el in param.getElementsByTagName("element")
]
elif param.nodeName.endswith("-vector"):
type = traits.List
values = [typesDict[param.nodeName[:-7]]]
traitsParams["sep"] = ","
else:
values = []
type = typesDict[param.nodeName]
if (
param.nodeName in ["file", "directory", "image", "transform"]
and param.getElementsByTagName("channel")[0].firstChild.nodeValue
== "output"
):
self.inputs.add_trait(
name, traits.Either(traits.Bool, File, **traitsParams)
)
undefined_traits[name] = Undefined
# traitsParams["exists"] = True
self._outputs_filenames[name] = self._gen_filename_from_param(param)
# undefined_output_traits[name] = Undefined
# self._outputs().add_trait(name, File(*values, **traitsParams))
self._outputs_nodes.append(param)
else:
if param.nodeName in ["file", "directory", "image", "transform"]:
traitsParams["exists"] = True
self.inputs.add_trait(name, type(*values, **traitsParams))
undefined_traits[name] = Undefined
self.inputs.trait_set(trait_change_notify=False, **undefined_traits)
for name in list(undefined_traits.keys()):
_ = getattr(self.inputs, name)
# self._outputs().trait_set(trait_change_notify=False, **undefined_output_traits)
def _gen_filename(self, name):
if name in self._outputs_filenames:
return os.path.join(os.getcwd(), self._outputs_filenames[name])
return None
def _gen_filename_from_param(self, param):
base = param.getElementsByTagName("name")[0].firstChild.nodeValue
fileExtensions = param.getAttribute("fileExtensions")
if fileExtensions:
ext = fileExtensions
else:
ext = {"image": ".nii", "transform": ".txt", "file": ""}[param.nodeName]
return base + ext
def _list_outputs(self):
outputs = self.output_spec().get()
for output_node in self._outputs_nodes:
name = output_node.getElementsByTagName("name")[0].firstChild.nodeValue
outputs[name] = getattr(self.inputs, name)
if isdefined(outputs[name]) and isinstance(outputs[name], bool):
if outputs[name]:
outputs[name] = self._gen_filename(name)
else:
outputs[name] = Undefined
return outputs
def _format_arg(self, name, spec, value):
if name in [
output_node.getElementsByTagName("name")[0].firstChild.nodeValue
for output_node in self._outputs_nodes
]:
if isinstance(value, bool):
fname = self._gen_filename(name)
else:
fname = value
return spec.argstr % fname
return super(SlicerCommandLine, self)._format_arg(name, spec, value)
# test = SlicerCommandLine(module="BRAINSFit")
# test.inputs.fixedVolume = "/home/filo/workspace/fmri_tumour/data/pilot1/10_co_COR_3D_IR_PREP.nii"
# test.inputs.movingVolume = "/home/filo/workspace/fmri_tumour/data/pilot1/2_line_bisection.nii"
# test.inputs.outputTransform = True
# test.inputs.transformType = ["Affine"]
# print test.cmdline
# print test.inputs
# print test._outputs()
# ret = test.run()
# test = SlicerCommandLine(name="BRAINSResample")
# test.inputs.referenceVolume = "/home/filo/workspace/fmri_tumour/data/pilot1/10_co_COR_3D_IR_PREP.nii"
# test.inputs.inputVolume = "/home/filo/workspace/fmri_tumour/data/pilot1/2_line_bisection.nii"
# test.inputs.outputVolume = True
# test.inputs.warpTransform = "/home/filo/workspace/nipype/nipype/interfaces/outputTransform.mat"
# print test.cmdline
# ret = test.run()
# print ret.runtime.stderr
# print ret.runtime.returncode