Skip to content

Commit be122ae

Browse files
committed
Move test to correct module.
1 parent dbac2f3 commit be122ae

3 files changed

Lines changed: 77 additions & 170 deletions

File tree

scilpy/tests/checks.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,37 @@
33
import numpy as np
44

55

6+
def _nd_array_match(_arr1, _arr2, _rtol=1E-05, _atol=1E-8):
7+
return np.allclose(_arr1, _arr2, rtol=_rtol, atol=_atol)
8+
9+
10+
def _mse_metrics(_arr1, _arr2):
11+
_mse = (_arr1 - _arr2) ** 2.
12+
return np.mean(_mse), np.max(_mse)
13+
14+
615
def assert_images_close(img1, img2):
716
dtype = img1.header.get_data_dtype()
817

918
assert np.allclose(img1.affine, img2.affine), "Images affines don't match"
1019

11-
assert np.allclose(
12-
img1.get_fdata(dtype=dtype), img2.get_fdata(dtype=dtype)), \
20+
assert _nd_array_match(img1.get_fdata(dtype=dtype),
21+
img2.get_fdata(dtype=dtype)), \
1322
"Images data don't match. MSE : {} | max SE : {}".format(
14-
np.mean((img1.get_fdata(dtype=dtype) -
15-
img2.get_fdata(dtype=dtype)) ** 2.),
16-
np.max((img1.get_fdata(dtype=dtype) -
17-
img2.get_fdata(dtype=dtype)) ** 2.))
23+
*_mse_metrics(img1.get_fdata(dtype=dtype),
24+
img2.get_fdata(dtype=dtype)))
25+
26+
27+
28+
def assert_images_not_close(img1, img2, affine_must_match=True):
29+
dtype = img1.header.get_data_dtype()
30+
31+
if affine_must_match:
32+
assert np.allclose(img1.affine, img2.affine), \
33+
"Images affines don't match"
34+
35+
assert not _nd_array_match(img1.get_fdata(dtype=dtype),
36+
img2.get_fdata(dtype=dtype)), \
37+
"Images data should not match. MSE : {} | max SE : {}".format(
38+
*_mse_metrics(img1.get_fdata(dtype=dtype),
39+
img2.get_fdata(dtype=dtype)))

scripts/tests/test_execute_angle_aware_bilateral_filtering.py

Lines changed: 0 additions & 116 deletions
This file was deleted.

scripts/tests/test_sh_to_aodf.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import tempfile
99

1010
from scilpy.io.fetcher import get_testing_files_dict, fetch_data, get_home
11+
from scilpy.tests.checks import assert_images_close, assert_images_not_close
1112

1213

1314
# If they already exist, this only takes 5 seconds (check md5sum)
@@ -16,53 +17,50 @@
1617
tmp_dir = tempfile.TemporaryDirectory()
1718

1819

19-
@pytest.fixture
20-
def mock_filtering(mocker, out_fodf):
21-
def _mock(*args, **kwargs):
22-
img = nib.load(out_fodf)
23-
return img.get_fdata().astype(np.float32)
24-
25-
script = 'scil_sh_to_aodf'
26-
filtering_fn = "angle_aware_bilateral_filtering"
27-
return mocker.patch("scripts.{}.{}".format(script, filtering_fn),
28-
side_effect=_mock, create=True)
29-
30-
3120
def test_help_option(script_runner):
3221
ret = script_runner.run('scil_sh_to_aodf.py', '--help')
3322
assert ret.success
3423

3524

36-
@pytest.mark.parametrize("in_fodf,out_fodf",
25+
@pytest.mark.parametrize("in_fodf,expected_results",
3726
[[os.path.join(data_path, 'fodf_descoteaux07_sub.nii.gz'),
38-
os.path.join(data_path, 'fodf_descoteaux07_sub_full.nii.gz')]])
39-
def test_asym_basis_output(script_runner, mock_filtering, in_fodf, out_fodf):
27+
os.path.join(data_path, 'fodf_descoteaux07_sub_full.nii.gz')]],
28+
scope='function')
29+
def test_asym_basis_output(script_runner, in_fodf, expected_results,
30+
mock_collector):
31+
4032
os.chdir(os.path.expanduser(tmp_dir.name))
33+
_mocks = mock_collector(["bilateral_filtering"], "scripts.scil_sh_to_aodf")
4134

4235
ret = script_runner.run('scil_sh_to_aodf.py',
4336
in_fodf, 'out_fodf1.nii.gz',
4437
'--sphere', 'repulsion100',
4538
'--sigma_angular', '1.0',
4639
'--sigma_spatial', '1.0',
4740
'--sigma_range', '1.0',
48-
'--sh_basis', 'descoteaux07', '-f',
41+
'--sh_basis', 'descoteaux07',
42+
'--processes', '1', '-f',
4943
print_result=True, shell=True)
5044

5145
assert ret.success
52-
mock_filtering.assert_called_once()
5346

54-
ret_fodf = nib.load("out_fodf1.nii.gz")
55-
test_fodf = nib.load(out_fodf)
56-
assert np.allclose(ret_fodf.get_fdata(), test_fodf.get_fdata())
47+
if _mocks["bilateral_filtering"]:
48+
_mocks["bilateral_filtering"].assert_called_once()
49+
50+
assert_images_close(nib.load(expected_results),
51+
nib.load("out_fodf1.nii.gz"))
5752

5853

