@@ -523,6 +523,34 @@ def update_uncombined_name(
523523 return filename
524524
525525
526+ def update_multiorient_name (
527+ metadata : dict [str , Any ],
528+ filename : str ,
529+ ) -> str :
530+ if "acq-" in filename :
531+ lgr .warning (
532+ "Not embedding multi-orientation information as prefix already uses acq- parameter."
533+ )
534+ return filename
535+ iop = metadata .get ("ImageOrientationPatientDICOM" )
536+ iop = [round (x ) for x in iop ]
537+ cross_prod = [
538+ iop [1 ] * iop [5 ] - iop [2 ] * iop [4 ],
539+ iop [2 ] * iop [3 ] - iop [0 ] * iop [5 ],
540+ iop [0 ] * iop [4 ] - iop [1 ] * iop [3 ],
541+ ]
542+ cross_prod = [abs (x ) for x in cross_prod ]
543+ slice_orient = ["sagittal" , "coronal" , "axial" ][cross_prod .index (1 )]
544+ bids_pairs = filename .split ("_" )
545+ # acq needs to be inserted right after sub- or ses-
546+ ses_or_sub_idx = sum (
547+ [bids_pair .split ("-" )[0 ] in ["sub" , "ses" ] for bids_pair in bids_pairs ]
548+ )
549+ bids_pairs .insert (ses_or_sub_idx , "acq-%s" % slice_orient )
550+ filename = "_" .join (bids_pairs )
551+ return filename
552+
553+
526554def convert (
527555 items : list [tuple [str , tuple [str , ...], list [str ]]],
528556 converter : str ,
@@ -953,6 +981,7 @@ def save_converted_files(
953981 echo_times : set [float ] = set ()
954982 channel_names : set [str ] = set ()
955983 image_types : set [str ] = set ()
984+ iops : set [str ] = set ()
956985 for metadata in bids_metas :
957986 if not metadata :
958987 continue
@@ -968,6 +997,10 @@ def save_converted_files(
968997 image_types .update (metadata ["ImageType" ])
969998 except KeyError :
970999 pass
1000+ try :
1001+ iops .update (str (metadata ["ImageOrientationPatientDICOM" ]))
1002+ except KeyError :
1003+ pass
9711004
9721005 is_multiecho = (
9731006 len (set (filter (bool , echo_times ))) > 1
@@ -978,6 +1011,7 @@ def save_converted_files(
9781011 is_complex = (
9791012 "M" in image_types and "P" in image_types
9801013 ) # Determine if data are complex (magnitude + phase)
1014+ is_multiorient = len (iops ) > 1
9811015 echo_times_lst = sorted (echo_times ) # also converts to list
9821016 channel_names_lst = sorted (channel_names ) # also converts to list
9831017
@@ -1008,6 +1042,11 @@ def save_converted_files(
10081042 bids_meta , this_prefix_basename , channel_names_lst
10091043 )
10101044
1045+ if is_multiorient :
1046+ this_prefix_basename = update_multiorient_name (
1047+ bids_meta , this_prefix_basename
1048+ )
1049+
10111050 # Fallback option:
10121051 # If we have failed to modify this_prefix_basename, because it didn't fall
10131052 # into any of the options above, just add the suffix at the end:
0 commit comments