|
| 1 | +import math |
| 2 | + |
| 3 | +import attr |
| 4 | +import numpy as np |
| 5 | +from pytest import approx |
| 6 | + |
| 7 | +from fastimgproto.sourcefind.fit import Gaussian2dParams |
| 8 | +from fastimgproto.sourcefind.metrics import ( |
| 9 | + mahalanobis_distance, |
| 10 | + pixel_distance, |
| 11 | + shape_difference, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def test_distance_metrics(): |
| 16 | + x_0, y_0 = 1., 4. |
| 17 | + g2d_x_long = Gaussian2dParams(x_centre=x_0, |
| 18 | + y_centre=y_0, |
| 19 | + amplitude=1, |
| 20 | + semimajor=2., |
| 21 | + semiminor=1., |
| 22 | + theta=0., |
| 23 | + ) |
| 24 | + |
| 25 | + # A Gaussian elongated in the y-direction (Note theta) |
| 26 | + g2d_y_long = attr.evolve(g2d_x_long, theta=np.pi / 2) |
| 27 | + |
| 28 | + x, y = x_0 + 2, y_0 |
| 29 | + assert mahalanobis_distance(g2d_x_long, x=x, y=y) == approx(1.0) |
| 30 | + assert mahalanobis_distance(g2d_y_long, x=x, y=y) == approx(2.0) |
| 31 | + assert pixel_distance(g2d_x_long, x, y) == pixel_distance(g2d_y_long, x, y) |
| 32 | + assert pixel_distance(g2d_x_long, x, y) == 2. |
| 33 | + |
| 34 | + x, y = x_0, y_0 + 1 |
| 35 | + assert mahalanobis_distance(g2d_x_long, x=x, y=y) == approx(1.0) |
| 36 | + assert mahalanobis_distance(g2d_y_long, x=x, y=y) == approx(0.5) |
| 37 | + assert pixel_distance(g2d_x_long, x, y) == pixel_distance(g2d_y_long, x, y) |
| 38 | + assert pixel_distance(g2d_x_long, x, y) == 1. |
| 39 | + |
| 40 | + g2d_diag_long = attr.evolve(g2d_x_long, theta=np.pi / 4.) |
| 41 | + x, y = x_0 + np.cos(np.pi / 4), y_0 + np.sin(np.pi / 4) |
| 42 | + assert mahalanobis_distance(g2d_diag_long, x=x, y=y) == approx(0.5) |
| 43 | + assert pixel_distance(g2d_diag_long, x, y) == approx(1.) |
| 44 | + |
| 45 | + |
| 46 | +def test_shape_metric(): |
| 47 | + g_symmetric_1 = Gaussian2dParams(x_centre=0., |
| 48 | + y_centre=0., |
| 49 | + amplitude=1, |
| 50 | + semimajor=1., |
| 51 | + semiminor=1., |
| 52 | + theta=0., |
| 53 | + ) |
| 54 | + |
| 55 | + assert shape_difference(g_symmetric_1, g_symmetric_1) == 0 |
| 56 | + |
| 57 | + translated_sym = attr.evolve(g_symmetric_1, x_centre=123, |
| 58 | + y_centre=456, ) |
| 59 | + assert shape_difference(g_symmetric_1, translated_sym) == 0 |
| 60 | + |
| 61 | + # These are all the same 'shape distance' from the symmetric circular |
| 62 | + # Gaussian: |
| 63 | + g_x_2 = attr.evolve(g_symmetric_1, semimajor=2., semiminor=1.) |
| 64 | + g_y_2 = attr.evolve(g_x_2, theta=np.pi / 2.) |
| 65 | + g_diag_2 = attr.evolve(g_x_2, theta=np.pi / 4.) |
| 66 | + g_x_half = attr.evolve(g_symmetric_1, semiminor=0.5) |
| 67 | + |
| 68 | + symmetric_to_elongated_by_2 = shape_difference(g_symmetric_1, g_x_2) |
| 69 | + for g_pars in (g_x_2, g_y_2, g_diag_2, g_x_half): |
| 70 | + assert shape_difference(g_symmetric_1, |
| 71 | + g_pars) == symmetric_to_elongated_by_2 |
| 72 | + |
| 73 | + # Twice the elongation factor -> Twice the distance metric |
| 74 | + assert shape_difference(g_x_half, g_x_2) == 2 * symmetric_to_elongated_by_2 |
| 75 | + |
| 76 | + # Elongated in both directions -> Twice the distance metric |
| 77 | + g_symmetric_2 = attr.evolve(g_symmetric_1, semimajor=2., semiminor=2.) |
| 78 | + assert (shape_difference(g_symmetric_1, g_symmetric_2) == |
| 79 | + approx(2 * symmetric_to_elongated_by_2)) |
| 80 | + |
| 81 | + # Elongated and rotated by 90-degrees: |
| 82 | + # (Equivalent to shrink in x, stretch in y) |
| 83 | + # -> Twice the distance metric |
| 84 | + assert (shape_difference(g_x_2, g_y_2) == |
| 85 | + approx(2 * symmetric_to_elongated_by_2)) |
| 86 | + |
| 87 | + # Rotation by 45-degrees: |
| 88 | + assert (symmetric_to_elongated_by_2 < |
| 89 | + shape_difference(g_diag_2, g_x_2) < |
| 90 | + 2 * symmetric_to_elongated_by_2) |
0 commit comments