1- import os . path
1+ from pathlib import Path
22from pydra import ShellCommandTask
33from pydra .engine .specs import ShellSpec , ShellOutSpec , File , Directory , SpecInfo
44
55
6- def dcm2niix_out_file (out_dir , filename , echo , compress ):
6+ def dcm2niix_out_file (out_dir , filename , echo , suffix , compress ):
77 # Append echo number of NIfTI echo to select is provided
8- if echo :
9- echo_suffix = f"_e{ echo } "
8+ if suffix :
9+ file_suffix = "_" + suffix
10+ elif echo :
11+ file_suffix = f"_e{ echo } "
1012 else :
11- echo_suffix = ""
13+ file_suffix = ""
1214
13- out_file = f"{ out_dir } /{ filename } { echo_suffix } .nii"
15+ out_file = f"{ out_dir } /{ filename } { file_suffix } .nii"
1416
1517 # If compressed, append the zip extension
1618 if compress in ("y" , "o" , "i" ):
1719 out_file += ".gz"
1820
19- return os .path .abspath (out_file )
21+ out_file = Path (out_file ).absolute ()
22+
23+ # Check to see if multiple echos exist in the DICOM dataset
24+ if not out_file .exists ():
25+ echoes = [
26+ str (p )
27+ for p in out_file .parent .iterdir ()
28+ if p .stem .startswith (filename + "_e" )
29+ ]
30+ if echoes :
31+ raise ValueError (
32+ "DICOM dataset contains multiple echos, please specify which "
33+ "echo you want via the 'echo' input:\n "
34+ "\n " .join (echoes )
35+ )
36+
37+ return out_file
2038
2139
2240def dcm2niix_out_json (out_dir , filename , echo ):
@@ -26,7 +44,7 @@ def dcm2niix_out_json(out_dir, filename, echo):
2644 else :
2745 echo_suffix = ""
2846
29- return os . path . abspath (f"{ out_dir } /{ filename } { echo_suffix } .json" )
47+ return Path (f"{ out_dir } /{ filename } { echo_suffix } .json" ). absolute ( )
3048
3149
3250input_fields = [
@@ -65,6 +83,22 @@ def dcm2niix_out_json(out_dir, filename, echo):
6583 "echoes are discovered in the dataset then dcm2niix will create "
6684 "separate files for each echo with the suffix '_e<echo-number>.nii'"
6785 ),
86+ "xor" : ["suffix" ],
87+ },
88+ ),
89+ (
90+ "suffix" ,
91+ str ,
92+ {
93+ "argstr" : "" ,
94+ "help_string" : (
95+ "A suffix to append to the out_file, used to select which "
96+ "of the disambiguated outputs to return (see https://github.com/"
97+ "rordenlab/dcm2niix/blob/master/FILENAMING.md"
98+ "#file-name-post-fixes-image-disambiguation) "
99+ ),
100+ "xor" : ["echo" ],
101+ "allowed_values" : ["Eq" , "ph" , "imaginary" , "MoCo" , "real" , "phMag" ],
68102 },
69103 ),
70104 (
0 commit comments