88import tempfile
99
1010from 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)
1617tmp_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-
3120def 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