def test_initial_position_outside_mask_y(self): sublattice = self.sublattice atom = [sublattice.atom_list[6]] atom[0].pixel_y -= 4 g = afr._fit_atom_positions_with_gaussian_model(atom, sublattice.image, mask_radius=2) assert not g
def test_initial_position_inside_mask_x(self): sublattice = self.sublattice atom = [sublattice.atom_list[6]] x0 = atom[0].pixel_x atom[0].pixel_x += 2 g = afr._fit_atom_positions_with_gaussian_model(atom, sublattice.image, mask_radius=4) assert_allclose(g[0].centre_x.value, x0, rtol=1e-2)
def test_outside_high_x(self): im_x, im_y = 90, 100 test_data = MakeTestData(im_x, im_y) test_data.add_atom(100, 50, amplitude=200, sigma_x=10, sigma_y=10) image = test_data.signal.data atom_position = Atom_Position(80, 50) gaussian_list = afr._fit_atom_positions_with_gaussian_model( [atom_position], image, mask_radius=30) if gaussian_list is not False: gaussian = gaussian_list[0] assert gaussian.centre_x.value > 0 assert gaussian.centre_x.value < im_x assert gaussian.centre_y.value > 0 assert gaussian.centre_y.value < im_y
def remove_atoms_from_image_using_2d_gaussian( image, sublattice, percent_to_nn=0.40, show_progressbar=True): """ Parameters ---------- image : NumPy 2D array sublattice : Atomap sublattice object percent_to_nn : float Percent to nearest neighbor. The function will find the closest nearest neighbor to the current atom position, and this value times percent_to_nn will be the radius of the mask centered on the atom position. Value should be somewhere between 0.01 (1%) and 1 (100%). Having a too low value might lead to bad fitting. show_progressbar : bool, default True Returns ------- subtracted_image : NumPy 2D array Examples -------- >>> atom_lattice = am.dummy_data.get_simple_atom_lattice_two_sublattices() >>> sublattice0 = atom_lattice.sublattice_list[0] >>> sublattice0.find_nearest_neighbors() >>> import atomap.tools as at >>> image_subtracted = at.remove_atoms_from_image_using_2d_gaussian( ... image=atom_lattice.image, sublattice=sublattice0, ... show_progressbar=False) >>> import hyperspy.api as hs >>> s = hs.signals.Signal2D(image_subtracted) >>> s.plot() Decrease percent_to_nn, to reduce the effect of overlapping atoms. For this dataset it won't change much, but might be very useful for real datasets. >>> image_subtracted = at.remove_atoms_from_image_using_2d_gaussian( ... image=atom_lattice.image, sublattice=sublattice0, ... percent_to_nn=0.2, show_progressbar=False) """ if sublattice.atom_list[0].nearest_neighbor_list is None: raise ValueError( "The atom_position objects does not seem to have a " "populated nearest neighbor list. " "Has sublattice.find_nearest_neighbors() been called?") model_image = np.zeros(image.shape) X, Y = np.meshgrid(np.arange( model_image.shape[1]), np.arange(model_image.shape[0])) for atom in tqdm( sublattice.atom_list, desc='Subtracting atoms', disable=not show_progressbar): percent_distance = percent_to_nn for i in range(10): g_list = _fit_atom_positions_with_gaussian_model( [atom], image, rotation_enabled=True, percent_to_nn=percent_distance) if g_list is False: if i == 9: break else: percent_distance *= 0.95 else: g = g_list[0] model_image += g.function(X, Y) break subtracted_image = copy.deepcopy(image) - model_image return(subtracted_image)
def test_too_small_percent_to_nn(self): sublattice = self.sublattice afr._fit_atom_positions_with_gaussian_model([sublattice.atom_list[6]], sublattice.image, percent_to_nn=0.01)
def test_wrong_input_1(self): sublattice = self.sublattice with pytest.raises(TypeError): afr._fit_atom_positions_with_gaussian_model( [sublattice.atom_list[5:7]], sublattice.image)
def test_5_atom(self): sublattice = self.sublattice g_list = afr._fit_atom_positions_with_gaussian_model( sublattice.atom_list[5:10], sublattice.image) assert len(g_list) == 5