def test_circle_point(): circle = Circle((0, 0), 1) actual = circle.point(np.pi / 4.) sq2 = np.sqrt(2.) np.testing.assert_array_almost_equal(actual, (sq2 / 2., sq2 / 2.)) actual = circle.point(0, True) np.testing.assert_array_equal(actual, (1, 0))
def test_extract_lines_along_arc(): circle = Circle((0, 0), 100) x = y = np.arange(110, dtype=int) X, Y = np.meshgrid(x, y, indexing='ij') phis = np.linspace(0, np.pi/2., 30) img = np.sqrt(X**2 + Y**2) img_2 = X**2 + Y**2 arc = [] num_points = 3 for phi in phis: e_r = circle.e_r(phi) mid_point = circle.point(phi) points = [mid_point + e_r * k for k in np.arange(-num_points, num_points + 1)] arc.append(np.array(points)) arc = np.array(arc) # Act actual = extract_lines_along_arc(img, arc) actual_2 = extract_lines_along_arc(img_2, arc) # Assert one = np.ones_like(phis) np.testing.assert_allclose(actual, 100*one, rtol=1.E-4) np.testing.assert_allclose(actual_2, 10**4 * one, rtol=1.E-3) # Quadratic scaling should shift the sum to the outside assert np.all(actual_2 > actual)
def test_get_arc_by_x(): calib_keys = ['0', '1', '2'] times = [100, 500, 900] circle_center = [(0, 0), (10, 0), (10, 10)] circle_radii = [9, 10, 11] em = ExtractionModel() # Set the circle points and test getting arc by calib_key for i, calib_key in enumerate(calib_keys): radius = circle_radii[i] circle = Circle(circle_center[i], radius) phis = [0, np.pi / 4, np.pi / 2] for phi in phis: (xdata, ydata) = circle.point(phi) em.add_point(calib_key, times[i], xdata, ydata) # Create angles at which to calculate the arc em.extraction_angles[calib_key] = [0, np.pi / 2] em.refresh_extraction_angles_interpolation() # Get the arc em.set_arc_width(2) arc = em.get_arc_by_calib_key(calib_key) expected_arc = [ np.array([[radius - 2, 0.0], [radius - 1, 0.0], [radius, 0.0], [radius + 1, 0.0], [radius + 2, 0.0]]) + circle_center[i], np.array([[0.0, radius - 2], [0.0, radius - 1], [0.0, radius], [0.0, radius + 1], [0.0, radius + 2]]) + circle_center[i] ] assert len(arc) == len(expected_arc) np.testing.assert_allclose(arc, expected_arc, rtol=0.05, atol=0.000001) """ Test getting arc by time """ # the exact time point of a calibration should yield # the same as the calibration key arc = em.get_arc_by_time(times[0]) expected_arc = em.get_arc_by_calib_key(calib_keys[0]) np.testing.assert_allclose(arc, expected_arc, rtol=0.05, atol=0.000001) # a time point after the last calibration should give # the value of the last calibration arc = em.get_arc_by_time(times[-1] + 500) expected_arc = em.get_arc_by_calib_key(calib_keys[-1]) np.testing.assert_allclose(arc, expected_arc, rtol=0.05, atol=0.000001) # a time point between two calibrations should give # an interpolated value arc = em.get_arc_by_time(np.mean(times[0:2])) radius_interpolated = np.mean(circle_radii[0:2]) circle_center_interpolated = (5, 0) expected_arc = [ np.array( [[radius_interpolated - 2, 0.0], [radius_interpolated - 1, 0.0], [radius_interpolated, 0.0], [radius_interpolated + 1, 0.0], [radius_interpolated + 2, 0.0]]) + circle_center_interpolated, np.array([[0.0, radius_interpolated - 2], [ 0.0, radius_interpolated - 1 ], [0.0, radius_interpolated], [0.0, radius_interpolated + 1], [0.0, radius_interpolated + 2]]) + circle_center_interpolated ] np.testing.assert_allclose(arc, expected_arc, rtol=0.05, atol=0.000001)