1010import re
1111import shutil
1212import sys
13+ import json
1314from types import ModuleType
1415from typing import TYPE_CHECKING , Any , List , Optional , cast
1516
@@ -620,16 +621,40 @@ def convert(
620621 description = "DICOM to NIfTI + .json sidecar conversion utility" ,
621622 tags = ["implementation" ],
622623 )
623- outname , scaninfo = (prefix + "." + outtype , prefix + scaninfo_suffix )
624+ outnames = [prefix + "." + t for t in ["nii" , "nii.gz" ]]
625+ scaninfo = prefix + scaninfo_suffix
624626
625- if not op .exists (outname ) or overwrite :
627+ if not any ([ op .exists (outname ) for outname in outnames ] ) or overwrite :
626628 tmpdir = tempdirs ("dcm2niix" )
627629
630+ # demote outtype if dcmconfig explicitly disables compression
631+ # and issue a warning (user supplied mixed signals here)
632+ if dcmconfig is not None :
633+ with open (dcmconfig ) as f :
634+ dcmconfig_dict = json .loads (f .read ())
635+ if dcmconfig_dict .get ('compress' , 'y' ) == 'n' :
636+ outtype = 'nii'
637+ lgr .warning ("Demoting outtype to uncompressed nifti, because "
638+ "compress is set to 'n' in supplied dcmconfig" )
639+
628640 # run conversion through nipype
629641 res , prov_file = nipype_convert (
630642 item_dicoms , prefix , with_prov , bids_options , tmpdir , dcmconfig
631643 )
632644
645+ # try to handle compression failures from dcm2niix
646+ if outtype == 'nii.gz' :
647+ converted_files = res .outputs .converted_files
648+ if isinstance (converted_files , list ):
649+ # we do not handle mixed compression results from dcm2niix, yet
650+ # fail, if any of the outputs weren't compressed properly
651+ assert all ([x .endswith ('nii.gz' ) for x in converted_files ])
652+ else :
653+ if converted_files .endswith ('nii' ):
654+ lgr .warning ("Conversion returned uncompressed nifti (>4GB?) - "
655+ "demoting outtype to 'nii'" )
656+ outtype = 'nii'
657+
633658 bids_outfiles = save_converted_files (
634659 res ,
635660 item_dicoms ,
@@ -653,8 +678,8 @@ def convert(
653678 tempdirs .rmtree (tmpdir )
654679 else :
655680 raise RuntimeError (
656- "was asked to convert into %s but destination already exists"
657- % (outname )
681+ "was asked to convert into %s.nii[.gz] but destination already exists"
682+ % (prefix )
658683 )
659684
660685 # add the taskname field to the json file(s):
0 commit comments