@@ -176,6 +176,7 @@ def __init__(
176176 silent = False ,
177177 tempdir_location = None ,
178178 ezbids = False ,
179+ ignore_dcm2niix_errors = False ,
179180 ):
180181 """
181182 This class is a simple wrapper for dcm2niix and contains methods to do the following in order:
@@ -223,7 +224,7 @@ def __init__(
223224 "dcm2niix not found, this module depends on it for conversions, exiting."
224225 )
225226 sys .exit (1 )
226-
227+ self . ignore_dcm2niix_errors = ignore_dcm2niix_errors
227228 # check for the version of dcm2niix
228229 minimum_version = "v1.0.20220720"
229230 version_string = subprocess .run ([self .dcm2niix_path , "-v" ], capture_output = True )
@@ -486,15 +487,23 @@ def run_dcm2niix(self):
486487 }
487488 self .telemetry_data .update (count_output_files (self .tempdir_location ))
488489
489- if convert .returncode != 0 :
490+ if (
491+ convert .returncode != 0
492+ or "error" in convert .stderr .decode ("utf-8" ).lower ()
493+ ) and not self .ignore_dcm2niix_errors :
490494 print (
491495 "Check output .nii files, dcm2iix returned these errors during conversion:"
492496 )
497+ # raise error if missing images is found
498+ if "missing images" in convert .stderr .decode ("utf8" ).lower ():
499+ error_message = convert .stderr .decode ("utf8" ).replace ("Error:" , "" )
500+ error_message = f"{ error_message } for dicoms in { image_folder } "
501+ raise FileNotFoundError (error_message )
493502 if (
494503 bytes ("Skipping existing file name" , "utf-8" ) not in convert .stdout
495504 or convert .stderr
496505 ):
497- print (convert .stderr )
506+ print (convert .stderr . decode ( "utf-8" ) )
498507 elif (
499508 convert .returncode != 0
500509 and bytes ("Error: Check sorted order" , "utf-8" ) in convert .stdout
@@ -1174,6 +1183,13 @@ def cli():
11741183 help = "Add fields to json output that are useful for ezBIDS or other conversion software. This will de-anonymize"
11751184 " pet2bids output and add AcquisitionDate an AcquisitionTime into the output json." ,
11761185 )
1186+ parser .add_argument (
1187+ "--ignore-dcm2niix-errors" ,
1188+ action = "store_true" ,
1189+ default = False ,
1190+ help = "Accept any NifTi produced by dcm2niix even if it contains errors. This flag should only be used for "
1191+ "batch processing and only if you're performing robust QC after the fact." ,
1192+ )
11771193 return parser
11781194
11791195
@@ -1341,6 +1357,7 @@ def main():
13411357 tempdir_location = cli_args .tempdir ,
13421358 silent = cli_args .silent ,
13431359 ezbids = cli_args .ezbids ,
1360+ ignore_dcm2niix_errors = cli_args .ignore_dcm2niix_errors ,
13441361 )
13451362
13461363 if cli_args .trc :
0 commit comments