def test_correct_ramp_x_y(self): array_x, array_y = np.meshgrid(range(64), range(64)) data_x = np.swapaxes(np.dstack((array_x, array_x)), 0, 2).astype("float64") data_y = np.swapaxes(np.dstack((array_y, array_y)), 0, 2).astype("float64") s_x = DPCSignal2D(data_x) s_y = DPCSignal2D(data_y) s_x_corr = s_x.correct_ramp(corner_size=0.05) s_y_corr = s_y.correct_ramp(corner_size=0.05) assert_allclose(s_x_corr.data, np.zeros_like(data_x), atol=1e-6) assert_allclose(s_y_corr.data, np.zeros_like(data_y), atol=1e-6) data_xy = np.swapaxes(np.dstack((array_x, array_y)), 0, 2).astype("float64") data_yx = np.swapaxes(np.dstack((array_y, array_x)), 0, 2).astype("float64") s_xy = DPCSignal2D(data_xy) s_yx = DPCSignal2D(data_yx) s_xy_corr = s_xy.correct_ramp(corner_size=0.05) s_yx_corr = s_yx.correct_ramp(corner_size=0.05) assert_allclose(s_xy_corr.data, np.zeros_like(data_xy), atol=1e-6) assert_allclose(s_yx_corr.data, np.zeros_like(data_yx), atol=1e-6) data_tilt = np.swapaxes( np.dstack((array_x + array_y, np.fliplr(array_x) + array_y)), 0, 2).astype("float64") s_tilt = DPCSignal2D(data_tilt) s_tilt_corr = s_tilt.correct_ramp() assert_allclose(s_tilt_corr.data, np.zeros_like(data_tilt), atol=1e-6) s_tilt.correct_ramp(out=s_tilt) assert_allclose(s_tilt.data, np.zeros_like(data_tilt), atol=1e-6)
def setup_method(self): # construct the surface, two point with Gaussian distribution coords = np.linspace(-20, 10, num=512) x, y = np.meshgrid(coords, coords) surface = np.exp(-(x ** 2 + y ** 2) / 2) + np.exp( -((x - 2) ** 2 + (y + 4) ** 2) / 2 ) # x and y phase gradient of the Gaussians, analytical form dx = x * (-np.exp(-(x ** 2) / 2 - y ** 2 / 2)) + (x - 2) * -np.exp( (-0.5 * (x - 2) ** 2 - 0.5 * (y + 4) ** 2) ) dy = y * (-np.exp(-(x ** 2) / 2 - y ** 2 / 2)) + (y + 4) * -np.exp( (-0.5 * (x - 2) ** 2 - 0.5 * (y + 4) ** 2) ) data = np.empty((2, 512, 512)) data[0] = dx data[1] = dy self.s = DPCSignal2D(data) self.s.axes_manager.signal_axes[0].axis = coords self.s.axes_manager.signal_axes[1].axis = coords # normalise for comparison later surface -= surface.mean() surface /= surface.std() self.surface = surface
def test_counterclockwise_45_degrees(self): s = DPCSignal2D((np.ones((90, 70)), np.zeros((90, 70)))) sin_rad = np.sin(np.deg2rad(45)) s_rot = s.rotate_beam_shifts(-45) data_x, data_y = s_rot.inav[0].data, s_rot.inav[1].data assert_almost_equal(data_x, np.ones_like(data_x) * sin_rad) assert_almost_equal(data_y, -np.ones_like(data_y) * sin_rad)
def setup_method(self): data = np.zeros((2, 100, 50)) for i in range(10, 90, 20): data[0, i:i + 10, 10:40] = 1.1 data[0, i + 10:i + 20, 10:40] = -1 self.s = DPCSignal2D(data) self.s_shape = self.s.axes_manager.signal_shape
def test_retain_metadata(self): s = DPCSignal2D(np.ones((2, 10, 5))) s.metadata.General.title = "test_data" filename = os.path.join(self.tmpdir.name, "test_metadata.hspy") s.save(filename) s_load = hs.load(filename) assert s_load.metadata.General.title == "test_data"
def test_get_bivariate_histogram(self): array_x, array_y = np.meshgrid(range(64), range(64)) data_tilt = np.swapaxes( np.dstack((array_x + array_y, np.fliplr(array_x) + array_y)), 0, 2).astype("float64") data_random = data_tilt + np.random.random(size=(2, 64, 64)) * 10 s_random = DPCSignal2D(data_random) s_random.get_bivariate_histogram()
def test_correct_ramp_flat(self): data0 = np.ones(shape=(2, 64, 64)) s0 = DPCSignal2D(data0) s0_corr = s0.correct_ramp(corner_size=0.05) assert (s0.data == data0).all() assert_allclose(s0_corr.data, np.zeros_like(data0), atol=1e-8) s0.correct_ramp(corner_size=0.05, out=s0) assert_allclose(s0.data, np.zeros_like(data0), atol=1e-8)
def test_get_color_signal(self): array_x, array_y = np.meshgrid(range(64), range(64)) data_tilt = np.swapaxes( np.dstack((array_x + array_y, np.fliplr(array_x) + array_y)), 0, 2).astype("float64") data_random = data_tilt + np.random.random(size=(2, 64, 64)) * 10 s_random = DPCSignal2D(data_random) s_random.get_color_signal() s_random.get_color_signal(rotation=45)
def test_correct_ramp_one_large_value(self): array_x, array_y = np.meshgrid(range(64), range(64)) data = np.swapaxes( np.dstack((array_x + array_y, np.fliplr(array_x) + array_y)), 0, 2).astype("float64") data[:, 20:30, 30:40] += 1000 s = DPCSignal2D(data) s_corr = s.correct_ramp() s_corr.data[:, 20:30, 30:40] -= 1000 assert_allclose(s_corr.data, np.zeros_like(data), atol=1e-8)
def test_load_signal2d(self): s = DPCSignal2D(np.ones((2, 10, 20))) assert s.axes_manager.signal_dimension == 2 assert s.axes_manager.navigation_dimension == 1 filename = os.path.join(self.tmpdir.name, "dpcsignal2d.hspy") s.save(filename) s_load = hs.load(filename) assert s.__class__ == s_load.__class__ assert s_load.axes_manager.signal_dimension == 2 assert s_load.axes_manager.navigation_dimension == 1
def test_random_negative_values(self): s = DPCSignal2D(np.zeros((2, 1000, 1000))) s.data[:, :300, :300] = np.random.random((300, 300)) * 10 - 5 s.data[:, :300, -300:] = np.random.random((300, 300)) * 10 - 5 s.data[:, -300:, :300] = np.random.random((300, 300)) * 10 - 5 s.data[:, -300:, -300:] = np.random.random((300, 300)) * 10 - 5 s_corr = s.correct_ramp(corner_size=0.3) assert approx(s_corr.data[:, 300:-300, :].mean(), abs=0.1) == 0.0 assert approx(s_corr.data[:, :, 300:-300].mean(), abs=0.1) == 0.0
def get_simple_dpc_signal(): """Get a simple DPCSignal2D with a zero point in the centre. Example ------- >>> s = pxm.dummy_data.get_simple_dpc_signal() """ data = np.mgrid[-5:5:100j, -5:5:100j] s = DPCSignal2D(data) return s
def test_correct_ramp_random(self): array_x, array_y = np.meshgrid(range(64), range(64)) data_tilt = np.swapaxes( np.dstack((array_x + array_y, np.fliplr(array_x) + array_y)), 0, 2 ).astype("float64") data_random = data_tilt + np.random.random(size=(2, 64, 64)) * 10 s_random = DPCSignal2D(data_random) s_random_corr = s_random.correct_ramp() assert_allclose(s_random_corr.data, np.zeros_like(data_random), atol=10) s_random.correct_ramp(out=s_random) assert_allclose(s_random.data, np.zeros_like(data_random), atol=10)
def test_correct_ramp_only_offset(self): data = np.ones((2, 50, 50)) data[1, :, :] = -3 s = DPCSignal2D(np.zeros((2, 50, 50))) s_corr1 = s.correct_ramp(only_offset=True) assert_allclose(s_corr1.data, np.zeros_like(data), atol=1e-8) data_ramp = np.mgrid[-5:5:50j, -5:5:50j] s.data += data_ramp s_corr2 = s.correct_ramp(only_offset=True) assert_allclose(s_corr2.data, data_ramp, atol=1e-8)
def test_get_color_image_with_indicator(self): s = DPCSignal2D(np.random.random(size=(2, 100, 100))) s.get_color_image_with_indicator() s.get_color_image_with_indicator( phase_rotation=45, indicator_rotation=10, autolim=True, autolim_sigma=1, scalebar_size=10, ) s.get_color_image_with_indicator(only_phase=True) fig, ax = subplots() s.get_color_image_with_indicator(ax=ax)
def get_square_dpc_signal(add_ramp=False): """Get a 2D DPC signal resembling a Landau domain. Parameters ---------- add_ramp : bool, default False If True, will add a ramp in the beam shift across the image to emulate the effects of d-scan. Returns ------- square_dpc_signal : DPCSignal2D Examples -------- >>> s = pxm.dummy_data.get_square_dpc_signal() >>> s.plot() Adding a ramp >>> s = pxm.dummy_data.get_square_dpc_signal(add_ramp=True) >>> s.plot() """ imX, imY = 300, 300 data_y, data_x = np.random.normal(loc=0.1, size=(2, imY, imX)) x, y = np.mgrid[-150 : 150 : imY * 1j, -150 : 150 : imX * 1j] t = np.arctan2(x, y) % (2 * np.pi) mask_xy = (x < 100) * (x > -100) * (y < 100) * (y > -100) mask0 = (t > np.pi / 4) * (t < 3 * np.pi / 4) * mask_xy mask1 = (t > 3 * np.pi / 4) * (t < 5 * np.pi / 4) * mask_xy mask2 = (t > 5 * np.pi / 4) * (t < 7 * np.pi / 4) * mask_xy mask3 = ((t > 7 * np.pi / 4) + (t < np.pi / 4)) * mask_xy data_y[mask0] -= np.random.normal(loc=3, scale=0.1, size=(imY, imX))[mask0] data_x[mask1] -= np.random.normal(loc=3, scale=0.1, size=(imY, imX))[mask1] data_y[mask2] += np.random.normal(loc=3, scale=0.1, size=(imY, imX))[mask2] data_x[mask3] += np.random.normal(loc=3, scale=0.1, size=(imY, imX))[mask3] if add_ramp: ramp_y, ramp_x = np.mgrid[18 : 28 : imY * 1j, -5.3 : 1.2 : imX * 1j] data_x += ramp_x data_y += ramp_y s = DPCSignal2D((data_y, data_x)) s.axes_manager.signal_axes[0].name = "Probe position x" s.axes_manager.signal_axes[1].name = "Probe position y" s.axes_manager.signal_axes[0].units = "nm" s.axes_manager.signal_axes[1].units = "nm" return s
def test_retain_axes_manager(self): s = DPCSignal2D(np.ones((2, 10, 5))) s_sa0 = s.axes_manager.signal_axes[0] s_sa1 = s.axes_manager.signal_axes[1] s_sa0.offset, s_sa1.offset, s_sa0.scale, s_sa1.scale = 20, 10, 0.2, 0.3 s_sa0.units, s_sa1.units, s_sa0.name, s_sa1.name = "a", "b", "e", "f" filename = os.path.join(self.tmpdir.name, "test_axes_manager.hspy") s.save(filename) s_load = hs.load(filename) assert s_load.axes_manager[1].offset == 20 assert s_load.axes_manager[2].offset == 10 assert s_load.axes_manager[1].scale == 0.2 assert s_load.axes_manager[2].scale == 0.3 assert s_load.axes_manager[1].units == "a" assert s_load.axes_manager[2].units == "b" assert s_load.axes_manager[1].name == "e" assert s_load.axes_manager[2].name == "f"
def get_stripe_pattern_dpc_signal(): """Get a 2D DPC signal with a stripe pattern. The stripe pattern only has an x-component, with alternating left/right directions. There is a small a net moment in the positive x-direction (leftwards). Returns ------- stripe_dpc_signal : DPCSignal2D Example ------- >>> s = pxm.dummy_data.get_stripe_pattern_dpc_signal() """ data = np.zeros((2, 100, 50)) for i in range(10, 90, 20): data[0, i : i + 10, 10:40] = 1.1 data[0, i + 10 : i + 20, 10:40] = -1 s = DPCSignal2D(data) return s
def test_large_values_not_in_corners(self): s = DPCSignal2D(np.zeros((2, 100, 100))) s.inav[0].data[:5, :5], s.inav[0].data[:5, -5:] = 10, 10 s.inav[0].data[-5:, :5], s.inav[0].data[-5:, -5:] = 10, 10 s.inav[1].data[:5, :5], s.inav[1].data[:5, -5:] = 30, 30 s.inav[1].data[-5:, :5], s.inav[1].data[-5:, -5:] = 30, 30 cross_array = np.zeros((100, 100)) cross_array[30:70, :], cross_array[:, 30:70] = 1000, 1000 s.data = s.data + cross_array s1 = s.correct_ramp(corner_size=0.05) s1.data = s1.data - cross_array assert_allclose(s1.inav[0].data[5:95, :], np.ones((90, 100)) * -10) assert_allclose(s1.inav[0].data[:, 5:95], np.ones((100, 90)) * -10) assert_allclose(s1.inav[0].data[5:95, :], np.ones((90, 100)) * -10) assert_allclose(s1.inav[1].data[5:95, :], np.ones((90, 100)) * -30) assert_allclose(s1.inav[1].data[:, 5:95], np.ones((100, 90)) * -30) assert_allclose(s1.inav[1].data[5:95, :], np.ones((90, 100)) * -30) assert_allclose(s1.isig[:5, :5].data, np.zeros((2, 5, 5)), atol=1e-7) assert_allclose(s1.isig[-5:, :5].data, np.zeros((2, 5, 5)), atol=1e-7) assert_allclose(s1.isig[:5, -5:].data, np.zeros((2, 5, 5)), atol=1e-7) assert_allclose(s1.isig[-5:, -5:].data, np.zeros((2, 5, 5)), atol=1e-7)
def setup_method(self): data = np.zeros((2, 25, 50)) data[0, 10, 5] = 10 data[1, 20, 15] = -10 self.s = DPCSignal2D(data)
def test_180_degrees(self): s = DPCSignal2D((np.ones((90, 70)), np.zeros((90, 70)))) s_rot = s.rotate_beam_shifts(180) data_x, data_y = s_rot.inav[0].data, s_rot.inav[1].data assert_almost_equal(data_x, -np.ones_like(data_x)) assert_almost_equal(data_y, np.zeros_like(data_y))
def test_get_phase_signal(self): s = DPCSignal2D(np.zeros((2, 100, 100))) s.get_phase_signal() s.get_phase_signal(rotation=45)
def test_get_magnitude_signal_zeros(self): s = DPCSignal2D(np.zeros((2, 100, 100))) s_magnitude = s.get_magnitude_signal() assert (s_magnitude.data == 0).all()
def test_cropped_dpcsignal(self): s = DPCSignal2D(np.random.random((2, 200, 200))) s_crop = s.isig[50:150, 50:150] s_crop.correct_ramp()
def test_create(self): data = np.ones(shape=(2, 10, 10)) DPCSignal2D(data) with pytest.raises(ValueError): DPCSignal2D(np.zeros(10))
def test_get_color_signal_zeros(self): s = DPCSignal2D(np.zeros((2, 100, 100))) s_color = s.get_color_signal() assert (s_color.data["R"] == 0).all() assert (s_color.data["G"] == 0).all() assert (s_color.data["B"] == 0).all()