Skip to content

Commit e8d6d8a

Browse files
committed
fix/refactor
1 parent be09025 commit e8d6d8a

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

heudiconv/convert.py

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,57 @@ def nipype_convert(
851851
return eg, prov_file
852852

853853

854+
def filter_partial_volumes(
855+
nii_files: list[str],
856+
bids_files: list[str],
857+
bids_metas: list[dict[str, Any]],
858+
):
859+
"""filter interrupted 4D scans volumes with missing slices on XA: see dcm2niix #742
860+
861+
Parameters
862+
----------
863+
nii_files : list[str]
864+
converted nifti filepaths
865+
bids_files: list[str]
866+
converted BIDS json filepaths
867+
bids_metas : list[dict[str, Any]]
868+
list of metadata dict loaded from BIDS json files
869+
870+
Returns
871+
-------
872+
nii_files
873+
filtered niftis
874+
bids_files
875+
filtered BIDS jsons
876+
bids_metas
877+
filtered BIDS metadata
878+
879+
"""
880+
partial_volumes = [not metadata.get("RawImage", True) for metadata in bids_metas]
881+
no_partial_volumes = not any(partial_volumes) or all(partial_volumes)
882+
883+
if no_partial_volumes:
884+
return nii_files, bids_files, bids_metas
885+
else:
886+
new_nii_files, new_bids_files, new_bids_metas = [], [], []
887+
for fl, bids_file, bids_meta, is_pv in zip(
888+
nii_files, bids_files, bids_metas, partial_volumes
889+
):
890+
if is_pv:
891+
# remove partial volume
892+
os.remove(fl)
893+
os.remove(bids_file)
894+
lgr.warning(f"dropped {fl} partial volume from interrupted series")
895+
else:
896+
new_nii_files.append(fl)
897+
new_bids_files.append(bids_file)
898+
new_bids_metas.append(bids_meta)
899+
print(bids_file)
900+
if len(new_nii_files) == 1:
901+
return new_nii_files[0], new_bids_files[0], new_bids_metas[0]
902+
return new_nii_files, new_bids_files, new_bids_metas
903+
904+
854905
def save_converted_files(
855906
res: Node,
856907
item_dicoms: list[str],
@@ -933,30 +984,9 @@ def save_converted_files(
933984
# preload since will be used in multiple spots
934985
bids_metas = [load_json(b) for b in bids_files if b]
935986

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-
)
987+
res_files, bids_files, bids_metas = filter_partial_volumes(
988+
res_files, bids_files, bids_metas
989+
)
960990

961991
if isinstance(res_files, list):
962992
suffixes = (

0 commit comments

Comments
 (0)