59-
@pytest.mark.parametrize("in_fodf,out_fodf,sym_fodf",
54+
@pytest.mark.parametrize("in_fodf,expected_results,sym_fodf",
6055
[[os.path.join(data_path, "fodf_descoteaux07_sub.nii.gz"),
6156
os.path.join(data_path, "fodf_descoteaux07_sub_full.nii.gz"),
62-
os.path.join(data_path, "fodf_descoteaux07_sub_sym.nii.gz")]])
63-
def test_sym_basis_output(
64-
script_runner, mock_filtering, in_fodf, out_fodf, sym_fodf):
57+
os.path.join(data_path, "fodf_descoteaux07_sub_sym.nii.gz")]],
58+
scope='function')
59+
def test_sym_basis_output(script_runner, in_fodf, expected_results, sym_fodf,
60+
mock_collector):
61+
6562
os.chdir(os.path.expanduser(tmp_dir.name))
63+
_mocks = mock_collector(["bilateral_filtering"], "scripts.scil_sh_to_aodf")
6664

6765
ret = script_runner.run('scil_sh_to_aodf.py',
6866
in_fodf,
@@ -72,22 +70,26 @@ def test_sym_basis_output(
7270
'--sigma_angular', '1.0',
7371
'--sigma_spatial', '1.0',
7472
'--sigma_range', '1.0',
75-
'--sh_basis', 'descoteaux07', '-f',
73+
'--sh_basis', 'descoteaux07',
74+
'--processes', '1', '-f',
7675
print_result=True, shell=True)
7776

7877
assert ret.success
79-
mock_filtering.assert_called_once()
8078

81-
ret_sym_fodf = nib.load("out_sym.nii.gz")
82-
test_sym_fodf = nib.load(sym_fodf)
83-
assert np.allclose(ret_sym_fodf.get_fdata(), test_sym_fodf.get_fdata())
79+
if _mocks["bilateral_filtering"]:
80+
_mocks["bilateral_filtering"].assert_called_once()
81+
82+
assert_images_close(nib.load(sym_fodf), nib.load("out_sym.nii.gz"))
8483

8584

86-
@pytest.mark.parametrize("in_fodf,out_fodf",
85+
@pytest.mark.parametrize("in_fodf,expected_results",
8786
[[os.path.join(data_path, "fodf_descoteaux07_sub_full.nii.gz"),
88-
os.path.join(data_path, "fodf_descoteaux07_sub_twice.nii.gz")]])
89-
def test_asym_input(script_runner, mock_filtering, in_fodf, out_fodf):
87+
os.path.join(data_path, "fodf_descoteaux07_sub_twice.nii.gz")]],
88+
scope='function')
89+
def test_asym_input(script_runner, in_fodf, expected_results, mock_collector):
90+
9091
os.chdir(os.path.expanduser(tmp_dir.name))
92+
_mocks = mock_collector(["bilateral_filtering"], "scripts.scil_sh_to_aodf")
9193

9294
ret = script_runner.run('scil_sh_to_aodf.py',
9395
in_fodf,
@@ -96,39 +98,38 @@ def test_asym_input(script_runner, mock_filtering, in_fodf, out_fodf):
9698
'--sigma_angular', '1.0',
9799
'--sigma_spatial', '1.0',
98100
'--sigma_range', '1.0',
99-
'--sh_basis', 'descoteaux07', '-f',
101+
'--sh_basis', 'descoteaux07',
102+
'--processes', '1', '-f',
100103
print_result=True, shell=True)
101104

102105
assert ret.success
103-
mock_filtering.assert_called_once()
104-
105-
ret_fodf = nib.load("out_fodf3.nii.gz")
106-
test_fodf = nib.load(out_fodf)
107-
assert np.allclose(ret_fodf.get_fdata(), test_fodf.get_fdata())
108106

107+
if _mocks["bilateral_filtering"]:
108+
_mocks["bilateral_filtering"].assert_called_once()
109+
110+
assert_images_close(nib.load(expected_results),
111+
nib.load("out_fodf3.nii.gz"))
109112

110-
@pytest.mark.parametrize("in_fodf,out_fodf",
113+
114+
@pytest.mark.parametrize("in_fodf,expected_results",
111115
[[os.path.join(data_path, 'fodf_descoteaux07_sub.nii.gz'),
112116
os.path.join(data_path, 'fodf_descoteaux07_sub_full.nii.gz')]])
113-
def test_cosine_method(script_runner, mock_filtering, in_fodf, out_fodf):
117+
def test_cosine_method(script_runner, in_fodf, expected_results):
118+
114119
os.chdir(os.path.expanduser(tmp_dir.name))
120+
# method cosine is fast and not mocked
115121

116122
ret = script_runner.run('scil_sh_to_aodf.py',
117123
in_fodf, 'out_fodf1.nii.gz',
118124
'--sphere', 'repulsion100',
119125
'--method', 'cosine',
120126
'--sh_basis', 'descoteaux07',
121-
'-f',
127+
'--processes', '1', '-f',
122128
print_result=True, shell=True)
123129

124130
assert ret.success
125131

126-
# method cosine is fast and not mocked
127-
mock_filtering.assert_not_called()
128-
129-
ret_fodf = nib.load("out_fodf1.nii.gz")
130-
test_fodf = nib.load(out_fodf)
131-
132132
# We expect the output to be different from the
133133
# one obtained with angle-aware bilateral filtering
134-
assert not np.allclose(ret_fodf.get_fdata(), test_fodf.get_fdata())
134+
assert_images_not_close(nib.load(expected_results),
135+
nib.load("out_fodf1.nii.gz"))

0 commit comments

Comments
 (0)