Skip to content

Commit a54296b

Browse files
committed
fix/refactor
1 parent 7eb9594 commit a54296b

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],
@@ -929,30 +980,9 @@ def save_converted_files(
929980
# preload since will be used in multiple spots
930981
bids_metas = [load_json(b) for b in bids_files if b]
931982

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-
)
983+
res_files, bids_files, bids_metas = filter_partial_volumes(
984+
res_files, bids_files, bids_metas
985+
)
956986

957987
if isinstance(res_files, list):
958988
suffixes = (

0 commit comments

Comments
 (0)