@@ -887,6 +887,7 @@ def save_converted_files(
887
887
888
888
bids_outfiles : list [str ] = []
889
889
res_files = res .outputs .converted_files
890
+ bids_files = res .outputs .bids
890
891
891
892
if not len (res_files ):
892
893
lgr .debug ("DICOMs {} were not converted" .format (item_dicoms ))
@@ -922,6 +923,42 @@ def save_converted_files(
922
923
# we should provide specific handling for fmap,
923
924
# dwi etc which might spit out multiple files
924
925
926
+ # Also copy BIDS files although they might need to
927
+ # be merged/postprocessed later
928
+ bids_files = (
929
+ sorted (bids_files )
930
+ if len (bids_files ) == len (res_files )
931
+ else [None ] * len (res_files )
932
+ )
933
+ # preload since will be used in multiple spots
934
+ bids_metas = [load_json (b ) for b in bids_files if b ]
935
+
936
+ # interrupted scans on XA: see dcm2niix #742
937
+ partial_volumes = [
938
+ bool (metadata .get ("RawImage" , True )) for metadata in bids_metas
939
+ ]
940
+ has_partial_volumes = any (partial_volumes ) and not all (partial_volumes )
941
+
942
+ if has_partial_volumes :
943
+ for fl , bids_meta , bids_file , is_pv in zip (
944
+ res_files , bids_metas , bids_files , partial_volumes
945
+ ):
946
+ if is_pv :
947
+ # remove partial volume
948
+ os .remove (fl )
949
+ os .remove (bids_file )
950
+ res_files .remove (fl )
951
+ bids_files .remove (bids_file )
952
+ bids_metas .remove (bids_meta )
953
+ lgr .warning (f"dropped { fl } partial volume from interrupted series" )
954
+ if len (res_files ) == 1 :
955
+ res_files , bids_files , bids_metas = (
956
+ res_files [0 ],
957
+ bids_files [0 ],
958
+ bids_metas [0 ],
959
+ )
960
+
961
+ if isinstance (res_files , list ):
925
962
suffixes = (
926
963
[str (i + 1 ) for i in range (len (res_files ))]
927
964
if (bids_options is not None )
@@ -938,16 +975,6 @@ def save_converted_files(
938
975
)
939
976
suffixes = [str (- i - 1 ) for i in range (len (res_files ))]
940
977
941
- # Also copy BIDS files although they might need to
942
- # be merged/postprocessed later
943
- bids_files = (
944
- sorted (res .outputs .bids )
945
- if len (res .outputs .bids ) == len (res_files )
946
- else [None ] * len (res_files )
947
- )
948
- # preload since will be used in multiple spots
949
- bids_metas = [load_json (b ) for b in bids_files if b ]
950
-
951
978
### Do we have a multi-echo series? ###
952
979
# Some Siemens sequences (e.g. CMRR's MB-EPI) set the label 'TE1',
953
980
# 'TE2', etc. in the 'ImageType' field. However, other seqs do not
@@ -1040,7 +1067,7 @@ def save_converted_files(
1040
1067
safe_movefile (res_files , outname , overwrite )
1041
1068
if isdefined (res .outputs .bids ):
1042
1069
try :
1043
- safe_movefile (res . outputs . bids , outname_bids , overwrite )
1070
+ safe_movefile (bids_files , outname_bids , overwrite )
1044
1071
bids_outfiles .append (outname_bids )
1045
1072
except TypeError : ##catch lists
1046
1073
raise TypeError ("Multiple BIDS sidecars detected." )
0 commit comments