Skip to content

Commit f68e073

Browse files
committed
Update output traits for BIDSDataGrabber
1 parent 8f5f380 commit f68e073

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

nipype/interfaces/io.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import glob
2525
import fnmatch
2626
import string
27+
import json
2728
import os
2829
import os.path as op
2930
import shutil
@@ -2685,16 +2686,12 @@ class BIDSDataGrabber(IOBase):
26852686
output_spec = DynamicTraitedSpec
26862687
_always_run = True
26872688

2688-
def __init__(self, infields=None, outfields=None, **kwargs):
2689+
def __init__(self, infields=None, **kwargs):
26892690
"""
26902691
Parameters
26912692
----------
26922693
infields : list of str
26932694
Indicates the input fields to be dynamically created
2694-
2695-
outfields: list of str
2696-
Indicates output fields to be dynamically created.
2697-
If no matching items, returns Undefined.
26982695
"""
26992696
super(BIDSDataGrabber, self).__init__(**kwargs)
27002697

@@ -2707,12 +2704,8 @@ def __init__(self, infields=None, outfields=None, **kwargs):
27072704
bids_config = join(dirname(gb.__file__), 'config', 'bids.json')
27082705
bids_config = json.load(open(bids_config, 'r'))
27092706
infields = [i['name'] for i in bids_config['entities']]
2710-
# if outfields is not set, return those defined in infields
2711-
if infields and outfields is None:
2712-
outfields = [infield for infield in infields]
27132707

27142708
self._infields = infields or []
2715-
self._outfields = outfields or []
27162709

27172710
# used for mandatory inputs check
27182711
undefined_traits = {}
@@ -2753,4 +2746,7 @@ def _list_outputs(self):
27532746
filelist = Undefined
27542747

27552748
outputs[key] = filelist
2756-
return outputs
2749+
return outputs
2750+
2751+
def _add_output_traits(self, base):
2752+
return add_traits(base, list(self.inputs.output_query.keys()))

nipype/interfaces/tests/test_bids.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

nipype/interfaces/tests/test_io.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import glob
1010
import shutil
1111
import os.path as op
12+
import sys
1213
from subprocess import Popen
1314
import hashlib
1415
from collections import namedtuple
1516

1617
import pytest
1718
import nipype
1819
import nipype.interfaces.io as nio
20+
from nipype.interfaces.base.traits_extension import isdefined
1921
from nipype.interfaces.base import Undefined
22+
from nipype.utils.filemanip import dist_is_editable
2023

2124
# Check for boto
2225
noboto = False
@@ -43,6 +46,16 @@
4346
except CalledProcessError:
4447
fakes3 = False
4548

49+
# check for bids
50+
have_pybids = True
51+
try:
52+
import bids
53+
from bids import grabbids as gb
54+
filepath = os.path.realpath(os.path.dirname(bids.__file__))
55+
datadir = os.path.realpath(os.path.join(filepath, 'grabbids/tests/data/'))
56+
except ImportError:
57+
have_pybids = False
58+
4659

4760
def test_datagrabber():
4861
dg = nio.DataGrabber()
@@ -439,4 +452,63 @@ def test_jsonsink(tmpdir, inputs_attributes):
439452
assert data == expected_data
440453

441454

455+
# There are three reasons these tests will be skipped:
456+
@pytest.mark.skipif(not have_pybids,
457+
reason="Pybids is not installed")
458+
@pytest.mark.skipif(sys.version_info < (3, 0),
459+
reason="Pybids no longer supports Python 2")
460+
@pytest.mark.skipif(not dist_is_editable('pybids'),
461+
reason="Pybids is not installed in editable mode")
462+
def test_bids_grabber(tmpdir):
463+
tmpdir.chdir()
464+
bg = nio.BIDSDataGrabber()
465+
bg.inputs.base_dir = os.path.join(datadir, 'ds005')
466+
bg.inputs.subject = '01'
467+
results = bg.run()
468+
assert os.path.basename(results.outputs.anat[0]) == 'sub-01_T1w.nii.gz'
469+
assert os.path.basename(results.outputs.func[0]) == (
470+
'sub-01_task-mixedgamblestask_run-01_bold.nii.gz')
471+
472+
473+
@pytest.mark.skipif(not have_pybids,
474+
reason="Pybids is not installed")
475+
@pytest.mark.skipif(sys.version_info < (3, 0),
476+
reason="Pybids no longer supports Python 2")
477+
@pytest.mark.skipif(not dist_is_editable('pybids'),
478+
reason="Pybids is not installed in editable mode")
479+
def test_bids_fields(tmpdir):
480+
tmpdir.chdir()
481+
bg = nio.BIDSDataGrabber(infields = ['subject'], outfields = ['dwi'])
482+
bg.inputs.base_dir = os.path.join(datadir, 'ds005')
483+
bg.inputs.subject = '01'
484+
bg.inputs.output_query['dwi'] = dict(modality='dwi')
485+
results = bg.run()
486+
assert os.path.basename(results.outputs.dwi[0]) == 'sub-01_dwi.nii.gz'
487+
488+
489+
@pytest.mark.skipif(not have_pybids,
490+
reason="Pybids is not installed")
491+
@pytest.mark.skipif(sys.version_info < (3, 0),
492+
reason="Pybids no longer supports Python 2")
493+
@pytest.mark.skipif(not dist_is_editable('pybids'),
494+
reason="Pybids is not installed in editable mode")
495+
def test_infields_outfields(tmpdir):
496+
tmpdir.chdir()
497+
infields = ['infield1', 'infield2']
498+
outfields = ['outfield1', 'outfield2']
499+
bg = nio.BIDSDataGrabber(infields=infields)
500+
for outfield in outfields:
501+
bg.inputs.output_query[outfield] = {'key': 'value'}
502+
503+
for infield in infields:
504+
assert(infield in bg.inputs.traits())
505+
assert(not(isdefined(bg.inputs.get()[infield])))
506+
507+
for outfield in outfields:
508+
assert(outfield in bg._outputs().traits())
509+
510+
# now try without defining outfields, we should get anat and func for free
511+
bg = nio.BIDSDataGrabber()
512+
for outfield in ['anat', 'func']:
513+
assert outfield in bg._outputs().traits()
442514

0 commit comments

Comments
 (0)