Skip to content

Commit 9083c79

Browse files
authored
Merge pull request #305 from Jammy2211/feature/refactor-mass-tests
Refactor mass profile unit tests to be more granular
2 parents 0791cc2 + 05abbd5 commit 9083c79

31 files changed

+779
-180
lines changed

test_autogalaxy/operate/test_deflections.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test__fermat_potential_from():
9292
)
9393

9494

95-
def test__hessian_from():
95+
def test__hessian_from__diagonal_grid__correct_values():
9696
grid = ag.Grid2DIrregular(values=[(0.5, 0.5), (1.0, 1.0)])
9797

9898
mp = ag.mp.Isothermal(
@@ -107,8 +107,15 @@ def test__hessian_from():
107107
assert hessian_yx == pytest.approx(np.array([-1.388165, -0.694099]), 1.0e-4)
108108
assert hessian_xx == pytest.approx(np.array([1.3883824, 0.694127]), 1.0e-4)
109109

110+
111+
def test__hessian_from__axis_aligned_grid__correct_values():
110112
grid = ag.Grid2DIrregular(values=[(1.0, 0.0), (0.0, 1.0)])
111113

114+
mp = ag.mp.Isothermal(
115+
centre=(0.0, 0.0), ell_comps=(0.0, -0.111111), einstein_radius=2.0
116+
)
117+
118+
od = LensCalc.from_mass_obj(mp)
112119
hessian_yy, hessian_xy, hessian_yx, hessian_xx = od.hessian_from(grid=grid)
113120

114121
assert hessian_yy == pytest.approx(np.array([0.0, 1.777699]), 1.0e-4)
@@ -149,7 +156,7 @@ def test__magnification_2d_via_hessian_from():
149156
assert magnification.in_list[1] == pytest.approx(-2.57591, 1.0e-4)
150157

151158

152-
def test__tangential_critical_curve_list_from():
159+
def test__tangential_critical_curve_list_from__radius_matches_einstein_radius():
153160
grid = ag.Grid2D.uniform(shape_native=(15, 15), pixel_scales=0.3)
154161

155162
mp = ag.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=2.0)
@@ -166,6 +173,8 @@ def test__tangential_critical_curve_list_from():
166173
x_critical_tangential**2 + y_critical_tangential**2
167174
) == pytest.approx(mp.einstein_radius**2, 5e-1)
168175

176+
177+
def test__tangential_critical_curve_list_from__centre_at_origin__curve_centred_on_origin():
169178
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
170179

171180
mp = ag.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=2.0)
@@ -179,6 +188,10 @@ def test__tangential_critical_curve_list_from():
179188
assert -0.03 < y_centre < 0.03
180189
assert -0.03 < x_centre < 0.03
181190

191+
192+
def test__tangential_critical_curve_list_from__offset_centre__curve_centred_on_offset():
193+
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
194+
182195
mp = ag.mp.IsothermalSph(centre=(0.5, 1.0), einstein_radius=2.0)
183196

184197
od = LensCalc.from_mass_obj(mp)
@@ -225,7 +238,7 @@ def test__tangential_critical_curve_list_from():
225238
# )
226239

227240

228-
def test__radial_critical_curve_list_from():
241+
def test__radial_critical_curve_list_from__centre_at_origin__curve_centred_on_origin():
229242
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
230243

231244
mp = ag.mp.PowerLawSph(centre=(0.0, 0.0), einstein_radius=2.0, slope=1.5)
@@ -239,6 +252,10 @@ def test__radial_critical_curve_list_from():
239252
assert -0.05 < y_centre < 0.05
240253
assert -0.05 < x_centre < 0.05
241254

255+
256+
def test__radial_critical_curve_list_from__offset_centre__curve_centred_on_offset():
257+
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
258+
242259
mp = ag.mp.PowerLawSph(centre=(0.5, 1.0), einstein_radius=2.0, slope=1.5)
243260

244261
od = LensCalc.from_mass_obj(mp)
@@ -271,7 +288,7 @@ def test__radial_critical_curve_list_from__compare_via_magnification():
271288
)
272289

273290

274-
def test__tangential_caustic_list_from():
291+
def test__tangential_caustic_list_from__centre_at_origin__caustic_centred_on_origin():
275292
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
276293

277294
mp = ag.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=2.0)
@@ -285,6 +302,10 @@ def test__tangential_caustic_list_from():
285302
assert -0.03 < y_centre < 0.03
286303
assert -0.03 < x_centre < 0.03
287304

305+
306+
def test__tangential_caustic_list_from__offset_centre__caustic_centred_on_offset():
307+
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
308+
288309
mp = ag.mp.IsothermalSph(centre=(0.5, 1.0), einstein_radius=2.0)
289310

