def test_add_image_noise_random_seed(self): test_data0 = tt.MakeTestData(100, 100) test_data0.add_image_noise(random_seed=0) s0 = test_data0.signal test_data1 = tt.MakeTestData(100, 100) test_data1.add_image_noise(random_seed=0) s1 = test_data1.signal assert (s0.data == s1.data).all()
def test_sigma_quantile(self): testdata0 = tt.MakeTestData(500, 500, sigma_quantile=5) testdata1 = tt.MakeTestData(500, 500, sigma_quantile=1) x, y = np.mgrid[0:490:30, 0:490:30] x, y = x.flatten(), y.flatten() testdata0.add_atom_list(x, y) testdata1.add_atom_list(x, y) assert testdata0.signal.data.sum() > testdata1.signal.data.sum()
def test_add_row_scan_distortion(self): x, y = np.mgrid[10:90:10, 10:90:10] x, y = x.flatten(), y.flatten() td = tt.MakeTestData(100, 100) td.add_atom_list(x=x, y=y, sigma_x=2, sigma_y=2) s0 = td.signal td = tt.MakeTestData(100, 100, add_row_scan_distortion=1) td.add_atom_list(x=x, y=y, sigma_x=2, sigma_y=2) s1 = td.signal assert not np.all(s0.data == s1.data)
def setup_method(self): test_data = tt.MakeTestData(520, 520) x, y = np.mgrid[10:510:8j, 10:510:8j] x, y = x.flatten(), y.flatten() test_data.add_atom_list(x, y) sublattice = test_data.sublattice self.sublattice = sublattice
def test_gaussian_list(self): x, y = np.mgrid[10:90:10, 10:90:10] x, y = x.flatten(), y.flatten() sx = np.random.random_sample(size=len(x)) sy = np.random.random_sample(size=len(x)) A = np.random.random_sample(size=len(x)) r = np.random.random_sample(size=len(x)) td = tt.MakeTestData(100, 100) td.add_atom_list(x=x, y=y, sigma_x=sx, sigma_y=sy, amplitude=A, rotation=r) gaussian_list = td.gaussian_list assert len(gaussian_list) == len(x) iterator = zip(x, y, sx, sy, A, r, gaussian_list) for tx, ty, tsx, tsy, tA, tr, gaussian in iterator: assert gaussian.centre_x.value == tx assert gaussian.centre_y.value == ty assert gaussian.sigma_x.value == tsx assert gaussian.sigma_y.value == tsy assert gaussian.A.value == tA assert gaussian.rotation.value == tr
def test_get_signal(self): imX0, imY0 = 100, 100 test_data0 = tt.MakeTestData(imX0, imY0) assert (imX0, imY0) == test_data0.data_extent assert test_data0.signal.axes_manager.shape == (imX0, imY0) assert not test_data0.signal.data.any() imX1, imY1 = 100, 39 test_data1 = tt.MakeTestData(imX1, imY1) assert (imX1, imY1) == test_data1.data_extent assert test_data1.signal.axes_manager.shape == (imX1, imY1) imX2, imY2 = 34, 65 test_data2 = tt.MakeTestData(imX2, imY2) assert (imX2, imY2) == test_data2.data_extent assert test_data2.signal.axes_manager.shape == (imX2, imY2)
def test_add_atom_list_all_lists(self): x, y = np.mgrid[10:90:10, 10:90:10] x, y = x.flatten(), y.flatten() sx = np.random.random_sample(size=len(x)) sy = np.random.random_sample(size=len(x)) A = np.random.random_sample(size=len(x)) r = np.random.random_sample(size=len(x)) td = tt.MakeTestData(100, 100) td.add_atom_list(x=x, y=y, sigma_x=sx, sigma_y=sy, amplitude=A, rotation=r) atom_list = td.sublattice.atom_list assert len(atom_list) == len(x) iterator = zip(x, y, sx, sy, A, r, atom_list) for tx, ty, tsx, tsy, tA, tr, atom in iterator: assert atom.pixel_x == tx assert atom.pixel_y == ty assert atom.sigma_x == tsx assert atom.sigma_y == tsy assert atom.amplitude_gaussian == tA assert atom.rotation == tr
def test_sublattice_generate_image(self): testdata = tt.MakeTestData(1000, 1000, sublattice_generate_image=False) x, y = np.mgrid[0:1000:150j, 0:1000:150j] x, y = x.flatten(), y.flatten() testdata.add_atom_list(x, y) sublattice = testdata.sublattice assert (sublattice.image == 0).all() assert len(sublattice.atom_list) == 150 * 150
def test_center_of_mass_mask_radius(self): x, y, sx, sy = 15, 20, 2, 2 test_data = tt.MakeTestData(50, 50) test_data.add_atom(x, y, sx, sy) sublattice = test_data.sublattice atom = sublattice.atom_list[0] image_data = test_data.signal.data atom.refine_position_using_center_of_mass(image_data, mask_radius=5) assert atom.pixel_x == approx(x) assert atom.pixel_y == approx(y)
def test_2d_gaussian_mask_radius(self): x, y, sx, sy = 15, 20, 2, 2 test_data = tt.MakeTestData(50, 50) test_data.add_atom(x, y, sx, sy) sublattice = test_data.sublattice atom = sublattice.atom_list[0] image_data = test_data.signal.data atom.refine_position_using_2d_gaussian(image_data, mask_radius=10) assert atom.pixel_x == approx(x) assert atom.pixel_y == approx(y) assert atom.sigma_x == approx(sx, rel=1e-4) assert atom.sigma_y == approx(sy, rel=1e-4)
def test_add_atom(self): x, y, sx, sy, A, r = 10, 5, 5, 9, 10, 2 td = tt.MakeTestData(50, 50) td.add_atom(x, y, sigma_x=sx, sigma_y=sy, amplitude=A, rotation=r) assert len(td.sublattice.atom_list) == 1 atom = td.sublattice.atom_list[0] assert atom.pixel_x == x assert atom.pixel_y == y assert atom.sigma_x == sx assert atom.sigma_y == sy assert atom.amplitude_gaussian == A assert atom.rotation == r
def test_add_image_noise(self): test_data0 = tt.MakeTestData(1000, 1000) mu0, sigma0 = 0, 0.005 test_data0.add_image_noise(mu=mu0, sigma=sigma0, only_positive=False) s0 = test_data0.signal assert approx(s0.data.mean(), abs=1e-5) == mu0 assert approx(s0.data.std(), abs=1e-2) == sigma0 test_data1 = tt.MakeTestData(1000, 1000) mu1, sigma1 = 10, 0.5 test_data1.add_image_noise(mu=mu1, sigma=sigma1, only_positive=False) s1 = test_data1.signal assert approx(s1.data.mean(), rel=1e-4) == mu1 assert approx(s1.data.std(), abs=1e-2) == sigma1 test_data2 = tt.MakeTestData(1000, 1000) mu2, sigma2 = 154.2, 1.98 test_data2.add_image_noise(mu=mu2, sigma=sigma2, only_positive=False) s2 = test_data2.signal assert approx(s2.data.mean(), rel=1e-4) == mu2 assert approx(s2.data.std(), rel=1e-2) == sigma2
def test_two_atoms(self): test_data = tt.MakeTestData(50, 100, sigma_quantile=8) x, y, A = [25, 25], [25, 75], [5, 10] test_data.add_atom_list(x=x, y=y, amplitude=A) s = test_data.signal i_points, i_record, p_record = integrate(s, x, y, max_radius=500) assert approx(i_points) == A assert i_record.axes_manager.signal_shape == (50, 100) assert (i_record.isig[:, :51].data == i_points[0]).all() assert (i_record.isig[:, 51:].data == i_points[1]).all() assert (p_record[:51] == 0).all() assert (p_record[51:] == 1).all()
def test_one_atom_center_of_mass(self): test_data = tt.MakeTestData(20, 20) x, y = 10, 15 test_data.add_atom(x, y) sublattice = test_data.sublattice atom = sublattice.atom_list[0] atom.pixel_x += 1 atom.refine_position = False sublattice.refine_atom_positions_using_center_of_mass(mask_radius=5) assert atom.pixel_x == x + 1 assert atom.pixel_y == y atom.refine_position = True sublattice.refine_atom_positions_using_center_of_mass(mask_radius=5) assert approx(atom.pixel_x, abs=0.001) == x assert approx(atom.pixel_y, abs=0.001) == y
def test_one_atom_2d_gaussian_refine(self): test_data = tt.MakeTestData(20, 20) x, y = 10, 15 test_data.add_atom(x, y) sublattice = test_data.sublattice atom = sublattice.atom_list[0] atom.pixel_x += 1 atom.refine_position = False sublattice.refine_atom_positions_using_2d_gaussian(mask_radius=5) assert atom.pixel_x == x + 1 assert atom.pixel_y == y atom.refine_position = True sublattice.refine_atom_positions_using_2d_gaussian(mask_radius=5) assert approx(atom.pixel_x) == x assert approx(atom.pixel_y) == y
def test_calculate_max_intensity(self): test_data = tt.MakeTestData(110, 110) x, y = np.mgrid[5:105:5j, 5:105:5j] x, y = x.flatten(), y.flatten() A = [1] * len(x) test_data.add_atom_list(x=x, y=y, amplitude=A) sublattice = test_data.sublattice sublattice.find_nearest_neighbors() sublattice.image /= sublattice.image.max() max_intensities = [] for atom in sublattice.atom_list: max_intensities.append( atom.calculate_max_intensity(sublattice.image)) assert approx(max_intensities) == A
def test_four_atoms(self): test_data = tt.MakeTestData(60, 100, sigma_quantile=8) x, y, A = [20, 20, 40, 40], [25, 75, 25, 75], [5, 10, 15, 20] test_data.add_atom_list(x=x, y=y, amplitude=A) s = test_data.signal i_points, i_record, p_record = integrate(s, x, y, max_radius=500) assert approx(i_points) == A assert (i_record.isig[:31, :51].data == i_points[0]).all() assert (i_record.isig[:31, 51:].data == i_points[1]).all() assert (i_record.isig[31:, :51].data == i_points[2]).all() assert (i_record.isig[31:, 51:].data == i_points[3]).all() assert (p_record[:51, :31] == 0).all() assert (p_record[51:, :31] == 1).all() assert (p_record[:51, 31:] == 2).all() assert (p_record[51:, 31:] == 3).all()
def test_max_radius_1(self): test_data = tt.MakeTestData(60, 100) x, y, A = [30, 30], [25, 75], [5, 10] test_data.add_atom_list(x=x, y=y, amplitude=A, sigma_x=0.1, sigma_y=0.1) s = test_data.signal i_points, i_record, p_record = integrate(s, x, y, max_radius=1) assert (i_points[1] / i_points[0]) == 2. assert i_record.data[y[0], x[0]] == i_points[0] assert i_record.data[y[1], x[1]] == i_points[1] i_record.data[y[0], x[0]] = 0 i_record.data[y[1], x[1]] = 0 assert not i_record.data.any()
def test_add_atom_list_simple(self): x, y = np.mgrid[10:90:10, 10:90:10] x, y = x.flatten(), y.flatten() sx, sy, A, r = 2.1, 1.3, 9.5, 1.4 td = tt.MakeTestData(100, 100) td.add_atom_list(x=x, y=y, sigma_x=sx, sigma_y=sy, amplitude=A, rotation=r) atom_list = td.sublattice.atom_list assert len(atom_list) == len(x) for tx, ty, atom in zip(x, y, atom_list): assert atom.pixel_x == tx assert atom.pixel_y == ty assert atom.sigma_x == sx assert atom.sigma_y == sy assert atom.amplitude_gaussian == A assert atom.rotation == r
def setup_method(self): self.tdata = tt.MakeTestData(200, 200) for i in range(4): x, y = np.mgrid[60 * i:(i + 1) * 60:15, 10:200:15] x, y = x.flatten(), y.flatten() self.tdata.add_atom_list(x, y, sigma_x=2, sigma_y=2, amplitude=(i + 1) * 20, rotation=0.4) self.tdata.add_image_noise(sigma=0.02) atom_positions = atom_finding.get_atom_positions(self.tdata.signal, 8, threshold_rel=0.1) self.sublattice = Sublattice(atom_positions, self.tdata.signal.data) self.sublattice.construct_zone_axes() self.sublattice.refine_atom_positions_using_2d_gaussian( self.sublattice.image)
def test_add_atom_list_wrong_input(self): x, y = np.mgrid[10:90:10, 10:90:10] x, y = x.flatten(), y.flatten() td = tt.MakeTestData(100, 100) with pytest.raises(ValueError): td.add_atom_list(x, y[10:]) sx = np.arange(10) with pytest.raises(ValueError): td.add_atom_list(x, y, sigma_x=sx) sy = np.arange(20) with pytest.raises(ValueError): td.add_atom_list(x, y, sigma_y=sy) A = np.arange(30) with pytest.raises(ValueError): td.add_atom_list(x, y, amplitude=A) r = np.arange(5) with pytest.raises(ValueError): td.add_atom_list(x, y, rotation=r)
def test_with_fitting(self): x_pos, y_pos = [[10, 10], [10, 20]] delta_pos = 1 test_data = tt.MakeTestData(30, 30) test_data.add_atom_list(x_pos, y_pos) sublattice = test_data.sublattice atom0, atom1 = sublattice.atom_list atom0.pixel_x += delta_pos atom0.pixel_y += delta_pos atom1.pixel_x += delta_pos atom1.pixel_y += delta_pos sublattice.toggle_atom_refine_position_with_gui() fig = plt.figure(1) x0, y0 = fig.axes[0].transData.transform( (x_pos[0] + delta_pos, y_pos[0] + delta_pos)) fig.canvas.button_press_event(x0, y0, 1) print(atom0.refine_position) print(atom1.refine_position) sublattice.refine_atom_positions_using_center_of_mass(mask_radius=4) assert atom0.pixel_x == (x_pos[0] + delta_pos) assert atom0.pixel_y == (y_pos[0] + delta_pos) assert atom1.pixel_x != (x_pos[1] + delta_pos) assert atom1.pixel_y != (y_pos[1] + delta_pos)
def setup_method(self): test_data = tt.MakeTestData(50, 50) test_data.add_atom_list(np.arange(5, 45, 5), np.arange(5, 45, 5)) self.atom_lattice = test_data.atom_lattice
import os import numpy as np import atomap.api as am import atomap.testing_tools as tt my_path = os.path.join(os.path.dirname(__file__), 'maketestdata') if not os.path.exists(my_path): os.makedirs(my_path) ##### s = am.dummy_data.get_dumbbell_signal() s.plot() s._plot.signal_plot.figure.savefig(os.path.join(my_path, 'dumbbell.png')) ##### t1 = tt.MakeTestData(20, 20) t1.add_atom(10, 10) s1 = t1.signal s1.plot() s1._plot.signal_plot.figure.savefig(os.path.join(my_path, 't1.png')) ##### t2 = tt.MakeTestData(200, 200) x, y = np.mgrid[0:200:10j, 0:200:10j] x, y = x.flatten(), y.flatten() t2.add_atom_list(x, y) s2 = t2.signal s2.plot() s2._plot.signal_plot.figure.savefig(os.path.join(my_path, 't2.png')) #####
def test_watershed_method_running(self): test_data = tt.MakeTestData(60, 100) x, y, A = [20, 20, 40, 40], [25, 75, 25, 75], [5, 10, 15, 20] test_data.add_atom_list(x=x, y=y, amplitude=A) s = test_data.signal i_points, i_record, p_record = integrate(s, x, y, method='Watershed')
def test_simple_init(self): tt.MakeTestData(100, 100)
def test_show_progressbar(self): test_data0 = tt.MakeTestData(100, 100, show_progressbar=True) assert test_data0._show_progressbar test_data1 = tt.MakeTestData(100, 100, show_progressbar=False) assert not test_data1._show_progressbar
def test_add_image_noise_only_positive(self): test_data0 = tt.MakeTestData(1000, 1000) test_data0.add_image_noise(mu=0, sigma=0.005, only_positive=True) s0 = test_data0.signal assert (s0.data > 0).all()