Skip to content

Commit d11b7a6

Browse files
Jammy2211claude
authored andcommitted
refactor inversion and mapper tests for granularity and clarity
Split monolithic tests into focused single-assertion tests with descriptive names encoding condition and expected result. Add @pytest.mark.parametrize for matrix property scenarios. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cbd68f0 commit d11b7a6

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

test_autoarray/inversion/inversion/test_abstract.py

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
directory = path.dirname(path.realpath(__file__))
88

99

10-
def test__has():
10+
def test__has__linear_obj_with_regularization__returns_true():
1111
reg = aa.m.MockRegularization()
1212
linear_obj = aa.m.MockLinearObj(regularization=reg)
1313
inversion = aa.m.MockInversion(linear_obj_list=[linear_obj])
1414

1515
assert inversion.has(cls=aa.AbstractRegularization) is True
1616

17+
18+
def test__has__linear_obj_without_regularization__returns_false():
1719
linear_obj = aa.m.MockLinearObj(regularization=None)
1820
inversion = aa.m.MockInversion(linear_obj_list=[linear_obj])
1921

2022
assert inversion.has(cls=aa.AbstractRegularization) is False
2123

2224

23-
def test__total_regularizations():
25+
def test__total_regularizations__one_regularized_one_unregularized__returns_one():
2426
reg = aa.m.MockRegularization()
2527

2628
linear_obj_0 = aa.m.MockLinearObj(regularization=reg)
@@ -30,16 +32,26 @@ def test__total_regularizations():
3032

3133
assert inversion.total_regularizations == 1
3234

35+
36+
def test__total_regularizations__both_regularized__returns_two():
37+
reg = aa.m.MockRegularization()
38+
39+
linear_obj_0 = aa.m.MockLinearObj(regularization=reg)
40+
3341
inversion = aa.m.MockInversion(linear_obj_list=[linear_obj_0, linear_obj_0])
3442

3543
assert inversion.total_regularizations == 2
3644

45+
46+
def test__total_regularizations__none_regularized__returns_zero():
47+
linear_obj_1 = aa.m.MockLinearObj(regularization=None)
48+
3749
inversion = aa.m.MockInversion(linear_obj_list=[linear_obj_1, linear_obj_1])
3850

3951
assert inversion.total_regularizations == 0
4052

4153

