Skip to content

Commit 3ed2c2d

Browse files
committed
rebase update_multiorient_name
1 parent 32409ed commit 3ed2c2d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

heudiconv/convert.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,34 @@ def update_uncombined_name(
521521
return filename
522522

523523

524+
def update_multiorient_name(
525+
metadata: dict[str, Any],
526+
filename: str,
527+
) -> str:
528+
if "acq-" in filename:
529+
lgr.warning(
530+
"Not embedding multi-orientation information as prefix already uses acq- parameter."
531+
)
532+
return filename
533+
iop = metadata.get("ImageOrientationPatientDICOM")
534+
iop = [round(x) for x in iop]
535+
cross_prod = [
536+
iop[1] * iop[5] - iop[2] * iop[4],
537+
iop[2] * iop[3] - iop[0] * iop[5],
538+
iop[0] * iop[4] - iop[1] * iop[3],
539+
]
540+
cross_prod = [abs(x) for x in cross_prod]
541+
slice_orient = ["sagittal", "coronal", "axial"][cross_prod.index(1)]
542+
bids_pairs = filename.split("_")
543+
# acq needs to be inserted right after sub- or ses-
544+
ses_or_sub_idx = sum(
545+
[bids_pair.split("-")[0] in ["sub", "ses"] for bids_pair in bids_pairs]
546+
)
547+
bids_pairs.insert(ses_or_sub_idx, "acq-%s" % slice_orient)
548+
filename = "_".join(bids_pairs)
549+
return filename
550+
551+
524552
def convert(
525553
items: list[tuple[str, tuple[str, ...], list[str]]],
526554
converter: str,
@@ -1029,6 +1057,7 @@ def rename_files() -> None:
10291057
echo_times: set[float] = set()
10301058
channel_names: set[str] = set()
10311059
image_types: set[str] = set()
1060+
iops: set[str] = set()
10321061
for metadata in bids_metas:
10331062
if not metadata:
10341063
continue
@@ -1044,6 +1073,12 @@ def rename_files() -> None:
10441073
image_types.update(metadata["ImageType"])
10451074
except KeyError:
10461075
pass
1076+
try:
1077+
iops.add(str(metadata["ImageOrientationPatientDICOM"]))
1078+
except KeyError:
1079+
pass
1080+
1081+
print(iops)
10471082

10481083
is_multiecho = (
10491084
len(set(filter(bool, echo_times))) > 1
@@ -1054,6 +1089,7 @@ def rename_files() -> None:
10541089
is_complex = (
10551090
"M" in image_types and "P" in image_types
10561091
) # Determine if data are complex (magnitude + phase)
1092+
is_multiorient = len(iops) > 1
10571093
echo_times_lst = sorted(echo_times) # also converts to list
10581094
channel_names_lst = sorted(channel_names) # also converts to list
10591095

@@ -1084,6 +1120,11 @@ def rename_files() -> None:
10841120
bids_meta, this_prefix_basename, channel_names_lst
10851121
)
10861122

1123+
if is_multiorient:
1124+
this_prefix_basename = update_multiorient_name(
1125+
bids_meta, this_prefix_basename
1126+
)
1127+
10871128
# Fallback option:
10881129
# If we have failed to modify this_prefix_basename, because it didn't fall
10891130
# into any of the options above, just add the suffix at the end:

heudiconv/tests/test_convert.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
bvals_are_zero,
1818
update_complex_name,
1919
update_multiecho_name,
20+
update_multiorient_name,
2021
update_uncombined_name,
2122
)
2223
from heudiconv.utils import load_heuristic
@@ -143,6 +144,18 @@ def test_update_uncombined_name() -> None:
143144
update_uncombined_name(metadata, base_fn, set(channel_names)) # type: ignore[arg-type]
144145

145146

147+
def test_update_multiorient_name() -> None:
148+
"""Unit testing for heudiconv.convert.update_multiorient_name(), which updates
149+
filenames with the acq field if appropriate.
150+
"""
151+
# Standard name update
152+
base_fn = "sub-X_ses-Y_task-Z_run-01_bold"
153+
metadata = {"ImageOrientationPatientDICOM": [0, 1, 0, 0, 0, -1]}
154+
out_fn_true = "sub-X_ses-Y_acq-sagittal_task-Z_run-01_bold"
155+
out_fn_test = update_multiorient_name(metadata, base_fn)
156+
assert out_fn_test == out_fn_true
157+
158+
146159
def test_b0dwi_for_fmap(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:
147160
"""Make sure we raise a warning when .bvec and .bval files
148161
are present but the modality is not dwi.

0 commit comments

Comments
 (0)