66
77BIDSDataGrabber: 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)
159
10+ Change directory to provide relative paths for doctests
11+ >>> import os
12+ >>> filepath = os.path.dirname( os.path.realpath( __file__ ) )
13+ >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data'))
14+ >>> os.chdir(datadir)
1615"""
1716from os .path import join , dirname
17+ import json
1818from .. import logging
1919from .base import (traits ,
2020 DynamicTraitedSpec ,
2424 Str ,
2525 Undefined )
2626
27+ have_pybids = True
2728try :
2829 from bids import grabbids as gb
29- import json
3030except ImportError :
3131 have_pybids = False
32- else :
33- have_pybids = True
3432
3533LOGGER = logging .getLogger ('workflows' )
3634
@@ -56,22 +54,14 @@ class BIDSDataGrabber(BaseInterface):
5654 Examples
5755 --------
5856
59- >>> from nipype.interfaces.bids_utils import BIDSDataGrabber
60- >>> from os.path import basename
61-
6257 By default, the BIDSDataGrabber fetches anatomical and functional images
6358 from a project, and makes BIDS entities (e.g. subject) available for
6459 filtering outputs.
6560
6661 >>> bg = BIDSDataGrabber()
6762 >>> bg.inputs.base_dir = 'ds005/'
6863 >>> bg.inputs.subject = '01'
69- >>> results = bg.run()
70- >>> basename(results.outputs.anat[0]) # doctest: +ALLOW_UNICODE
71- 'sub-01_T1w.nii.gz'
72-
73- >>> basename(results.outputs.func[0]) # doctest: +ALLOW_UNICODE
74- 'sub-01_task-mixedgamblestask_run-01_bold.nii.gz'
64+ >>> results = bg.run() # doctest: +SKIP
7565
7666
7767 Dynamically created, user-defined output fields can also be defined to
@@ -83,9 +73,7 @@ class BIDSDataGrabber(BaseInterface):
8373 >>> bg.inputs.base_dir = 'ds005/'
8474 >>> bg.inputs.subject = '01'
8575 >>> 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'
76+ >>> results = bg.run() # doctest: +SKIP
8977
9078 """
9179 input_spec = BIDSDataGrabberInputSpec
@@ -104,32 +92,32 @@ def __init__(self, infields=None, **kwargs):
10492 If no matching items, returns Undefined.
10593 """
10694 super (BIDSDataGrabber , self ).__init__ (** kwargs )
107- if not have_pybids :
108- raise ImportError (
109- "The BIDSEventsGrabber interface requires pybids."
110- " Please make sure it is installed." )
11195
11296 if not isdefined (self .inputs .output_query ):
11397 self .inputs .output_query = {"func" : {"modality" : "func" },
11498 "anat" : {"modality" : "anat" }}
11599
116- # If infields is None , use all BIDS entities
117- if infields is None :
100+ # If infields is empty , use all BIDS entities
101+ if not infields is None and have_pybids :
118102 bids_config = join (dirname (gb .__file__ ), 'config' , 'bids.json' )
119103 bids_config = json .load (open (bids_config , 'r' ))
120104 infields = [i ['name' ] for i in bids_config ['entities' ]]
121105
122- self ._infields = infields
106+ self ._infields = infields or []
123107
124108 # used for mandatory inputs check
125109 undefined_traits = {}
126- for key in infields :
110+ for key in self . _infields :
127111 self .inputs .add_trait (key , traits .Any )
128112 undefined_traits [key ] = kwargs [key ] if key in kwargs else Undefined
129113
130114 self .inputs .trait_set (trait_change_notify = False , ** undefined_traits )
131115
132116 def _run_interface (self , runtime ):
117+ if not have_pybids :
118+ raise ImportError (
119+ "The BIDSEventsGrabber interface requires pybids."
120+ " Please make sure it is installed." )
133121 return runtime
134122
135123 def _list_outputs (self ):
0 commit comments