290311
od = LensCalc.from_mass_obj(mp)
@@ -319,7 +340,7 @@ def test__tangential_caustic_list_from():
319340
# )
320341

321342

322-
def test__radial_caustic_list_from():
343+
def test__radial_caustic_list_from__radius_check__correct_mean_radius():
323344
grid = ag.Grid2D.uniform(shape_native=(20, 20), pixel_scales=0.2)
324345

325346
mp = ag.mp.PowerLawSph(centre=(0.0, 0.0), einstein_radius=2.0, slope=1.5)
@@ -336,6 +357,8 @@ def test__radial_caustic_list_from():
336357
0.25, 5e-1
337358
)
338359

360+
361+
def test__radial_caustic_list_from__centre_at_origin__caustic_centred_on_origin():
339362
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
340363

341364
mp = ag.mp.PowerLawSph(centre=(0.0, 0.0), einstein_radius=2.0, slope=1.5)
@@ -349,6 +372,10 @@ def test__radial_caustic_list_from():
349372
assert -0.2 < y_centre < 0.2
350373
assert -0.35 < x_centre < 0.35
351374

375+
376+
def test__radial_caustic_list_from__offset_centre__caustic_centred_near_offset():
377+
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
378+
352379
mp = ag.mp.PowerLawSph(centre=(0.5, 1.0), einstein_radius=2.0, slope=1.5)
353380

354381
od = LensCalc.from_mass_obj(mp)
@@ -410,7 +437,7 @@ def test__tangential_critical_curve_area_list_from():
410437
)
411438

412439

413-
def test__einstein_radius_list_from():
440+
def test__einstein_radius_list_from__isothermal_sph__correct_einstein_radius():
414441
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
415442

416443
mp = ag.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=2.0)
@@ -420,6 +447,10 @@ def test__einstein_radius_list_from():
420447

421448
assert einstein_radius_list[0] == pytest.approx(2.0, 1e-1)
422449

450+
451+
def test__einstein_radius_list_from__isothermal_elliptical__correct_einstein_radius():
452+
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
453+
423454
mp = ag.mp.Isothermal(
424455
centre=(0.0, 0.0), einstein_radius=2.0, ell_comps=(0.0, -0.25)
425456
)
@@ -430,7 +461,7 @@ def test__einstein_radius_list_from():
430461
assert einstein_radius_list[0] == pytest.approx(1.9360, 1e-1)
431462

432463

433-
def test__einstein_radius_from():
464+
def test__einstein_radius_from__isothermal_sph__correct_einstein_radius():
434465
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
435466

436467
mp = ag.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=2.0)
@@ -440,6 +471,10 @@ def test__einstein_radius_from():
440471

441472
assert einstein_radius == pytest.approx(2.0, 1e-1)
442473

474+
475+
def test__einstein_radius_from__isothermal_elliptical__correct_einstein_radius():
476+
grid = ag.Grid2D.uniform(shape_native=(50, 50), pixel_scales=0.2)
477+
443478
mp = ag.mp.Isothermal(
444479
centre=(0.0, 0.0), einstein_radius=2.0, ell_comps=(0.0, -0.25)
445480
)

test_autogalaxy/profiles/mass/abstract/test_abstract.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ def mass_within_radius_of_profile_from_grid_calculation(radius, profile):
2626
return mass_total
2727

2828

29-
def test__deflections_2d_via_potential_2d_from():
29+
def test__deflections_2d_via_potential_2d_from__isothermal_sph():
3030
mp = ag.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=2.0)
3131

3232
grid = ag.Grid2D.uniform(shape_native=(10, 10), pixel_scales=0.05)
3333

3434
deflections_via_calculation = mp.deflections_yx_2d_from(grid=grid)
35-
3635
deflections_via_potential = mp.deflections_2d_via_potential_2d_from(grid=grid)
3736

