Skip to content

Commit eef6f46

Browse files
committed
add/fit-in suggestions
1 parent 7f82416 commit eef6f46

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

heudiconv/convert.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,16 @@ def prep_conversion(
297297
)
298298

299299

300+
IMAGETYPE_TO_PARTS = {
301+
"M": "mag",
302+
"MAGNITUDE": "mag",
303+
"P": "phase",
304+
"PHASE": "phase",
305+
"REAL": "real",
306+
"IMAGINARY": "imag",
307+
}
308+
309+
300310
def update_complex_name(metadata: dict[str, Any], filename: str) -> str:
301311
"""
302312
Insert `_part-<mag|phase|real|imag>` entity into filename if data are from a
@@ -330,18 +340,21 @@ def update_complex_name(metadata: dict[str, Any], filename: str) -> str:
330340
if any(ut in filename for ut in unsupported_types):
331341
return filename
332342

333-
# Check to see if it is magnitude or phase part:
334343
img_type = cast(List[str], metadata.get("ImageType", []))
335-
if "M" in img_type or "MAGNITUDE" in img_type:
336-
part = "mag"
337-
elif "P" in img_type or "PHASE" in img_type:
338-
part = "phase"
339-
elif "REAL" in img_type:
340-
part = "real"
341-
elif "IMAGINARY" in img_type:
342-
part = "imag"
344+
345+
present_parts = set(
346+
IMAGETYPE_TO_PARTS[tp] for tp in img_type if tp in IMAGETYPE_TO_PARTS
347+
)
348+
if not present_parts:
349+
raise RuntimeError(
350+
f"Data type could not be inferred from the ImageType={img_type}. Known types are: {sorted(IMAGETYPE_TO_PARTS)}"
351+
)
352+
elif len(present_parts) == 1:
353+
part = present_parts.pop()
343354
else:
344-
raise RuntimeError("Data type could not be inferred from the metadata.")
355+
raise RuntimeError(
356+
f"Data type could not be inferred from the ImageType={img_type}. Multiple BIDS parts matched: {present_parts}"
357+
)
345358

346359
# Determine scan suffix
347360
filetype = "_" + filename.split("_")[-1]
@@ -979,13 +992,12 @@ def save_converted_files(
979992
is_uncombined = (
980993
len(set(filter(bool, channel_names))) > 1
981994
) # Check for uncombined data
982-
CPLX_PARTS = ["MAGNITUDE", "PHASE", "IMAGINARY", "REAL"]
983995
is_complex = len(
984996
set(
985997
[
986998
part
987999
for its in image_types
988-
for part in CPLX_PARTS
1000+
for part in IMAGETYPE_TO_PARTS.keys()
9891001
if part in its or part[0] in its
9901002
]
9911003
)

0 commit comments

Comments
 (0)