Esempio n. 1
0
def test_run_interface():
    mc = mlab.MatlabCommand(matlab_cmd='foo_m')
    yield assert_raises(ValueError, mc.run) # script is mandatory
    mc.inputs.script = 'a=1;'
    yield assert_raises(IOError, mc.run) # foo_m is not an executable
    cwd = os.getcwd()
    basedir = mkdtemp()
    os.chdir(basedir)
    res = mlab.MatlabCommand(script='foo', paths=[basedir], mfile=True).run() # bypasses ubuntu dash issue
    yield assert_equal(res.runtime.returncode, 1)
    res = mlab.MatlabCommand(script='a=1;', paths=[basedir], mfile=True).run() # bypasses ubuntu dash issue
    yield assert_equal(res.runtime.returncode, 0)
    os.chdir(cwd)
    rmtree(basedir)
Esempio n. 2
0
def test_reslice():
    moving = example_data(infile="functional.nii")
    space_defining = example_data(infile="T1.nii")
    reslice = spmu.Reslice(matlab_cmd="mymatlab_version")
    assert_equal(reslice.inputs.matlab_cmd, "mymatlab_version")
    reslice.inputs.in_file = moving
    reslice.inputs.space_defining = space_defining
    assert_equal(reslice.inputs.interp, 0)
    assert_raises(TraitError, reslice.inputs.trait_set, interp="nearest")
    assert_raises(TraitError, reslice.inputs.trait_set, interp=10)
    reslice.inputs.interp = 1
    script = reslice._make_matlab_command(None)
    outfile = fname_presuffix(moving, prefix="r")
    assert_equal(reslice.inputs.out_file, outfile)
    expected = "\nflags.mean=0;\nflags.which=1;\nflags.mask=0;"
    assert_equal(expected in script.replace(" ", ""), True)
    expected_interp = "flags.interp = 1;\n"
    assert_equal(expected_interp in script, True)
    assert_equal("spm_reslice(invols, flags);" in script, True)
Esempio n. 3
0
def test_reslice():
    moving = example_data(infile='functional.nii')
    space_defining = example_data(infile='T1.nii')
    reslice = spmu.Reslice(matlab_cmd='mymatlab_version')
    assert_equal(reslice.inputs.matlab_cmd, 'mymatlab_version')
    reslice.inputs.in_file = moving
    reslice.inputs.space_defining = space_defining
    assert_equal(reslice.inputs.interp, 0)
    assert_raises(TraitError, reslice.inputs.trait_set, interp='nearest')
    assert_raises(TraitError, reslice.inputs.trait_set, interp=10)
    reslice.inputs.interp = 1
    script = reslice._make_matlab_command(None)
    outfile = fname_presuffix(moving, prefix='r')
    assert_equal(reslice.inputs.out_file, outfile)
    expected = '\nflags.mean=0;\nflags.which=1;\nflags.mask=0;'
    assert_equal(expected in script.replace(' ', ''), True)
    expected_interp = 'flags.interp = 1;\n'
    assert_equal(expected_interp in script, True)
    assert_equal('spm_reslice(invols, flags);' in script, True)
def test_reslice():
    moving = example_data(infile='functional.nii')
    space_defining = example_data(infile='T1.nii')
    reslice = spmu.Reslice(matlab_cmd='mymatlab_version')
    assert_equal(reslice.inputs.matlab_cmd, 'mymatlab_version')
    reslice.inputs.in_file = moving
    reslice.inputs.space_defining = space_defining
    assert_equal(reslice.inputs.interp, 0)
    assert_raises(TraitError, reslice.inputs.trait_set, interp='nearest')
    assert_raises(TraitError, reslice.inputs.trait_set, interp=10)
    reslice.inputs.interp = 1
    script = reslice._make_matlab_command(None)
    outfile = fname_presuffix(moving, prefix='r')
    assert_equal(reslice.inputs.out_file, outfile)
    expected = '\nflags.mean=0;\nflags.which=1;\nflags.mask=0;'
    assert_equal(expected in script.replace(' ', ''), True)
    expected_interp = 'flags.interp = 1;\n'
    assert_equal(expected_interp in script, True)
    assert_equal('spm_reslice(invols, flags);' in script, True)
