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_intersect_circle_rectangle(): circle = Circle((-1, -1), 2) rect = Rectangle((1, 1)) actual = circle.intersection(rect) cut = 0.72955 expected = [(0, cut), (cut, 0)] np.testing.assert_array_almost_equal(actual, expected, decimal=5)
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_circle_angle(): circle = Circle((0, 0), 1) sq2 = np.sqrt(2.) actual = circle.angle((sq2, sq2)) np.testing.assert_almost_equal(actual, np.pi / 4.) actual = circle.angle((0, 1)) np.testing.assert_almost_equal(actual, np.pi / 2.) actual = circle.angle((0, -1)) np.testing.assert_almost_equal(actual, 3 * np.pi / 2.)
def test_discretize_arc(): circle = Circle((0, 0), 100) img_shape = (100, 100) num_points = 100 expected = np.linspace(np.pi / 2., 0, num_points) actual = discretize_arc(circle, img_shape, num_points) np.testing.assert_array_equal(actual, expected)
def update_extraction_angles(self): if not hasattr(self, 'image_shape') or self.image_shape is None: return for calib_key, circle_fit in self.circle_fits.items(): circle = Circle(circle_fit.center, circle_fit.radius) phis = discretize_arc(circle, self.image_shape, num_points=500) self.extraction_angles[calib_key] = phis self.refresh_extraction_angles_interpolation()
def get_arc_by_time(self, time): """ Returns the arc at which to interpolate the 2D image for the 1D spectrum Parameters ---------- time: time point Returns ------- The arc with pixel positions """ arc = np.empty(0) try: center, radius = self.get_circle_fit_by_time(time) circle = Circle(center, radius) phis = self.get_extraction_angles_by_time(time) arc = self.get_arc_from_circle_phis(circle, phis) finally: return arc
def get_arc_by_calib_key(self, calib_key): """ Returns the arc at which to interpolate the 2D image for the 1D spectrum Parameters ---------- calib_key: the calibration number Returns ------- The arc with pixel positions """ arc = np.empty(0) try: center, radius = self.get_circle_fit(calib_key) circle = Circle(center, radius) phis = self.get_extraction_angles(calib_key) arc = self.get_arc_from_circle_phis(circle, phis) finally: return arc
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)