Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffwoollard committed Apr 2, 2022
1 parent 1958a68 commit 5451fd1
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions tests/test_expectation_maximization.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_grid_SO3_uniform(test_ir, n_particles):
def test_generate_xy_plane(test_ir, n_pix):
"""Test generation of xy plane."""
xy_plane = test_ir.generate_xy_plane(n_pix)
assert xy_plane.shape == (n_pix**2, 3)
assert xy_plane.shape == (n_pix ** 2, 3)


def test_generate_slices(test_ir, n_particles, n_pix):
Expand All @@ -101,9 +101,9 @@ def test_generate_slices(test_ir, n_particles, n_pix):

slices, xyz_rotated = test_ir.generate_slices(map_3d, xy_plane, n_pix, rots)

assert xy_plane.shape == (n_pix**2, 3)
assert xy_plane.shape == (n_pix ** 2, 3)
assert slices.shape == (n_particles, n_pix, n_pix)
assert xyz_rotated.shape == (n_pix**2, 3)
assert xyz_rotated.shape == (n_pix ** 2, 3)


def test_apply_ctf_to_slice(test_ir, n_pix):
Expand Down Expand Up @@ -201,10 +201,15 @@ def test_compute_fsc(test_ir, n_pix):
def test_binary_mask_3d(test_ir):
"""Test binary_mask_3d.
Sums shell through an axis, then converts to circle,
Tests the limit of infinite n_pix. Use high n_pix so good approx.
1. Sums shell through an axis, then converts to circle,
then checks if circle/square ratio agrees with largest
circle inscribed in square. Should be pi/4 by trigonometry,
in the limit of infinite n_pix. Use high n_pix so good approx.
circle inscribed in square. Should be pi/4.
2. Make shells at sizes r and r/2 and check ratios of perimeter
of circle (mid slice) and surface area of sphere.
3. Make filled sphere of sizes r and r/2 and check ratio of volume.
"""
n_pix = 512

Expand All @@ -221,6 +226,41 @@ def test_binary_mask_3d(test_ir):
circle_to_square_ratio = circle.mean()
assert np.isclose(circle_to_square_ratio, np.pi / 4, atol=1e-3)

mask = test_ir.binary_mask_3d(center, radius, shape, fill=True, shell_thickness=1)
circle = mask[n_pix // 2]
circle_to_square_ratio = circle.mean()
assert np.isclose(circle_to_square_ratio, np.pi / 4, atol=1e-3)

r_half = radius / 2
for shell_thickness in [1, 2]:
mask_r = test_ir.binary_mask_3d(
center, radius, shape, fill=False, shell_thickness=1
)
mask_r_half = test_ir.binary_mask_3d(
center, r_half, shape, fill=False, shell_thickness=1
)
perimeter_ratio = mask_r[n_pix // 2].sum() / mask_r_half[n_pix // 2].sum()
assert np.isclose(2, perimeter_ratio, atol=0.1)
if shell_thickness == 1:
assert np.isclose(
mask_r[n_pix // 2].sum() / (2 * np.pi * radius), 1, atol=0.1
)
assert np.isclose(
mask_r_half[n_pix // 2].sum() / (2 * np.pi * r_half), 1, atol=0.1
)

surface_area_ratio = mask_r.sum() / mask_r_half.sum()
surface_area_ratio_analytic = (radius / r_half) ** 2
assert np.isclose(surface_area_ratio, surface_area_ratio_analytic, atol=0.1)

mask_r = test_ir.binary_mask_3d(center, radius, shape, fill=True, shell_thickness=1)
mask_r_half = test_ir.binary_mask_3d(
center, r_half, shape, fill=True, shell_thickness=1
)
volume_ratio = mask_r.sum() / mask_r_half.sum()
volume_ratio_analytic = (radius / r_half) ** 3
assert np.isclose(volume_ratio, volume_ratio_analytic, atol=0.005)


def test_expand_1d_to_3d(test_ir, n_pix):
"""Test expansion of 1D array into spherical shell."""
Expand Down

0 comments on commit 5451fd1

Please sign in to comment.