Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions test_autoarray/inversion/inversion/test_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
directory = path.dirname(path.realpath(__file__))


def test__has():
def test__has__linear_obj_with_regularization__returns_true():
reg = aa.m.MockRegularization()
linear_obj = aa.m.MockLinearObj(regularization=reg)
inversion = aa.m.MockInversion(linear_obj_list=[linear_obj])

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


def test__has__linear_obj_without_regularization__returns_false():
linear_obj = aa.m.MockLinearObj(regularization=None)
inversion = aa.m.MockInversion(linear_obj_list=[linear_obj])

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


def test__total_regularizations():
def test__total_regularizations__one_regularized_one_unregularized__returns_one():
reg = aa.m.MockRegularization()

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

assert inversion.total_regularizations == 1


def test__total_regularizations__both_regularized__returns_two():
reg = aa.m.MockRegularization()

linear_obj_0 = aa.m.MockLinearObj(regularization=reg)

inversion = aa.m.MockInversion(linear_obj_list=[linear_obj_0, linear_obj_0])

assert inversion.total_regularizations == 2


def test__total_regularizations__none_regularized__returns_zero():
linear_obj_1 = aa.m.MockLinearObj(regularization=None)

inversion = aa.m.MockInversion(linear_obj_list=[linear_obj_1, linear_obj_1])

assert inversion.total_regularizations == 0


