Skip to content
Snippets Groups Projects
Commit 2a8929a3 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

TEST: update wrapper tests ("output" -> "stdout"). New test for results

attribute access. New test for fileOrText decorator
parent aac90673
No related branches found
No related tags found
No related merge requests found
...@@ -48,7 +48,7 @@ def test_bet(): ...@@ -48,7 +48,7 @@ def test_bet():
bet = op.join(fsldir, 'bin', 'bet') bet = op.join(fsldir, 'bin', 'bet')
result = fw.bet('input', 'output', mask=True, c=(10, 20, 30)) result = fw.bet('input', 'output', mask=True, c=(10, 20, 30))
expected = (bet + ' input output', ('-m', '-c 10 20 30')) expected = (bet + ' input output', ('-m', '-c 10 20 30'))
assert checkResult(result.output[0], *expected, stripdir=[2]) assert checkResult(result.stdout[0], *expected, stripdir=[2])
def test_robustfov(): def test_robustfov():
...@@ -56,7 +56,7 @@ def test_robustfov(): ...@@ -56,7 +56,7 @@ def test_robustfov():
rfov = op.join(fsldir, 'bin', 'robustfov') rfov = op.join(fsldir, 'bin', 'robustfov')
result = fw.robustfov('input', 'output', b=180) result = fw.robustfov('input', 'output', b=180)
expected = (rfov + ' -i input', ('-r output', '-b 180')) expected = (rfov + ' -i input', ('-r output', '-b 180'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_eddy_cuda(): def test_eddy_cuda():
...@@ -73,7 +73,7 @@ def test_eddy_cuda(): ...@@ -73,7 +73,7 @@ def test_eddy_cuda():
'--out=out', '--out=out',
'--dont_mask_output')) '--dont_mask_output'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_topup(): def test_topup():
...@@ -81,7 +81,7 @@ def test_topup(): ...@@ -81,7 +81,7 @@ def test_topup():
topup = op.join(fsldir, 'bin', 'topup') topup = op.join(fsldir, 'bin', 'topup')
result = fw.topup('imain', 'datain', minmet=1) result = fw.topup('imain', 'datain', minmet=1)
expected = topup + ' --imain=imain --datain=datain --minmet=1' expected = topup + ' --imain=imain --datain=datain --minmet=1'
assert result.output[0] == expected assert result.stdout[0] == expected
def test_flirt(): def test_flirt():
...@@ -90,7 +90,7 @@ def test_flirt(): ...@@ -90,7 +90,7 @@ def test_flirt():
result = fw.flirt('src', 'ref', usesqform=True, anglerep='euler') result = fw.flirt('src', 'ref', usesqform=True, anglerep='euler')
expected = (flirt + ' -in src -ref ref', expected = (flirt + ' -in src -ref ref',
('-usesqform', '-anglerep euler')) ('-usesqform', '-anglerep euler'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_applyxfm(): def test_applyxfm():
...@@ -102,7 +102,7 @@ def test_applyxfm(): ...@@ -102,7 +102,7 @@ def test_applyxfm():
'-out out', '-out out',
'-init mat', '-init mat',
'-interp trilinear')) '-interp trilinear'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_applyxfm(): def test_applyxfm():
...@@ -113,7 +113,7 @@ def test_applyxfm(): ...@@ -113,7 +113,7 @@ def test_applyxfm():
expected = (applyxfm + ' src ref out mat', expected = (applyxfm + ' src ref out mat',
('-fourdigit', ('-fourdigit',
'-userprefix boo')) '-userprefix boo'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_invxfm(): def test_invxfm():
...@@ -121,7 +121,7 @@ def test_invxfm(): ...@@ -121,7 +121,7 @@ def test_invxfm():
cnvxfm = op.join(fsldir, 'bin', 'convert_xfm') cnvxfm = op.join(fsldir, 'bin', 'convert_xfm')
result = fw.invxfm('mat', 'output') result = fw.invxfm('mat', 'output')
expected = cnvxfm + ' -omat output -inverse mat' expected = cnvxfm + ' -omat output -inverse mat'
assert result.output[0] == expected assert result.stdout[0] == expected
def test_concatxfm(): def test_concatxfm():
...@@ -129,7 +129,7 @@ def test_concatxfm(): ...@@ -129,7 +129,7 @@ def test_concatxfm():
cnvxfm = op.join(fsldir, 'bin', 'convert_xfm') cnvxfm = op.join(fsldir, 'bin', 'convert_xfm')
result = fw.concatxfm('mat1', 'mat2', 'output') result = fw.concatxfm('mat1', 'mat2', 'output')
expected = cnvxfm + ' -omat output -concat mat2 mat1' expected = cnvxfm + ' -omat output -concat mat2 mat1'
assert result.output[0] == expected assert result.stdout[0] == expected
def test_mcflirt(): def test_mcflirt():
...@@ -140,7 +140,7 @@ def test_mcflirt(): ...@@ -140,7 +140,7 @@ def test_mcflirt():
('-out output', ('-out output',
'-cost normcorr', '-cost normcorr',
'-dof 12')) '-dof 12'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_fnirt(): def test_fnirt():
...@@ -153,7 +153,7 @@ def test_fnirt(): ...@@ -153,7 +153,7 @@ def test_fnirt():
'--iout=iout', '--iout=iout',
'--fout=fout', '--fout=fout',
'--subsamp=8,6,4,2')) '--subsamp=8,6,4,2'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_applywarp(): def test_applywarp():
...@@ -162,7 +162,7 @@ def test_applywarp(): ...@@ -162,7 +162,7 @@ def test_applywarp():
result = fw.applywarp('src', 'ref', 'out', warp='warp', abs=True, super=True) result = fw.applywarp('src', 'ref', 'out', warp='warp', abs=True, super=True)
expected = (applywarp + ' --in=src --ref=ref --out=out', expected = (applywarp + ' --in=src --ref=ref --out=out',
('--warp=warp', '--abs', '--super')) ('--warp=warp', '--abs', '--super'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_invwarp(): def test_invwarp():
...@@ -172,7 +172,7 @@ def test_invwarp(): ...@@ -172,7 +172,7 @@ def test_invwarp():
rel=True, noconstraint=True) rel=True, noconstraint=True)
expected = (invwarp + ' --warp=warp --ref=ref --out=out', expected = (invwarp + ' --warp=warp --ref=ref --out=out',
('--rel', '--noconstraint')) ('--rel', '--noconstraint'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_convertwarp(): def test_convertwarp():
...@@ -181,7 +181,7 @@ def test_convertwarp(): ...@@ -181,7 +181,7 @@ def test_convertwarp():
result = fw.convertwarp('out', 'ref', absout=True, jacobian='jacobian') result = fw.convertwarp('out', 'ref', absout=True, jacobian='jacobian')
expected = (cnvwarp + ' --ref=ref --out=out', expected = (cnvwarp + ' --ref=ref --out=out',
('--absout', '--jacobian=jacobian')) ('--absout', '--jacobian=jacobian'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_fugue(): def test_fugue():
...@@ -193,7 +193,7 @@ def test_fugue(): ...@@ -193,7 +193,7 @@ def test_fugue():
'--warp=warp', '--warp=warp',
'--median', '--median',
'--dwell=10')) '--dwell=10'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
...@@ -203,7 +203,7 @@ def test_sigloss(): ...@@ -203,7 +203,7 @@ def test_sigloss():
result = fw.sigloss('input', 'sigloss', mask='mask', te=0.5) result = fw.sigloss('input', 'sigloss', mask='mask', te=0.5)
expected = (sigloss + ' --in input --sigloss sigloss', expected = (sigloss + ' --in input --sigloss sigloss',
('--mask mask', '--te 0.5')) ('--mask mask', '--te 0.5'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_prelude(): def test_prelude():
...@@ -217,7 +217,7 @@ def test_prelude(): ...@@ -217,7 +217,7 @@ def test_prelude():
'--out=out', '--out=out',
'--labelslices', '--labelslices',
'--start=5')) '--start=5'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_melodic(): def test_melodic():
...@@ -226,7 +226,7 @@ def test_melodic(): ...@@ -226,7 +226,7 @@ def test_melodic():
result = fw.melodic('input', dim=50, mask='mask', Oall=True) result = fw.melodic('input', dim=50, mask='mask', Oall=True)
expected = (melodic + ' --in=input', expected = (melodic + ' --in=input',
('--dim=50', '--mask=mask', '--Oall')) ('--dim=50', '--mask=mask', '--Oall'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_fsl_regfilt(): def test_fsl_regfilt():
...@@ -236,7 +236,7 @@ def test_fsl_regfilt(): ...@@ -236,7 +236,7 @@ def test_fsl_regfilt():
filter=(1, 2, 3, 4), vn=True) filter=(1, 2, 3, 4), vn=True)
expected = (regfilt + ' --in=input --out=output --design=design', expected = (regfilt + ' --in=input --out=output --design=design',
('--filter=1,2,3,4', '--vn')) ('--filter=1,2,3,4', '--vn'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
...@@ -245,7 +245,7 @@ def test_fslreorient2std(): ...@@ -245,7 +245,7 @@ def test_fslreorient2std():
r2std = op.join(fsldir, 'bin', 'fslreorient2std') r2std = op.join(fsldir, 'bin', 'fslreorient2std')
result = fw.fslreorient2std('input', 'output') result = fw.fslreorient2std('input', 'output')
expected = r2std + ' input output' expected = r2std + ' input output'
assert result.output[0] == expected assert result.stdout[0] == expected
def test_fslroi(): def test_fslroi():
...@@ -254,15 +254,15 @@ def test_fslroi(): ...@@ -254,15 +254,15 @@ def test_fslroi():
result = fw.fslroi('input', 'output', 1, 10) result = fw.fslroi('input', 'output', 1, 10)
expected = fslroi + ' input output 1 10' expected = fslroi + ' input output 1 10'
assert result.output[0] == expected assert result.stdout[0] == expected
result = fw.fslroi('input', 'output', 1, 10, 2, 20, 3, 30) result = fw.fslroi('input', 'output', 1, 10, 2, 20, 3, 30)
expected = fslroi + ' input output 1 10 2 20 3 30' expected = fslroi + ' input output 1 10 2 20 3 30'
assert result.output[0] == expected assert result.stdout[0] == expected
result = fw.fslroi('input', 'output', 1, 10, 2, 20, 3, 30, 4, 40) result = fw.fslroi('input', 'output', 1, 10, 2, 20, 3, 30, 4, 40)
expected = fslroi + ' input output 1 10 2 20 3 30 4 40' expected = fslroi + ' input output 1 10 2 20 3 30 4 40'
assert result.output[0] == expected assert result.stdout[0] == expected
def test_slicer(): def test_slicer():
...@@ -270,7 +270,7 @@ def test_slicer(): ...@@ -270,7 +270,7 @@ def test_slicer():
slicer = op.join(fsldir, 'bin', 'slicer') slicer = op.join(fsldir, 'bin', 'slicer')
result = fw.slicer('input1', 'input2', i=(20, 100), x=(20, 'x.png')) result = fw.slicer('input1', 'input2', i=(20, 100), x=(20, 'x.png'))
expected = slicer + ' input1 input2 -i 20 100 -x 20 x.png' expected = slicer + ' input1 input2 -i 20 100 -x 20 x.png'
assert result.output[0] == expected assert result.stdout[0] == expected
def test_cluster(): def test_cluster():
...@@ -280,7 +280,7 @@ def test_cluster(): ...@@ -280,7 +280,7 @@ def test_cluster():
fractional=True, osize='osize') fractional=True, osize='osize')
expected = (cluster + ' --in=input --thresh=thresh', expected = (cluster + ' --in=input --thresh=thresh',
('--fractional', '--osize=osize')) ('--fractional', '--osize=osize'))
assert checkResult(result.output[0], *expected) assert checkResult(result.stdout[0], *expected)
def test_fslmaths(): def test_fslmaths():
...@@ -300,7 +300,7 @@ def test_fslmaths(): ...@@ -300,7 +300,7 @@ def test_fslmaths():
'-inm inmim', '-bptf 1 10', 'output'] '-inm inmim', '-bptf 1 10', 'output']
expected = ' '.join(expected) expected = ' '.join(expected)
assert result.output[0] == expected assert result.stdout[0] == expected
# test LOAD output # test LOAD output
with tempdir() as td, mockFSLDIR(bin=('fslmaths',)) as fsldir: with tempdir() as td, mockFSLDIR(bin=('fslmaths',)) as fsldir:
...@@ -328,12 +328,12 @@ def test_fast(): ...@@ -328,12 +328,12 @@ def test_fast():
result = fw.fast('input', 'myseg', n_classes=3) result = fw.fast('input', 'myseg', n_classes=3)
expected = [cmd, '-v', '--out=myseg', '--class=3', 'input'] expected = [cmd, '-v', '--out=myseg', '--class=3', 'input']
assert result.output[0] == ' '.join(expected) assert result.stdout[0] == ' '.join(expected)
result = fw.fast(('in1', 'in2', 'in3'), 'myseg', n_classes=3) result = fw.fast(('in1', 'in2', 'in3'), 'myseg', n_classes=3)
expected = [cmd, '-v', '--out=myseg', '--class=3', 'in1', 'in2', 'in3'] expected = [cmd, '-v', '--out=myseg', '--class=3', 'in1', 'in2', 'in3']
assert result.output[0] == ' '.join(expected) assert result.stdout[0] == ' '.join(expected)
...@@ -348,4 +348,4 @@ def test_fsl_anat(): ...@@ -348,4 +348,4 @@ def test_fsl_anat():
expected = [cmd, '-i', 't1', '-o', 'fsl_anat', '-t', 'T1', expected = [cmd, '-i', 't1', '-o', 'fsl_anat', '-t', 'T1',
'-s', '25'] '-s', '25']
assert result.output[0] == ' '.join(expected) assert result.stdout[0] == ' '.join(expected)
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
import os.path as op import os.path as op
import os import os
import shlex import shlex
import pathlib
import textwrap import textwrap
try: from unittest import mock try: from unittest import mock
...@@ -353,6 +354,30 @@ def test_fileOrThing_sequence(): ...@@ -353,6 +354,30 @@ def test_fileOrThing_sequence():
assert np.all(func(infiles[0], wutils.LOAD)['out'] == inputs[0]) assert np.all(func(infiles[0], wutils.LOAD)['out'] == inputs[0])
def test_fileOrText():
@wutils.fileOrText('input', 'output')
def func(input, output):
data = open(input).read()
data = ''.join(['{}{}'.format(c, c) for c in data])
open(output, 'wt').write(data)
with tempdir.tempdir():
data = 'abcdefg'
exp = 'aabbccddeeffgg'
open('input.txt', 'wt').write(data)
func(pathlib.Path('input.txt'), pathlib.Path('output.txt'))
assert open('output.txt').read() == exp
func('abcdefg', pathlib.Path('output.txt'))
assert open('output.txt').read() == exp
assert func('12345', wutils.LOAD).output == '1122334455'
def test_fileOrThing_outprefix(): def test_fileOrThing_outprefix():
@wutils.fileOrImage('img', outprefix='output_base') @wutils.fileOrImage('img', outprefix='output_base')
...@@ -496,6 +521,73 @@ def test_fileOrThing_outprefix_directory(): ...@@ -496,6 +521,73 @@ def test_fileOrThing_outprefix_directory():
assert np.all(np.asanyarray(res[op.join('foo', 'myout_imgs', 'img4')].dataobj) == exp4) assert np.all(np.asanyarray(res[op.join('foo', 'myout_imgs', 'img4')].dataobj) == exp4)
def test_fileOrThing_results():
@wutils.fileOrArray('input', 'regular_output', outprefix='outpref')
def func(input, regular_output, outpref):
input = np.loadtxt(input)
regout = input * 2
prefouts = []
for i in range(3, 6):
prefouts.append(input * i)
np.savetxt(regular_output, regout)
for i, o in enumerate(prefouts):
np.savetxt('{}_{}.txt'.format(outpref, i), o)
return ('return', 'value')
input = np.random.randint(1, 10, (3, 3))
infile = 'input.txt'
exp = [input * i for i in range(2, 6)]
with tempdir.tempdir():
np.savetxt(infile, input)
result = func('input.txt', 'regout.txt', 'outpref')
assert len(result) == 0
assert result.stdout == ('return', 'value')
assert (np.loadtxt('regout.txt') == exp[0]).all()
for i in range(3):
assert (np.loadtxt('outpref_{}.txt'.format(i)) == exp[i+1]).all()
result = func(input, 'regout.txt', 'outpref')
assert len(result) == 0
assert result.stdout == ('return', 'value')
assert (np.loadtxt('regout.txt') == exp[0]).all()
for i in range(3):
assert (np.loadtxt('outpref_{}.txt'.format(i)) == exp[i+1]).all()
result = func(input, wutils.LOAD, 'outpref')
assert len(result) == 1
assert result.stdout == ('return', 'value')
assert (result .regular_output == exp[0]).all()
assert (result['regular_output'] == exp[0]).all()
for i in range(3):
assert (np.loadtxt('outpref_{}.txt'.format(i)) == exp[i+1]).all()
# todo outpref
result = func(input, wutils.LOAD, wutils.LOAD)
assert len(result) == 4
assert result.stdout == ('return', 'value')
assert (result .regular_output == exp[0]).all()
assert (result['regular_output'] == exp[0]).all()
assert (result .outpref_0 == exp[1]).all()
assert (result['outpref_0'] == exp[1]).all()
assert (result .outpref_1 == exp[2]).all()
assert (result['outpref_1'] == exp[2]).all()
assert (result .outpref_2 == exp[3]).all()
assert (result['outpref_2'] == exp[3]).all()
for i in range(3):
assert (np.loadtxt('outpref_{}.txt'.format(i)) == exp[i+1]).all()
def test_chained_fileOrImageAndArray(): def test_chained_fileOrImageAndArray():
@wutils.fileOrImage('image', 'outimage') @wutils.fileOrImage('image', 'outimage')
@wutils.fileOrArray('array', 'outarray') @wutils.fileOrArray('array', 'outarray')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment