Skip to content

Commit

Permalink
Merge pull request #135 from oesteban/fix/test-template-names-early
Browse files Browse the repository at this point in the history
FIX: Check template identifiers are valid early
  • Loading branch information
oesteban authored Nov 8, 2019
2 parents 845d573 + 07f0ef5 commit a3c1a0a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ include smriprep/_version.py
include smriprep/data/reports/*
include smriprep/data/boilerplate.bib
include smriprep/data/itkIdentityTransform.txt
include VERSION
include smriprep/VERSION
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,8 @@

apidoc_module_dir = '../smriprep'
apidoc_output_dir = 'api'
apidoc_excluded_paths = ['tests']
apidoc_excluded_paths = ['conftest.py', '*/tests/*', 'tests/*', 'data/*', 'conf/*']
apidoc_separate_modules = True
# apidoc_extra_args = ['--templatedir=_templates/apidoc/', '--no-headings', '--module-first', '-d 1', '-T']
apidoc_extra_args = ['--module-first', '-d 1', '-T']

# -- Options for intersphinx extension ---------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ doc =
pydotplus
sphinx >= 2.1.2
sphinx-argparse
sphinx-versioning @ git+https://github.com/rwblair/sphinxcontrib-versioning.git@39b40b0b84bf872fc398feff05344051bbce0f63
sphinx_rtd_theme
sphinxcontrib-apidoc ~= 0.3.0
sphinxcontrib-napoleon @ git+https://github.com/AleksandarPetrov/napoleon.git@0dc3f28a309ad602be5f44a9049785a1026451b3
sphinxcontrib-versioning @ git+https://github.com/rwblair/sphinxcontrib-versioning.git@39b40b0b84bf872fc398feff05344051bbce0f63
docs =
%(doc)s
duecredit = duecredit
Expand Down
11 changes: 8 additions & 3 deletions smriprep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
"""

from .__about__ import ( # noqa
__version__,
from .__about__ import (
__copyright__,
__credits__,
__version__,
)

__all__ = [
'__copyright__',
'__credits__',
'__version__',
]
Empty file added smriprep/cli/tests/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions smriprep/cli/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Test the output-spaces parser."""
from collections import OrderedDict
import pytest
from .. import utils as u


TEST_TEMPLATES = (
'MNI152NLin2009cAsym',
'MNIInfant',
'MNI152NLin6Asym',
)


def test_output_spaces(monkeypatch):
"""Check the --output-spaces argument parser."""
with monkeypatch.context() as m:
m.setattr(u, '_TF_TEMPLATES', TEST_TEMPLATES)
assert u.output_space('MNI152NLin2009cAsym') == ('MNI152NLin2009cAsym', {})
assert u.output_space('MNI152NLin2009cAsym:native') == \
('MNI152NLin2009cAsym', {'native': True})
assert u.output_space('MNI152NLin2009cAsym:res-2') == \
('MNI152NLin2009cAsym', {'res': '2'})
assert u.output_space('MNIInfant:res-2:cohort-1') == \
('MNIInfant', {'res': '2', 'cohort': '1'})

with pytest.raises(ValueError):
u.output_space('UnkownTemplate')


def test_template_parser(monkeypatch):
"""Check the --output-spaces argument parser."""
with monkeypatch.context() as m:
m.setattr(u, '_TF_TEMPLATES', TEST_TEMPLATES)

assert list(u._template(['MNI152NLin2009cAsym']).keys()) == ['MNI152NLin2009cAsym']
assert u._template(['MNI152NLin2009cAsym', 'MNI152NLin2009cAsym:res-2']) == \
OrderedDict([('MNI152NLin2009cAsym', {})])

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

with pytest.raises(ValueError):
u._template(['MNI152NLin6Asym:res-2', 'UnkownTemplate:res-2'])
42 changes: 4 additions & 38 deletions smriprep/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""CLI Utilities."""
from argparse import Action
from templateflow.api import templates as get_templates

TEMPLATES = get_templates()
from ..conf import TF_TEMPLATES as _TF_TEMPLATES


class ParseTemplates(Action):
Expand All @@ -15,20 +13,7 @@ def __call__(self, parser, namespace, values, option_string=None):


def _template(inlist):
"""
Return an OrderedDict with templates.
>>> list(_template(['MNI152NLin2009cAsym']).keys())
['MNI152NLin2009cAsym']
>>> _template(['MNI152NLin2009cAsym', 'MNI152NLin2009cAsym:res-2'])
OrderedDict([('MNI152NLin2009cAsym', {})])
>>> _template(['MNI152NLin2009cAsym', 'MNI152NLin2009cAsym:res-2',
... 'MNI152NLin6Asym:res-2', 'MNI152NLin6Asym'])
OrderedDict([('MNI152NLin2009cAsym', {}), ('MNI152NLin6Asym', {'res': '2'})])
"""
"""Return an OrderedDict with templates."""
from collections import OrderedDict
if isinstance(inlist, str):
inlist = [inlist]
Expand All @@ -41,34 +26,15 @@ def _template(inlist):


def output_space(value):
"""
Parse one element of ``--output-spaces``.
>>> output_space('MNI152NLin2009cAsym')
('MNI152NLin2009cAsym', {})
>>> output_space('MNI152NLin2009cAsym:native')
('MNI152NLin2009cAsym', {'native': True})
>>> output_space('MNI152NLin2009cAsym:res-2')
('MNI152NLin2009cAsym', {'res': '2'})
>>> output_space('MNIInfant:res-2:cohort-1') == ('MNIInfant', {'res': '2', 'cohort': '1'})
True
>>> output_space('UnkownTemplate') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
ValueError:
"""
"""Parse one element of ``--output-spaces``."""
tpl_args = value.split(':')
template = tpl_args[0]
spec = {}
for modifier in tpl_args[1:]:
mitems = modifier.split('-', 1)
spec[mitems[0]] = len(mitems) == 1 or mitems[1]

if template not in TEMPLATES:
if template not in _TF_TEMPLATES:
raise ValueError("""\
Template identifier "{}" not found. Please, make sure TemplateFlow is \
correctly installed and contains the given template identifiers.""".format(template))
Expand Down
4 changes: 4 additions & 0 deletions smriprep/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""sMRIPrep settings."""
from templateflow.api import templates as _get_templates

TF_TEMPLATES = tuple(_get_templates())
6 changes: 0 additions & 6 deletions smriprep/workflows/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:

from .anatomical import init_anat_preproc_wf

__all__ = ['init_anat_preproc_wf']
4 changes: 2 additions & 2 deletions smriprep/workflows/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from nipype.interfaces.ants.base import Info as ANTsInfo

from templateflow import api as tf
from templateflow.api import get_metadata
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.ants import ImageMath
from niworkflows.interfaces.mni import RobustMNINormalization
Expand Down Expand Up @@ -103,7 +103,7 @@ def init_anat_norm_wf(

# Append template citations to description
for template, _ in templates:
template_meta = tf.get_metadata(template)
template_meta = get_metadata(template)
template_refs = ['@%s' % template.lower()]

if template_meta.get('RRID', None):
Expand Down

0 comments on commit a3c1a0a

Please sign in to comment.