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

Merge branch 'enh/pathutils' into 'master'

Enh/pathutils

See merge request fsl/fslpy!247
parents 29e59f44 898176f8
No related branches found
No related tags found
No related merge requests found
Pipeline #5577 passed
#!/usr/bin/env python
#
# test_imrm.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import os
from fsl.utils.tempdir import tempdir
import fsl.scripts.imrm as imrm
from tests import touch
def test_imrm_usage():
assert imrm.main([]) != 0
def test_imrm():
# (files present, command, expected)
tests = [
('a.nii', 'a', ''),
('a.nii.gz', 'a', ''),
('a.img a.hdr', 'a', ''),
('a.img', 'a', ''),
('a.hdr', 'a', ''),
('a.nii b.nii', 'a', 'b.nii'),
('a.nii b.nii', 'a b', ''),
('a.nii b.nii', 'a b.nii', ''),
# suffix doesn't have to be correct
('a.nii.gz', 'a.nii', ''),
# files don't exist -> no problem
('a.nii', 'b', 'a.nii'),
]
for files, command, expected in tests:
with tempdir():
for f in files.split():
touch(f)
print('files', files)
print('command', command)
print('expected', expected)
ret = imrm.main(('imrm ' + command).split())
assert ret == 0
assert sorted(os.listdir()) == sorted(expected.split())
#!/usr/bin/env python
#
# test_imtest.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import os
import os.path as op
import fsl.utils.path as fslpath
from fsl.utils.tempdir import tempdir
import fsl.scripts.imtest as imtest
from tests import CaptureStdout, touch
def test_wrongargs():
cap = CaptureStdout()
with cap:
assert imtest.main([]) == 0
assert cap.stdout.strip() == '0'
def test_imtest():
# (files, input, expected)
tests = [
('a.nii', 'a', '1'),
('a.nii', 'a.nii', '1'),
('a.nii', 'a.nii.gz', '1'), # imtest is suffix-agnostic
('a.img a.hdr', 'a', '1'),
('a.img a.hdr', 'a.img', '1'),
('a.img a.hdr', 'a.hdr', '1'),
('a.img', 'a', '0'),
('a.img', 'a.img', '0'),
('a.img', 'a.hdr', '0'),
('a.hdr', 'a', '0'),
('a.hdr', 'a.img', '0'),
('a.hdr', 'a.hdr', '0'),
('dir/a.nii', 'dir/a', '1'),
('dir/a.img dir/a.hdr', 'dir/a', '1'),
]
for files, input, expected in tests:
with tempdir():
for f in files.split():
dirname = op.dirname(f)
if dirname != '':
os.makedirs(dirname, exist_ok=True)
touch(f)
cap = CaptureStdout()
with cap:
assert imtest.main([input]) == 0
assert cap.stdout.strip() == expected
# test that sym-links are
# followed correctly
with tempdir():
touch('image.nii.gz')
os.symlink('image.nii.gz', 'link.nii.gz')
cap = CaptureStdout()
with cap:
assert imtest.main(['link']) == 0
assert cap.stdout.strip() == '1'
# sym-links in sub-directories
# (old imtest would not work
# in this scenario)
with tempdir():
os.mkdir('subdir')
impath = op.join('subdir', 'image.nii.gz')
lnpath = op.join('subdir', 'link.nii.gz')
touch(impath)
os.symlink('image.nii.gz', lnpath)
cap = CaptureStdout()
with cap:
assert imtest.main([lnpath]) == 0
assert cap.stdout.strip() == '1'
#!/usr/bin/env python
#
# test_remove_ext.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import fsl.scripts.remove_ext as remove_ext
from tests import CaptureStdout
def test_usage():
assert remove_ext.main([]) != 0
def test_remove_ext():
# (input, expected output)
tests = [
('a', 'a'),
('a.nii', 'a'),
('a.nii.gz', 'a'),
('a.txt', 'a.txt'),
('a.nii b.img c.hdr', 'a b c'),
('a.nii b.img b.hdr', 'a b b'),
('a b.img c.txt', 'a b c.txt'),
('a.nii.gz b c.mnc', 'a b c'),
]
for input, expected in tests:
cap = CaptureStdout()
with cap:
ret = remove_ext.main(input.split())
assert ret == 0
got = cap.stdout.split()
assert sorted(got) == sorted(expected.split())
#!/usr/bin/env python
#
# test_vest2text.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import numpy as np
import fsl.data.vest as fslvest
import fsl.scripts.Text2Vest as Text2Vest
import fsl.scripts.Vest2Text as Vest2Text
from tests import tempdir
def test_usage():
assert Vest2Text.main([]) == 0
assert Text2Vest.main([]) == 0
def test_Vest2Text():
with tempdir():
data = np.random.random((20, 10))
vest = fslvest.generateVest(data)
with open('data.vest', 'wt') as f:
f.write(vest)
assert Vest2Text.main(['data.vest', 'data.txt']) == 0
got = np.loadtxt('data.txt')
assert np.all(np.isclose(data, got))
def test_Text2Vest():
with tempdir():
data = np.random.random((20, 10))
np.savetxt('data.txt', data)
assert Text2Vest.main(['data.txt', 'data.vest']) == 0
got = fslvest.loadVestFile('data.vest', ignoreHeader=False)
assert np.all(np.isclose(data, got))
......@@ -6,16 +6,20 @@
#
import os.path as op
import shutil
import tempfile
import warnings
import os.path as op
import textwrap as tw
import io
import shutil
import tempfile
import warnings
import numpy as np
import pytest
import numpy as np
import pytest
import fsl.data.vest as vest
from tests import tempdir
testfile1 = """%!VEST-LUT
%%BeginInstance
......@@ -214,3 +218,75 @@ def test_loadVestLutFile():
finally:
shutil.rmtree(testdir)
def test_generateVest():
def readvest(vstr):
lines = vstr.split('\n')
nrows = [l for l in lines if 'NumPoints' in l][0]
ncols = [l for l in lines if 'NumWaves' in l][0]
nrows = int(nrows.split()[1])
ncols = int(ncols.split()[1])
data = '\n'.join(lines[3:])
data = np.loadtxt(io.StringIO(data)).reshape((nrows, ncols))
return ((nrows, ncols), data)
# shape, expectedshape
tests = [
((10, ), ( 1, 10)),
((10, 1), (10, 1)),
(( 1, 10), ( 1, 10)),
(( 3, 5), ( 3, 5)),
(( 5, 3), ( 5, 3))
]
for shape, expshape in tests:
data = np.random.random(shape)
vstr = vest.generateVest(data)
gotshape, gotdata = readvest(vstr)
data = data.reshape(expshape)
assert expshape == gotshape
assert np.all(np.isclose(data, gotdata))
def test_loadVestFile():
def genvest(data, path, shapeOverride=None):
if shapeOverride is None:
nrows, ncols = data.shape
else:
nrows, ncols = shapeOverride
with open(path, 'wt') as f:
f.write(f'/NumWaves {ncols}\n')
f.write(f'/NumPoints {nrows}\n')
f.write( '/Matrix\n')
if np.issubdtype(data.dtype, np.integer): fmt = '%d'
else: fmt = '%0.12f'
np.savetxt(f, data, fmt=fmt)
with tempdir():
data = np.random.randint(1, 100, (10, 20))
genvest(data, 'data.vest')
assert np.all(data == vest.loadVestFile('data.vest'))
data = np.random.random((20, 10))
genvest(data, 'data.vest')
assert np.all(np.isclose(data, vest.loadVestFile('data.vest')))
# should pass
vest.loadVestFile('data.vest', ignoreHeader=False)
# invalid VEST header
genvest(data, 'data.vest', (10, 20))
# default behaviour - ignore header
assert np.all(np.isclose(data, vest.loadVestFile('data.vest')))
with pytest.raises(ValueError):
vest.loadVestFile('data.vest', ignoreHeader=False)
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