1+ import attrs
12from pathlib import Path
23from pydra import ShellCommandTask
34from pydra .engine .specs import ShellSpec , ShellOutSpec , File , Directory , SpecInfo
45
56
6- def dcm2niix_out_file (out_dir , filename , echo , suffix , compress ):
7+ def out_file_path (out_dir , filename , echo , suffix , ext ):
78 # Append echo number of NIfTI echo to select is provided
89 if suffix :
910 file_suffix = "_" + suffix
@@ -12,39 +13,37 @@ def dcm2niix_out_file(out_dir, filename, echo, suffix, compress):
1213 else :
1314 file_suffix = ""
1415
15- out_file = f"{ out_dir } /{ filename } { file_suffix } .nii"
16+ fpath = Path ( f"{ out_dir } /{ filename } { file_suffix } { ext } " ). absolute ()
1617
17- # If compressed, append the zip extension
18- if compress in ("y" , "o" , "i" ):
19- out_file += ".gz"
18+ # Check to see if multiple echos exist in the DICOM dataset
19+ if not fpath .exists ():
20+ neighbours = [str (p ) for p in fpath .parent .iterdir () if p .name .endswith (ext )]
21+ raise ValueError (
22+ f"\n Did not find expected file '{ fpath } ' after DICOM -> NIfTI "
23+ "conversion, instead found (check provided echo and suffix):\n "
24+ + "\n " .join (neighbours )
25+ )
2026
21- out_file = Path ( out_file ). absolute ()
27+ return fpath
2228
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- )
3629
37- return out_file
30+ def dcm2niix_out_file ( out_dir , filename , echo , suffix , compress ):
3831
32+ ext = ".nii"
33+ # If compressed, append the zip extension
34+ if compress in ("y" , "o" , "i" ):
35+ ext += ".gz"
36+
37+ return out_file_path (out_dir , filename , echo , suffix , ext )
3938
40- def dcm2niix_out_json (out_dir , filename , echo ):
39+
40+ def dcm2niix_out_json (out_dir , filename , echo , suffix , bids ):
4141 # Append echo number of NIfTI echo to select is provided
42- if echo :
43- echo_suffix = f"_e { echo } "
42+ if bids in ( "y" , "o" ) :
43+ fpath = out_file_path ( out_dir , filename , echo , suffix , ".json" )
4444 else :
45- echo_suffix = ""
46-
47- return Path (f"{ out_dir } /{ filename } { echo_suffix } .json" ).absolute ()
45+ fpath = attrs .NOTHING
46+ return fpath
4847
4948
5049input_fields = [
0 commit comments