Skip to content

Commit ab6ed8d

Browse files
committed
Update data input bids tutorial to use nipype BIDSDataGrabber
1 parent ec60826 commit ab6ed8d

File tree

1 file changed

+39
-98
lines changed

1 file changed

+39
-98
lines changed

notebooks/basic_data_input_bids.ipynb

100644100755
Lines changed: 39 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
"cell_type": "code",
172172
"execution_count": null,
173173
"metadata": {
174-
"solution2": "hidden",
174+
"solution2": "shown",
175175
"solution2_first": true
176176
},
177177
"outputs": [],
@@ -183,7 +183,7 @@
183183
"cell_type": "code",
184184
"execution_count": null,
185185
"metadata": {
186-
"solution2": "hidden"
186+
"solution2": "shown"
187187
},
188188
"outputs": [],
189189
"source": [
@@ -198,40 +198,7 @@
198198
"metadata": {},
199199
"source": [
200200
"## Including `pybids` in your `nipype` workflow\n",
201-
"This is great, but what we really want is to include this into our `nipype` workflows. How to do this? We can create our own custom `BIDSDataGrabber` using a `Function` Interface. First, we need a plain Python function that for a given subject label and dataset location will return a list of BOLD files."
202-
]
203-
},
204-
{
205-
"cell_type": "code",
206-
"execution_count": null,
207-
"metadata": {},
208-
"outputs": [],
209-
"source": [
210-
"def get_niftis(subject_id, data_dir):\n",
211-
" # Remember that all the necessary imports need to be INSIDE the function for the Function Interface to work!\n",
212-
" from bids.layout import BIDSLayout\n",
213-
" \n",
214-
" layout = BIDSLayout(data_dir)\n",
215-
" \n",
216-
" bolds = layout.get(subject=subject_id, type=\"bold\", return_type='file', extensions=['nii', 'nii.gz'])\n",
217-
" \n",
218-
" return bolds"
219-
]
220-
},
221-
{
222-
"cell_type": "code",
223-
"execution_count": null,
224-
"metadata": {},
225-
"outputs": [],
226-
"source": [
227-
"get_niftis('01', '/data/ds000114')"
228-
]
229-
},
230-
{
231-
"cell_type": "markdown",
232-
"metadata": {},
233-
"source": [
234-
"Ok we got our function. Now we need to wrap it inside a Node object."
201+
"This is great, but what we really want is to include this into our nipype workflows. To do this, we can import the BIDSDataGrabber Interface, which wraps around pybids."
235202
]
236203
},
237204
{
@@ -240,20 +207,14 @@
240207
"metadata": {},
241208
"outputs": [],
242209
"source": [
210+
"from nipype.interfaces.io import BIDSDataGrabber\n",
243211
"from nipype.pipeline import Node, MapNode, Workflow\n",
244-
"from nipype.interfaces.utility import IdentityInterface, Function"
245-
]
246-
},
247-
{
248-
"cell_type": "code",
249-
"execution_count": null,
250-
"metadata": {},
251-
"outputs": [],
252-
"source": [
253-
"BIDSDataGrabber = Node(Function(function=get_niftis, input_names=[\"subject_id\",\n",
254-
" \"data_dir\"],\n",
255-
" output_names=[\"bolds\"]), name=\"BIDSDataGrabber\")\n",
256-
"BIDSDataGrabber.inputs.data_dir = \"/data/ds000114\""
212+
"from nipype.interfaces.utility import Function\n",
213+
"\n",
214+
"bg = Node(BIDSDataGrabber(), name='bids-grabber')\n",
215+
"bg.inputs.base_dir = '/data/ds000114'\n",
216+
"bg.inputs.subject = '01'\n",
217+
"bg.inputs.output_query = {'bolds': dict(type='bold')}"
257218
]
258219
},
259220
{
@@ -262,16 +223,15 @@
262223
"metadata": {},
263224
"outputs": [],
264225
"source": [
265-
"BIDSDataGrabber.inputs.subject_id='01'\n",
266-
"res = BIDSDataGrabber.run()\n",
226+
"res = bg.run()\n",
267227
"res.outputs"
268228
]
269229
},
270230
{
271231
"cell_type": "markdown",
272232
"metadata": {},
273233
"source": [
274-
"Works like a charm! (hopefully :) Lets put it in a workflow. We are not going to analyze any data, but for demonstration purposes, we will add a couple of nodes that pretend to analyze their inputs"
234+
"Works like a charm! Lets put it in a workflow. We are not going to analyze any data, but for demonstration purposes, we will add a couple of nodes that pretend to analyze their inputs"
275235
]
276236
},
277237
{
@@ -294,7 +254,7 @@
294254
"outputs": [],
295255
"source": [
296256
"wf = Workflow(name=\"bids_demo\")\n",
297-
"wf.connect(BIDSDataGrabber, \"bolds\", analyzeBOLD, \"paths\")\n",
257+
"wf.connect(bg, \"bolds\", analyzeBOLD, \"paths\")\n",
298258
"wf.run()"
299259
]
300260
},
@@ -310,7 +270,7 @@
310270
"cell_type": "code",
311271
"execution_count": null,
312272
"metadata": {
313-
"solution2": "hidden",
273+
"solution2": "shown",
314274
"solution2_first": true
315275
},
316276
"outputs": [],
@@ -322,29 +282,18 @@
322282
"cell_type": "code",
323283
"execution_count": null,
324284
"metadata": {
325-
"solution2": "hidden"
285+
"solution2": "shown"
326286
},
327287
"outputs": [],
328288
"source": [
329289
"from nipype.pipeline import Node, MapNode, Workflow\n",
330-
"from nipype.interfaces.utility import IdentityInterface, Function\n",
331-
"\n",
332-
"def get_T1w(subject_id, data_dir):\n",
333-
" from bids.layout import BIDSLayout\n",
334-
" \n",
335-
" layout = BIDSLayout(data_dir)\n",
336-
" \n",
337-
" T1w = layout.get(subject=subject_id, modality=\"anat\", return_type='file')\n",
338-
" \n",
339-
" return T1w\n",
290+
"from nipype.interfaces.io import BIDSDataGrabber\n",
340291
"\n",
341-
"ex2_BIDSDataGrabber = Node(Function(function=get_T1w, \n",
342-
" input_names=[\"subject_id\", \"data_dir\"],\n",
343-
" output_names=[\"T1w\"]), \n",
344-
" name=\"ex2\")\n",
345-
"ex2_BIDSDataGrabber.inputs.data_dir = \"/data/ds000114\"\n",
292+
"ex2_BIDSDataGrabber = BIDSDataGrabber(outfields = ['T1w'])\n",
293+
"ex2_BIDSDataGrabber.inputs.base_dir = '/data/ds000114'\n",
294+
"ex2_BIDSDataGrabber.inputs.subject = '10'\n",
295+
"ex2_BIDSDataGrabber.inputs.output_query = {'T1w': dict(modality='anat')}\n",
346296
"\n",
347-
"ex2_BIDSDataGrabber.inputs.subject_id='10'\n",
348297
"ex2_res = ex2_BIDSDataGrabber.run()\n",
349298
"ex2_res.outputs"
350299
]
@@ -363,7 +312,12 @@
363312
"metadata": {},
364313
"outputs": [],
365314
"source": [
366-
"BIDSDataGrabber.iterables = ('subject_id', layout.get_subjects()[:2])\n",
315+
"bg_all = Node(BIDSDataGrabber(), name='bids-grabber')\n",
316+
"bg_all.inputs.base_dir = '/data/ds000114'\n",
317+
"bg_all.inputs.output_query = {'bolds': dict(type='bold')}\n",
318+
"bg_all.iterables = ('subject', layout.get_subjects()[:2])\n",
319+
"wf = Workflow(name=\"bids_demo\")\n",
320+
"wf.connect(bg_all, \"bolds\", analyzeBOLD, \"paths\")\n",
367321
"wf.run()"
368322
]
369323
},
@@ -411,11 +365,13 @@
411365
{
412366
"cell_type": "code",
413367
"execution_count": null,
414-
"metadata": {},
368+
"metadata": {
369+
"scrolled": false
370+
},
415371
"outputs": [],
416372
"source": [
417373
"wf = Workflow(name=\"bids_demo\")\n",
418-
"wf.connect(BIDSDataGrabber, \"bolds\", analyzeBOLD2, \"path\")\n",
374+
"wf.connect(bg, \"bolds\", analyzeBOLD2, \"path\")\n",
419375
"wf.run()"
420376
]
421377
},
@@ -431,7 +387,7 @@
431387
"cell_type": "code",
432388
"execution_count": null,
433389
"metadata": {
434-
"solution2": "hidden",
390+
"solution2": "shown",
435391
"solution2_first": true
436392
},
437393
"outputs": [],
@@ -443,39 +399,24 @@
443399
"cell_type": "code",
444400
"execution_count": null,
445401
"metadata": {
446-
"solution2": "hidden"
402+
"solution2": "shown"
447403
},
448404
"outputs": [],
449405
"source": [
450406
"from nipype.pipeline import Node, MapNode, Workflow\n",
451-
"from nipype.interfaces.utility import IdentityInterface, Function\n",
452-
"\n",
453-
"# let's start from BidsDataGrabber again\n",
454-
"def get_niftis(subject_id, data_dir):\n",
455-
" # Remember that all the necesary imports need to be INSIDE the function \n",
456-
" # for the Function Interface to work!\n",
457-
" from bids.layout import BIDSLayout\n",
458-
" \n",
459-
" layout = BIDSLayout(data_dir)\n",
460-
" \n",
461-
" bolds = layout.get(subject=subject_id, type=\"bold\", return_type='file', extensions=['nii', 'nii.gz'])\n",
462-
" \n",
463-
" return bolds\n",
464-
"\n",
407+
"from nipype.interfaces.io import BIDSDataGrabber\n",
465408
"\n",
466-
"ex3_BIDSDataGrabber = Node(Function(function=get_niftis, \n",
467-
" input_names=[\"subject_id\", \"data_dir\"],\n",
468-
" output_names=[\"bolds\"]), \n",
469-
" name=\"ex3_BIDSDataGrabber\")\n",
470-
"ex3_BIDSDataGrabber.inputs.data_dir = \"/data/ds000114\"\n",
471-
"ex3_BIDSDataGrabber.inputs.subject_id='01'"
409+
"ex3_BIDSDataGrabber = Node(BIDSDataGrabber(), name='bids-grabber')\n",
410+
"ex3_BIDSDataGrabber.inputs.base_dir = '/data/ds000114'\n",
411+
"ex3_BIDSDataGrabber.inputs.subject = '01'\n",
412+
"ex3_BIDSDataGrabber.inputs.output_query = {'bolds': dict(type='bold')}"
472413
]
473414
},
474415
{
475416
"cell_type": "code",
476417
"execution_count": null,
477418
"metadata": {
478-
"solution2": "hidden"
419+
"solution2": "shown"
479420
},
480421
"outputs": [],
481422
"source": [
@@ -516,7 +457,7 @@
516457
"name": "python",
517458
"nbconvert_exporter": "python",
518459
"pygments_lexer": "ipython3",
519-
"version": "3.6.5"
460+
"version": "3.6.6"
520461
}
521462
},
522463
"nbformat": 4,

0 commit comments

Comments
 (0)