def test_zero_signal(self): test_data_1 = mdtd.MakeTestData(default=False) assert (test_data_1.signal.data == 0.0).all() test_data_2 = mdtd.MakeTestData(default=True) test_data_2.set_signal_zero() assert (test_data_2.signal.data == 0.0).all()
def test_lw_r(self): test0 = mdtd.MakeTestData(size_x=120, size_y=100) test0.add_ring_ellipse(x0=50, y0=50, semi_len0=20, semi_len1=30) test1 = mdtd.MakeTestData(size_x=120, size_y=100) test1.add_ring_ellipse(x0=50, y0=50, semi_len0=20, semi_len1=30, lw_r=3) assert test0.signal.data.sum() < test1.signal.data.sum()
def setup_method(self): self.r0, self.r1 = 10, 20 test_data0 = mdtd.MakeTestData(100, 100) test_data0.add_ring(50, 50, self.r0) test_data1 = mdtd.MakeTestData(100, 100) test_data1.add_ring(50, 50, self.r1) s0, s1 = test_data0.signal, test_data1.signal s0.axes_manager[0].offset, s0.axes_manager[1].offset = -50, -50 s1.axes_manager[0].offset, s1.axes_manager[1].offset = -50, -50 self.s0, self.s1 = s0, s1
def test_rotation(self): test0 = mdtd.MakeTestData(size_x=120, size_y=100) test0.add_ring_ellipse(x0=50, y0=50, semi_len0=20, semi_len1=30) test1 = mdtd.MakeTestData(size_x=120, size_y=100) test1.add_ring_ellipse(x0=50, y0=50, semi_len0=20, semi_len1=30, rotation=1) assert not (test0.signal.data == test1.signal.data).all()
def test_init(self): mdtd.MakeTestData(default=True) mdtd.MakeTestData(size_x=1, size_y=10, scale=0.05) mdtd.MakeTestData( size_x=200, size_y=100, scale=2, default=False, blur=False, blur_sigma=3.0, downscale=False, )
def test_with_downscale_blur_default(self): test = mdtd.MakeTestData(size_x=120, size_y=100) test.add_disk_ellipse(x0=50, y0=50, semi_len0=10, semi_len1=13, intensity=2)
def test_ring_inner_radius(self): r, lw = 20, 2 x0, y0, scale = 50, 50, 1 test_data = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=False, downscale=False) test_data.add_ring(x0=x0, y0=y0, r=r, intensity=10, lw_pix=lw) r_inner = 20 - 2.5 * scale s_h0 = test_data.signal.isig[x0, :y0 + 1] s_h0_edge = s_h0.axes_manager[0].index2value(s_h0.data[::-1].argmax()) r_h0 = s_h0_edge - 0.5 * scale assert r_h0 == r_inner s_h1 = test_data.signal.isig[x0, y0:] s_h1_edge = s_h1.axes_manager[0].index2value(s_h1.data.argmax()) r_h1 = s_h1_edge - 0.5 * scale - 50 assert r_h1 == r_inner s_h2 = test_data.signal.isig[:x0 + 1, y0] s_h2_edge = s_h2.axes_manager[0].index2value(s_h2.data[::-1].argmax()) r_h2 = s_h2_edge - 0.5 * scale assert r_h2 == r_inner s_h3 = test_data.signal.isig[x0:, y0] s_h3_edge = s_h3.axes_manager[0].index2value(s_h3.data.argmax()) r_h3 = s_h3_edge - 0.5 * scale - 50 assert r_h3 == r_inner
def test_large_disk(self): test_data_1 = mdtd.MakeTestData(size_x=10, size_y=10, scale=0.01, default=False) test_data_1.add_disk(x0=5, y0=5, r=20, intensity=100) assert (test_data_1.signal.data > 0.0).all()
def get_hot_pixel_signal(lazy=False): """Get Diffraction2D signal with a disk in the middle. Has 4 pixels with value equal to 50000, to simulate hot pixels. Example ------- >>> s = pxm.dummy_data.get_hot_pixel_signal() Lazy signal >>> s_lazy = pxm.dummy_data.get_hot_pixel_signal(lazy=True) """ data = mdtd.MakeTestData(size_x=128, size_y=128, default=False, blur=True) data.add_disk(64, 64, r=30, intensity=10000) s = data.signal s.change_dtype("int64") s.data += gaussian_filter(s.data, sigma=50) s.data[76, 4] = 50000 s.data[12, 102] = 50000 s.data[32, 10] = 50000 s.data[120, 61] = 50000 if lazy: s = LazyDiffraction2D(s) s.data = da.from_array(s.data, chunks=(64, 64)) else: s = Diffraction2D(s) return s
def test_ring_radius_outside_image(self): test = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=False, downscale=False) test.add_ring(x0=50, y0=50, r=300, intensity=19, lw_pix=1) assert not test.signal.data.any()
def test_very_large_radius_no_cover(self): test = mdtd.MakeTestData(size_x=120, size_y=100) test.add_ring_ellipse(x0=50, y0=50, semi_len0=200, semi_len1=159, intensity=2) assert not test.signal.data.any()
def test_ring_cover_whole_image(self): intensity = 10.0 test = mdtd.MakeTestData(size_x=50, size_y=100, default=False, blur=False, downscale=False) test.add_ring(x0=25, y0=200, r=150, intensity=intensity, lw_pix=100) assert (test.signal.data == intensity).all()
def get_single_ring_diffraction_signal(): """Get HyperSpy 2D signal with a single ring with centre position. The ring has a centre at x=105 and y=67, and radius=40. """ data = mdtd.MakeTestData(size_x=200, size_y=150, default=False, blur=True) x, y = 105, 67 data.add_ring(x, y, r=40) s = data.signal s.axes_manager[0].offset, s.axes_manager[1].offset = -x, -y return s
def test_refine_signal_centre_position(self): test_data = mdtd.MakeTestData( 70, 70, blur=False, downscale=False, default=False ) x, y, r = 35, 35, 15 test_data.add_ring(x, y, r) s = test_data.signal s.axes_manager.signal_axes[0].offset = -x + 1 s.axes_manager.signal_axes[1].offset = -y - 1 ra.refine_signal_centre_position(s, (10.0, 20.0), angleN=6) assert s.axes_manager.signal_axes[0].offset == -x assert s.axes_manager.signal_axes[1].offset == -y
def test_get_optimal_centre_position(self): x0, y0 = 300.0, 300.0 test_data = mdtd.MakeTestData(size_x=600, size_y=600, default=False) test_data.add_ring(x0=x0, y0=y0, r=200, intensity=10, lw_pix=2) s = test_data.signal s.axes_manager[0].offset = -301.0 s.axes_manager[1].offset = -301.0 s_centre_position = ra.get_optimal_centre_position( s, radial_signal_span=(180, 210), steps=2, step_size=1) x, y = ra.get_coordinate_of_min(s_centre_position) assert (x0 - 0.5) <= x and x <= (x0 + 0.5) assert (x0 - 0.5) <= x and x <= (x0 + 0.5)
def test_disk_cover_all(self): test = mdtd.MakeTestData(size_x=120, size_y=100, default=False, blur=False, downscale=False) test.add_disk_ellipse(x0=50, y0=50, semi_len0=100, semi_len1=130, intensity=2) assert (test.signal.data == 2).all()
def test_get_optimal_centre_position_off_centre_non_square(self): test_data = mdtd.MakeTestData(size_x=300, size_y=400, default=False, blur=True, downscale=False) x0, y0 = 150, 170 test_data.add_ring(x0=x0, y0=y0, r=100, intensity=10, lw_pix=1) s = test_data.signal s.axes_manager[0].offset = -x0 - 2 s.axes_manager[1].offset = -y0 - 3 s_c = ra.get_optimal_centre_position( test_data.signal, radial_signal_span=(90, 110), steps=3, step_size=1.0, angleN=8, ) min_pos0 = ra.get_coordinate_of_min(s_c) s.axes_manager[0].offset = -x0 + 2 s.axes_manager[1].offset = -y0 - 3 s_c = ra.get_optimal_centre_position( test_data.signal, radial_signal_span=(90, 110), steps=3, step_size=1.0, angleN=8, ) min_pos1 = ra.get_coordinate_of_min(s_c) s.axes_manager[0].offset = -x0 - 2 s.axes_manager[1].offset = -y0 + 3 s_c = ra.get_optimal_centre_position( test_data.signal, radial_signal_span=(90, 110), steps=3, step_size=1.0, angleN=8, ) min_pos2 = ra.get_coordinate_of_min(s_c) s.axes_manager[0].offset = -x0 + 2 s.axes_manager[1].offset = -y0 + 3 s_c = ra.get_optimal_centre_position( test_data.signal, radial_signal_span=(90, 110), steps=3, step_size=1.0, angleN=8, ) min_pos3 = ra.get_coordinate_of_min(s_c) for min_pos in [min_pos0, min_pos1, min_pos2, min_pos3]: assert approx(min_pos[0]) == x0 assert approx(min_pos[1]) == y0
def test_disk_cover_nothing(self): test = mdtd.MakeTestData(size_x=120, size_y=100, default=False, blur=False, downscale=False) test.add_disk_ellipse(x0=350, y0=50, semi_len0=10, semi_len1=10, intensity=2) assert not test.signal.data.any()
def test_blur(self): test0 = mdtd.MakeTestData(size_x=120, size_y=100, downscale=False, blur=True, default=False) test0.add_ring_ellipse(x0=50, y0=50, semi_len0=8, semi_len1=13, intensity=2) test1 = mdtd.MakeTestData(size_x=120, size_y=100, downscale=False, blur=False, default=False) test1.add_ring_ellipse(x0=50, y0=50, semi_len0=8, semi_len1=13, intensity=2) assert not (test0.signal.data == test1.signal.data).all()
def test_ring_rectangle_image(self): intensity = 10 test = mdtd.MakeTestData(size_x=100, size_y=50, default=False, blur=False, downscale=False) test.add_ring(x0=50, y0=25, r=1, intensity=intensity, lw_pix=0) assert (test.signal.isig[50, 25].data == 0.0).all() test.signal.data[25, 50] = intensity assert (test.signal.isig[49:52, 24:27].data == intensity).all() test.signal.data[24:27, 49:52] = 0.0 assert not test.signal.data.any()
def test_ring_radius2(self): intensity = 10 test = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=False, downscale=False) test.add_ring(x0=50, y0=50, r=2, intensity=intensity, lw_pix=0) assert (test.signal.isig[49:52, 49:52].data == 0.0).all() test.signal.data[49:52, 49:52] = intensity assert (test.signal.isig[48:53, 48:53].data == intensity).all() test.signal.data[48:53, 48:53] = 0.0 assert not test.signal.data.any()
def test_ring_outer_radius(self): r, lw = 20, 1 x0, y0, scale = 0, 0, 1 ring_1 = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=False, downscale=False) ring_1.add_ring(x0=x0, y0=y0, r=r, intensity=10, lw_pix=lw) s_h0 = ring_1.signal.isig[x0, :] s_h0_edge = s_h0.axes_manager[0].index2value(s_h0.data[::-1].argmax()) r_h0 = s_h0.data.size - s_h0_edge - 0.5 * scale r_out = r + lw + 0.5 * scale assert r_h0 == r_out s_h1 = ring_1.signal.isig[:, y0] s_h1_edge = s_h1.axes_manager[0].index2value(s_h1.data[::-1].argmax()) r_h1 = s_h1.data.size - s_h1_edge - 0.5 * scale assert r_h1 == r_out r, lw = 20, 1 x0, y0, scale = 100, 100, 1 ring_2 = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=False, downscale=False) ring_2.add_ring(x0=x0, y0=y0, r=r, intensity=10, lw_pix=lw) s_h2 = ring_2.signal.isig[-1, :] s_h2_edge = s_h2.axes_manager[0].index2value(s_h2.data.argmax()) r_h2 = 100 - (s_h2_edge - 0.5 * scale) r_out = r + lw + 0.5 * scale assert r_h2 == r_out s_h3 = ring_2.signal.isig[:, -1] s_h3_edge = s_h3.axes_manager[0].index2value(s_h3.data.argmax()) r_h3 = 100 - (s_h3_edge - 0.5 * scale) assert r_h3 == r_out
def test_determine_ellipse(self, rot): test_data = mdtd.MakeTestData(200, 200, default=False) test_data.add_disk(x0=100, y0=100, r=5, intensity=30) test_data.add_ring_ellipse(x0=100, y0=100, semi_len0=63, semi_len1=70, rotation=rot) s = test_data.signal s.set_signal_type("electron_diffraction") mask = np.zeros_like(s.data, dtype=bool) mask[100 - 20:100 + 20, 100 - 20:100 + 20] = True center, affine = ret.determine_ellipse(s, mask=mask, use_ransac=False) s.unit = "k_nm^-1" s.beam_energy = 200 s.axes_manager.signal_axes[0].scale = 0.1 s.axes_manager.signal_axes[1].scale = 0.1 s.set_ai(center=center, affine=affine) s_az = s.get_azimuthal_integral2d(npt=100) assert (np.sum((s_az.sum(axis=0).isig[10:] > 1).data) < 10)
def test_disk_semi_len1_very_small(self): test = mdtd.MakeTestData(size_x=120, size_y=100, default=False, blur=False, downscale=False) x0, y0 = 50, 40 intensity = 2 test.add_disk_ellipse(x0=x0, y0=y0, semi_len0=1000, semi_len1=0.1, intensity=intensity, rotation=0) data = test.signal.data assert (data[y0, :] == intensity).all() data[y0, :] = 0 assert not data.any()
def test_get_radius_vs_angle(self): test_data = mdtd.MakeTestData( 101, 121, blur=False, downscale=False, default=False ) x, y, r0, r1 = 51, 55, 21, 35 test_data.add_ring(x, y, r0) test_data.add_ring(x, y, r1) s = test_data.signal s.axes_manager.signal_axes[0].offset = -x s.axes_manager.signal_axes[1].offset = -y s_ar0 = ra.get_radius_vs_angle( s, radial_signal_span=(15, 25), show_progressbar=False ) s_ar1 = ra.get_radius_vs_angle( s, radial_signal_span=(28, 40), show_progressbar=False ) np.testing.assert_allclose(s_ar0.data, r0, 3) np.testing.assert_allclose(s_ar1.data, r1, 3)
def test_ring_position_downscale_and_blur(self): x0, y0, r = 50, 50, 18 test = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=True, downscale=True) test.add_ring(x0=x0, y0=y0, r=r, intensity=20, lw_pix=1) s_h0 = test.signal.isig[x0, 0:y0] max_h0 = s_h0.axes_manager[0].index2value(s_h0.data.argmax()) assert max_h0 == y0 - r s_h1 = test.signal.isig[x0, y0:] max_h1 = s_h1.axes_manager[0].index2value(s_h1.data.argmax()) assert max_h1 == y0 + r s_v0 = test.signal.isig[0:x0, y0] max_v0 = s_v0.axes_manager[0].index2value(s_v0.data.argmax()) assert max_v0 == x0 - r s_v1 = test.signal.isig[x0:, y0] max_v1 = s_v1.axes_manager[0].index2value(s_v1.data.argmax()) assert max_v1 == x0 + r
def test_repr(self): test = mdtd.MakeTestData(size_x=250) repr_string = test.__repr__() assert str(250) in repr_string
def test_repr(self): test = mdtd.MakeTestData() test.add_ring_ellipse(x0=70) repr_string = test.z_list[0].__repr__() assert str(70) in repr_string
def test_different_signal_size(self): s0 = mdtd.MakeTestData(100, 100).signal s1 = mdtd.MakeTestData(100, 150).signal with pytest.raises(ValueError): ra.get_angle_image_comparison(s0, s1)
def test_simple_disks(self): test0 = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=False, downscale=False) test0.add_disk(50, 50, 1, 20) assert (test0.signal.isig[49:52, 49:52].data == 20).all() test0.signal.data[49:52, 49:52] = 0 assert not test0.signal.data.any() test1 = mdtd.MakeTestData(size_x=100, size_y=100, default=False, blur=False, downscale=False) test1.add_disk(20, 40, 2, 30) assert (test1.signal.isig[18:23, 38:43].data == 30).all() test1.signal.data[38:43, 18:23] = 0 assert not test1.signal.data.any() test2 = mdtd.MakeTestData(size_x=80, size_y=120, default=False, blur=False, downscale=False) test2.add_disk(50, 40, 1, 10) test2.add_disk(20, 30, 2, 30) assert (test2.signal.isig[49:52, 39:42].data == 10).all() assert (test2.signal.isig[19:22, 29:32].data == 30).all() test2.signal.data[39:42, 49:52] = 0 test2.signal.data[28:33, 18:23] = 0 assert not test2.signal.data.any() test3 = mdtd.MakeTestData(size_x=150, size_y=50, default=False, blur=False, downscale=False) test3.add_disk(200, 400, 2, 30) assert not test3.signal.data.any() test4 = mdtd.MakeTestData(size_x=150, size_y=50, default=False, blur=False, downscale=False) test4.add_disk(50, 50, 500, 500) assert (test4.signal.data == 500).all() test5 = mdtd.MakeTestData(size_x=100, size_y=200, default=False, blur=False, downscale=True) test5.add_disk(50, 50, 500, 10) assert (test5.signal.data == 10).all() test6 = mdtd.MakeTestData(size_x=100, size_y=200, default=False, blur=True, downscale=False) test6.add_disk(50, 30, 500, 10) test6_ref = np.full_like(test6.signal.data, 10.0) np.testing.assert_allclose(test6.signal.data, test6_ref, rtol=1e-05)