Esempio n. 5
0
def test_dicom_import():
    dicom = example_data(infile="dicomdir/123456-1-1.dcm")
    di = spmu.DicomImport(matlab_cmd="mymatlab")
    assert_equal(di.inputs.matlab_cmd, "mymatlab")
    assert_equal(di.inputs.output_dir_struct, "flat")
    assert_equal(di.inputs.output_dir, "./converted_dicom")
    assert_equal(di.inputs.format, "nii")
    assert_equal(di.inputs.icedims, False)
    assert_raises(TraitError, di.inputs.trait_set, output_dir_struct="wrong")
    assert_raises(TraitError, di.inputs.trait_set, format="FAT")
    assert_raises(TraitError, di.inputs.trait_set, in_files=["does_sfd_not_32fn_exist.dcm"])
    di.inputs.in_files = [dicom]
    assert_equal(di.inputs.in_files, [dicom])
Esempio n. 6
0
def test_dicom_import():
    dicom = example_data(infile='dicomdir/123456-1-1.dcm')
    di = spmu.DicomImport(matlab_cmd='mymatlab')
    assert_equal(di.inputs.matlab_cmd, 'mymatlab')
    assert_equal(di.inputs.output_dir_struct, 'flat')
    assert_equal(di.inputs.output_dir, './converted_dicom')
    assert_equal(di.inputs.format, 'nii')
    assert_equal(di.inputs.icedims, False)
    assert_raises(TraitError, di.inputs.trait_set, output_dir_struct='wrong')
    assert_raises(TraitError, di.inputs.trait_set, format='FAT')
    assert_raises(TraitError, di.inputs.trait_set, in_files=['does_sfd_not_32fn_exist.dcm'])
    di.inputs.in_files = [dicom]
    assert_equal(di.inputs.in_files, [dicom])
Esempio n. 7
0
def test_dicom_import():
    dicom = example_data(infile='dicomdir/123456-1-1.dcm')
    di = spmu.DicomImport(matlab_cmd='mymatlab')
    assert_equal(di.inputs.matlab_cmd, 'mymatlab')
    assert_equal(di.inputs.output_dir_struct, 'flat')
    assert_equal(di.inputs.output_dir, './converted_dicom')
    assert_equal(di.inputs.format, 'nii')
    assert_equal(di.inputs.icedims, False)
    assert_raises(TraitError, di.inputs.trait_set, output_dir_struct='wrong')
    assert_raises(TraitError, di.inputs.trait_set, format='FAT')
    assert_raises(TraitError, di.inputs.trait_set, in_files=['does_sfd_not_32fn_exist.dcm'])
    di.inputs.in_files = [dicom]
    assert_equal(di.inputs.in_files, [dicom])
