11import attrs
2+ import typing as ty
23from pathlib import Path
34from pydra import ShellCommandTask
45from pydra .engine .specs import ShellSpec , ShellOutSpec , File , Directory , SpecInfo
@@ -9,19 +10,26 @@ def out_file_path(out_dir, filename, suffix, ext):
910 created by Dcm2niix (see https://github.com/rordenlab/dcm2niix/blob/master/FILENAMING.md)
1011 """
1112
12- fpath = Path (f"{ out_dir } /{ filename } { suffix } { ext } " ).absolute ()
13+ fpath = Path (out_dir ) / filename
14+ fpath = fpath .with_suffix ((suffix if suffix else "" ) + ext ).absolute ()
1315
1416 # Check to see if multiple echos exist in the DICOM dataset
1517 if not fpath .exists ():
16- neighbours = [str (p ) for p in fpath .parent .iterdir () if p .name .endswith (ext )]
17- raise ValueError (
18- f"\n Did not find expected file '{ fpath } ' (suffix={ suffix } ) "
19- "after DICOM -> NIfTI conversion, please see "
20- "https://github.com/rordenlab/dcm2niix/blob/master/FILENAMING.md for the "
21- "list of suffixes that dcm2niix produces and provide an appropriate "
22- "suffix. Found the following files with matching extensions:\n "
23- + "\n " .join (neighbours )
24- )
18+ if suffix is not None : # NB: doesn't match attrs.NOTHING
19+ neighbours = [
20+ str (p ) for p in fpath .parent .iterdir () if p .name .endswith (ext )
21+ ]
22+ raise ValueError (
23+ f"\n Did not find expected file '{ fpath } ' (suffix={ suffix } ) "
24+ "after DICOM -> NIfTI conversion, please see "
25+ "https://github.com/rordenlab/dcm2niix/blob/master/FILENAMING.md for the "
26+ "list of suffixes that dcm2niix produces and provide an appropriate "
27+ "suffix, or set suffix to None to ignore matching a single file and use "
28+ "the list returned in 'out_files' instead. Found the following files "
29+ "with matching extensions:\n " + "\n " .join (neighbours )
30+ )
31+ else :
32+ fpath = attrs .NOTHING # Did not find output path and
2533
2634 return fpath
2735
@@ -45,6 +53,14 @@ def dcm2niix_out_json(out_dir, filename, suffix, bids):
4553 return fpath
4654
4755
56+ def dcm2niix_out_files (out_dir , filename ):
57+ return [
58+ str (p .absolute ())
59+ for p in Path (out_dir ).iterdir ()
60+ if p .name .startswith (filename )
61+ ]
62+
63+
4864input_fields = [
4965 (
5066 "in_dir" ,
@@ -75,12 +91,15 @@ def dcm2niix_out_json(out_dir, filename, suffix, bids):
7591 "suffix" ,
7692 str ,
7793 {
78- "argstr" : "" ,
7994 "help_string" : (
80- "A suffix to append to the out_file, used to select which "
81- "of the disambiguated outputs to return (see https://github.com/"
95+ "The suffix appended to the output filename, used to select which "
96+ "of the disambiguated nifti files created by dcm2niix to return "
97+ "in this field (see https://github.com/"
8298 "rordenlab/dcm2niix/blob/master/FILENAMING.md"
83- "#file-name-post-fixes-image-disambiguation) "
99+ "#file-name-post-fixes-image-disambiguation). Set to None to skip "
100+ "matching a single file (out_file will be set to attrs.NOTHING if the "
101+ "base filepath without suffixes doesn't exist) and handle the list of "
102+ "output files returned in 'out_files' instead."
84103 ),
85104 "xor" : ["echo" ],
86105 "allowed_values" : ["Eq" , "ph" , "imaginary" , "MoCo" , "real" , "phMag" ],
@@ -335,7 +354,10 @@ def dcm2niix_out_json(out_dir, filename, suffix, bids):
335354 "out_file" ,
336355 File ,
337356 {
338- "help_string" : "output NIfTI image" ,
357+ "help_string" : (
358+ "output NIfTI image. If multiple nifti files are created and 'suffix' "
359+ "is provided then " ,
360+ ),
339361 "callable" : dcm2niix_out_file ,
340362 "mandatory" : True ,
341363 },
@@ -344,7 +366,7 @@ def dcm2niix_out_json(out_dir, filename, suffix, bids):
344366 "out_json" ,
345367 File ,
346368 {
347- "help_string" : "output BIDS side-car JSON" ,
369+ "help_string" : "output BIDS side-car JSON corresponding to 'out_file' " ,
348370 # "requires": [("bids", 'y')], FIXME: should be either 'y' or 'o'
349371 "callable" : dcm2niix_out_json ,
350372 },
@@ -365,6 +387,18 @@ def dcm2niix_out_json(out_dir, filename, suffix, bids):
365387 "output_file_template" : "{out_dir}/{filename}.bvec" ,
366388 },
367389 ),
390+ (
391+ "out_files" ,
392+ ty .List [File ],
393+ {
394+ "help_string" : (
395+ "all output files in a list, including files disambiguated "
396+ "by their suffixes (e.g. echoes, phase-maps, etc... see "
397+ "https://github.com/rordenlab/dcm2niix/blob/master/FILENAMING.md"
398+ ),
399+ "callable" : dcm2niix_out_files ,
400+ },
401+ ),
368402]
369403
370404Dcm2NiixOutputSpec = SpecInfo (
0 commit comments