@@ -590,7 +590,7 @@ def get_key_info_for_fmap_assignment(json_file, matching_parameter='Shims'):
590
590
----------
591
591
json_file : str or os.path
592
592
path to the json file
593
- matching_parameter : str in ['shims', 'imaging_volume']
593
+ matching_parameter : str in AllowedFmapParameterMatching
594
594
matching_parameter that will be used to match runs
595
595
596
596
Returns:
@@ -620,7 +620,7 @@ def get_key_info_for_fmap_assignment(json_file, matching_parameter='Shims'):
620
620
return key_info
621
621
622
622
623
- def find_compatible_fmaps_for_run (json_file , fmap_groups , matching_parameter = 'Shims' ):
623
+ def find_compatible_fmaps_for_run (json_file , fmap_groups , matching_parameters = [ 'Shims' ] ):
624
624
"""
625
625
Finds compatible fmaps for a given run, for populate_intended_for.
626
626
@@ -631,36 +631,48 @@ def find_compatible_fmaps_for_run(json_file, fmap_groups, matching_parameter='Sh
631
631
fmap_groups : dict
632
632
key: prefix common to the group
633
633
value: list of all fmap paths in the group
634
- matching_parameter : str in ['shims', 'imaging_volume']
635
- matching_parameter that will be used to match runs
634
+ matching_parameters : list of str from AllowedFmapParameterMatching
635
+ matching_parameters that will be used to match runs
636
636
637
637
Returns:
638
638
-------
639
639
compatible_fmap_groups : dict
640
640
Subset of the fmap_groups which match json_file, according
641
- to the matching_parameter .
641
+ to the matching_parameters .
642
642
key: prefix common to the group
643
643
value: list of all fmap paths in the group
644
644
"""
645
- if matching_parameter not in AllowedFmapParameterMatching :
646
- raise ValueError (
647
- "Fmap matching_parameter %s not allowed." % matching_parameter
648
- )
645
+ if type (matching_parameters ) is not list :
646
+ matching_parameters = [matching_parameters ]
649
647
650
648
lgr .debug ('Looking for fmaps for %s' , json_file )
651
- json_info = get_key_info_for_fmap_assignment (json_file , matching_parameter )
649
+ json_info = {}
650
+ for param in matching_parameters :
651
+ if param not in AllowedFmapParameterMatching :
652
+ raise ValueError (
653
+ "Fmap matching_parameter %s not allowed." % param
654
+ )
655
+ json_info [param ] = get_key_info_for_fmap_assignment (json_file , param )
652
656
653
657
compatible_fmap_groups = {}
654
658
for fm_key , fm_group in fmap_groups .items ():
655
- # check the key_info for one (the first) of the fmaps in the group:
656
- fm_info = get_key_info_for_fmap_assignment (fm_group [0 ], matching_parameter )
657
- if json_info == fm_info :
659
+ # check the key_info (for all parameters) for one (the first) of
660
+ # the fmaps in the group:
661
+ compatible = False
662
+ for param in matching_parameters :
663
+ fm_info = get_key_info_for_fmap_assignment (fm_group [0 ], param )
664
+ if json_info [param ] == fm_info :
665
+ compatible = True
666
+ else :
667
+ compatible = False
668
+ continue # don't bother checking more params
669
+ if compatible :
658
670
compatible_fmap_groups [fm_key ] = fm_group
659
671
660
672
return compatible_fmap_groups
661
673
662
674
663
- def find_compatible_fmaps_for_session (path_to_bids_session , matching_parameter = 'Shims' ):
675
+ def find_compatible_fmaps_for_session (path_to_bids_session , matching_parameters = [ 'Shims' ] ):
664
676
"""
665
677
Finds compatible fmaps for all non-fmap runs in a session.
666
678
@@ -669,18 +681,21 @@ def find_compatible_fmaps_for_session(path_to_bids_session, matching_parameter='
669
681
path_to_bids_session : str or os.path
670
682
path to the session folder (or to the subject folder, if there are no
671
683
sessions).
672
- matching_parameter : str in ['shims', 'imaging_volume']
673
- matching_parameter that will be used to match runs
684
+ matching_parameters : list of str from AllowedFmapParameterMatching
685
+ matching_parameters that will be used to match runs
674
686
675
687
Returns:
676
688
-------
677
689
compatible_fmap : dict
678
690
Dict of compatible_fmaps_groups (values) for each non-fmap run (keys)
679
691
"""
680
- if matching_parameter not in AllowedFmapParameterMatching :
681
- raise ValueError (
682
- "Fmap matching_parameter %s not allowed." % matching_parameter
683
- )
692
+ if type (matching_parameters ) is not list :
693
+ matching_parameters = [matching_parameters ]
694
+ for param in matching_parameters :
695
+ if param not in AllowedFmapParameterMatching :
696
+ raise ValueError (
697
+ "Fmap matching_parameter %s not allowed." % param
698
+ )
684
699
685
700
lgr .debug ('Looking for fmaps for session: %s' , path_to_bids_session )
686
701
@@ -704,7 +719,7 @@ def find_compatible_fmaps_for_session(path_to_bids_session, matching_parameter='
704
719
705
720
# Loop through session_jsons and find the compatible fmap_groups for each
706
721
compatible_fmaps = {
707
- j : find_compatible_fmaps_for_run (j , fmap_groups , matching_parameter )
722
+ j : find_compatible_fmaps_for_run (j , fmap_groups , matching_parameters )
708
723
for j in session_jsons
709
724
}
710
725
return compatible_fmaps
@@ -722,7 +737,7 @@ def select_fmap_from_compatible_groups(json_file, compatible_fmap_groups, criter
722
737
compatible_fmap_groups : dict
723
738
fmap_groups that are compatible with the specific json_file
724
739
criterion : str in ['First', 'Closest']
725
- matching_parameter that will be used to decide which fmap to use
740
+ matching_parameters that will be used to decide which fmap to use
726
741
727
742
Returns:
728
743
-------
@@ -798,11 +813,11 @@ def select_fmap_from_compatible_groups(json_file, compatible_fmap_groups, criter
798
813
return selected_fmap_key
799
814
800
815
801
- def populate_intended_for (path_to_bids_session , matching_parameter = 'Shims' , criterion = 'Closest' ):
816
+ def populate_intended_for (path_to_bids_session , matching_parameters = 'Shims' , criterion = 'Closest' ):
802
817
"""
803
818
Adds the 'IntendedFor' field to the fmap .json files in a session folder.
804
819
It goes through the session folders and for every json file, it finds
805
- compatible_fmaps: fmaps that have the same matching_parameter as the json
820
+ compatible_fmaps: fmaps that have the same matching_parameters as the json
806
821
file (e.g., same 'Shims').
807
822
808
823
If there are more than one compatible_fmaps, it will use the criterion
@@ -816,17 +831,20 @@ def populate_intended_for(path_to_bids_session, matching_parameter='Shims', crit
816
831
path_to_bids_session : str or os.path
817
832
path to the session folder (or to the subject folder, if there are no
818
833
sessions).
819
- matching_parameter : str in ['shims', 'imaging_volume']
820
- matching_parameter that will be used to match runs
834
+ matching_parameters : list of str from AllowedFmapParameterMatching
835
+ matching_parameters that will be used to match runs
821
836
criterion : str in ['First', 'Closest']
822
- matching_parameter that will be used to decide which of the matching
837
+ matching_parameters that will be used to decide which of the matching
823
838
fmaps to use
824
839
"""
825
840
826
- if matching_parameter not in AllowedFmapParameterMatching :
827
- raise ValueError (
828
- "Fmap matching_parameter %s not allowed." % matching_parameter
829
- )
841
+ if type (matching_parameters ) is not list :
842
+ matching_parameters = [matching_parameters ]
843
+ for param in matching_parameters :
844
+ if param not in AllowedFmapParameterMatching :
845
+ raise ValueError (
846
+ "Fmap matching_parameter %s not allowed." % param
847
+ )
830
848
if criterion not in AllowedCriteriaForFmapAssignment :
831
849
raise ValueError (
832
850
"Fmap assignment criterion '%s' not allowed." % criterion
@@ -851,7 +869,7 @@ def populate_intended_for(path_to_bids_session, matching_parameter='Shims', crit
851
869
852
870
compatible_fmaps = find_compatible_fmaps_for_session (
853
871
path_to_bids_session ,
854
- matching_parameter = matching_parameter
872
+ matching_parameters = matching_parameters
855
873
)
856
874
selected_fmaps = {}
857
875
for json_file , fmap_groups in compatible_fmaps .items ():
0 commit comments