diff --git a/niworkflows/utils/spaces.py b/niworkflows/utils/spaces.py index fc5e93e4dff..09a2b84bdf2 100644 --- a/niworkflows/utils/spaces.py +++ b/niworkflows/utils/spaces.py @@ -708,11 +708,17 @@ def __call__(self, parser, namespace, values, option_string=None): if not values: # option was called without any output spaces, so user does not want outputs spaces.checkpoint() + + invalid_spaces = [] for val in values: val = val.rstrip(':') + space = val.split(':', 1)[0] + if space in NONSTANDARD_REFERENCES and ':' in val: + invalid_spaces.append(val) + if ( - val not in NONSTANDARD_REFERENCES - and not val.split(':')[0].startswith('fs') + space not in NONSTANDARD_REFERENCES + and not space.startswith('fs') and ':res-' not in val and ':resolution-' not in val ): @@ -721,8 +727,16 @@ def __call__(self, parser, namespace, values, option_string=None): # https://github.com/nipreps/niworkflows/pull/457#discussion_r375510227 # https://github.com/nipreps/niworkflows/pull/494 val = f'{val}:res-native' + for sp in Reference.from_string(val): spaces.add(sp) + + if invalid_spaces: + raise ValueError( + 'Modifiers are not allowed for nonstandard spaces. ' + f'Invalid space(s): {", ".join(invalid_spaces)}' + ) + setattr(namespace, self.dest, spaces) diff --git a/niworkflows/utils/tests/test_spaces.py b/niworkflows/utils/tests/test_spaces.py index 1f37c56b778..ae9c4644be0 100644 --- a/niworkflows/utils/tests/test_spaces.py +++ b/niworkflows/utils/tests/test_spaces.py @@ -104,3 +104,16 @@ def test_space_action_edgecases(parser, flag, expected): pargs = parser.parse_known_args(flag)[0] spaces = pargs.spaces assert spaces.is_cached() is expected + + +@pytest.mark.parametrize( + ('spaces', 'expected'), + [ + (('func:res-01',), 'Modifiers are not allowed for nonstandard spaces.'), + (('anat:den-91k',), 'Modifiers are not allowed for nonstandard spaces.'), + (('shouldraise',), 'space identifier "shouldraise" is invalid.'), + ], +) +def test_space_action_invalid_spaces(parser, spaces, expected): + with pytest.raises(ValueError, match=expected): + parser.parse_known_args(args=('--spaces',) + spaces)[0]