From 44544e41165ba97f209fc253cea8d4b851e6ed76 Mon Sep 17 00:00:00 2001 From: bpinsard Date: Thu, 8 Aug 2024 14:57:32 -0400 Subject: [PATCH] add/fit-in suggestions --- heudiconv/convert.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/heudiconv/convert.py b/heudiconv/convert.py index b48e26fa..55ebd95c 100644 --- a/heudiconv/convert.py +++ b/heudiconv/convert.py @@ -297,6 +297,16 @@ def prep_conversion( ) +IMAGETYPE_TO_PARTS = { + "M": "mag", + "MAGNITUDE": "mag", + "P": "phase", + "PHASE": "phase", + "REAL": "real", + "IMAGINARY": "imag", +} + + def update_complex_name(metadata: dict[str, Any], filename: str) -> str: """ Insert `_part-` entity into filename if data are from a @@ -330,18 +340,21 @@ def update_complex_name(metadata: dict[str, Any], filename: str) -> str: if any(ut in filename for ut in unsupported_types): return filename - # Check to see if it is magnitude or phase part: img_type = cast(List[str], metadata.get("ImageType", [])) - if "M" in img_type or "MAGNITUDE" in img_type: - part = "mag" - elif "P" in img_type or "PHASE" in img_type: - part = "phase" - elif "REAL" in img_type: - part = "real" - elif "IMAGINARY" in img_type: - part = "imag" + + present_parts = set( + IMAGETYPE_TO_PARTS[tp] for tp in img_type if tp in IMAGETYPE_TO_PARTS + ) + if not present_parts: + raise RuntimeError( + f"Data type could not be inferred from the ImageType={img_type}. Known types are: {sorted(IMAGETYPE_TO_PARTS)}" + ) + elif len(present_parts) == 1: + part = present_parts.pop() else: - raise RuntimeError("Data type could not be inferred from the metadata.") + raise RuntimeError( + f"Data type could not be inferred from the ImageType={img_type}. Multiple BIDS parts matched: {present_parts}" + ) # Determine scan suffix filetype = "_" + filename.split("_")[-1] @@ -979,13 +992,12 @@ def save_converted_files( is_uncombined = ( len(set(filter(bool, channel_names))) > 1 ) # Check for uncombined data - CPLX_PARTS = ["MAGNITUDE", "PHASE", "IMAGINARY", "REAL"] is_complex = len( set( [ part for its in image_types - for part in CPLX_PARTS + for part in IMAGETYPE_TO_PARTS.keys() if part in its or part[0] in its ] )