42-
def test__index_range_list_from():
54+
def test__param_range_list_from__linear_obj_and_mapper__correct_ranges_per_class():
4355
inversion = aa.m.MockInversion(
4456
linear_obj_list=[
4557
aa.m.MockLinearObj(parameters=2, regularization=None),
@@ -51,7 +63,7 @@ def test__index_range_list_from():
5163
assert inversion.param_range_list_from(cls=aa.Mapper) == [[2, 3]]
5264

5365

54-
def test__no_regularization_index_list():
66+
def test__no_regularization_index_list__all_unregularized__returns_all_parameter_indices():
5567
inversion = aa.m.MockInversion(
5668
linear_obj_list=[
5769
aa.m.MockLinearObj(parameters=2, regularization=None),
@@ -61,6 +73,8 @@ def test__no_regularization_index_list():
6173

6274
assert inversion.no_regularization_index_list == [0, 1, 2]
6375

76+
77+
def test__no_regularization_index_list__mixed_regularized_and_unregularized__returns_only_unregularized_indices():
6478
inversion = aa.m.MockInversion(
6579
linear_obj_list=[
6680
aa.m.MockMapper(parameters=10, regularization=aa.m.MockRegularization()),
@@ -73,7 +87,7 @@ def test__no_regularization_index_list():
7387
assert inversion.no_regularization_index_list == [10, 11, 12, 33, 34, 35, 36]
7488

7589

76-
def test__mapping_matrix():
90+
def test__mapping_matrix__two_mappers__concatenates_mapping_matrices_horizontally():
7791
mapper_0 = aa.m.MockMapper(mapping_matrix=np.ones((2, 2)))
7892
mapper_1 = aa.m.MockMapper(mapping_matrix=2.0 * np.ones((2, 3)))
7993

@@ -215,7 +229,7 @@ def test__curvature_matrix_via_sparse_operator__includes_source_interpolation__i
215229
)
216230

217231

218-
def test__curvature_reg_matrix_reduced():
232+
def test__curvature_reg_matrix_reduced__regularized_and_unregularized__removes_unregularized_rows_cols():
219233
curvature_reg_matrix = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
220234

221235
linear_obj_list = [
@@ -232,7 +246,7 @@ def test__curvature_reg_matrix_reduced():
232246
).all()
233247

234248

235-
def test__regularization_matrix():
249+
def test__regularization_matrix__two_regularized_mappers__assembles_block_diagonal_matrix():
236250
reg_0 = aa.m.MockRegularization(regularization_matrix=np.ones((2, 2)))
237251
reg_1 = aa.m.MockRegularization(regularization_matrix=2.0 * np.ones((3, 3)))
238252

@@ -256,7 +270,7 @@ def test__regularization_matrix():
256270
assert inversion.regularization_matrix == pytest.approx(regularization_matrix)
257271

258272

259-
def test__reconstruction_reduced():
273+
def test__reconstruction_reduced__regularized_and_unregularized__returns_only_regularized_parameters():
260274
linear_obj_list = [
261275
aa.m.MockMapper(parameters=2, regularization=aa.m.MockRegularization()),
262276
aa.m.MockLinearObj(parameters=1, regularization=None),
@@ -269,7 +283,7 @@ def test__reconstruction_reduced():
269283
assert (inversion.reconstruction_reduced == np.array([1.0, 2.0])).all()
270284

271285

272-
def test__reconstruction_dict():
286+
def test__reconstruction_dict__single_linear_obj_and_mapper__splits_reconstruction_correctly():
273287
reconstruction = np.array([0.0, 1.0, 1.0, 1.0])
274288

275289
linear_obj = aa.m.MockLinearObj(parameters=1)
@@ -282,6 +296,8 @@ def test__reconstruction_dict():
282296
assert (inversion.reconstruction_dict[linear_obj] == np.zeros(1)).all()
283297
assert (inversion.reconstruction_dict[mapper] == np.ones(3)).all()
284298

299+
300+
def test__reconstruction_dict__multiple_linear_objs_and_mappers__splits_reconstruction_correctly():
285301
reconstruction = np.array([0.0, 1.0, 1.0, 2.0, 2.0, 2.0])
286302

287303
linear_obj = aa.m.MockLinearObj(parameters=1)
@@ -297,7 +313,7 @@ def test__reconstruction_dict():
297313
assert (inversion.reconstruction_dict[mapper_1] == 2.0 * np.ones(3)).all()
298314

299315

300-
def test__mapped_reconstructed_data_dict():
316+
def test__mapped_reconstructed_data_dict__single_linear_obj__returns_correct_data_and_sum():
301317
linear_obj_0 = aa.m.MockLinearObj()
302318

303319
mapped_reconstructed_data_dict = {linear_obj_0: np.ones(3)}
@@ -312,6 +328,9 @@ def test__mapped_reconstructed_data_dict():
312328
assert (inversion.mapped_reconstructed_data_dict[linear_obj_0] == np.ones(3)).all()
313329
assert (inversion.mapped_reconstructed_data == np.ones(3)).all()
314330

331+
332+
def test__mapped_reconstructed_data_dict__two_linear_objs__sums_contributions_correctly():
333+
linear_obj_0 = aa.m.MockLinearObj()
315334
linear_obj_1 = aa.m.MockLinearObj()
316335

317336
mapped_reconstructed_data_dict = {
@@ -333,7 +352,7 @@ def test__mapped_reconstructed_data_dict():
333352
assert (inversion.mapped_reconstructed_data == 3.0 * np.ones(2)).all()
334353

335354

336-
def test__mapped_reconstructed_operated_data_dict():
355+
def test__mapped_reconstructed_operated_data_dict__single_linear_obj__returns_correct_data_and_sum():
337356
linear_obj_0 = aa.m.MockLinearObj()
338357

339358
mapped_reconstructed_operated_data_dict = {linear_obj_0: np.ones(3)}
@@ -350,6 +369,9 @@ def test__mapped_reconstructed_operated_data_dict():
350369
).all()
351370
assert (inversion.mapped_reconstructed_operated_data == np.ones(3)).all()
352371

372+
373+
def test__mapped_reconstructed_operated_data_dict__two_linear_objs__sums_contributions_correctly():
374+
linear_obj_0 = aa.m.MockLinearObj()
353375
linear_obj_1 = aa.m.MockLinearObj()
354376

355377
mapped_reconstructed_operated_data_dict = {
@@ -374,7 +396,7 @@ def test__mapped_reconstructed_operated_data_dict():
374396
assert (inversion.mapped_reconstructed_operated_data == 3.0 * np.ones(2)).all()
375397

376398

377-
def test__mapped_reconstructed_operated_data():
399+
def test__mapped_reconstructed_operated_data__single_linear_obj__returns_correct_operated_data():
378400
linear_obj_0 = aa.m.MockLinearObj()
379401

380402
mapped_reconstructed_operated_data_dict = {linear_obj_0: np.ones(3)}
@@ -391,6 +413,9 @@ def test__mapped_reconstructed_operated_data():
391413
).all()
392414
assert (inversion.mapped_reconstructed_operated_data == np.ones(3)).all()
393415

416+
417+
def test__mapped_reconstructed_operated_data__two_linear_objs__sums_operated_data_correctly():
418+
linear_obj_0 = aa.m.MockLinearObj()
394419
linear_obj_1 = aa.m.MockLinearObj()
395420

396421
mapped_reconstructed_operated_data_dict = {
@@ -415,7 +440,7 @@ def test__mapped_reconstructed_operated_data():
415440
assert (inversion.mapped_reconstructed_operated_data == 3.0 * np.ones(2)).all()
416441

417442

418-
def test__data_subtracted_dict():
443+
def test__data_subtracted_dict__single_linear_obj__subtracts_other_contributions_from_data():
419444
linear_obj_0 = aa.m.MockLinearObj()
420445

421446
mapped_reconstructed_operated_data_dict = {linear_obj_0: np.ones(3)}
@@ -429,6 +454,9 @@ def test__data_subtracted_dict():
429454

430455
assert (inversion.data_subtracted_dict[linear_obj_0] == 3.0 * np.ones(3)).all()
431456

457+
458+
def test__data_subtracted_dict__two_linear_objs__subtracts_other_contributions_from_data():
459+
linear_obj_0 = aa.m.MockLinearObj()
432460
linear_obj_1 = aa.m.MockLinearObj()
433461

434462
mapped_reconstructed_operated_data_dict = {
@@ -447,7 +475,7 @@ def test__data_subtracted_dict():
447475
assert (inversion.data_subtracted_dict[linear_obj_1] == 2.0 * np.ones(3)).all()
448476

449477

450-
def test__regularization_term():
478+
def test__regularization_term__identity_matrix__computes_sum_of_squared_reconstruction():
451479
reconstruction = np.array([1.0, 1.0, 1.0])
452480

453481
regularization_matrix = np.array(
@@ -478,6 +506,8 @@ def test__regularization_term():
478506

479507
assert inversion.regularization_term == 3.0
480508

509+
510+
def test__regularization_term__tridiagonal_matrix__computes_weighted_regularization_term():
481511
reconstruction = np.array([2.0, 3.0, 5.0])
482512

483513
regularization_matrix = np.array(
@@ -509,7 +539,7 @@ def test__regularization_term():
509539
assert inversion.regularization_term == 34.0
510540

511541

512-
def test__determinant_of_positive_definite_matrix_via_cholesky():
542+
def test__determinant_of_positive_definite_matrix_via_cholesky__identity_matrix__matches_numpy_log_det():
513543
matrix = np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
514544

515545
inversion = aa.m.MockInversion(
@@ -523,6 +553,8 @@ def test__determinant_of_positive_definite_matrix_via_cholesky():
523553
inversion.log_det_curvature_reg_matrix_term, 1e-4
524554
)
525555

556+
557+
def test__determinant_of_positive_definite_matrix_via_cholesky__tridiagonal_matrix__matches_numpy_log_det():
526558
matrix = np.array([[2.0, -1.0, 0.0], [-1.0, 2.0, -1.0], [0.0, -1.0, 2.0]])
527559

528560
inversion = aa.m.MockInversion(
@@ -537,7 +569,7 @@ def test__determinant_of_positive_definite_matrix_via_cholesky():
537569
)
538570

539571

540-
def test__reconstruction_noise_map():
572+
def test__reconstruction_noise_map__asymmetric_curvature_reg_matrix__correct_diagonal_noise_values():
541573
curvature_reg_matrix = np.array([[1.0, 1.0, 1.0], [1.0, 2.0, 1.0], [1.0, 1.0, 3.0]])
542574

543575
inversion = aa.m.MockInversion(curvature_reg_matrix=curvature_reg_matrix)
@@ -550,7 +582,7 @@ def test__reconstruction_noise_map():
550582
)
551583

552584

553-
def test__max_pixel_list_from_and_centre():
585+
def test__max_pixel_list_from_and_centre__returns_top_pixels_and_brightest_centre():
554586

555587
source_plane_mesh_grid = aa.Grid2DIrregular(
556588
[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [5.0, 0.0]]
@@ -581,7 +613,7 @@ def test__max_pixel_list_from_and_centre():
581613
assert inversion.max_pixel_centre().in_list == [(5.0, 6.0)]
582614

583615

584-
def test__max_pixel_list_from__filter_neighbors():
616+
def test__max_pixel_list_from__filter_neighbors__excludes_adjacent_pixels_from_top_list():
585617
source_plane_mesh_grid = aa.Grid2DIrregular(
586618
[
587619
[1.0, 1.0],

test_autoarray/inversion/pixelization/mappers/test_abstract.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import autoarray as aa
55

66

7-
def test__pix_indexes_for_slim_indexes__different_types_of_lists_input():
7+
def test__pix_indexes_for_slim_indexes__rectangular_mapper__correct_pixel_index_mapping():
88
mapper = aa.m.MockMapper(
99
interpolator=aa.m.MockInterpolator(
1010
mappings=np.array([[0], [0], [0], [0], [0], [0], [0], [0]]),
@@ -20,6 +20,8 @@ def test__pix_indexes_for_slim_indexes__different_types_of_lists_input():
2020

2121
assert pixe_indexes_for_slim_indexes == [0, 1, 2, 3, 4, 5, 6, 7]
2222

23+
24+
def test__pix_indexes_for_slim_indexes__delaunay_mapper__correct_pixel_index_mapping():
2325
mapper = aa.m.MockMapper(
2426
interpolator=aa.m.MockInterpolator(
2527
mappings=np.array([[0], [0], [0], [0], [3], [4], [4], [7]]),
@@ -36,7 +38,7 @@ def test__pix_indexes_for_slim_indexes__different_types_of_lists_input():
3638
assert pixe_indexes_for_slim_indexes == [[0, 1, 2, 3], [5, 6]]
3739

3840

39-
def test__sub_slim_indexes_for_pix_index():
41+
def test__sub_slim_indexes_for_pix_index__multi_pixel_mappings__groups_slim_indexes_by_pixel():
4042
mapper = aa.m.MockMapper(
4143
interpolator=aa.m.MockInterpolator(
4244
mappings=np.array(
@@ -68,7 +70,7 @@ def test__sub_slim_indexes_for_pix_index():
6870
]
6971

7072

71-
def test__data_weight_total_for_pix_from():
73+
def test__data_weight_total_for_pix_from__multi_pixel_mappings__sums_weights_per_pixel():
7274
mapper = aa.m.MockMapper(
7375
interpolator=aa.m.MockInterpolator(
7476
mappings=np.array(
@@ -131,7 +133,7 @@ def test__adaptive_pixel_signals_from___matches_util(grid_2d_7x7, image_7x7):
131133
assert (pixel_signals == pixel_signals_util).all()
132134

133135

134-
def test__mapped_to_source_from(grid_2d_7x7):
136+
def test__mapped_to_source_from__delaunay_mapper__matches_mapping_matrix_util(grid_2d_7x7):
135137
mesh_grid = aa.Grid2D.no_mask(
136138
values=[[0.1, 0.1], [1.1, 0.6], [2.1, 0.1], [0.4, 1.1], [1.1, 7.1], [2.1, 1.1]],
137139
shape_native=(3, 2),

test_autoarray/inversion/pixelization/mappers/test_delaunay.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414

1515

16-
def test__pixel_weights_delaunay_from():
16+
def test__pixel_weights_delaunay_from__two_data_points__returns_correct_barycentric_weights():
1717
data_grid = np.array([[0.1, 0.1], [1.0, 1.0]])
1818

1919
mesh_grid = np.array([[0.0, 0.0], [0.1, 0.0], [0.2, 0.0]])
@@ -29,7 +29,7 @@ def test__pixel_weights_delaunay_from():
2929
assert (pixel_weights == np.array([[0.25, 0.5, 0.25], [1.0, 0.0, 0.0]])).all()
3030

3131

32-
def test__pix_indexes_for_sub_slim_index__matches_util(grid_2d_sub_1_7x7):
32+
def test__pix_indexes_for_sub_slim_index__delaunay_mesh__matches_util_and_expected_values(grid_2d_sub_1_7x7):
3333
mesh_grid = aa.Grid2D.no_mask(
3434
values=[[0.1, 0.1], [1.1, 0.6], [2.1, 0.1], [0.4, 1.1], [1.1, 7.1], [2.1, 1.1]],
3535
shape_native=(3, 2),

test_autoarray/inversion/pixelization/mappers/test_rectangular.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pytest
1111

1212

13-
def test__pix_indexes_for_sub_slim_index__matches_util():
13+
def test__pix_indexes_for_sub_slim_index__rectangular_uniform_mesh__matches_util():
1414
grid = aa.Grid2D.no_mask(
1515
values=[
1616
[1.5, -1.0],
@@ -51,7 +51,7 @@ def test__pix_indexes_for_sub_slim_index__matches_util():
5151
assert mapper.pix_weights_for_sub_slim_index == pytest.approx(weights, 1.0e-4)
5252

5353

54-
def test__pixel_signals_from__matches_util(grid_2d_sub_1_7x7, image_7x7):
54+
def test__pixel_signals_from__rectangular_adapt_density_mesh__matches_util(grid_2d_sub_1_7x7, image_7x7):
5555

5656
mesh_grid = overlay_grid_from(
5757
shape_native=(3, 3), grid=grid_2d_sub_1_7x7.over_sampled, buffer=1e-8

0 commit comments

Comments
 (0)