|
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, TraitError
|
| 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()
|
@@ -536,3 +549,64 @@ def test_jsonsink(tmpdir, inputs_attributes):
|
536 | 549 | data = simplejson.load(f)
|
537 | 550 |
|
538 | 551 | assert data == expected_data
|
| 552 | + |
| 553 | + |
| 554 | +# There are three reasons these tests will be skipped: |
| 555 | +@pytest.mark.skipif(not have_pybids, |
| 556 | + reason="Pybids is not installed") |
| 557 | +@pytest.mark.skipif(sys.version_info < (3, 0), |
| 558 | + reason="Pybids no longer supports Python 2") |
| 559 | +@pytest.mark.skipif(not dist_is_editable('pybids'), |
| 560 | + reason="Pybids is not installed in editable mode") |
| 561 | +def test_bids_grabber(tmpdir): |
| 562 | + tmpdir.chdir() |
| 563 | + bg = nio.BIDSDataGrabber() |
| 564 | + bg.inputs.base_dir = os.path.join(datadir, 'ds005') |
| 565 | + bg.inputs.subject = '01' |
| 566 | + results = bg.run() |
| 567 | + assert os.path.basename(results.outputs.anat[0]) == 'sub-01_T1w.nii.gz' |
| 568 | + assert os.path.basename(results.outputs.func[0]) == ( |
| 569 | + 'sub-01_task-mixedgamblestask_run-01_bold.nii.gz') |
| 570 | + |
| 571 | + |
| 572 | +@pytest.mark.skipif(not have_pybids, |
| 573 | + reason="Pybids is not installed") |
| 574 | +@pytest.mark.skipif(sys.version_info < (3, 0), |
| 575 | + reason="Pybids no longer supports Python 2") |
| 576 | +@pytest.mark.skipif(not dist_is_editable('pybids'), |
| 577 | + reason="Pybids is not installed in editable mode") |
| 578 | +def test_bids_fields(tmpdir): |
| 579 | + tmpdir.chdir() |
| 580 | + bg = nio.BIDSDataGrabber(infields = ['subject'], outfields = ['dwi']) |
| 581 | + bg.inputs.base_dir = os.path.join(datadir, 'ds005') |
| 582 | + bg.inputs.subject = '01' |
| 583 | + bg.inputs.output_query['dwi'] = dict(modality='dwi') |
| 584 | + results = bg.run() |
| 585 | + assert os.path.basename(results.outputs.dwi[0]) == 'sub-01_dwi.nii.gz' |
| 586 | + |
| 587 | + |
| 588 | +@pytest.mark.skipif(not have_pybids, |
| 589 | + reason="Pybids is not installed") |
| 590 | +@pytest.mark.skipif(sys.version_info < (3, 0), |
| 591 | + reason="Pybids no longer supports Python 2") |
| 592 | +@pytest.mark.skipif(not dist_is_editable('pybids'), |
| 593 | + reason="Pybids is not installed in editable mode") |
| 594 | +def test_infields_outfields(tmpdir): |
| 595 | + tmpdir.chdir() |
| 596 | + infields = ['infield1', 'infield2'] |
| 597 | + outfields = ['outfield1', 'outfield2'] |
| 598 | + bg = nio.BIDSDataGrabber(infields=infields) |
| 599 | + for outfield in outfields: |
| 600 | + bg.inputs.output_query[outfield] = {'key': 'value'} |
| 601 | + |
| 602 | + for infield in infields: |
| 603 | + assert(infield in bg.inputs.traits()) |
| 604 | + assert(not(isdefined(bg.inputs.get()[infield]))) |
| 605 | + |
| 606 | + for outfield in outfields: |
| 607 | + assert(outfield in bg._outputs().traits()) |
| 608 | + |
| 609 | + # now try without defining outfields, we should get anat and func for free |
| 610 | + bg = nio.BIDSDataGrabber() |
| 611 | + for outfield in ['anat', 'func']: |
| 612 | + assert outfield in bg._outputs().traits() |
0 commit comments