3837
mean_error = np.mean(
@@ -41,14 +40,15 @@ def test__deflections_2d_via_potential_2d_from():
4140

4241
assert mean_error < 1e-4
4342

43+
44+
def test__deflections_2d_via_potential_2d_from__isothermal_ell_comps_1():
4445
sie = ag.mp.Isothermal(
4546
centre=(0.0, 0.0), ell_comps=(0.111111, 0.0), einstein_radius=2.0
4647
)
4748

4849
grid = ag.Grid2D.uniform(shape_native=(10, 10), pixel_scales=0.05)
4950

5051
deflections_via_calculation = sie.deflections_yx_2d_from(grid=grid)
51-
5252
deflections_via_potential = sie.deflections_2d_via_potential_2d_from(grid=grid)
5353

5454
mean_error = np.mean(
@@ -57,14 +57,15 @@ def test__deflections_2d_via_potential_2d_from():
5757

5858
assert mean_error < 1e-4
5959

60+
61+
def test__deflections_2d_via_potential_2d_from__isothermal_ell_comps_2():
6062
sie = ag.mp.Isothermal(
6163
centre=(0.0, 0.0), ell_comps=(0.0, -0.111111), einstein_radius=2.0
6264
)
6365

6466
grid = ag.Grid2D.uniform(shape_native=(10, 10), pixel_scales=0.05)
6567

6668
deflections_via_calculation = sie.deflections_yx_2d_from(grid=grid)
67-
6869
deflections_via_potential = sie.deflections_2d_via_potential_2d_from(grid=grid)
6970

7071
mean_error = np.mean(
@@ -74,17 +75,21 @@ def test__deflections_2d_via_potential_2d_from():
7475
assert mean_error < 1e-4
7576

7677

77-
def test__mass_angular_within_circle_from():
78+
def test__mass_angular_within_circle_from__sph_einstein_radius_2():
7879
mp = ag.mp.IsothermalSph(einstein_radius=2.0)
7980

8081
mass = mp.mass_angular_within_circle_from(radius=2.0)
8182
assert math.pi * mp.einstein_radius * 2.0 == pytest.approx(mass, 1e-3)
8283

84+
85+
def test__mass_angular_within_circle_from__sph_einstein_radius_4():
8386
mp = ag.mp.IsothermalSph(einstein_radius=4.0)
8487

8588
mass = mp.mass_angular_within_circle_from(radius=4.0)
8689
assert math.pi * mp.einstein_radius * 4.0 == pytest.approx(mass, 1e-3)
8790

91+
92+
def test__mass_angular_within_circle_from__sph_grid_integration():
8893
mp = ag.mp.IsothermalSph(einstein_radius=2.0)
8994

9095
mass_grid = mass_within_radius_of_profile_from_grid_calculation(
@@ -96,23 +101,29 @@ def test__mass_angular_within_circle_from():
96101
assert mass_grid == pytest.approx(mass, 0.02)
97102

98103

99-
def test__average_convergence_of_1_radius():
104+
def test__average_convergence_of_1_radius__isothermal_sph():
100105
mp = ag.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=2.0)
101106

102107
assert mp.average_convergence_of_1_radius == pytest.approx(2.0, 1e-4)
103108

109+
110+
def test__average_convergence_of_1_radius__isothermal_low_ellipticity():
104111
sie = ag.mp.Isothermal(
105112
centre=(0.0, 0.0), einstein_radius=1.0, ell_comps=(0.0, 0.111111)
106113
)
107114

108115
assert sie.average_convergence_of_1_radius == pytest.approx(1.0, 1e-4)
109116

117+
118+
def test__average_convergence_of_1_radius__isothermal_medium_ellipticity():
110119
sie = ag.mp.Isothermal(
111120
centre=(0.0, 0.0), einstein_radius=3.0, ell_comps=(0.0, 0.333333)
112121
)
113122

114123
assert sie.average_convergence_of_1_radius == pytest.approx(3.0, 1e-4)
115124

125+
126+
def test__average_convergence_of_1_radius__isothermal_high_ellipticity():
116127
sie = ag.mp.Isothermal(
117128
centre=(0.0, 0.0), einstein_radius=8.0, ell_comps=(0.0, 0.666666)
118129
)
@@ -163,7 +174,7 @@ def test__extract_attribute():
163174
mp.extract_attribute(cls=ag.LightProfile, attr_name="einstein_radius")
164175

165176

166-
def test__regression__centre_of_profile_in_right_place():
177+
def test__regression__centre_of_profile_in_right_place__isothermal():
167178
grid = ag.Grid2D.uniform(shape_native=(7, 7), pixel_scales=1.0)
168179

169180
mass_profile = ag.mp.Isothermal(centre=(1.999999, 0.9999999), einstein_radius=1.0)
@@ -184,6 +195,10 @@ def test__regression__centre_of_profile_in_right_place():
184195
assert deflections.native[1, 4, 1] > 0
185196
assert deflections.native[1, 3, 1] < 0
186197

198+
199+
def test__regression__centre_of_profile_in_right_place__isothermal_sph():
200+
grid = ag.Grid2D.uniform(shape_native=(7, 7), pixel_scales=1.0)
201+
187202
mass_profile = ag.mp.IsothermalSph(centre=(2.0, 1.0), einstein_radius=1.0)
188203
convergence = mass_profile.convergence_2d_from(grid=grid)
189204
max_indexes = np.unravel_index(

0 commit comments

Comments
 (0)