Skip to content

Commit 7411d6f

Browse files
committed
Update output traits for BIDSDataGrabber
1 parent f28ead6 commit 7411d6f

File tree

3 files changed

+80
-60
lines changed

3 files changed

+80
-60
lines changed

nipype/interfaces/io.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import glob
2626
import fnmatch
2727
import string
28+
import json
2829
import os
2930
import os.path as op
3031
import shutil
@@ -2772,16 +2773,12 @@ class BIDSDataGrabber(IOBase):
27722773
output_spec = DynamicTraitedSpec
27732774
_always_run = True
27742775

2775-
def __init__(self, infields=None, outfields=None, **kwargs):
2776+
def __init__(self, infields=None, **kwargs):
27762777
"""
27772778
Parameters
27782779
----------
27792780
infields : list of str
27802781
Indicates the input fields to be dynamically created
2781-
2782-
outfields: list of str
2783-
Indicates output fields to be dynamically created.
2784-
If no matching items, returns Undefined.
27852782
"""
27862783
super(BIDSDataGrabber, self).__init__(**kwargs)
27872784

@@ -2794,12 +2791,8 @@ def __init__(self, infields=None, outfields=None, **kwargs):
27942791
bids_config = join(dirname(gb.__file__), 'config', 'bids.json')
27952792
bids_config = json.load(open(bids_config, 'r'))
27962793
infields = [i['name'] for i in bids_config['entities']]
2797-
# if outfields is not set, return those defined in infields
2798-
if infields and outfields is None:
2799-
outfields = [infield for infield in infields]
28002794

28012795
self._infields = infields or []
2802-
self._outfields = outfields or []
28032796

28042797
# used for mandatory inputs check
28052798
undefined_traits = {}
@@ -2840,4 +2833,7 @@ def _list_outputs(self):
28402833
filelist = Undefined
28412834

28422835
outputs[key] = filelist
2843-
return outputs
2836+
return outputs
2837+
2838+
def _add_output_traits(self, base):
2839+
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: 74 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, TraitError
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()
@@ -536,3 +549,64 @@ def test_jsonsink(tmpdir, inputs_attributes):
536549
data = simplejson.load(f)
537550

538551
assert data == expected_data
552+
553+
554+
# There are three reasons these tests will be skipped:
555+
@pytest.mark.skipif(not have_pybids,
556+
reason="Pybids is not installed")
557+
@pytest.mark.skipif(sys.version_info < (3, 0),
558+
reason="Pybids no longer supports Python 2")
559+
@pytest.mark.skipif(not dist_is_editable('pybids'),
560+
reason="Pybids is not installed in editable mode")
561+
def test_bids_grabber(tmpdir):
562+
tmpdir.chdir()
563+
bg = nio.BIDSDataGrabber()
564+
bg.inputs.base_dir = os.path.join(datadir, 'ds005')
565+
bg.inputs.subject = '01'
566+
results = bg.run()
567+
assert os.path.basename(results.outputs.anat[0]) == 'sub-01_T1w.nii.gz'
568+
assert os.path.basename(results.outputs.func[0]) == (
569+
'sub-01_task-mixedgamblestask_run-01_bold.nii.gz')
570+
571+
572+
@pytest.mark.skipif(not have_pybids,
573+
reason="Pybids is not installed")
574+
@pytest.mark.skipif(sys.version_info < (3, 0),
575+
reason="Pybids no longer supports Python 2")
576+
@pytest.mark.skipif(not dist_is_editable('pybids'),
577+
reason="Pybids is not installed in editable mode")
578+
def test_bids_fields(tmpdir):
579+
tmpdir.chdir()
580+
bg = nio.BIDSDataGrabber(infields = ['subject'], outfields = ['dwi'])
581+
bg.inputs.base_dir = os.path.join(datadir, 'ds005')
582+
bg.inputs.subject = '01'
583+
bg.inputs.output_query['dwi'] = dict(modality='dwi')
584+
results = bg.run()
585+
assert os.path.basename(results.outputs.dwi[0]) == 'sub-01_dwi.nii.gz'
586+
587+
588+
@pytest.mark.skipif(not have_pybids,
589+
reason="Pybids is not installed")
590+
@pytest.mark.skipif(sys.version_info < (3, 0),
591+
reason="Pybids no longer supports Python 2")
592+
@pytest.mark.skipif(not dist_is_editable('pybids'),
593+
reason="Pybids is not installed in editable mode")
594+
def test_infields_outfields(tmpdir):
595+
tmpdir.chdir()
596+
infields = ['infield1', 'infield2']
597+
outfields = ['outfield1', 'outfield2']
598+
bg = nio.BIDSDataGrabber(infields=infields)
599+
for outfield in outfields:
600+
bg.inputs.output_query[outfield] = {'key': 'value'}
601+
602+
for infield in infields:
603+
assert(infield in bg.inputs.traits())
604+
assert(not(isdefined(bg.inputs.get()[infield])))
605+
606+
for outfield in outfields:
607+
assert(outfield in bg._outputs().traits())
608+
609+
# now try without defining outfields, we should get anat and func for free
610+
bg = nio.BIDSDataGrabber()
611+
for outfield in ['anat', 'func']:
612+
assert outfield in bg._outputs().traits()

0 commit comments

Comments
 (0)