Esempio n. 8
0
def test_bet():
    tmp_infile, tp_dir = setup_infile()
    better = fsl.BET()
    yield assert_equal(better.cmd, 'bet')

    # Test raising error with mandatory args absent
    yield assert_raises(ValueError, better.run)

    # Test generated outfile name
    better.inputs.in_file = tmp_infile
    outfile = fsl_name(better, 'foo_brain')
    outpath = os.path.join(os.getcwd(), outfile)
    realcmd = 'bet %s %s' % (tmp_infile, outpath)
    yield assert_equal(better.cmdline, realcmd)
    # Test specified outfile name
    outfile = fsl_name(better, '/newdata/bar')
    better.inputs.out_file = outfile
    realcmd = 'bet %s %s' % (tmp_infile, outfile)
    yield assert_equal(better.cmdline, realcmd)

    # infile foo.nii doesn't exist
    def func():
        better.run(in_file='foo2.nii', out_file='bar.nii')
    yield assert_raises(TraitError, func)

    # .run() based parameter setting
    better = fsl.BET()
    better.inputs.frac = 0.40
    outfile = fsl_name(better, 'outfile')
    betted = better.run(in_file=tmp_infile, out_file=outfile)
    yield assert_equal(betted.interface.inputs.in_file, tmp_infile)
    yield assert_equal(betted.interface.inputs.out_file, outfile)
    realcmd = 'bet %s %s -f 0.40' % (tmp_infile, outfile)
    yield assert_equal(betted.runtime.cmdline, realcmd)

    # Our options and some test values for them
    # Should parallel the opt_map structure in the class for clarity
    opt_map = {
        'outline':            ('-o', True),
        'mask':               ('-m', True),
        'skull':              ('-s', True),
        'no_output':           ('-n', True),
        'frac':               ('-f 0.40', 0.4),
        'vertical_gradient':  ('-g 0.75', 0.75),
        'radius':             ('-r 20', 20),
        'center':             ('-c 54 75 80', [54, 75, 80]),
        'threshold':          ('-t', True),
        'mesh':               ('-e', True),
        #'verbose':            ('-v', True),
        #'flags':              ('--i-made-this-up', '--i-made-this-up'),
            }
    # Currently we don't test -R, -S, -B, -Z, -F, -A or -A2

    # test each of our arguments
    better = fsl.BET()
    outfile = fsl_name(better, 'foo_brain')
    outpath = os.path.join(os.getcwd(), outfile)
    for name, settings in opt_map.items():
        better = fsl.BET(**{name: settings[1]})
        # Add mandatory input
        better.inputs.in_file = tmp_infile
        realcmd =  ' '.join([better.cmd, tmp_infile, outpath, settings[0]])
        yield assert_equal(better.cmdline, realcmd)
    teardown_infile(tmp_dir)
Esempio n. 9
0
def test_flirt():
    # setup
    tmpdir, infile, reffile = setup_flirt()
    
    flirter = fsl.FLIRT()
    yield assert_equal(flirter.cmd, 'flirt')

    flirter.inputs.bins = 256
    flirter.inputs.cost = 'mutualinfo'

    flirted = flirter.run(in_file=infile, reference=reffile,
                          out_file='outfile', out_matrix_file='outmat.mat')
    flirt_est = flirter.run(in_file=infile, reference=reffile,
                            out_matrix_file='outmat.mat')
    yield assert_not_equal(flirter, flirted)
    yield assert_not_equal(flirted, flirt_est)

    yield assert_equal(flirter.inputs.bins, flirted.interface.inputs.bins)
    yield assert_equal(flirter.inputs.cost, flirt_est.interface.inputs.cost)
    realcmd = 'flirt -in %s -ref %s -out outfile -omat outmat.mat ' \
        '-bins 256 -cost mutualinfo' % (infile, reffile)
    yield assert_equal(flirted.runtime.cmdline, realcmd)

    flirter = fsl.FLIRT()
    # infile not specified
    yield assert_raises(ValueError, flirter.run)
    flirter.inputs.in_file = infile
    # reference not specified
    yield assert_raises(ValueError, flirter.run)
    flirter.inputs.reference = reffile
    res = flirter.run()
    # Generate outfile and outmatrix
    pth, fname, ext = split_filename(infile)
    outfile = os.path.join(os.getcwd(),
                           fsl_name(flirter, '%s_flirt' %fname))
    outmat = '%s_flirt.mat' % fname
    outmat = os.path.join(os.getcwd(), outmat)
    realcmd = 'flirt -in %s -ref %s -out %s -omat %s' % (infile, reffile,
                                                         outfile, outmat)
    yield assert_equal(res.interface.cmdline, realcmd)

    _, tmpfile = tempfile.mkstemp(suffix = '.nii', dir = tmpdir)
    # Loop over all inputs, set a reasonable value and make sure the
    # cmdline is updated correctly.
    for key, trait_spec in sorted(fsl.FLIRT.input_spec().traits().items()):
        # Skip mandatory inputs and the trait methods
        if key in ('trait_added', 'trait_modified', 'in_file', 'reference',
                   'environ', 'output_type', 'out_file', 'out_matrix_file',
                   'in_matrix_file'):
            continue
        param = None
        value = None
        if key == 'args':
            param = '-v'
            value = '-v'
        elif isinstance(trait_spec.trait_type, File):
            value = tmpfile
            param = trait_spec.argstr  % value
        elif trait_spec.default is False:
            param = trait_spec.argstr
            value = True
        elif key in ('searchr_x', 'searchr_y', 'searchr_z'):
            value = [-45, 45]
            param = trait_spec.argstr % ' '.join(str(elt) for elt in value)
        else:
            value = trait_spec.default
            param = trait_spec.argstr % value
        cmdline = 'flirt -in %s -ref %s' % (infile, reffile)
        # Handle autogeneration of outfile
        pth, fname, ext = split_filename(infile)
        outfile = os.path.join(os.getcwd(),
                               fsl_name(fsl.FLIRT(),'%s_flirt' % fname))
        outfile = ' '.join(['-out', outfile])
        # Handle autogeneration of outmatrix
        outmatrix = '%s_flirt.mat' % fname
        outmatrix = os.path.join(os.getcwd(), outmatrix)
        outmatrix = ' '.join(['-omat', outmatrix])
        # Build command line
        cmdline = ' '.join([cmdline, outfile, outmatrix, param])
        flirter = fsl.FLIRT(in_file = infile, reference = reffile)
        setattr(flirter.inputs, key, value)
        yield assert_equal(flirter.cmdline, cmdline)

    # Test OutputSpec
    flirter = fsl.FLIRT(in_file = infile, reference = reffile)
    pth, fname, ext = split_filename(infile)
    flirter.inputs.out_file = ''.join(['foo', ext])
    flirter.inputs.out_matrix_file = ''.join(['bar', ext])
    outs = flirter._list_outputs()
    yield assert_equal(outs['out_file'], flirter.inputs.out_file)
    yield assert_equal(outs['out_matrix_file'], flirter.inputs.out_matrix_file)

    teardown_flirt(tmpdir)
