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