Skip to content

Commit 2119ccc

Browse files
authored
Merge pull request #233 from Jammy2211/feature/refactor-regularizations-tests
Refactor regularization tests for granularity and clarity
2 parents c519aac + 45bf52d commit 2119ccc

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

test_autoarray/inversion/regularizations/test_adapt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55

6-
def test__weight_list__matches_util():
6+
def test__regularization_weights_from__3_pixel_signals_inner_10_outer_15__matches_util():
77
reg = aa.reg.Adapt(inner_coefficient=10.0, outer_coefficient=15.0)
88

99
pixel_signals = np.array([0.21, 0.586, 0.45])
@@ -19,7 +19,7 @@ def test__weight_list__matches_util():
1919
assert (weight_list == weight_list_util).all()
2020

2121

22-
def test__regularization_matrix__matches_util():
22+
def test__regularization_matrix_from__9_pixel_grid_inner_1_outer_2__diagonal_value_approximately_18():
2323
reg = aa.reg.Adapt(inner_coefficient=1.0, outer_coefficient=2.0, signal_scale=1.0)
2424

2525
pixel_signals = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])

test_autoarray/inversion/regularizations/test_constant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
np.set_printoptions(threshold=np.inf)
66

77

8-
def test__regularization_matrix():
8+
def test__regularization_matrix_from__6_pixel_mesh_coefficient_2__diagonal_value_approximately_8():
99

1010
reg = aa.reg.Constant(coefficient=2.0)
1111

test_autoarray/inversion/regularizations/test_exponential_kernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
np.set_printoptions(threshold=np.inf)
77

88

9-
def test__regularization_matrix():
9+
def test__regularization_matrix_from__6_pixel_mesh_coefficient_1_scale_2__correct_diagonal_value():
1010
reg = aa.reg.ExponentialKernel(coefficient=1.0, scale=2.0)
1111

