|
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | +import shutil |
| 6 | + |
| 7 | +from tempfile import mkdtemp |
| 8 | +from shutil import rmtree |
| 9 | + |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +import nibabel as nb |
| 13 | + |
| 14 | +from nose import with_setup |
| 15 | + |
| 16 | +from nipype.testing import ( assert_equal, assert_not_equal, |
| 17 | + assert_raises, skipif, example_data) |
| 18 | +import nipype.interfaces.fsl.epi as fsl |
| 19 | +from nipype.interfaces.fsl import Info, no_fsl |
| 20 | +from nipype.interfaces.base import Undefined |
| 21 | + |
| 22 | +# nosetests --with-doctest path_to/test_fsl.py |
| 23 | + |
| 24 | +@skipif(no_fsl) |
| 25 | +def test_eddy_correct1(): |
| 26 | + input_map = dict(args = dict(argstr='%s',), |
| 27 | + environ = dict(), |
| 28 | + in_file = dict(argstr='%s',mandatory=True,), |
| 29 | + out_file = dict(argstr='%s',), |
| 30 | + output_type = dict(), |
| 31 | + ref_num = dict(mandatory=True,argstr='%d',), |
| 32 | + ) |
| 33 | + instance = fsl.EddyCorrect() |
| 34 | + for key, metadata in input_map.items(): |
| 35 | + for metakey, value in metadata.items(): |
| 36 | + yield assert_equal, getattr(instance.inputs.traits()[key], metakey), value |
| 37 | + |
| 38 | + |
| 39 | +def skip_epi_tests(): |
| 40 | + """XXX These tests are skipped until we clean up some of this code |
| 41 | + """ |
| 42 | + return True |
| 43 | + |
| 44 | +def create_files_in_directory(): |
| 45 | + outdir = os.path.realpath(mkdtemp()) |
| 46 | + cwd = os.getcwd() |
| 47 | + os.chdir(outdir) |
| 48 | + filelist = ['a.nii','b.nii'] |
| 49 | + for f in filelist: |
| 50 | + hdr = nb.Nifti1Header() |
| 51 | + shape = (3,3,3,4) |
| 52 | + hdr.set_data_shape(shape) |
| 53 | + img = np.random.random(shape) |
| 54 | + nb.save(nb.Nifti1Image(img,np.eye(4),hdr), |
| 55 | + os.path.join(outdir,f)) |
| 56 | + return filelist, outdir, cwd |
| 57 | + |
| 58 | +def clean_directory(outdir, old_wd): |
| 59 | + if os.path.exists(outdir): |
| 60 | + rmtree(outdir) |
| 61 | + os.chdir(old_wd) |
| 62 | + |
| 63 | + |
| 64 | +# test eddy_correct |
| 65 | +@skipif(no_fsl) |
| 66 | +def test_eddy_correct2(): |
| 67 | + filelist, outdir, cwd = create_files_in_directory() |
| 68 | + eddy = fsl.EddyCorrect() |
| 69 | + |
| 70 | + # make sure command gets called |
| 71 | + yield assert_equal, eddy.cmd, 'eddy_correct' |
| 72 | + |
| 73 | + # test raising error with mandatory args absent |
| 74 | + yield assert_raises, ValueError, eddy.run |
| 75 | + |
| 76 | + # .inputs based parameters setting |
| 77 | + eddy.inputs.in_file = filelist[0] |
| 78 | + eddy.inputs.out_file = 'foo_eddc.nii' |
| 79 | + eddy.inputs.ref_num = 100 |
| 80 | + yield assert_equal, eddy.cmdline, 'eddy_correct %s foo_eddc.nii 100'%filelist[0] |
| 81 | + |
| 82 | + # .run based parameter setting |
| 83 | + eddy2 = fsl.EddyCorrect(in_file=filelist[0], out_file='foo_ec.nii', ref_num=20) |
| 84 | + yield assert_equal, eddy2.cmdline, 'eddy_correct %s foo_ec.nii 20'%filelist[0] |
| 85 | + |
| 86 | + # test arguments for opt_map |
| 87 | + # eddy_correct class doesn't have opt_map{} |
| 88 | + clean_directory(outdir, cwd) |
| 89 | + |
| 90 | + |
| 91 | + |
0 commit comments