Skip to content

Commit 78c15e4

Browse files
committed
fix+tst: ensure no pybids does not break testing
1 parent 96c359d commit 78c15e4

File tree

4 files changed

+85
-34
lines changed

4 files changed

+85
-34
lines changed

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ before_install:
3737
conda install python=${TRAVIS_PYTHON_VERSION} &&
3838
conda config --add channels conda-forge &&
3939
conda install -y nipype icu &&
40-
rm -r ${CONDA_HOME}/lib/python${TRAVIS_PYTHON_VERSION}/site-packages/nipype*;
41-
pushd $HOME;
42-
git clone https://github.com/INCF/pybids.git;
43-
cd pybids;
44-
pip install -e .;
45-
popd; }
40+
rm -r ${CONDA_HOME}/lib/python${TRAVIS_PYTHON_VERSION}/site-packages/nipype*; }
4641
# Add install of vtk and mayavi to test mesh (disabled): conda install -y vtk mayavi
4742
- travis_retry apt_inst
4843
- travis_retry conda_inst

nipype/interfaces/bids_utils.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
77
BIDSDataGrabber: Query data from BIDS dataset using pybids grabbids.
88
9-
Change directory to provide relative paths for doctests
10-
>>> import os
11-
>>> import bids
12-
>>> filepath = os.path.realpath(os.path.dirname(bids.__file__))
13-
>>> datadir = os.path.realpath(os.path.join(filepath, 'grabbids/tests/data/'))
14-
>>> os.chdir(datadir)
15-
169
"""
1710
from os.path import join, dirname
11+
import json
1812
from .. import logging
1913
from .base import (traits,
2014
DynamicTraitedSpec,
@@ -24,13 +18,11 @@
2418
Str,
2519
Undefined)
2620

21+
have_pybids = True
2722
try:
2823
from bids import grabbids as gb
29-
import json
3024
except ImportError:
3125
have_pybids = False
32-
else:
33-
have_pybids = True
3426

3527
LOGGER = logging.getLogger('workflows')
3628

@@ -56,36 +48,33 @@ class BIDSDataGrabber(BaseInterface):
5648
Examples
5749
--------
5850
59-
>>> from nipype.interfaces.bids_utils import BIDSDataGrabber
60-
>>> from os.path import basename
61-
6251
By default, the BIDSDataGrabber fetches anatomical and functional images
6352
from a project, and makes BIDS entities (e.g. subject) available for
6453
filtering outputs.
6554
66-
>>> bg = BIDSDataGrabber()
67-
>>> bg.inputs.base_dir = 'ds005/'
68-
>>> bg.inputs.subject = '01'
69-
>>> results = bg.run()
70-
>>> basename(results.outputs.anat[0]) # doctest: +ALLOW_UNICODE
71-
'sub-01_T1w.nii.gz'
55+
>>> bg = BIDSDataGrabber() # doctest: +SKIP
56+
>>> bg.inputs.base_dir = 'ds005/' # doctest: +SKIP
57+
>>> bg.inputs.subject = '01' # doctest: +SKIP
58+
>>> results = bg.run() # doctest # doctest: +SKIP
59+
>>> basename(results.outputs.anat[0]) # doctest: +SKIP
60+
'sub-01_T1w.nii.gz' # doctest: +SKIP
7261
73-
>>> basename(results.outputs.func[0]) # doctest: +ALLOW_UNICODE
74-
'sub-01_task-mixedgamblestask_run-01_bold.nii.gz'
62+
>>> basename(results.outputs.func[0]) # doctest: +SKIP
63+
'sub-01_task-mixedgamblestask_run-01_bold.nii.gz' # doctest: +SKIP
7564
7665
7766
Dynamically created, user-defined output fields can also be defined to
7867
return different types of outputs from the same project. All outputs
7968
are filtered on common entities, which can be explicitly defined as
8069
infields.
8170
82-
>>> bg = BIDSDataGrabber(infields = ['subject'], outfields = ['dwi'])
83-
>>> bg.inputs.base_dir = 'ds005/'
84-
>>> bg.inputs.subject = '01'
85-
>>> bg.inputs.output_query['dwi'] = dict(modality='dwi')
86-
>>> results = bg.run()
87-
>>> basename(results.outputs.dwi[0]) # doctest: +ALLOW_UNICODE
88-
'sub-01_dwi.nii.gz'
71+
>>> bg = BIDSDataGrabber(infields = ['subject'], outfields = ['dwi']) # doctest: +SKIP
72+
>>> bg.inputs.base_dir = 'ds005/' # doctest: +SKIP
73+
>>> bg.inputs.subject = '01' # doctest: +SKIP
74+
>>> bg.inputs.output_query['dwi'] = dict(modality='dwi') # doctest: +SKIP
75+
>>> results = bg.run() # doctest: +SKIP
76+
>>> basename(results.outputs.dwi[0]) # doctest: +SKIP
77+
'sub-01_dwi.nii.gz' # doctest: +SKIP
8978
9079
"""
9180
input_spec = BIDSDataGrabberInputSpec
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import json
3+
import sys
4+
5+
import pytest
6+
from nipype.interfaces.bids_utils import BIDSDataGrabber
7+
from nipype.utils.filemanip import dist_is_editable
8+
9+
have_pybids = True
10+
try:
11+
import bids
12+
from bids import grabbids as gb
13+
filepath = os.path.realpath(os.path.dirname(bids.__file__))
14+
datadir = os.path.realpath(os.path.join(filepath, 'grabbids/tests/data/'))
15+
except ImportError:
16+
have_pybids = False
17+
18+
19+
# There are three reasons these tests will be skipped:
20+
@pytest.mark.skipif(not have_pybids,
21+
reason="Pybids is not installed")
22+
@pytest.mark.skipif(sys.version_info < (3, 0),
23+
reason="Pybids no longer supports Python 2")
24+
@pytest.mark.skipif(not dist_is_editable('pybids'),
25+
reason="Pybids is not installed in editable mode")
26+
def test_bids_grabber(tmpdir):
27+
tmpdir.chdir()
28+
bg = BIDSDataGrabber()
29+
bg.inputs.base_dir = os.path.join(datadir, 'ds005')
30+
bg.inputs.subject = '01'
31+
results = bg.run()
32+
assert os.path.basename(results.outputs.anat[0]) == 'sub-01_T1w.nii.gz'
33+
assert os.path.basename(results.outputs.func[0]) == (
34+
'sub-01_task-mixedgamblestask_run-01_bold.nii.gz')
35+
36+
37+
@pytest.mark.skipif(not have_pybids,
38+
reason="Pybids is not installed")
39+
@pytest.mark.skipif(sys.version_info < (3, 0),
40+
reason="Pybids no longer supports Python 2")
41+
@pytest.mark.skipif(not dist_is_editable('pybids'),
42+
reason="Pybids is not installed in editable mode")
43+
def test_bids_fields(tmpdir):
44+
tmpdir.chdir()
45+
bg = BIDSDataGrabber(infields = ['subject'], outfields = ['dwi'])
46+
bg.inputs.base_dir = os.path.join(datadir, 'ds005')
47+
bg.inputs.subject = '01'
48+
bg.inputs.output_query['dwi'] = dict(modality='dwi')
49+
results = bg.run()
50+
assert os.path.basename(results.outputs.dwi[0]) == 'sub-01_dwi.nii.gz'

nipype/utils/filemanip.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,20 @@ def write_rst_dict(info, prefix=''):
646646
for key, value in sorted(info.items()):
647647
out.append('{}* {} : {}'.format(prefix, key, str(value)))
648648
return '\n'.join(out) + '\n\n'
649+
650+
651+
def dist_is_editable(dist):
652+
"""Is distribution an editable install?
653+
654+
Parameters
655+
----------
656+
dist : string
657+
Package name
658+
659+
# Borrowed from `pip`'s' API
660+
"""
661+
for path_item in sys.path:
662+
egg_link = os.path.join(path_item, dist + '.egg-link')
663+
if os.path.isfile(egg_link):
664+
return True
665+
return False

0 commit comments

Comments
 (0)