Skip to content

Commit 49c5c8f

Browse files
committed
fix/refactor
1 parent 0de8f52 commit 49c5c8f

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
@@ -849,6 +849,57 @@ def nipype_convert(
849849
return eg, prov_file
850850

851851

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

939-
# interrupted scans on XA: see dcm2niix #742
940-
partial_volumes = [
941-
bool(metadata.get("RawImage", True)) for metadata in bids_metas
942-
]
943-
has_partial_volumes = any(partial_volumes) and not all(partial_volumes)
944-
945-
if has_partial_volumes:
946-
for fl, bids_meta, bids_file, is_pv in zip(
947-
res_files, bids_metas, bids_files, partial_volumes
948-
):
949-
if is_pv:
950-
# remove partial volume
951-
os.remove(fl)
952-
os.remove(bids_file)
953-
res_files.remove(fl)
954-
bids_files.remove(bids_file)
955-
bids_metas.remove(bids_meta)
956-
lgr.warning(f"dropped {fl} partial volume from interrupted series")
957-
if len(res_files) == 1:
958-
res_files, bids_files, bids_metas = (
959-
res_files[0],
960-
bids_files[0],
961-
bids_metas[0],
962-
)
990+
res_files, bids_files, bids_metas = filter_partial_volumes(
991+
res_files, bids_files, bids_metas
992+
)
963993

964994
if isinstance(res_files, list):
965995
suffixes = (

0 commit comments

Comments
 (0)