Esempio n. 10
0
    origdir = os.getcwd()
    os.chdir(tempdir)

    node = pe.Node(utility.Function(input_names=["size"],
                                    output_names=["random_array"],
                                    function=make_random_array),
                   name="should_fail")
    try:
        node.inputs.size = 10
        node.run()
    finally:
        os.chdir(origdir)
        shutil.rmtree(tempdir)


assert_raises(NameError, should_fail)


def test_function_with_imports():

    tempdir = os.path.realpath(mkdtemp())
    origdir = os.getcwd()
    os.chdir(tempdir)

    node = pe.Node(utility.Function(input_names=["size"],
                                    output_names=["random_array"],
                                    function=make_random_array,
                                    imports=["import numpy as np"]),
                   name="should_not_fail")
    print(node.inputs.function_str)
    try:
Esempio n. 11
0
    origdir = os.getcwd()
    os.chdir(tempdir)

    node = pe.Node(utility.Function(input_names=["size"],
                                    output_names=["random_array"],
                                    function=make_random_array),
                   name="should_fail")
    try:
        node.inputs.size = 10
        node.run()
    finally:
        os.chdir(origdir)
        shutil.rmtree(tempdir)


assert_raises(NameError, should_fail)


def test_function_with_imports():

    tempdir = os.path.realpath(mkdtemp())
    origdir = os.getcwd()
    os.chdir(tempdir)

    node = pe.Node(utility.Function(input_names=["size"],
                                    output_names=["random_array"],
                                    function=make_random_array,
                                    imports=["import numpy as np"]),
                   name="should_not_fail")
    print node.inputs.function_str
    try:
Esempio n. 12
0
def test_init():
    yield assert_raises(Exception, pe.Workflow)
    pipe = pe.Workflow(name='pipe')
    yield assert_equal(type(pipe._graph), nx.DiGraph)
    yield assert_equal(pipe._execgraph, None)