@@ -1820,14 +1820,24 @@ def build_path(filepath, entities, out_dir):
18201820 ... )
18211821 '/output/sub-01/ses-01/func/sub-01_ses-01_task-rest_acq-VAR_bold.nii.gz'
18221822
1823+ It can change the datatype, but will warn the user.
1824+ >>> build_path(
1825+ ... "/input/sub-01/ses-01/anat/sub-01_ses-01_asl.nii.gz",
1826+ ... {"datatype": "perf", "acquisition": "VAR", "suffix": "asl"},
1827+ ... "/output",
1828+ ... )
1829+ WARNING: DATATYPE CHANGE DETECTED
1830+ '/output/sub-01/ses-01/perf/sub-01_ses-01_acq-VAR_asl.nii.gz'
1831+
18231832 It expects a longitudinal structure, so providing a cross-sectional filename won't work.
1833+ XXX: This is a bug.
18241834 >>> build_path(
18251835 ... "/input/sub-01/func/sub-01_task-rest_run-01_bold.nii.gz",
18261836 ... {"task": "rest", "acquisition": "VAR", "echo": 1, "suffix": "bold"},
18271837 ... "/output",
18281838 ... )
18291839 Traceback (most recent call last):
1830- TypeError: can only concatenate str (not "NoneType") to str
1840+ ValueError: Could not extract subject or session from ...
18311841 """
18321842 exts = Path (filepath ).suffixes
18331843 old_ext = "" .join (exts )
@@ -1844,11 +1854,12 @@ def build_path(filepath, entities, out_dir):
18441854
18451855 sub = get_key_name (filepath , "sub" )
18461856 ses = get_key_name (filepath , "ses" )
1847- sub_ses = sub + "_" + ses
1848-
18491857 if "run" in entities .keys () and "run-0" in filepath :
18501858 # XXX: This adds an extra leading zero to run.
18511859 entities ["run" ] = "0" + str (entities ["run" ])
1860+ if sub is None or ses is None :
1861+ raise ValueError (f"Could not extract subject or session from { filepath } " )
1862+
18521863
18531864 filename = "_" .join ([f"{ key } -{ entities [key ]} " for key in entity_file_keys ])
18541865 filename = (
@@ -1857,7 +1868,7 @@ def build_path(filepath, entities, out_dir):
18571868 .replace ("reconstruction" , "rec" )
18581869 )
18591870 if len (filename ) > 0 :
1860- filename = sub_ses + "_" + filename + "_" + suffix + old_ext
1871+ filename = f" { sub } _ { ses } _ { filename } _ { suffix } { old_ext } "
18611872 else :
18621873 raise ValueError (f"Could not construct new filename for { filepath } " )
18631874
@@ -1872,10 +1883,10 @@ def build_path(filepath, entities, out_dir):
18721883 if "datatype" in entities .keys ():
18731884 dtype_new = entities ["datatype" ]
18741885 if entities ["datatype" ] != dtype_orig :
1875- print ("WARNING: DATATYPE CHANGE DETECETD " )
1886+ print ("WARNING: DATATYPE CHANGE DETECTED " )
18761887 else :
18771888 dtype_new = dtype_orig
18781889
18791890 # Construct the new filename
1880- new_path = out_dir + "/" + sub + "/" + ses + "/" + dtype_new + "/" + filename
1891+ new_path = str ( Path ( out_dir ) / sub / ses / dtype_new / filename )
18811892 return new_path
0 commit comments