Skip to content

Commit d53e50b

Browse files
committed
Check arguments for populate_intended_for functions only at top level
Each of the auxiliary functions don't need to do it again.
1 parent 51d1a96 commit d53e50b

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

heudiconv/bids.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ def find_fmap_groups(fmap_dir):
585585
def get_key_info_for_fmap_assignment(json_file, matching_parameter):
586586
"""
587587
Gets key information needed to assign fmaps to other modalities.
588+
(Note: It is the responsibility of the calling function to make sure
589+
the arguments are OK)
588590
589591
Parameters:
590592
----------
@@ -603,10 +605,6 @@ def get_key_info_for_fmap_assignment(json_file, matching_parameter):
603605
raise FileNotFoundError(
604606
errno.ENOENT, os.strerror(errno.ENOENT), json_file
605607
)
606-
if matching_parameter not in AllowedFmapParameterMatching:
607-
raise ValueError(
608-
"Fmap matching_parameter %s not allowed." % matching_parameter
609-
)
610608

611609
# loop through the possible criteria and extract the info needed
612610
if matching_parameter == 'Shims':
@@ -636,13 +634,18 @@ def get_key_info_for_fmap_assignment(json_file, matching_parameter):
636634
# We want to force the matching, so just return some string
637635
# regardless of the image
638636
key_info = [KeyInfoForForce]
637+
else:
638+
# fallback:
639+
key_info = []
639640

640641
return key_info
641642

642643

643644
def find_compatible_fmaps_for_run(json_file, fmap_groups, matching_parameters):
644645
"""
645646
Finds compatible fmaps for a given run, for populate_intended_for.
647+
(Note: It is the responsibility of the calling function to make sure
648+
the arguments are OK)
646649
647650
Parameters:
648651
----------
@@ -662,16 +665,9 @@ def find_compatible_fmaps_for_run(json_file, fmap_groups, matching_parameters):
662665
key: prefix common to the group
663666
value: list of all fmap paths in the group
664667
"""
665-
if type(matching_parameters) is not list:
666-
matching_parameters = [matching_parameters]
667-
668668
lgr.debug('Looking for fmaps for %s', json_file)
669669
json_info = {}
670670
for param in matching_parameters:
671-
if param not in AllowedFmapParameterMatching:
672-
raise ValueError(
673-
"Fmap matching_parameter %s not allowed." % param
674-
)
675671
json_info[param] = get_key_info_for_fmap_assignment(json_file, param)
676672

677673
compatible_fmap_groups = {}
@@ -698,6 +694,8 @@ def find_compatible_fmaps_for_run(json_file, fmap_groups, matching_parameters):
698694
def find_compatible_fmaps_for_session(path_to_bids_session, matching_parameters):
699695
"""
700696
Finds compatible fmaps for all non-fmap runs in a session.
697+
(Note: It is the responsibility of the calling function to make sure
698+
the arguments are OK)
701699
702700
Parameters:
703701
----------
@@ -712,15 +710,6 @@ def find_compatible_fmaps_for_session(path_to_bids_session, matching_parameters)
712710
compatible_fmap : dict
713711
Dict of compatible_fmaps_groups (values) for each non-fmap run (keys)
714712
"""
715-
if not isinstance(matching_parameters, list):
716-
assert isinstance(matching_parameters, str), "matching_parameters must be a str or a list, got %s" % matching_parameters
717-
matching_parameters = [matching_parameters]
718-
for param in matching_parameters:
719-
if param not in AllowedFmapParameterMatching:
720-
raise ValueError(
721-
"Fmap matching_parameter %s not allowed." % param
722-
)
723-
724713
lgr.debug('Looking for fmaps for session: %s', path_to_bids_session)
725714

726715
# Resolve path (eliminate '..')
@@ -753,6 +742,8 @@ def select_fmap_from_compatible_groups(json_file, compatible_fmap_groups, criter
753742
"""
754743
Selects the fmap that will be used to correct for distortions in json_file
755744
from the compatible fmap_groups list, based on the given criterion
745+
(Note: It is the responsibility of the calling function to make sure
746+
the arguments are OK)
756747
757748
Parameters:
758749
----------
@@ -768,11 +759,6 @@ def select_fmap_from_compatible_groups(json_file, compatible_fmap_groups, criter
768759
selected_fmap_key : str or os.path
769760
key from the compatible_fmap_groups for the selected fmap group
770761
"""
771-
if criterion not in AllowedCriteriaForFmapAssignment:
772-
raise ValueError(
773-
"Fmap assignment criterion '%s' not allowed." % criterion
774-
)
775-
776762
if len(compatible_fmap_groups) == 0:
777763
return None
778764
# if compatible_fmap_groups has only one entry, that's it:
@@ -862,7 +848,8 @@ def populate_intended_for(path_to_bids_session, matching_parameters, criterion):
862848
fmaps to use
863849
"""
864850

865-
if type(matching_parameters) is not list:
851+
if not isinstance(matching_parameters, list):
852+
assert isinstance(matching_parameters, str), "matching_parameters must be a str or a list, got %s" % matching_parameters
866853
matching_parameters = [matching_parameters]
867854
for param in matching_parameters:
868855
if param not in AllowedFmapParameterMatching:

heudiconv/tests/test_bids.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ def test_get_key_info_for_fmap_assignment(tmpdir):
144144
)
145145

146146
# Finally: invalid matching_parameters:
147-
with pytest.raises(ValueError):
148-
assert get_key_info_for_fmap_assignment(
147+
assert get_key_info_for_fmap_assignment(
149148
json_name, matching_parameter='Invalid'
150-
)
149+
) == []
151150

152151

153152
def generate_scans_tsv(session_struct):
@@ -709,7 +708,7 @@ def test_find_compatible_fmaps_for_run(tmpdir, simulation_function, match_param)
709708
compatible_fmaps = find_compatible_fmaps_for_run(
710709
json_file,
711710
expected_fmap_groups,
712-
matching_parameters=match_param
711+
matching_parameters=[match_param]
713712
)
714713
assert compatible_fmaps == expected_compatible_fmaps[json_file]
715714

@@ -757,7 +756,7 @@ def test_find_compatible_fmaps_for_session(
757756
session_folder = op.join(str(tmpdir), folder)
758757
_, _, _, expected_compatible_fmaps = simulation_function(session_folder)
759758

760-
compatible_fmaps = find_compatible_fmaps_for_session(session_folder, matching_parameters=match_param)
759+
compatible_fmaps = find_compatible_fmaps_for_session(session_folder, matching_parameters=[match_param])
761760

762761
assert compatible_fmaps == expected_compatible_fmaps
763762

0 commit comments

Comments
 (0)