1212
source_plane_mesh_grid = aa.Grid2D.no_mask(

test_autoarray/inversion/regularizations/test_gaussian_kernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
np.set_printoptions(threshold=np.inf)
77

88

9-
def test__regularization_matrix():
9+
def test__regularization_matrix_from__6_pixel_mesh_coefficient_1_scale_2__correct_diagonal_value():
1010
reg = aa.reg.GaussianKernel(coefficient=1.0, scale=2.0)
1111

1212
source_plane_mesh_grid = aa.Grid2D.no_mask(

test_autoarray/inversion/regularizations/test_matern_kernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
np.set_printoptions(threshold=np.inf)
77

88

9-
def test__regularization_matrix():
9+
def test__regularization_matrix_from__6_pixel_mesh_coefficient_1_scale_2_nu_2__correct_diagonal_value():
1010

1111
reg = aa.reg.MaternKernel(coefficient=1.0, scale=2.0, nu=2.0)
1212

test_autoarray/inversion/regularizations/test_regularization_util.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66

7-
def test__zeroth_regularization_matrix_from():
7+
def test__zeroth_regularization_matrix_from__coefficient_1_pixels_3__identity_diagonal_matrix():
88
regularization_matrix = aa.util.regularization.zeroth_regularization_matrix_from(
99
coefficient=1.0, pixels=3
1010
)
@@ -15,6 +15,8 @@ def test__zeroth_regularization_matrix_from():
1515
).all()
1616
assert abs(np.linalg.det(regularization_matrix)) > 1e-8
1717

18+
19+
def test__zeroth_regularization_matrix_from__coefficient_2_pixels_2__scaled_identity_matrix():
1820
regularization_matrix = aa.util.regularization.zeroth_regularization_matrix_from(
1921
coefficient=2.0, pixels=2
2022
)
@@ -23,13 +25,7 @@ def test__zeroth_regularization_matrix_from():
2325
assert abs(np.linalg.det(regularization_matrix)) > 1e-8
2426

2527

26-
def test__constant_regularization_matrix_from():
27-
# Here, we define the neighbors first here and make the B matrices based on them.
28-
29-
# You'll notice that actually, the B Matrix doesn't have to have the -1's going down the diagonal and we
30-
# don't have to have as many B matrices as we do the pix pixel with the most vertices. We can combine
31-
# the rows of each B matrix wherever we like ;0.
32-
28+
def test__constant_regularization_matrix_from__3_pixel_chain__matches_b_matrix_computation():
3329
neighbors = np.array([[1, 2, -1], [0, -1, -1], [0, -1, -1]])
3430

3531
neighbors_sizes = np.array([2, 1, 1])
@@ -47,6 +43,8 @@ def test__constant_regularization_matrix_from():
4743
assert (regularization_matrix == test_regularization_matrix).all()
4844
assert abs(np.linalg.det(regularization_matrix)) > 1e-8
4945

46+
47+
def test__constant_regularization_matrix_from__4_pixel_ring_coefficient_1__matches_b_matrix_computation():
5048
b_matrix = np.array([[-1, 1, 0, 0], [0, -1, 1, 0], [0, 0, -1, 1], [1, 0, 0, -1]])
5149

5250
test_regularization_matrix = np.matmul(b_matrix.T, b_matrix) + 1e-8 * np.identity(4)
@@ -64,6 +62,8 @@ def test__constant_regularization_matrix_from():
6462
assert (regularization_matrix == test_regularization_matrix).all()
6563
assert abs(np.linalg.det(regularization_matrix)) > 1e-8
6664

65+
66+
def test__constant_regularization_matrix_from__4_pixel_ring_coefficient_2__scaled_regularization_matrix():
6767
neighbors = np.array(
6868
[[1, 3, -1, -1], [0, 2, -1, -1], [1, 3, -1, -1], [0, 2, -1, -1]]
6969
)
@@ -83,6 +83,8 @@ def test__constant_regularization_matrix_from():
8383
assert (regularization_matrix == test_regularization_matrix).all()
8484
assert abs(np.linalg.det(regularization_matrix)) > 1e-8
8585

86+
87+
def test__constant_regularization_matrix_from__9_pixel_grid__matches_b_matrix_computation():
8688
neighbors = np.array(
8789
[
8890
[1, 3, -1, -1],
@@ -164,7 +166,7 @@ def test__constant_zeroth_regularization_matrix_from():
164166
assert abs(np.linalg.det(regularization_matrix)) > 1e-8
165167

166168

167-
def test__adapt_regularization_weights_from():
169+
def test__adapt_regularization_weights_from__uniform_signals_equal_inner_outer__all_weights_one():
168170
pixel_signals = np.array([1.0, 1.0, 1.0])
169171

170172
weight_list = aa.util.regularization.adapt_regularization_weights_from(
@@ -173,6 +175,8 @@ def test__adapt_regularization_weights_from():
173175

174176
assert (weight_list == np.array([1.0, 1.0, 1.0])).all()
175177

178+
179+
def test__adapt_regularization_weights_from__non_uniform_signals_equal_inner_outer__all_weights_one():
176180
pixel_signals = np.array([0.25, 0.5, 0.75])
177181

178182
weight_list = aa.util.regularization.adapt_regularization_weights_from(
@@ -181,6 +185,8 @@ def test__adapt_regularization_weights_from():
181185

182186
assert (weight_list == np.array([1.0, 1.0, 1.0])).all()
183187

188+
189+
def test__adapt_regularization_weights_from__non_uniform_signals_outer_zero__weights_scale_with_signal():
184190
pixel_signals = np.array([0.25, 0.5, 0.75])
185191

186192
weight_list = aa.util.regularization.adapt_regularization_weights_from(
@@ -189,6 +195,8 @@ def test__adapt_regularization_weights_from():
189195

190196
assert (weight_list == np.array([0.25**2.0, 0.5**2.0, 0.75**2.0])).all()
191197

198+
199+
def test__adapt_regularization_weights_from__non_uniform_signals_inner_zero__weights_scale_inversely():
192200
pixel_signals = np.array([0.25, 0.5, 0.75])
193201

194202
weight_list = aa.util.regularization.adapt_regularization_weights_from(
@@ -198,7 +206,7 @@ def test__adapt_regularization_weights_from():
198206
assert (weight_list == np.array([0.75**2.0, 0.5**2.0, 0.25**2.0])).all()
199207

200208

201-
def test__brightness_zeroth_regularization_weights_from():
209+
def test__brightness_zeroth_regularization_weights_from__uniform_max_signals__all_zero_weights():
202210
pixel_signals = np.array([1.0, 1.0, 1.0])
203211

204212
weight_list = aa.util.regularization.brightness_zeroth_regularization_weights_from(
@@ -207,6 +215,8 @@ def test__brightness_zeroth_regularization_weights_from():
207215

208216
assert (weight_list == np.array([0.0, 0.0, 0.0])).all()
209217

218+
219+
def test__brightness_zeroth_regularization_weights_from__non_uniform_signals_coefficient_1__complement_weights():
210220
pixel_signals = np.array([0.25, 0.5, 0.75])
211221

212222
weight_list = aa.util.regularization.brightness_zeroth_regularization_weights_from(
@@ -215,6 +225,8 @@ def test__brightness_zeroth_regularization_weights_from():
215225

216226
assert (weight_list == np.array([0.75, 0.5, 0.25])).all()
217227

228+
229+
def test__brightness_zeroth_regularization_weights_from__non_uniform_signals_coefficient_2__scaled_complement_weights():
218230
pixel_signals = np.array([0.25, 0.5, 0.75])
219231

220232
weight_list = aa.util.regularization.brightness_zeroth_regularization_weights_from(
@@ -224,7 +236,7 @@ def test__brightness_zeroth_regularization_weights_from():
224236
assert (weight_list == np.array([1.5, 1.0, 0.5])).all()
225237

226238

227-
def test__weighted_regularization_matrix_from():
239+
def test__weighted_regularization_matrix_from__4_pixel_cycle_uniform_weights__matches_b_matrix():
228240
neighbors = np.array([[2], [3], [0], [1]])
229241

230242
b_matrix = np.array([[-1, 0, 1, 0], [0, -1, 0, 1], [1, 0, -1, 0], [0, 1, 0, -1]])
@@ -240,6 +252,8 @@ def test__weighted_regularization_matrix_from():
240252

241253
assert regularization_matrix == pytest.approx(test_regularization_matrix, 1.0e-4)
242254

255+
256+
def test__weighted_regularization_matrix_from__3_pixel_chain_uniform_weights__matches_b_matrix():
243257
# Here, we define the neighbors first here and make the B matrices based on them.
244258

245259
# You'll notice that actually, the B Matrix doesn't have to have the -1's going down the diagonal and we
@@ -271,6 +285,8 @@ def test__weighted_regularization_matrix_from():
271285

272286
assert regularization_matrix == pytest.approx(test_regularization_matrix, 1.0e-4)
273287

288+
289+
def test__weighted_regularization_matrix_from__4_pixel_ring_uniform_weights__matches_combined_b_matrix():
274290
b_matrix_1 = np.array([[-1, 1, 0, 0], [0, -1, 1, 0], [0, 0, -1, 1], [1, 0, 0, -1]])
275291

276292
test_regularization_matrix_1 = np.matmul(b_matrix_1.T, b_matrix_1)
@@ -294,6 +310,8 @@ def test__weighted_regularization_matrix_from():
294310

295311
assert regularization_matrix == pytest.approx(test_regularization_matrix, 1.0e-4)
296312

313+
314+
def test__weighted_regularization_matrix_from__6_pixel_graph_uniform_weights__matches_b_matrix():
297315
# Again, lets exploit the freedom we have when setting up our B matrices to make matching it to pairs a
298316
# lot less Stressful.
299317

@@ -378,6 +396,8 @@ def test__weighted_regularization_matrix_from():
378396

379397
assert regularization_matrix == pytest.approx(test_regularization_matrix, 1.0e-4)
380398

399+
400+
def test__weighted_regularization_matrix_from__4_pixel_non_uniform_weights__matches_weighted_b_matrix():
381401
# Simple case, where we have just one regularization direction, regularizing pixel 0 -> 1 and 1 -> 2.
382402

383403
# This means our B matrix is:
@@ -412,6 +432,8 @@ def test__weighted_regularization_matrix_from():
412432

413433
assert regularization_matrix == pytest.approx(test_regularization_matrix, 1.0e-4)
414434

435+
436+
def test__weighted_regularization_matrix_from__6_pixel_non_uniform_weights__matches_weighted_b_matrix():
415437
neighbors = np.array(
416438
[
417439
[1, 4, -1, -1],
@@ -494,7 +516,7 @@ def test__weighted_regularization_matrix_from():
494516
assert regularization_matrix == pytest.approx(test_regularization_matrix, 1.0e-4)
495517

496518

497-
def test__brightness_zeroth_regularization_matrix_from():
519+
def test__brightness_zeroth_regularization_matrix_from__uniform_weights__identity_diagonal_matrix():
498520
regularization_weights = np.ones(shape=(3,))
499521

500522
regularization_matrix = (
@@ -507,6 +529,8 @@ def test__brightness_zeroth_regularization_matrix_from():
507529
np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]), 1.0e-4
508530
)
509531

532+
533+
def test__brightness_zeroth_regularization_matrix_from__non_uniform_weights__squared_weights_on_diagonal():
510534
regularization_weights = np.array([1.0, 2.0, 3.0])
511535

512536
regularization_matrix = (
@@ -578,7 +602,7 @@ def splitted_data():
578602
return splitted_mappings, splitted_sizes, splitted_weights
579603

580604

581-
def test__reg_split_from(splitted_data):
605+
def test__reg_split_from__splitted_mapping_data__produces_correct_mappings_sizes_and_weights(splitted_data):
582606

583607
splitted_mappings, splitted_sizes, splitted_weights = splitted_data
584608

@@ -649,7 +673,7 @@ def test__reg_split_from(splitted_data):
649673
assert splitted_weights == pytest.approx(expected_weights, abs=1.0e-4)
650674

651675

652-
def test__constant_pixel_splitted_regularization_matrix(splitted_data):
676+
def test__pixel_splitted_regularization_matrix_from__uniform_weights__correct_regularization_matrix(splitted_data):
653677

654678
splitted_mappings, splitted_sizes, splitted_weights = splitted_data
655679

@@ -676,6 +700,11 @@ def test__constant_pixel_splitted_regularization_matrix(splitted_data):
676700

677701
assert pytest.approx(regularization_matrix, 1e-4) == np.array(expected_reg_matrix)
678702

703+
704+
def test__pixel_splitted_regularization_matrix_from__non_uniform_weights__correct_regularization_matrix(splitted_data):
705+
706+
splitted_mappings, splitted_sizes, splitted_weights = splitted_data
707+
679708
regularization_weights = np.array([2.0, 4.0, 2.0, 2.0, 2.0])
680709

681710
regularization_matrix = (

0 commit comments

Comments
 (0)