Skip to content

Commit

Permalink
Merge pull request #141 from oesteban/fix/parser-exceptions
Browse files Browse the repository at this point in the history
FIX: Allow setting nonstandard spaces for parser
  • Loading branch information
effigies authored Nov 22, 2019
2 parents 200ef2e + cfbc59a commit bad6ce6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions smriprep/cli/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ def test_template_parser(monkeypatch):

with pytest.raises(ValueError):
u._template(['MNI152NLin6Asym:res-2', 'UnkownTemplate:res-2'])

with pytest.raises(ValueError):
u._template(['MNI152NLin6Asym:res-2', 'func'])

assert u._template(['MNI152NLin6Asym:res-2', 'fsnative']) == \
OrderedDict([('MNI152NLin6Asym', {'res': '2'}), ('fsnative', None)])

u.ParseTemplates.set_nonstandard_spaces('func')
assert u._template(['MNI152NLin2009cAsym:res-2', 'func']) == \
OrderedDict([('MNI152NLin2009cAsym', {'res': '2'}), ('func', None)])

u.ParseTemplates.set_nonstandard_spaces(['func', 'fsnative'])
assert u._template(['MNI152NLin2009cAsym:res-2', 'func', 'fsnative']) == \
OrderedDict([('MNI152NLin2009cAsym', {'res': '2'}),
('func', None), ('fsnative', None)])
18 changes: 17 additions & 1 deletion smriprep/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""CLI Utilities."""
from argparse import Action
from ..conf import TF_TEMPLATES as _TF_TEMPLATES
from ..conf import TF_TEMPLATES as _TF_TEMPLATES, LEGACY_SPACES


class ParseTemplates(Action):
"""Manipulate a string of templates with modifiers."""

EXCEPTIONS = tuple()

def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, _template(values))

@classmethod
def set_nonstandard_spaces(cls, inlist):
"""Set permissible nonstandard spaces names."""
if isinstance(inlist, str):
inlist = [inlist]

cls.EXCEPTIONS = tuple(inlist)


def _template(inlist):
"""Return an OrderedDict with templates."""
Expand All @@ -34,6 +44,12 @@ def output_space(value):
mitems = modifier.split('-', 1)
spec[mitems[0]] = len(mitems) == 1 or mitems[1]

if template in ParseTemplates.EXCEPTIONS:
return template, None

if template in LEGACY_SPACES:
return template, None

if template not in _TF_TEMPLATES:
raise ValueError("""\
Template identifier "{}" not found. Please, make sure TemplateFlow is \
Expand Down
2 changes: 2 additions & 0 deletions smriprep/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
from templateflow.api import templates as _get_templates

TF_TEMPLATES = tuple(_get_templates())
LEGACY_SPACES = ('fsnative', 'fsaverage4', 'fsaverage5',
'fsaverage6', 'fsaverage')

0 comments on commit bad6ce6

Please sign in to comment.