Skip to content

Commit 01e5792

Browse files
committed
added interface for dcm2niix
1 parent c46b932 commit 01e5792

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

nipype/interfaces/dcm2nii.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,126 @@ def _gen_filename(self, name):
180180
f.close()
181181
return config_file
182182
return None
183+
184+
## dcm2niix update
185+
186+
class Dcm2niixInputSpec(CommandLineInputSpec):
187+
source_names = InputMultiPath(File(exists=True), argstr="%s", position=-1,
188+
copyfile=False, mandatory=True, xor=['source_dir'])
189+
source_dir = Directory(exists=True, argstr="%s", position=-1, mandatory=True,
190+
xor=['source_names'])
191+
out_filename = traits.Str('%q', argstr="-f %s", usedefault=True)
192+
output_dir = Directory(exists=True, argstr='-o %s', genfile=True)
193+
bids_format = traits.Bool(True, argstr='-b', usedefault=True)
194+
compress = traits.Enum('i', ['y','i','n'], argstr='-z %s', usedefault=True)
195+
single_file = traits.Bool(False, argstr='-s', usedefault=True)
196+
verbose = traits.Bool(False, argstr='-v', usedefault=True)
197+
198+
199+
200+
class Dcm2niixOutputSpec(TraitedSpec):
201+
converted_files = OutputMultiPath(File(exists=True))
202+
bvecs = OutputMultiPath(File(exists=True))
203+
bvals = OutputMultiPath(File(exists=True))
204+
bids = OutputMultiPath(File(exists=True))
205+
206+
207+
class Dcm2niix(CommandLine):
208+
"""Uses MRICRON's dcm2niix to convert dicom files
209+
Examples
210+
========
211+
>>> from nipype.interfaces.dcm2niix import Dcm2niix
212+
>>> converter = Dcm2niix()
213+
>>> converter.inputs.source_names = ['functional_1.dcm', 'functional_2.dcm']
214+
>>> converter.inputs.compress = 'i'
215+
>>> converter.inputs.output_dir = '.'
216+
>>> converter.cmdline
217+
'dcm2niix -b y -o my/output/dir -z y -s n functional_1.dcm'
218+
"""
219+
220+
input_spec = Dcm2niixInputSpec
221+
output_spec = Dcm2niixOutputSpec
222+
_cmd = 'dcm2niix'
223+
224+
def _format_arg(self, opt, spec, val):
225+
if opt in ['bids_format', 'single_file', 'verbose']:
226+
227+
spec = deepcopy(spec)
228+
229+
if val:
230+
spec.argstr += ' y'
231+
else:
232+
spec.argstr += ' n'
233+
val = True
234+
if opt == 'source_names':
235+
return spec.argstr % val[0]
236+
return super(Dcm2niix, self)._format_arg(opt, spec, val)
237+
238+
def _run_interface(self, runtime):
239+
new_runtime = super(Dcm2niix, self)._run_interface(runtime)
240+
(self.output_files,
241+
self.bvecs, self.bvals, self.bids) = self._parse_stdout(new_runtime.stdout)
242+
return new_runtime
243+
244+
def _parse_stdout(self, stdout):
245+
files = []
246+
bvecs = []
247+
bvals = []
248+
bids = []
249+
skip = False
250+
find_b = False
251+
252+
for line in stdout.split("\n"):
253+
if not skip:
254+
out_file = None
255+
if line.startswith("Convert "): # output
256+
fname = str(re.search('\S+/\S+', line).group(0))
257+
258+
if isdefined(self.inputs.output_dir):
259+
output_dir = self.inputs.output_dir
260+
else:
261+
output_dir = self._gen_filename('output_dir')
262+
263+
out_file = os.path.abspath(os.path.join(output_dir, fname))
264+
265+
# extract bvals
266+
if find_b:
267+
bvecs.append(out_file + ".bvec")
268+
bvals.append(out_file + ".bval")
269+
find_b = False
270+
271+
# next scan will have bvals/bvecs
272+
elif 'DTI gradient directions' in line:
273+
find_b = True
274+
275+
else:
276+
pass
277+
278+
if out_file:
279+
files.append(out_file + ".nii.gz")
280+
281+
if self.inputs.bids_format:
282+
bids.append(out_file + ".bids")
283+
284+
continue
285+
286+
skip = False
287+
288+
# just return what was done
289+
if not bids:
290+
return files, bvecs, bvals
291+
else:
292+
return files, bvecs, bvals, bids
293+
294+
def _list_outputs(self):
295+
outputs = self.output_spec().get()
296+
outputs['converted_files'] = self.output_files
297+
outputs['bvecs'] = self.bvecs
298+
outputs['bvals'] = self.bvals
299+
outputs['bids'] = self.bids
300+
return outputs
301+
302+
def _gen_filename(self, name):
303+
if name == 'output_dir':
304+
return os.getcwd()
305+
return None

0 commit comments

Comments
 (0)