6
6
7
7
BIDSDataGrabber: Query data from BIDS dataset using pybids grabbids.
8
8
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
9
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)
16
15
"""
17
16
from os .path import join , dirname
17
+ import json
18
18
from .. import logging
19
19
from .base import (traits ,
20
20
DynamicTraitedSpec ,
24
24
Str ,
25
25
Undefined )
26
26
27
+ have_pybids = True
27
28
try :
28
29
from bids import grabbids as gb
29
- import json
30
30
except ImportError :
31
31
have_pybids = False
32
- else :
33
- have_pybids = True
34
32
35
33
LOGGER = logging .getLogger ('workflows' )
36
34
@@ -56,22 +54,14 @@ class BIDSDataGrabber(BaseInterface):
56
54
Examples
57
55
--------
58
56
59
- >>> from nipype.interfaces.bids_utils import BIDSDataGrabber
60
- >>> from os.path import basename
61
-
62
57
By default, the BIDSDataGrabber fetches anatomical and functional images
63
58
from a project, and makes BIDS entities (e.g. subject) available for
64
59
filtering outputs.
65
60
66
61
>>> bg = BIDSDataGrabber()
67
62
>>> bg.inputs.base_dir = 'ds005/'
68
63
>>> 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
75
65
76
66
77
67
Dynamically created, user-defined output fields can also be defined to
@@ -83,9 +73,7 @@ class BIDSDataGrabber(BaseInterface):
83
73
>>> bg.inputs.base_dir = 'ds005/'
84
74
>>> bg.inputs.subject = '01'
85
75
>>> 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
89
77
90
78
"""
91
79
input_spec = BIDSDataGrabberInputSpec
@@ -104,32 +92,32 @@ def __init__(self, infields=None, **kwargs):
104
92
If no matching items, returns Undefined.
105
93
"""
106
94
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." )
111
95
112
96
if not isdefined (self .inputs .output_query ):
113
97
self .inputs .output_query = {"func" : {"modality" : "func" },
114
98
"anat" : {"modality" : "anat" }}
115
99
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 :
118
102
bids_config = join (dirname (gb .__file__ ), 'config' , 'bids.json' )
119
103
bids_config = json .load (open (bids_config , 'r' ))
120
104
infields = [i ['name' ] for i in bids_config ['entities' ]]
121
105
122
- self ._infields = infields
106
+ self ._infields = infields or []
123
107
124
108
# used for mandatory inputs check
125
109
undefined_traits = {}
126
- for key in infields :
110
+ for key in self . _infields :
127
111
self .inputs .add_trait (key , traits .Any )
128
112
undefined_traits [key ] = kwargs [key ] if key in kwargs else Undefined
129
113
130
114
self .inputs .trait_set (trait_change_notify = False , ** undefined_traits )
131
115
132
116
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." )
133
121
return runtime
134
122
135
123
def _list_outputs (self ):
0 commit comments