@@ -297,6 +297,16 @@ def prep_conversion(
297
297
)
298
298
299
299
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
+
300
310
def update_complex_name (metadata : dict [str , Any ], filename : str ) -> str :
301
311
"""
302
312
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:
330
340
if any (ut in filename for ut in unsupported_types ):
331
341
return filename
332
342
333
- # Check to see if it is magnitude or phase part:
334
343
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 ()
343
354
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
+ )
345
358
346
359
# Determine scan suffix
347
360
filetype = "_" + filename .split ("_" )[- 1 ]
@@ -979,13 +992,12 @@ def save_converted_files(
979
992
is_uncombined = (
980
993
len (set (filter (bool , channel_names ))) > 1
981
994
) # Check for uncombined data
982
- CPLX_PARTS = ["MAGNITUDE" , "PHASE" , "IMAGINARY" , "REAL" ]
983
995
is_complex = len (
984
996
set (
985
997
[
986
998
part
987
999
for its in image_types
988
- for part in CPLX_PARTS
1000
+ for part in IMAGETYPE_TO_PARTS . keys ()
989
1001
if part in its or part [0 ] in its
990
1002
]
991
1003
)
0 commit comments