From 2fa377aed966796e1fdeb932ac1a49c0ad215256 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Wed, 24 Jul 2019 11:53:33 +0100 Subject: [PATCH] TEST: More basic tests for x5 scripts --- tests/test_scripts/test_fsl_apply_x5.py | 13 ++++ tests/test_scripts/test_fsl_convert_x5.py | 83 +++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/tests/test_scripts/test_fsl_apply_x5.py b/tests/test_scripts/test_fsl_apply_x5.py index 96c2740b1..dbe2bd3a7 100644 --- a/tests/test_scripts/test_fsl_apply_x5.py +++ b/tests/test_scripts/test_fsl_apply_x5.py @@ -3,6 +3,8 @@ import numpy as np +import pytest + import fsl.scripts.fsl_apply_x5 as fsl_apply_x5 import fsl.data.image as fslimage import fsl.utils.image.resample as resample @@ -31,6 +33,17 @@ def _random_image(aff, shape=None): return fslimage.Image(data, xform=aff) +def test_help(): + def run(args): + with pytest.raises(SystemExit) as e: + fsl_apply_x5.main(args) + assert e.value.code == 0 + + run([]) + run(['-h']) + + + def test_linear(): with tempdir.tempdir(): diff --git a/tests/test_scripts/test_fsl_convert_x5.py b/tests/test_scripts/test_fsl_convert_x5.py index 4d54e18f3..e65fd9e46 100644 --- a/tests/test_scripts/test_fsl_convert_x5.py +++ b/tests/test_scripts/test_fsl_convert_x5.py @@ -2,6 +2,9 @@ import os.path as op +import hashlib + +import pytest import numpy as np @@ -28,6 +31,20 @@ def random_image(): return fslimage.Image(data, xform=aff) +def test_convert_flirt_help(): + + def run(args): + with pytest.raises(SystemExit) as e: + fsl_convert_x5.main(args) + assert e.value.code == 0 + + run([]) + run(['-h']) + run(['flirt']) + run(['fnirt']) + run(['flirt', '-h']) + run(['fnirt', '-h']) + def test_convert_flirt(): with tempdir.tempdir(): @@ -60,6 +77,42 @@ def test_convert_flirt(): assert np.all(np.isclose(gotxform, xform)) +def test_convert_flirt_sameformat(): + with tempdir.tempdir(): + src = random_image() + ref = random_image() + src.save('src') + ref.save('ref') + + xform = affine.compose( + np.random.randint(1, 10, 3), + np.random.randint(-100, 100, 3), + (np.random.random(3) - 0.5) * np.pi) + + np.savetxt('src2ref.mat', xform) + + # test both .mat and .x5 + fsl_convert_x5.main('flirt -s src -r ref ' + 'src2ref.mat src2ref.x5'.split()) + + # mat -> mat + fsl_convert_x5.main('flirt -s src -r ref ' + 'src2ref.mat copy.mat'.split()) + + # x5 -> x5 + fsl_convert_x5.main('flirt -s src -r ref ' + 'src2ref.x5 copy.x5'.split()) + + with open('src2ref.mat', 'rb') as f: origmat = hashlib.md5(f.read()).digest() + with open('copy.mat', 'rb') as f: copymat = hashlib.md5(f.read()).digest() + with open('src2ref.x5', 'rb') as f: origx5 = hashlib.md5(f.read()).digest() + with open('copy.x5', 'rb') as f: copyx5 = hashlib.md5(f.read()).digest() + + assert origmat == copymat + assert origx5 == copyx5 + + + def test_convert_fnirt_deformation_field(): datadir = op.join(op.dirname(__file__), '..', @@ -124,3 +177,33 @@ def test_convert_fnirt_coefficient_field(): diff = np.abs(dfnii.data - df.data) tols = {'rtol' : 1e-5, 'atol' : 1e-5} assert np.all(np.isclose(dfnii.data, df.data, **tols)) + + +def test_convert_fnirt_sameformat(): + + datadir = op.join(op.dirname(__file__), '..', + 'test_transform', 'testdata', 'nonlinear') + srcfile = op.join(datadir, 'src.nii.gz') + reffile = op.join(datadir, 'ref.nii.gz') + dffile = op.join(datadir, 'displacementfield.nii.gz') + + with tempdir.tempdir(): + + base = list('fnirt -s {} -r {}'.format(srcfile, reffile).split()) + + # test both .mat and .x5 + fsl_convert_x5.main(base + [dffile, 'src2ref.x5']) + + # nii -> nii + fsl_convert_x5.main(base + [dffile, 'copy.nii.gz']) + + # x5 -> x5 + fsl_convert_x5.main(base + [dffile, 'copy.x5']) + + with open(dffile, 'rb') as f: origdef = hashlib.md5(f.read()).digest() + with open('copy.nii.gz', 'rb') as f: copydef = hashlib.md5(f.read()).digest() + with open('src2ref.x5', 'rb') as f: origx5 = hashlib.md5(f.read()).digest() + with open('copy.x5', 'rb') as f: copyx5 = hashlib.md5(f.read()).digest() + + assert origdef == copydef + assert origx5 == copyx5 -- GitLab