1- # Copyright (C) 2016-2024 C-PAC Developers
1+ # Copyright (C) 2016-2025 C-PAC Developers
22
33# This file is part of C-PAC.
44
@@ -324,7 +324,8 @@ def bids_retrieve_params(bids_config_dict, f_dict, dbg=False):
324324
325325 for k , v in params .items ():
326326 if isinstance (v , str ):
327- params [k ] = v .encode ("ascii" , errors = "ignore" )
327+ # Force all strings to be ASCII-compatible UTF-8 (by removing all non-ASCII characters)
328+ params [k ] = v .encode ("ascii" , errors = "ignore" ).decode ("ascii" )
328329
329330 return params
330331
@@ -802,12 +803,10 @@ def bids_gen_cpac_sublist(
802803 return sublist
803804
804805
805- def collect_bids_files_configs (bids_dir , aws_input_creds = "" ):
806- """
807- :param bids_dir:
808- :param aws_input_creds:
809- :return:
810- """
806+ def collect_bids_files_configs (
807+ bids_dir : str , aws_input_creds : Optional [str ] = ""
808+ ) -> tuple [list [str ], dict [str , Any ]]:
809+ """Collect NIfTI file paths and JSON configurations from a BIDS directory."""
811810 file_paths = []
812811 config_dict = {}
813812
@@ -853,7 +852,7 @@ def collect_bids_files_configs(bids_dir, aws_input_creds=""):
853852 except Exception as e :
854853 msg = (
855854 f"Error retrieving { s3_obj .key .replace (prefix , '' )} "
856- f" ({ e . message } )"
855+ f" ({ getattr ( e , ' message' , str ( e )) } )"
857856 )
858857 raise SpecifiedBotoCoreError (msg ) from e
859858 elif "nii" in str (s3_obj .key ):
@@ -862,7 +861,7 @@ def collect_bids_files_configs(bids_dir, aws_input_creds=""):
862861 )
863862
864863 else :
865- for root , dirs , files in os .walk (bids_dir , topdown = False , followlinks = True ):
864+ for root , _dirs , files in os .walk (bids_dir , topdown = False , followlinks = True ):
866865 if files :
867866 for f in files :
868867 for suf in suffixes :
@@ -1086,26 +1085,26 @@ def cl_strip_brackets(arg_list):
10861085
10871086
10881087def create_cpac_data_config (
1089- bids_dir ,
1090- participant_labels = None ,
1091- aws_input_creds = None ,
1092- skip_bids_validator = False ,
1093- only_one_anat = True ,
1094- ):
1088+ bids_dir : str ,
1089+ participant_labels : Optional [ list [ str ]] = None ,
1090+ aws_input_creds : Optional [ str ] = None ,
1091+ skip_bids_validator : bool = False ,
1092+ only_one_anat : bool = True ,
1093+ ) -> list :
10951094 """
10961095 Create a C-PAC data config YAML file from a BIDS directory.
10971096
10981097 Parameters
10991098 ----------
1100- bids_dir : str
1099+ bids_dir
11011100
1102- participant_labels : list or None
1101+ participant_labels
11031102
11041103 aws_input_creds
11051104
1106- skip_bids_validator : bool
1105+ skip_bids_validator
11071106
1108- only_one_anat : bool
1107+ only_one_anat
11091108 The "anat" key for a subject expects a string value, but we
11101109 can temporarily store a list instead by passing True here if
11111110 we will be filtering that list down to a single string later
@@ -1129,8 +1128,10 @@ def create_cpac_data_config(
11291128 ]
11301129
11311130 if not file_paths :
1132- UTLOGGER .error ("Did not find data for %s" , ", " .join (participant_labels ))
1133- sys .exit (1 )
1131+ if participant_labels :
1132+ UTLOGGER .error ("Did not find data for %s" , ", " .join (participant_labels ))
1133+ msg = f"Did not find data in { bids_dir } "
1134+ raise FileNotFoundError (msg )
11341135
11351136 raise_error = not skip_bids_validator
11361137
@@ -1145,7 +1146,8 @@ def create_cpac_data_config(
11451146
11461147 if not sub_list :
11471148 UTLOGGER .error ("Did not find data in %s" , bids_dir )
1148- sys .exit (1 )
1149+ msg = f"Did not find data in { bids_dir } "
1150+ raise FileNotFoundError (msg )
11491151
11501152 return sub_list
11511153
0 commit comments