|
9 | 9 | import glob
|
10 | 10 | import shutil
|
11 | 11 | import os.path as op
|
| 12 | +import sys |
12 | 13 | from subprocess import Popen
|
13 | 14 | import hashlib
|
14 | 15 | from collections import namedtuple
|
15 | 16 |
|
16 | 17 | import pytest
|
17 | 18 | import nipype
|
18 | 19 | import nipype.interfaces.io as nio
|
| 20 | +from nipype.interfaces.base.traits_extension import isdefined |
19 | 21 | from nipype.interfaces.base import Undefined
|
| 22 | +from nipype.utils.filemanip import dist_is_editable |
20 | 23 |
|
21 | 24 | # Check for boto
|
22 | 25 | noboto = False
|
|
43 | 46 | except CalledProcessError:
|
44 | 47 | fakes3 = False
|
45 | 48 |
|
| 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 | + |
46 | 59 |
|
47 | 60 | def test_datagrabber():
|
48 | 61 | dg = nio.DataGrabber()
|
@@ -439,4 +452,63 @@ def test_jsonsink(tmpdir, inputs_attributes):
|
439 | 452 | assert data == expected_data
|
440 | 453 |
|
441 | 454 |
|
| 455 | +# There are three reasons these tests will be skipped: |
| 456 | +@pytest.mark.skipif(not have_pybids, |
| 457 | + reason="Pybids is not installed") |
| 458 | +@pytest.mark.skipif(sys.version_info < (3, 0), |
| 459 | + reason="Pybids no longer supports Python 2") |
| 460 | +@pytest.mark.skipif(not dist_is_editable('pybids'), |
| 461 | + reason="Pybids is not installed in editable mode") |
| 462 | +def test_bids_grabber(tmpdir): |
| 463 | + tmpdir.chdir() |
| 464 | + bg = nio.BIDSDataGrabber() |
| 465 | + bg.inputs.base_dir = os.path.join(datadir, 'ds005') |
| 466 | + bg.inputs.subject = '01' |
| 467 | + results = bg.run() |
| 468 | + assert os.path.basename(results.outputs.anat[0]) == 'sub-01_T1w.nii.gz' |
| 469 | + assert os.path.basename(results.outputs.func[0]) == ( |
| 470 | + 'sub-01_task-mixedgamblestask_run-01_bold.nii.gz') |
| 471 | + |
| 472 | + |
| 473 | +@pytest.mark.skipif(not have_pybids, |
| 474 | + reason="Pybids is not installed") |
| 475 | +@pytest.mark.skipif(sys.version_info < (3, 0), |
| 476 | + reason="Pybids no longer supports Python 2") |
| 477 | +@pytest.mark.skipif(not dist_is_editable('pybids'), |
| 478 | + reason="Pybids is not installed in editable mode") |
| 479 | +def test_bids_fields(tmpdir): |
| 480 | + tmpdir.chdir() |
| 481 | + bg = nio.BIDSDataGrabber(infields = ['subject'], outfields = ['dwi']) |
| 482 | + bg.inputs.base_dir = os.path.join(datadir, 'ds005') |
| 483 | + bg.inputs.subject = '01' |
| 484 | + bg.inputs.output_query['dwi'] = dict(modality='dwi') |
| 485 | + results = bg.run() |
| 486 | + assert os.path.basename(results.outputs.dwi[0]) == 'sub-01_dwi.nii.gz' |
| 487 | + |
| 488 | + |
| 489 | +@pytest.mark.skipif(not have_pybids, |
| 490 | + reason="Pybids is not installed") |
| 491 | +@pytest.mark.skipif(sys.version_info < (3, 0), |
| 492 | + reason="Pybids no longer supports Python 2") |
| 493 | +@pytest.mark.skipif(not dist_is_editable('pybids'), |
| 494 | + reason="Pybids is not installed in editable mode") |
| 495 | +def test_infields_outfields(tmpdir): |
| 496 | + tmpdir.chdir() |
| 497 | + infields = ['infield1', 'infield2'] |
| 498 | + outfields = ['outfield1', 'outfield2'] |
| 499 | + bg = nio.BIDSDataGrabber(infields=infields) |
| 500 | + for outfield in outfields: |
| 501 | + bg.inputs.output_query[outfield] = {'key': 'value'} |
| 502 | + |
| 503 | + for infield in infields: |
| 504 | + assert(infield in bg.inputs.traits()) |
| 505 | + assert(not(isdefined(bg.inputs.get()[infield]))) |
| 506 | + |
| 507 | + for outfield in outfields: |
| 508 | + assert(outfield in bg._outputs().traits()) |
| 509 | + |
| 510 | + # now try without defining outfields, we should get anat and func for free |
| 511 | + bg = nio.BIDSDataGrabber() |
| 512 | + for outfield in ['anat', 'func']: |
| 513 | + assert outfield in bg._outputs().traits() |
442 | 514 |
|
0 commit comments