Skip to content
Snippets Groups Projects
Commit 045de4aa authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Mesh and imagewrapper.naninfrange unit tests

parent de6dcde6
No related branches found
No related tags found
No related merge requests found
...@@ -286,6 +286,47 @@ def test_sliceTupleToSliceObj(): ...@@ -286,6 +286,47 @@ def test_sliceTupleToSliceObj():
assert func(slices) == sliceobj assert func(slices) == sliceobj
def test_naninfrange():
# numinf, numnan, expectedResult
tests = [( 0, 0, (0, 100)),
( 0, 1, (0, 100)),
( 1, 0, (0, 100)),
( 1, 1, (0, 100)),
( 5, 5, (0, 100)),
( 0, 'all', (np.nan, np.nan)),
('all', 0, (np.nan, np.nan))]
# Test non floating point data as wll
data = np.linspace(0, 100, 100, dtype=np.uint32)
assert imagewrap.naninfrange(data) == (0, 100)
for numinf, numnan, expected in tests:
data = np.linspace(0, 100, 100)
if numinf == 'all': data[:] = np.inf
elif numnan == 'all': data[:] = np.nan
nanoff = 1
if numinf != 'all':
for i in range(1, numinf + 1):
data[i] = np.inf
nanoff += numinf
if numnan != 'all':
for i in range(nanoff, numnan + nanoff):
data[i] = np.nan
result = imagewrap.naninfrange(data)
if np.isfinite(expected[0]): assert result[0] == expected[0]
elif np.isnan( expected[0]): assert np.isnan(result[0])
elif np.isinf( expected[0]): assert np.isinf(result[0])
if np.isfinite(expected[1]): assert result[1] == expected[1]
elif np.isnan( expected[1]): assert np.isnan(result[1])
elif np.isinf( expected[1]): assert np.isinf(result[1])
def test_adjustCoverage(): def test_adjustCoverage():
# TODO Randomise # TODO Randomise
......
#!/usr/bin/env python
#
# test_mesh.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import os.path as op
import shutil
import tempfile
import numpy as np
import pytest
import fsl.data.mesh as fslmesh
datadir = op.join(op.dirname(__file__), 'testdata')
def test_create_mesh():
# Test:
# - create from file
# - create from inmem data
testbase = 'test_mesh.vtk'
testfile = op.join(datadir, testbase)
verts, lens, indices = fslmesh.loadVTKPolydataFile(testfile)
mesh1 = fslmesh.TriangleMesh(testfile)
mesh2 = fslmesh.TriangleMesh(verts, indices)
assert mesh1.name == testbase
assert mesh2.name == 'TriangleMesh'
assert mesh1.dataSource == testfile
assert mesh2.dataSource is None
assert mesh1.vertices.shape == (642, 3)
assert mesh2.vertices.shape == (642, 3)
assert mesh1.indices.shape == (1280, 3)
assert mesh2.indices.shape == (1280, 3)
minbounds = np.array([ 59.50759888, 88.43039703, 72.10890198])
maxbounds = np.array([ 77.72619629, 128.40600586, 94.82050323])
mesh1Bounds = mesh1.getBounds()
mesh2Bounds = mesh2.getBounds()
assert np.all(np.isclose(mesh1Bounds[0], minbounds))
assert np.all(np.isclose(mesh1Bounds[1], maxbounds))
assert np.all(np.isclose(mesh2Bounds[0], minbounds))
assert np.all(np.isclose(mesh2Bounds[1], maxbounds))
def test_mesh_loadVertexData():
meshfile = op.join(datadir, 'test_mesh.vtk')
datafile = op.join(datadir, 'test_mesh_data.txt')
memdata = np.random.randint(1, 100, 642)
mesh = fslmesh.TriangleMesh(meshfile)
assert mesh.loadVertexData(datafile).shape == (642,)
assert np.all(mesh.loadVertexData('inmemdata', memdata) == memdata)
assert mesh.getVertexData(datafile).shape == (642,)
assert np.all(mesh.getVertexData('inmemdata') == memdata)
mesh.clearVertexData()
assert mesh.getVertexData(datafile).shape == (642,)
assert np.all(mesh.loadVertexData('inmemdata', memdata) == memdata)
def test_loadVTKPolydataFile():
testfile = op.join(datadir, 'test_mesh.vtk')
verts, lens, indices = fslmesh.loadVTKPolydataFile(testfile)
assert verts.shape == (642, 3)
assert indices.shape == (3840, )
assert lens.shape == (1280, )
assert np.all(lens == 3)
def test_getFIRSTPrefix():
failures = [
'blob.txt',
'blob.vtk',
'blob.nii.gz']
passes = [
('blurgh-L_Thal_first.vtk', 'blurgh'),
('blurgh-L_Accu_first.vtk', 'blurgh'),
('blurgh_bufuu-R_Hipp_first.vtk', 'blurgh_bufuu'),
]
for f in failures:
with pytest.raises(ValueError):
fslmesh.getFIRSTPrefix(f)
for fname, expected in passes:
assert fslmesh.getFIRSTPrefix(fname) == expected
def test_findReferenceImage():
testdir = tempfile.mkdtemp()
vtkfiles = ['struc-L_Thal_first.vtk',
'struc_first-L_Thal_first.vtk']
assert fslmesh.findReferenceImage('nofile') is None
try:
for fname in vtkfiles:
assert fslmesh.findReferenceImage(fname) is None
prefix = fslmesh.getFIRSTPrefix(fname)
imgfname = op.join(testdir, '{}.nii.gz'.format(prefix))
fname = op.join(testdir, fname)
with open(fname, 'wt') as f: f.write(fname)
with open(imgfname, 'wt') as f: f.write(imgfname)
assert fslmesh.findReferenceImage(fname) == imgfname
finally:
shutil.rmtree(testdir)
This diff is collapsed.
26
14
39
12
31
60
82
98
62
44
4
92
73
36
15
30
64
54
86
98
64
80
8
85
47
63
63
47
19
96
62
32
23
76
96
72
68
69
87
46
43
84
88
70
44
85
57
78
18
6
70
16
36
25
91
81
94
56
4
48
41
88
99
66
69
97
32
57
61
77
75
30
15
57
20
15
59
4
61
17
80
27
92
67
39
79
56
96
34
24
68
2
9
27
35
59
28
3
55
3
84
14
15
78
53
99
18
52
60
60
92
46
9
19
81
59
58
93
17
20
77
33
87
17
83
34
26
9
90
37
52
10
46
45
27
84
25
63
30
78
55
10
46
3
58
35
49
42
36
60
36
22
78
97
5
51
38
95
93
89
57
51
93
61
14
80
74
54
40
44
5
21
41
63
80
19
11
57
35
39
48
11
66
17
80
82
24
22
4
29
34
32
32
5
68
43
42
17
88
56
61
59
85
91
27
5
44
66
25
88
61
10
57
65
21
10
61
62
40
42
21
62
16
6
37
21
38
37
60
29
23
91
60
81
60
81
91
38
55
1
61
85
18
22
86
35
29
79
29
34
79
91
1
73
30
12
74
8
32
7
8
7
90
75
47
52
39
17
44
18
67
72
83
83
85
40
22
53
39
86
9
96
49
80
96
80
72
9
15
28
14
37
23
94
79
94
90
69
84
15
60
42
39
45
91
63
53
29
5
87
20
54
89
45
57
15
20
21
42
2
59
19
91
51
95
55
20
53
64
3
13
88
19
92
62
92
48
92
78
40
62
9
15
75
17
77
19
38
31
3
51
24
36
93
15
58
61
9
2
16
71
10
20
48
43
67
81
94
42
30
92
75
30
97
13
64
56
98
29
89
83
57
96
85
10
76
27
51
87
84
91
41
76
96
5
85
57
74
94
8
7
80
24
25
63
27
43
4
56
94
10
79
72
84
12
41
61
34
40
44
94
21
16
82
89
57
18
73
22
51
80
75
31
5
5
59
77
48
47
80
2
2
52
90
51
69
71
68
68
57
34
96
11
10
88
81
79
22
18
82
27
39
79
91
41
86
27
64
20
53
76
67
53
32
89
43
98
76
62
65
41
37
69
44
59
14
84
54
94
55
5
73
12
5
4
99
47
65
73
42
40
58
94
71
36
69
46
20
88
2
3
98
31
34
41
67
2
55
41
73
4
11
53
82
92
98
20
60
56
54
32
78
93
85
59
20
24
96
56
8
53
26
9
29
70
42
10
69
88
25
15
2
21
41
65
53
32
79
36
74
91
29
88
75
86
57
33
41
94
50
96
5
31
71
13
75
5
98
32
38
46
59
77
61
51
25
82
72
42
50
89
90
70
98
45
16
64
73
65
8
87
72
24
80
93
11
37
4
56
77
26
42
92
37
55
81
1
64
97
60
51
82
57
39
18
72
19
85
95
77
80
84
87
78
83
87
47
47
66
62
30
57
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