@@ -359,3 +359,81 @@ def _list_outputs(self):
359
359
_ , fname = os .path .split (filename )
360
360
outputs ['out_files' ].append (os .path .realpath ('w%s' % fname ))
361
361
return outputs
362
+
363
+ class DicomImportInputSpec (SPMCommandInputSpec ):
364
+ in_files = InputMultiPath (
365
+ File (exists = True ),
366
+ mandatory = True ,
367
+ field = 'data' ,
368
+ desc = 'dicom files to be converted' )
369
+ output_dir_struct = traits .Enum (
370
+ 'flat' , 'series' , 'patname' , 'patid_date' , 'patid' , 'date_time' ,
371
+ field = 'root' ,
372
+ usedefault = True ,
373
+ desc = 'directory structure for the output.' )
374
+ output_dir = traits .Str ('./' ,
375
+ field = 'outdir' ,
376
+ usedefault = True ,
377
+ desc = 'output directory.' )
378
+ format = traits .Enum (
379
+ 'nii' , 'img' ,
380
+ field = 'convopts.format' ,
381
+ usedefault = True ,
382
+ desc = 'output format.' )
383
+ icedims = traits .Bool (False ,
384
+ field = 'convopts.icedims' ,
385
+ usedefault = True ,
386
+ desc = 'If image sorting fails, one can try using the additional\
387
+ SIEMENS ICEDims information to create unique filenames.\
388
+ Use this only if there would be multiple volumes with\
389
+ exactly the same file names.' )
390
+
391
+ class DicomImportOutputSpec (TraitedSpec ):
392
+ out_files = OutputMultiPath (File (exists = True ),
393
+ desc = 'converted files' )
394
+
395
+ class DicomImport (SPMCommand ):
396
+ """ Uses spm to comvert DICOM files to nii or img+hdr.
397
+
398
+ Examples
399
+ --------
400
+
401
+ >>> import nipype.interfaces.spm.utils as spmu
402
+ >>> di = spmu.DicomImport()
403
+ >>> di.inputs.in_files = ['functional_1.dcm', 'functional_2.dcm']
404
+ >>> di.run() # doctest: +SKIP
405
+ """
406
+
407
+ input_spec = DicomImportInputSpec
408
+ output_spec = DicomImportOutputSpec
409
+
410
+ _jobtype = 'util'
411
+ _jobname = 'dicom'
412
+
413
+ def _format_arg (self , opt , spec , val ):
414
+ """Convert input to appropriate format for spm
415
+ """
416
+ if opt == 'in_files' :
417
+ return np .array (val , dtype = object )
418
+ if opt == 'output_dir' :
419
+ return np .array ([val ], dtype = object )
420
+ if opt == 'output_dir' :
421
+ return os .path .abspath (val )
422
+ if opt == 'icedims' :
423
+ if val :
424
+ return 1
425
+ return 0
426
+ return super (DicomImport , self )._format_arg (opt , spec , val )
427
+
428
+ def _run_interface (self , runtime ):
429
+ od = os .path .abspath (self .inputs .output_dir )
430
+ if not os .path .isdir (od ):
431
+ os .mkdir (od )
432
+ return super (DicomImport , self )._run_interface (runtime )
433
+
434
+ def _list_outputs (self ):
435
+ from glob import glob
436
+ outputs = self ._outputs ().get ()
437
+ od = os .path .abspath (self .inputs .output_dir )
438
+ outputs ['out_files' ] = glob (os .path .join (od , '*' ))
439
+ return outputs
0 commit comments