def test__index_range_list_from():
def test__param_range_list_from__linear_obj_and_mapper__correct_ranges_per_class():
inversion = aa.m.MockInversion(
linear_obj_list=[
aa.m.MockLinearObj(parameters=2, regularization=None),
Expand All @@ -51,7 +63,7 @@ def test__index_range_list_from():
assert inversion.param_range_list_from(cls=aa.Mapper) == [[2, 3]]


def test__no_regularization_index_list():
def test__no_regularization_index_list__all_unregularized__returns_all_parameter_indices():
inversion = aa.m.MockInversion(
linear_obj_list=[
aa.m.MockLinearObj(parameters=2, regularization=None),
Expand All @@ -61,6 +73,8 @@ def test__no_regularization_index_list():

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


def test__no_regularization_index_list__mixed_regularized_and_unregularized__returns_only_unregularized_indices():
inversion = aa.m.MockInversion(
linear_obj_list=[
aa.m.MockMapper(parameters=10, regularization=aa.m.MockRegularization()),
Expand All @@ -73,7 +87,7 @@ def test__no_regularization_index_list():
assert inversion.no_regularization_index_list == [10, 11, 12, 33, 34, 35, 36]


def test__mapping_matrix():
def test__mapping_matrix__two_mappers__concatenates_mapping_matrices_horizontally():
mapper_0 = aa.m.MockMapper(mapping_matrix=np.ones((2, 2)))
mapper_1 = aa.m.MockMapper(mapping_matrix=2.0 * np.ones((2, 3)))

Expand Down Expand Up @@ -215,7 +229,7 @@ def test__curvature_matrix_via_sparse_operator__includes_source_interpolation__i
)


def test__curvature_reg_matrix_reduced():
def test__curvature_reg_matrix_reduced__regularized_and_unregularized__removes_unregularized_rows_cols():
curvature_reg_matrix = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])

linear_obj_list = [
Expand All @@ -232,7 +246,7 @@ def test__curvature_reg_matrix_reduced():
).all()


def test__regularization_matrix():
def test__regularization_matrix__two_regularized_mappers__assembles_block_diagonal_matrix():
reg_0 = aa.m.MockRegularization(regularization_matrix=np.ones((2, 2)))
reg_1 = aa.m.MockRegularization(regularization_matrix=2.0 * np.ones((3, 3)))

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


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


def test__reconstruction_dict():
def test__reconstruction_dict__single_linear_obj_and_mapper__splits_reconstruction_correctly():
reconstruction = np.array([0.0, 1.0, 1.0, 1.0])

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


def test__reconstruction_dict__multiple_linear_objs_and_mappers__splits_reconstruction_correctly():
reconstruction = np.array([0.0, 1.0, 1.0, 2.0, 2.0, 2.0])

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


def test__mapped_reconstructed_data_dict():
def test__mapped_reconstructed_data_dict__single_linear_obj__returns_correct_data_and_sum():
linear_obj_0 = aa.m.MockLinearObj()

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


def test__mapped_reconstructed_data_dict__two_linear_objs__sums_contributions_correctly():
linear_obj_0 = aa.m.MockLinearObj()
linear_obj_1 = aa.m.MockLinearObj()

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


def test__mapped_reconstructed_operated_data_dict():
def test__mapped_reconstructed_operated_data_dict__single_linear_obj__returns_correct_data_and_sum():
linear_obj_0 = aa.m.MockLinearObj()

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


def test__mapped_reconstructed_operated_data_dict__two_linear_objs__sums_contributions_correctly():
linear_obj_0 = aa.m.MockLinearObj()
linear_obj_1 = aa.m.MockLinearObj()

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


def test__mapped_reconstructed_operated_data():
def test__mapped_reconstructed_operated_data__single_linear_obj__returns_correct_operated_data():
linear_obj_0 = aa.m.MockLinearObj()

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


def test__mapped_reconstructed_operated_data__two_linear_objs__sums_operated_data_correctly():
linear_obj_0 = aa.m.MockLinearObj()
linear_obj_1 = aa.m.MockLinearObj()

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


def test__data_subtracted_dict():
def test__data_subtracted_dict__single_linear_obj__subtracts_other_contributions_from_data():
linear_obj_0 = aa.m.MockLinearObj()

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

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


def test__data_subtracted_dict__two_linear_objs__subtracts_other_contributions_from_data():
linear_obj_0 = aa.m.MockLinearObj()
linear_obj_1 = aa.m.MockLinearObj()

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


def test__regularization_term():
def test__regularization_term__identity_matrix__computes_sum_of_squared_reconstruction():
reconstruction = np.array([1.0, 1.0, 1.0])

regularization_matrix = np.array(
Expand Down Expand Up @@ -478,6 +506,8 @@ def test__regularization_term():

assert inversion.regularization_term == 3.0


def test__regularization_term__tridiagonal_matrix__computes_weighted_regularization_term():
reconstruction = np.array([2.0, 3.0, 5.0])

regularization_matrix = np.array(
Expand Down Expand Up @@ -509,7 +539,7 @@ def test__regularization_term():
assert inversion.regularization_term == 34.0


def test__determinant_of_positive_definite_matrix_via_cholesky():
def test__determinant_of_positive_definite_matrix_via_cholesky__identity_matrix__matches_numpy_log_det():
matrix = np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])

inversion = aa.m.MockInversion(
Expand All @@ -523,6 +553,8 @@ def test__determinant_of_positive_definite_matrix_via_cholesky():
inversion.log_det_curvature_reg_matrix_term, 1e-4
)


def test__determinant_of_positive_definite_matrix_via_cholesky__tridiagonal_matrix__matches_numpy_log_det():
matrix = np.array([[2.0, -1.0, 0.0], [-1.0, 2.0, -1.0], [0.0, -1.0, 2.0]])

inversion = aa.m.MockInversion(
Expand All @@ -537,7 +569,7 @@ def test__determinant_of_positive_definite_matrix_via_cholesky():
)


def test__reconstruction_noise_map():
def test__reconstruction_noise_map__asymmetric_curvature_reg_matrix__correct_diagonal_noise_values():
curvature_reg_matrix = np.array([[1.0, 1.0, 1.0], [1.0, 2.0, 1.0], [1.0, 1.0, 3.0]])

inversion = aa.m.MockInversion(curvature_reg_matrix=curvature_reg_matrix)
Expand All @@ -550,7 +582,7 @@ def test__reconstruction_noise_map():
)


def test__max_pixel_list_from_and_centre():
def test__max_pixel_list_from_and_centre__returns_top_pixels_and_brightest_centre():

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


def test__max_pixel_list_from__filter_neighbors():
def test__max_pixel_list_from__filter_neighbors__excludes_adjacent_pixels_from_top_list():
source_plane_mesh_grid = aa.Grid2DIrregular(
[
[1.0, 1.0],
Expand Down
10 changes: 6 additions & 4 deletions test_autoarray/inversion/pixelization/mappers/test_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import autoarray as aa


def test__pix_indexes_for_slim_indexes__different_types_of_lists_input():
def test__pix_indexes_for_slim_indexes__rectangular_mapper__correct_pixel_index_mapping():
mapper = aa.m.MockMapper(
interpolator=aa.m.MockInterpolator(
mappings=np.array([[0], [0], [0], [0], [0], [0], [0], [0]]),
Expand All @@ -20,6 +20,8 @@ def test__pix_indexes_for_slim_indexes__different_types_of_lists_input():

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


def test__pix_indexes_for_slim_indexes__delaunay_mapper__correct_pixel_index_mapping():
mapper = aa.m.MockMapper(
interpolator=aa.m.MockInterpolator(
mappings=np.array([[0], [0], [0], [0], [3], [4], [4], [7]]),
Expand All @@ -36,7 +38,7 @@ def test__pix_indexes_for_slim_indexes__different_types_of_lists_input():
assert pixe_indexes_for_slim_indexes == [[0, 1, 2, 3], [5, 6]]


def test__sub_slim_indexes_for_pix_index():
def test__sub_slim_indexes_for_pix_index__multi_pixel_mappings__groups_slim_indexes_by_pixel():
mapper = aa.m.MockMapper(
interpolator=aa.m.MockInterpolator(
mappings=np.array(
Expand Down Expand Up @@ -68,7 +70,7 @@ def test__sub_slim_indexes_for_pix_index():
]


def test__data_weight_total_for_pix_from():
def test__data_weight_total_for_pix_from__multi_pixel_mappings__sums_weights_per_pixel():
mapper = aa.m.MockMapper(
interpolator=aa.m.MockInterpolator(
mappings=np.array(
Expand Down Expand Up @@ -131,7 +133,7 @@ def test__adaptive_pixel_signals_from___matches_util(grid_2d_7x7, image_7x7):
assert (pixel_signals == pixel_signals_util).all()


def test__mapped_to_source_from(grid_2d_7x7):
def test__mapped_to_source_from__delaunay_mapper__matches_mapping_matrix_util(grid_2d_7x7):
mesh_grid = aa.Grid2D.no_mask(
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]],
shape_native=(3, 2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)


def test__pixel_weights_delaunay_from():
def test__pixel_weights_delaunay_from__two_data_points__returns_correct_barycentric_weights():
data_grid = np.array([[0.1, 0.1], [1.0, 1.0]])

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


def test__pix_indexes_for_sub_slim_index__matches_util(grid_2d_sub_1_7x7):
def test__pix_indexes_for_sub_slim_index__delaunay_mesh__matches_util_and_expected_values(grid_2d_sub_1_7x7):
mesh_grid = aa.Grid2D.no_mask(
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]],
shape_native=(3, 2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest


def test__pix_indexes_for_sub_slim_index__matches_util():
def test__pix_indexes_for_sub_slim_index__rectangular_uniform_mesh__matches_util():
grid = aa.Grid2D.no_mask(
values=[
[1.5, -1.0],
Expand Down Expand Up @@ -51,7 +51,7 @@ def test__pix_indexes_for_sub_slim_index__matches_util():
assert mapper.pix_weights_for_sub_slim_index == pytest.approx(weights, 1.0e-4)


def test__pixel_signals_from__matches_util(grid_2d_sub_1_7x7, image_7x7):
def test__pixel_signals_from__rectangular_adapt_density_mesh__matches_util(grid_2d_sub_1_7x7, image_7x7):

mesh_grid = overlay_grid_from(
shape_native=(3, 3), grid=grid_2d_sub_1_7x7.over_sampled, buffer=1e-8
Expand Down
Loading