|
171 | 171 | "cell_type": "code",
|
172 | 172 | "execution_count": null,
|
173 | 173 | "metadata": {
|
174 |
| - "solution2": "hidden", |
| 174 | + "solution2": "shown", |
175 | 175 | "solution2_first": true
|
176 | 176 | },
|
177 | 177 | "outputs": [],
|
|
183 | 183 | "cell_type": "code",
|
184 | 184 | "execution_count": null,
|
185 | 185 | "metadata": {
|
186 |
| - "solution2": "hidden" |
| 186 | + "solution2": "shown" |
187 | 187 | },
|
188 | 188 | "outputs": [],
|
189 | 189 | "source": [
|
|
198 | 198 | "metadata": {},
|
199 | 199 | "source": [
|
200 | 200 | "## 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." |
235 | 202 | ]
|
236 | 203 | },
|
237 | 204 | {
|
|
240 | 207 | "metadata": {},
|
241 | 208 | "outputs": [],
|
242 | 209 | "source": [
|
| 210 | + "from nipype.interfaces.io import BIDSDataGrabber\n", |
243 | 211 | "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')}" |
257 | 218 | ]
|
258 | 219 | },
|
259 | 220 | {
|
|
262 | 223 | "metadata": {},
|
263 | 224 | "outputs": [],
|
264 | 225 | "source": [
|
265 |
| - "BIDSDataGrabber.inputs.subject_id='01'\n", |
266 |
| - "res = BIDSDataGrabber.run()\n", |
| 226 | + "res = bg.run()\n", |
267 | 227 | "res.outputs"
|
268 | 228 | ]
|
269 | 229 | },
|
270 | 230 | {
|
271 | 231 | "cell_type": "markdown",
|
272 | 232 | "metadata": {},
|
273 | 233 | "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" |
275 | 235 | ]
|
276 | 236 | },
|
277 | 237 | {
|
|
294 | 254 | "outputs": [],
|
295 | 255 | "source": [
|
296 | 256 | "wf = Workflow(name=\"bids_demo\")\n",
|
297 |
| - "wf.connect(BIDSDataGrabber, \"bolds\", analyzeBOLD, \"paths\")\n", |
| 257 | + "wf.connect(bg, \"bolds\", analyzeBOLD, \"paths\")\n", |
298 | 258 | "wf.run()"
|
299 | 259 | ]
|
300 | 260 | },
|
|
310 | 270 | "cell_type": "code",
|
311 | 271 | "execution_count": null,
|
312 | 272 | "metadata": {
|
313 |
| - "solution2": "hidden", |
| 273 | + "solution2": "shown", |
314 | 274 | "solution2_first": true
|
315 | 275 | },
|
316 | 276 | "outputs": [],
|
|
322 | 282 | "cell_type": "code",
|
323 | 283 | "execution_count": null,
|
324 | 284 | "metadata": {
|
325 |
| - "solution2": "hidden" |
| 285 | + "solution2": "shown" |
326 | 286 | },
|
327 | 287 | "outputs": [],
|
328 | 288 | "source": [
|
329 | 289 | "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", |
340 | 291 | "\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", |
346 | 296 | "\n",
|
347 |
| - "ex2_BIDSDataGrabber.inputs.subject_id='10'\n", |
348 | 297 | "ex2_res = ex2_BIDSDataGrabber.run()\n",
|
349 | 298 | "ex2_res.outputs"
|
350 | 299 | ]
|
|
363 | 312 | "metadata": {},
|
364 | 313 | "outputs": [],
|
365 | 314 | "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", |
367 | 321 | "wf.run()"
|
368 | 322 | ]
|
369 | 323 | },
|
|
411 | 365 | {
|
412 | 366 | "cell_type": "code",
|
413 | 367 | "execution_count": null,
|
414 |
| - "metadata": {}, |
| 368 | + "metadata": { |
| 369 | + "scrolled": false |
| 370 | + }, |
415 | 371 | "outputs": [],
|
416 | 372 | "source": [
|
417 | 373 | "wf = Workflow(name=\"bids_demo\")\n",
|
418 |
| - "wf.connect(BIDSDataGrabber, \"bolds\", analyzeBOLD2, \"path\")\n", |
| 374 | + "wf.connect(bg, \"bolds\", analyzeBOLD2, \"path\")\n", |
419 | 375 | "wf.run()"
|
420 | 376 | ]
|
421 | 377 | },
|
|
431 | 387 | "cell_type": "code",
|
432 | 388 | "execution_count": null,
|
433 | 389 | "metadata": {
|
434 |
| - "solution2": "hidden", |
| 390 | + "solution2": "shown", |
435 | 391 | "solution2_first": true
|
436 | 392 | },
|
437 | 393 | "outputs": [],
|
|
443 | 399 | "cell_type": "code",
|
444 | 400 | "execution_count": null,
|
445 | 401 | "metadata": {
|
446 |
| - "solution2": "hidden" |
| 402 | + "solution2": "shown" |
447 | 403 | },
|
448 | 404 | "outputs": [],
|
449 | 405 | "source": [
|
450 | 406 | "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", |
465 | 408 | "\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')}" |
472 | 413 | ]
|
473 | 414 | },
|
474 | 415 | {
|
475 | 416 | "cell_type": "code",
|
476 | 417 | "execution_count": null,
|
477 | 418 | "metadata": {
|
478 |
| - "solution2": "hidden" |
| 419 | + "solution2": "shown" |
479 | 420 | },
|
480 | 421 | "outputs": [],
|
481 | 422 | "source": [
|
|
516 | 457 | "name": "python",
|
517 | 458 | "nbconvert_exporter": "python",
|
518 | 459 | "pygments_lexer": "ipython3",
|
519 |
| - "version": "3.6.5" |
| 460 | + "version": "3.6.6" |
520 | 461 | }
|
521 | 462 | },
|
522 | 463 | "nbformat": 4,
|
|
0 commit comments