Exemplo n.º 1
0
 def setup_method(self):
     pos0 = np.array([[5, 10], [10, 15]])
     pos1 = np.array([[20, 25], [30, 35]])
     sublattice0 = Sublattice(pos0, np.zeros((40, 40)))
     sublattice1 = Sublattice(pos1, np.zeros((40, 40)))
     self.atom_lattice = Atom_Lattice(
         np.zeros((40, 40)), sublattice_list=[sublattice0, sublattice1])
     self.x_pos = np.concatenate((pos0[:, 0], pos1[:, 0]))
     self.y_pos = np.concatenate((pos0[:, 1], pos1[:, 1]))
Exemplo n.º 2
0
 def setup_method(self):
     image_data = np.random.random(size=(100, 100))
     position_list = []
     for x in range(10, 100, 5):
         for y in range(10, 100, 5):
             position_list.append([x, y])
     sublattice = Sublattice(np.array(position_list), image_data)
     sublattice.find_nearest_neighbors()
     self.sublattice = sublattice
Exemplo n.º 3
0
    def setup_method(self):
        image_data = np.arange(10000).reshape(100, 100)
        peaks0 = np.arange(20).reshape(10, 2)
        peaks1 = np.arange(26).reshape(13, 2)

        sublattice0 = Sublattice(atom_position_list=peaks0, image=image_data)
        sublattice1 = Sublattice(atom_position_list=peaks1, image=image_data)
        self.atom_lattice = Atom_Lattice()
        self.atom_lattice.sublattice_list.extend([sublattice0, sublattice1])
        self.atom_lattice.image0 = image_data
Exemplo n.º 4
0
    def test_make_construct_zone_axes(self):
        sublattice = Sublattice(self.peaks, self.s)
        sublattice.construct_zone_axes()

        zv0 = sublattice.zones_axis_average_distances[0]
        zv1 = sublattice.zones_axis_average_distances[1]
        len_zv0 = len(sublattice.atom_planes_by_zone_vector[zv0])
        len_zv1 = len(sublattice.atom_planes_by_zone_vector[zv1])

        assert len_zv0 == 10
        assert len_zv1 == 10
Exemplo n.º 5
0
 def setup_method(self):
     self.atoms_N = 10
     image_data = np.arange(10000).reshape(100, 100)
     peaks = np.arange(20).reshape(self.atoms_N, 2)
     sublattice = Sublattice(peaks, image_data)
     sublattice.original_image = image_data
     for atom in sublattice.atom_list:
         atom.sigma_x = 2.
         atom.sigma_y = 2.
         atom.amplitude_gaussian = 10.
         atom.amplitude_max_intensity = 10.
     self.sublattice = sublattice
Exemplo n.º 6
0
    def test_find_b_cation_atoms(self):
        a_sublattice = Sublattice(
            self.peaks, np.rot90(np.fliplr(self.s_adf_modified.data)))
        a_sublattice.pixel_size = self.pixel_size
        afr.construct_zone_axes_from_sublattice(a_sublattice)

        zone_vector_100 = a_sublattice.zones_axis_average_distances[1]
        b_atom_list = a_sublattice.find_missing_atoms_from_zone_vector(
            zone_vector_100)
        b_sublattice = Sublattice(
            b_atom_list, np.rot90(np.fliplr(self.s_adf_modified.data)))
        assert len(b_sublattice.atom_list) == 221
Exemplo n.º 7
0
 def test_different_dtypes(self):
     dtype_list = [
         'float64', 'float32', 'int64', 'int32', 'int16', 'int8', 'uint64',
         'uint32', 'uint16', 'uint8'
     ]
     for dtype in dtype_list:
         atom_positions = [range(10), range(10)]
         image_data = np.random.randint(0, 127, size=(10, 10)).astype(dtype)
         Sublattice(atom_positions, image_data)
     with pytest.raises(ValueError):
         atom_positions = [range(10), range(10)]
         image_data = np.random.randint(0, 127,
                                        size=(10, 10)).astype('float16')
         Sublattice(atom_positions, image_data)
Exemplo n.º 8
0
    def test_save_load_atom_lattice_atom_values(self):
        image_data = np.arange(10000).reshape(100, 100)

        atom0_pos = np.random.random(size=(30, 2)) * 10
        atom0_sigma_x = np.random.random(size=30)
        atom0_sigma_y = np.random.random(size=30)
        atom0_rot = np.random.random(size=30)
        atom1_pos = np.random.random(size=(30, 2)) * 10
        atom1_sigma_x = np.random.random(size=30)
        atom1_sigma_y = np.random.random(size=30)
        atom1_rot = np.random.random(size=30)

        sublattice0 = Sublattice(atom_position_list=atom0_pos,
                                 image=image_data)
        sublattice1 = Sublattice(atom_position_list=atom1_pos,
                                 image=image_data)
        for i, atom in enumerate(sublattice0.atom_list):
            atom.sigma_x = atom0_sigma_x[i]
            atom.sigma_y = atom0_sigma_y[i]
            atom.rotation = atom0_rot[i]
        for i, atom in enumerate(sublattice1.atom_list):
            atom.sigma_x = atom1_sigma_x[i]
            atom.sigma_y = atom1_sigma_y[i]
            atom.rotation = atom1_rot[i]

        atom_lattice = Atom_Lattice()
        atom_lattice.sublattice_list.extend([sublattice0, sublattice1])
        atom_lattice.image0 = image_data

        tmpdir = tempfile.TemporaryDirectory()
        save_path = os.path.join(tmpdir.name, "atomic_lattice.hdf5")

        atom_lattice.save(filename=save_path, overwrite=True)
        atom_lattice_load = load_atom_lattice_from_hdf5(
            save_path, construct_zone_axes=False)
        sl0 = atom_lattice_load.sublattice_list[0]
        sl1 = atom_lattice_load.sublattice_list[1]

        assert (sl0.x_position == atom0_pos[:, 0]).all()
        assert (sl0.y_position == atom0_pos[:, 1]).all()
        assert (sl1.x_position == atom1_pos[:, 0]).all()
        assert (sl1.y_position == atom1_pos[:, 1]).all()
        assert (sl0.sigma_x == atom0_sigma_x).all()
        assert (sl0.sigma_y == atom0_sigma_y).all()
        assert (sl1.sigma_x == atom1_sigma_x).all()
        assert (sl1.sigma_y == atom1_sigma_y).all()
        assert (sl0.rotation == atom0_rot).all()
        assert (sl1.rotation == atom1_rot).all()
Exemplo n.º 9
0
    def sublattice(self):
        atom_list = []
        for atom in self.__sublattice.atom_list:
            new_atom = Atom_Position(
                x=atom.pixel_x, y=atom.pixel_y,
                sigma_x=atom.sigma_x, sigma_y=atom.sigma_y,
                rotation=atom.rotation, amplitude=atom.amplitude_gaussian)
            atom_list.append(new_atom)

        if self._sublattice_generate_image:
            image = self.signal.data
        else:
            image = np.zeros(self.data_extent[::-1])
        sublattice = Sublattice([], image)
        sublattice.atom_list = atom_list
        return sublattice
Exemplo n.º 10
0
    def setup_method(self):
        test_data = tt.MakeTestData(520, 520)
        x, y = np.mgrid[10:510:20j, 10:510:20j]
        x, y = x.flatten(), y.flatten()
        test_data.add_atom_list(x, y)
        s = test_data.signal

        atom_positions = afr.get_atom_positions(
            signal=s,
            separation=10,
            threshold_rel=0.02,
        )
        sublattice = Sublattice(atom_position_list=atom_positions,
                                image=s.data)
        sublattice.find_nearest_neighbors()
        self.sublattice = sublattice
Exemplo n.º 11
0
 def test_input_signal_and_original_image_wrong_dim(self):
     s = Signal2D(np.zeros((100, 50)))
     s_orig = Signal2D(np.zeros((100, 50, 9)))
     with pytest.raises(ValueError):
         Sublattice(atom_position_list=self.peaks,
                    image=s,
                    original_image=s_orig)
Exemplo n.º 12
0
 def test_wrong_original_image_dimension_input(self):
     image = np.zeros((100, 50))
     original_image = np.zeros((100, 50, 5))
     with pytest.raises(ValueError):
         Sublattice(atom_position_list=self.peaks,
                    image=image,
                    original_image=original_image)
Exemplo n.º 13
0
 def test_get_zone_vector_index(self):
     sublattice = Sublattice(self.peaks, self.s)
     sublattice.construct_zone_axes()
     zone_axis_index = sublattice.get_zone_vector_index(
         sublattice.zones_axis_average_distances_names[0])
     assert zone_axis_index == 0
     with pytest.raises(ValueError):
         sublattice.get_zone_vector_index('(99, 99)')
Exemplo n.º 14
0
 def test_scaling(self):
     sublattice = Sublattice([
         [10, 10],
     ],
                             np.ones((20, 20)),
                             pixel_size=0.2)
     atom_lattice = Atom_Lattice(np.ones((100, 100)),
                                 sublattice_list=[sublattice])
     signal = atom_lattice.signal
     assert signal.axes_manager.signal_axes[0].scale == 0.2
     assert signal.axes_manager.signal_axes[1].scale == 0.2
Exemplo n.º 15
0
    def test_manual_processing(self):
        s_adf_filename = os.path.join(my_path, "datasets",
                                      "test_ADF_cropped.hdf5")
        s = load(s_adf_filename)
        s.change_dtype('float32')
        atom_positions = afr.get_atom_positions(
            signal=s,
            separation=17,
            threshold_rel=0.02,
        )
        sublattice = Sublattice(atom_position_list=atom_positions,
                                image=s.data)
        sublattice.find_nearest_neighbors()
        sublattice.refine_atom_positions_using_center_of_mass(
            sublattice.image, percent_to_nn=0.4)
        sublattice.refine_atom_positions_using_2d_gaussian(sublattice.image,
                                                           percent_to_nn=0.4)

        Atom_Lattice(image=s.data, sublattice_list=[sublattice])
        sublattice.construct_zone_axes()
Exemplo n.º 16
0
 def test_2d_gaussian_all_arguments(self):
     sublattice = Sublattice(self.xy, self.image_data)
     sublattice.find_nearest_neighbors()
     sublattice.refine_atom_positions_using_2d_gaussian(
         image_data=self.image_data,
         percent_to_nn=0.3,
         rotation_enabled=False)
Exemplo n.º 17
0
def make_atom_lattice_dumbbell_structure(
        s, position_list, dumbbell_vector, show_progressbar=True):
    """
    Make Atom_Lattice object from image of dumbbell structure.

    Parameters
    ----------
    s : HyperSpy 2D signal
    position_list : list of atomic positions
        In the form [[x0, y0], [x1, y1], [x2, y2], ...]
    dumbbell_vector : tuple
    show_progressbar : bool, default True

    Returns
    -------
    dumbbell_lattice: Atomap Dumbbell_Lattice object

    Examples
    --------
    >>> import temul.external.atomap_devel_012.api as am
    >>> import temul.external.atomap_devel_012.initial_position_finding as ipf
    >>> from temul.external.atomap_devel_012.atom_finding_refining import get_atom_positions
    >>> s = am.dummy_data.get_dumbbell_signal()
    >>> position_list = get_atom_positions(s, separation=16)
    >>> dumbbell_vector = ipf.find_dumbbell_vector(s, 4)
    >>> dumbbell_lattice = ipf.make_atom_lattice_dumbbell_structure(
    ...     s, position_list, dumbbell_vector)
    """
    dumbbell_list0, dumbbell_list1 = _get_dumbbell_arrays(
        s, position_list, dumbbell_vector,
        show_progressbar=show_progressbar)
    s_modified = do_pca_on_signal(s)
    sublattice0 = Sublattice(
        atom_position_list=dumbbell_list0,
        original_image=s.data,
        image=s_modified.data,
        color='blue')
    sublattice1 = Sublattice(
        atom_position_list=dumbbell_list1,
        original_image=s.data,
        image=s_modified.data,
        color='red')
    sublattice0.find_nearest_neighbors()
    sublattice1.find_nearest_neighbors()
    atom_lattice = Dumbbell_Lattice(
        image=sublattice0.image,
        name="Dumbbell structure",
        sublattice_list=[sublattice0, sublattice1])
    return(atom_lattice)
Exemplo n.º 18
0
    def test_repr(self):
        sublattice = Sublattice(self.peaks, self.s)
        sublattice.name = 'test planes'
        sublattice.construct_zone_axes()

        repr_str = '<Sublattice, test planes (atoms:%s,planes:%s)>' % (len(
            sublattice.atom_list), len(sublattice.atom_planes_by_zone_vector))
        assert sublattice.__repr__() == repr_str
Exemplo n.º 19
0
def get_simple_cubic_sublattice_positions_on_vac(image_noise=False):
    '''
    Create a simple cubic structure similar to `get_simple_cubic_sublattice`
    above but the atom positions are also overlaid on the vacancy positions.
    '''

    temp_sub = _make_simple_cubic_testdata(image_noise=image_noise,
                                           with_vacancies=False).sublattice
    temp_pos = np.asarray([temp_sub.x_position, temp_sub.y_position]).T
    image = _make_simple_cubic_testdata(image_noise=image_noise,
                                        with_vacancies=True).signal
    sublattice = Sublattice(temp_pos, image.data)

    return sublattice
Exemplo n.º 20
0
def get_polarised_single_sublattice_rotated(image_noise=False, rotation=45):
    sublattice = get_polarised_single_sublattice(image_noise=image_noise)
    sig = sublattice.signal
    sig.map(scipy.ndimage.rotate, angle=rotation, reshape=False)
    # sig.plot()
    atom_positions = get_atom_positions(sig, separation=7)
    rot_sublattice = Sublattice(atom_positions, image=sig.data)
    rot_sublattice.find_nearest_neighbors()
    rot_sublattice.refine_atom_positions_using_center_of_mass()
    # rot_sublattice.plot()
    return rot_sublattice
Exemplo n.º 21
0
 def test_center_of_mass_dtypes(self):
     sublattice = Sublattice(self.xy, self.image_data)
     sublattice.find_nearest_neighbors()
     image_data = 127 * (self.image_data / self.image_data.max())
     dtype_list = [
         'float64', 'float32', 'float16', 'int64', 'int32', 'int16', 'int8',
         'uint64', 'uint32', 'uint16', 'uint8'
     ]
     for dtype in dtype_list:
         sublattice.refine_atom_positions_using_center_of_mass(
             image_data=image_data.astype(dtype))
Exemplo n.º 22
0
 def test_all_parameters(self):
     sublattice = self.sublattice
     sublattice.construct_zone_axes()
     z_list = self.z_list
     sub0 = Sublattice(np.array([[18, 15]]), image=sublattice.image)
     s = sublattice.get_property_map(
         sublattice.x_position,
         sublattice.y_position,
         z_list,
         atom_plane_list=[sublattice.atom_plane_list[0]],
         add_zero_value_sublattice=sub0,
         upscale_map=4)
     assert s.axes_manager[0].scale == 0.25
     assert s.axes_manager[1].scale == 0.25
     assert s.data[20:100, 20:100].mean() <= 1
     assert s.axes_manager[0].size == 120
     assert s.axes_manager[1].size == 120
     assert len(s.metadata['Markers'].keys()) == 4
Exemplo n.º 23
0
def sine_wave_sublattice():

    image_size = 110

    x = np.linspace(0, image_size - 10, 35)
    y_func = 2 * np.sin(2 * np.pi * (x + 5) / 1)
    # plt.scatter(x,y_func)
    y_shift_list = range(10, image_size - 20, 10)
    x_list, y_list = [], []
    for y_shift in y_shift_list:
        y = y_func + y_shift
        y_list.extend(y)
        x_list.extend(x)

    x_list, y_list = np.asarray(x_list) + 5, np.asarray(y_list) + 5
    atom_positions = np.dstack((x_list, y_list))[0]
    image_data = np.random.random((image_size, image_size))

    # plt.figure()
    # plt.scatter(atom_positions.T[0], atom_positions.T[1])
    sublattice = Sublattice(atom_positions, image_data)
    # sublattice.plot()
    return (sublattice)
Exemplo n.º 24
0
def make_atom_lattice_from_image(s_image0,
                                 process_parameter=None,
                                 pixel_separation=None,
                                 s_image1=None,
                                 debug_plot=False):
    if s_image0.data.dtype == 'float16':
        raise ValueError(
            "s_image0 has the dtype float16, which is not supported. "
            "Convert it to something else, for example using "
            "s_image0.change_dtype('float64')")
    image0_filename = _get_signal_name(s_image0)

    name = image0_filename

    s_image0 = s_image0.deepcopy()
    s_image0_modified = run_image_filtering(s_image0)

    if process_parameter is None:
        process_parameter = pp.GenericStructure()

    image0_scale = s_image0.axes_manager[0].scale
    if pixel_separation is None:
        if process_parameter.peak_separation is None:
            raise ValueError("pixel_separation is not set.\
                    Either set it in the process_parameter.peak_separation\
                    or pixel_separation parameter")
        else:
            pixel_separation = process_parameter.peak_separation / image0_scale
    initial_atom_position_list = get_atom_positions(
        s_image0_modified, separation=pixel_separation)

    if s_image1 is not None:
        if s_image1.data.dtype == 'float16':
            raise ValueError(
                "s_image1 has the dtype float16, which is not supported. "
                "Convert it to something else, for example using "
                "s_image1.change_dtype('float64')")
        s_image1 = s_image1.deepcopy()
        s_image1.data = 1. / s_image1.data
        image1_data = s_image1.data

    #################################

    image0_data = s_image0.data
    image0_data_modified = s_image0_modified.data

    atom_lattice = Atom_Lattice(name=name)
    atom_lattice._original_filename = image0_filename
    atom_lattice.image0 = image0_data
    if s_image1 is not None:
        atom_lattice.image1 = image1_data
    atom_lattice._pixel_separation = pixel_separation

    for sublattice_index in range(process_parameter.number_of_sublattices):
        sublattice_para = process_parameter.get_sublattice_from_order(
            sublattice_index)

        if sublattice_para.image_type == 0:
            s_image = s_image0
            image_data = image0_data
            image_data_modified = image0_data_modified
        if sublattice_para.image_type == 1:
            if s_image1 is not None:
                s_image = s_image1
                image_data = image1_data
                image_data_modified = image1_data
            else:
                break

        if sublattice_para.sublattice_order == 0:
            sublattice = Sublattice(initial_atom_position_list,
                                    image_data_modified)
        else:
            temp_sublattice = atom_lattice.get_sublattice(
                sublattice_para.sublattice_position_sublattice)
            temp_zone_vector_index = temp_sublattice.get_zone_vector_index(
                sublattice_para.sublattice_position_zoneaxis)
            zone_vector = temp_sublattice.zones_axis_average_distances[
                temp_zone_vector_index]
            atom_list = temp_sublattice.find_missing_atoms_from_zone_vector(
                zone_vector)

            sublattice = Sublattice(atom_list, image_data)

        zone_axis_para_list = False
        if hasattr(sublattice_para, 'zone_axis_list'):
            zone_axis_para_list = sublattice_para.zone_axis_list

        sublattice._plot_color = sublattice_para.color
        sublattice.name = sublattice_para.name
        sublattice.pixel_size = s_image.axes_manager[0].scale
        sublattice._pixel_separation = pixel_separation
        sublattice.original_image = image_data
        atom_lattice.sublattice_list.append(sublattice)
        if debug_plot:
            sublattice.plot_atom_list_on_image_data(figname=sublattice.name +
                                                    "_initial_position.jpg")
        for atom in sublattice.atom_list:
            atom.sigma_x = sublattice._pixel_separation / 10.
            atom.sigma_y = sublattice._pixel_separation / 10.
        if not (sublattice_para.sublattice_order == 0):
            construct_zone_axes_from_sublattice(
                sublattice, zone_axis_para_list=zone_axis_para_list)
            atom_subtract_config = sublattice_para.atom_subtract_config
            image_data = sublattice.image
            for atom_subtract_para in atom_subtract_config:
                temp_sublattice = atom_lattice.get_sublattice(
                    atom_subtract_para['sublattice'])
                neighbor_distance = atom_subtract_para['neighbor_distance']
                image_data = remove_atoms_from_image_using_2d_gaussian(
                    image_data,
                    temp_sublattice,
                    percent_to_nn=neighbor_distance)
            sublattice.image = image_data
            sublattice.original_image = image_data

        refinement_config = sublattice_para.refinement_config
        refinement_neighbor_distance = refinement_config['neighbor_distance']
        refinement_steps = refinement_config['config']
        for refinement_step in refinement_steps:
            if refinement_step[0] == 'image_data':
                refinement_step[0] = sublattice.original_image
            elif refinement_step[0] == 'image_data_modified':
                refinement_step[0] = sublattice.image
            else:
                refinement_step[0] = sublattice.original_image

        refine_sublattice(sublattice, refinement_steps,
                          refinement_neighbor_distance)

        if sublattice_para.sublattice_order == 0:
            sublattice.construct_zone_axes(
                zone_axis_para_list=zone_axis_para_list)

    return (atom_lattice)
Exemplo n.º 25
0
class MakeTestData(object):

    def __init__(self, image_x, image_y, sublattice_generate_image=True):
        """
        Class for generating test datasets of atomic resolution
        STEM images.

        Parameters
        ----------
        image_x, image_y : int
            Size of the image data.
        sublattice_generate_image : bool, default True
            When generating sublattices, a raster image is generated to
            complement the atom position objects (found in sublattice.image).
            For large amounts of atom positions, this can take a very long
            time. If sublattice_generate_image is False, this image will not
            be generated. Useful for generating sublattice objects for testing
            quicker, when only the atom positions themselves are needed.

        Attributes
        ----------
        signal : HyperSpy 2D Signal
        sublattice : Atomap Sublattice
        atom_lattice : Atomap Atom_Lattice
        gaussian_list : list of 2D Gaussians objects

        Examples
        --------
        >>> from temul.external.atomap_devel_012.testing_tools import MakeTestData
        >>> test_data = MakeTestData(200, 200)
        >>> test_data.add_atom(x=10, y=20)
        >>> test_data.signal.plot()

        Adding many atoms

        >>> test_data = MakeTestData(200, 200)
        >>> import numpy as np
        >>> x, y = np.mgrid[0:200:10j, 0:200:10j]
        >>> x, y = x.flatten(), y.flatten()
        >>> test_data.add_atom_list(x, y)
        >>> test_data.signal.plot()

        Adding many atoms with different parameters

        >>> test_data = MakeTestData(200, 200)
        >>> x, y = np.mgrid[0:200:10j, 0:200:10j]
        >>> x, y = x.flatten(), y.flatten()
        >>> sx, sy = np.random.random(len(x)), np.random.random(len(x))
        >>> A, r = np.random.random(len(x))*10, np.random.random(len(x))*3.14
        >>> test_data.add_atom_list(x, y, sigma_x=sx, sigma_y=sy,
        ...         amplitude=A, rotation=r)
        >>> test_data.signal.plot()

        The class also generates a sublattice object

        >>> test_data = MakeTestData(200, 200)
        >>> import numpy as np
        >>> x, y = np.mgrid[0:200:10j, 0:200:10j]
        >>> x, y = x.flatten(), y.flatten()
        >>> test_data.add_atom_list(x, y)
        >>> test_data.sublattice.plot()

        Also Atom_Lattice objects

        >>> atom_lattice = test_data.atom_lattice
        >>> atom_lattice.plot()

        Generating a sublattice with 22500 atoms quickly, by not
        generating the image

        >>> test_data = MakeTestData(200, 200, sublattice_generate_image=False)
        >>> import numpy as np
        >>> x, y = np.mgrid[0:1000:150j, 0:1000:150j]
        >>> x, y = x.flatten(), y.flatten()
        >>> test_data.add_atom_list(x, y)
        >>> sublattice = test_data.sublattice

        """
        self.data_extent = (image_x, image_y)
        self._image_noise = False
        self._sublattice_generate_image = sublattice_generate_image
        self.__sublattice = Sublattice([], np.zeros((2, 2)))
        self.__sublattice.atom_list = []

    @property
    def signal(self):
        signal = self.__sublattice.get_model_image(
            image_shape=self.data_extent, show_progressbar=False)
        if self._image_noise is not False:
            signal.data += self._image_noise
        return signal

    @property
    def gaussian_list(self):
        gaussian_list = []
        for atom in self.__sublattice.atom_list:
            gaussian_list.append(atom.as_gaussian())
        return gaussian_list

    @property
    def sublattice(self):
        atom_list = []
        for atom in self.__sublattice.atom_list:
            new_atom = Atom_Position(
                x=atom.pixel_x, y=atom.pixel_y,
                sigma_x=atom.sigma_x, sigma_y=atom.sigma_y,
                rotation=atom.rotation, amplitude=atom.amplitude_gaussian)
            atom_list.append(new_atom)

        if self._sublattice_generate_image:
            image = self.signal.data
        else:
            image = np.zeros(self.data_extent[::-1])
        sublattice = Sublattice([], image)
        sublattice.atom_list = atom_list
        return sublattice

    @property
    def atom_lattice(self):
        sublattice = self.sublattice
        atom_lattice = Atom_Lattice(image=sublattice.image,
                                    sublattice_list=[sublattice])
        return atom_lattice

    def add_atom(self, x, y, sigma_x=1, sigma_y=1, amplitude=1, rotation=0):
        """
        Add a single atom to the test data.

        Parameters
        ----------
        x, y : numbers
            Position of the atom.
        sigma_x, sigma_y : numbers, default 1
        amplitude : number, default 1
        rotation : number, default 0

        Examples
        --------
        >>> from temul.external.atomap_devel_012.testing_tools import MakeTestData
        >>> test_data = MakeTestData(200, 200)
        >>> test_data.add_atom(x=10, y=20)
        >>> test_data.signal.plot()
        """
        atom = Atom_Position(
            x=x, y=y, sigma_x=sigma_x, sigma_y=sigma_y,
            rotation=rotation, amplitude=amplitude)
        self.__sublattice.atom_list.append(atom)

    def add_atom_list(
            self, x, y, sigma_x=1, sigma_y=1, amplitude=1, rotation=0):
        """
        Add several atoms to the test data.

        Parameters
        ----------
        x, y : iterable
            Position of the atoms. Must be iterable, and have the same size.
        sigma_x, sigma_y : number or iterable, default 1
            If number: all the atoms will have the same sigma.
            Use iterable for setting different sigmas for different atoms.
            If iterable: must be same length as x and y iterables.
        amplitude : number or iterable, default 1
            If number: all the atoms will have the same amplitude.
            Use iterable for setting different amplitude for different atoms.
            If iterable: must be same length as x and y iterables.
        rotation : number or iterable, default 0
            If number: all the atoms will have the same rotation.
            Use iterable for setting different rotation for different atoms.
            If iterable: must be same length as x and y iterables.

        Examples
        --------
        >>> from temul.external.atomap_devel_012.testing_tools import MakeTestData
        >>> test_data = MakeTestData(200, 200)
        >>> import numpy as np
        >>> x, y = np.mgrid[0:200:10j, 0:200:10j]
        >>> x, y = x.flatten(), y.flatten()
        >>> test_data.add_atom_list(x, y)
        >>> test_data.signal.plot()

        """
        if len(x) != len(y):
            raise ValueError("x and y needs to have the same length")

        if isiterable(sigma_x):
            if len(sigma_x) != len(x):
                raise ValueError("sigma_x and x needs to have the same length")
        else:
            sigma_x = [sigma_x] * len(x)

        if isiterable(sigma_y):
            if len(sigma_y) != len(y):
                raise ValueError("sigma_y and x needs to have the same length")
        else:
            sigma_y = [sigma_y] * len(x)

        if isiterable(amplitude):
            if len(amplitude) != len(x):
                raise ValueError(
                    "amplitude and x needs to have the same length")
        else:
            amplitude = [amplitude] * len(x)

        if isiterable(rotation):
            if len(rotation) != len(x):
                raise ValueError(
                    "rotation and x needs to have the same length")
        else:
            rotation = [rotation] * len(x)
        iterator = zip(x, y, sigma_x, sigma_y, amplitude, rotation)
        for tx, ty, tsigma_x, tsigma_y, tamplitude, trotation in iterator:
            self.add_atom(tx, ty, tsigma_x, tsigma_y, tamplitude, trotation)

    def add_image_noise(
            self, mu=0, sigma=0.005, only_positive=False, random_seed=None):
        """
        Add white noise to the image signal. The noise component is Gaussian
        distributed, with a default expectation value at 0, and a sigma of
        0.005. If only_positive is set to True, the absolute value of the
        noise is added to the signal. This can be useful for avoiding negative
        values in the image signal.

        Parameters
        ----------
        mu : int, float
            The expectation value of the Gaussian distribution, default is 0
        sigma : int, float
            The standard deviation of the Gaussian distribution, default
            is 0.005.
        only_positive : bool
            Default is False. If True, the absolute value of the noise is added
            to the image signal.
        random_seed : int, optional
            Set the random seed of the noise, which gives the same image
            noise each time. Useful for testing and comparing images.

        Example
        -------
        >>> from temul.external.atomap_devel_012.testing_tools import MakeTestData
        >>> test_data = MakeTestData(300, 300)
        >>> import numpy as np
        >>> x, y = np.mgrid[10:290:15j, 10:290:15j]
        >>> test_data.add_atom_list(x.flatten(), y.flatten(), sigma_x=3,
        ...     sigma_y=3)
        >>> test_data.add_image_noise()
        >>> test_data.signal.plot()

        Using a specific random seed

        >>> test_data.add_image_noise(random_seed=0)

        """
        if random_seed is not None:
            np.random.seed(random_seed)
        shape = self.signal.axes_manager.shape
        noise = normal(mu, sigma, shape)
        if only_positive:
            self._image_noise = np.absolute(noise)
        else:
            self._image_noise = noise
Exemplo n.º 26
0
 def setup_method(self):
     atoms_N = 10
     image_data = np.arange(10000).reshape(100, 100)
     peaks = np.arange(20).reshape(atoms_N, 2)
     self.sublattice = Sublattice(peaks, image_data)
Exemplo n.º 27
0
    def __init__(self, image_x, image_y, sublattice_generate_image=True):
        """
        Class for generating test datasets of atomic resolution
        STEM images.

        Parameters
        ----------
        image_x, image_y : int
            Size of the image data.
        sublattice_generate_image : bool, default True
            When generating sublattices, a raster image is generated to
            complement the atom position objects (found in sublattice.image).
            For large amounts of atom positions, this can take a very long
            time. If sublattice_generate_image is False, this image will not
            be generated. Useful for generating sublattice objects for testing
            quicker, when only the atom positions themselves are needed.

        Attributes
        ----------
        signal : HyperSpy 2D Signal
        sublattice : Atomap Sublattice
        atom_lattice : Atomap Atom_Lattice
        gaussian_list : list of 2D Gaussians objects

        Examples
        --------
        >>> from temul.external.atomap_devel_012.testing_tools import MakeTestData
        >>> test_data = MakeTestData(200, 200)
        >>> test_data.add_atom(x=10, y=20)
        >>> test_data.signal.plot()

        Adding many atoms

        >>> test_data = MakeTestData(200, 200)
        >>> import numpy as np
        >>> x, y = np.mgrid[0:200:10j, 0:200:10j]
        >>> x, y = x.flatten(), y.flatten()
        >>> test_data.add_atom_list(x, y)
        >>> test_data.signal.plot()

        Adding many atoms with different parameters

        >>> test_data = MakeTestData(200, 200)
        >>> x, y = np.mgrid[0:200:10j, 0:200:10j]
        >>> x, y = x.flatten(), y.flatten()
        >>> sx, sy = np.random.random(len(x)), np.random.random(len(x))
        >>> A, r = np.random.random(len(x))*10, np.random.random(len(x))*3.14
        >>> test_data.add_atom_list(x, y, sigma_x=sx, sigma_y=sy,
        ...         amplitude=A, rotation=r)
        >>> test_data.signal.plot()

        The class also generates a sublattice object

        >>> test_data = MakeTestData(200, 200)
        >>> import numpy as np
        >>> x, y = np.mgrid[0:200:10j, 0:200:10j]
        >>> x, y = x.flatten(), y.flatten()
        >>> test_data.add_atom_list(x, y)
        >>> test_data.sublattice.plot()

        Also Atom_Lattice objects

        >>> atom_lattice = test_data.atom_lattice
        >>> atom_lattice.plot()

        Generating a sublattice with 22500 atoms quickly, by not
        generating the image

        >>> test_data = MakeTestData(200, 200, sublattice_generate_image=False)
        >>> import numpy as np
        >>> x, y = np.mgrid[0:1000:150j, 0:1000:150j]
        >>> x, y = x.flatten(), y.flatten()
        >>> test_data.add_atom_list(x, y)
        >>> sublattice = test_data.sublattice

        """
        self.data_extent = (image_x, image_y)
        self._image_noise = False
        self._sublattice_generate_image = sublattice_generate_image
        self.__sublattice = Sublattice([], np.zeros((2, 2)))
        self.__sublattice.atom_list = []
Exemplo n.º 28
0
 def test_input_signal_wrong_dimensions(self):
     s = Signal2D(np.zeros((100, 50, 10)))
     with pytest.raises(ValueError):
         Sublattice(atom_position_list=self.peaks, image=s)
Exemplo n.º 29
0
def load_atom_lattice_from_hdf5(filename, construct_zone_axes=True):
    """
    Load an Atomap HDF5-file, restoring a saved Atom_Lattice.

    Parameters
    ----------
    filename : string
        Filename of the HDF5-file.
    construct_zone_axes : bool
        If True, find relations between atomic positions by
        constructing atomic planes. Default True.

    Returns
    -------
    Atomap Atom_Lattice object

    """
    h5f = h5py.File(filename, 'r')
    atom_lattice = Atom_Lattice()
    sublattice_list = []
    sublattice_index_list = []
    for group_name in h5f:
        if ('atom_lattice' in group_name) or ('sublattice' in group_name):
            sublattice_set = h5f[group_name]
            modified_image_data = sublattice_set['modified_image_data'][:]
            original_image_data = sublattice_set['original_image_data'][:]
            atom_position_array = sublattice_set['atom_positions'][:]

            if 'sublattice_index' in sublattice_set.attrs.keys():
                sublattice_index_list.append(
                    sublattice_set.attrs['sublattice_index'])

            sublattice = Sublattice(atom_position_array, modified_image_data)
            sublattice.original_image = original_image_data

            if 'sigma_x' in sublattice_set.keys():
                sigma_x_array = sublattice_set['sigma_x'][:]
                for atom, sigma_x in zip(sublattice.atom_list, sigma_x_array):
                    atom.sigma_x = sigma_x
            if 'sigma_y' in sublattice_set.keys():
                sigma_y_array = sublattice_set['sigma_y'][:]
                for atom, sigma_y in zip(sublattice.atom_list, sigma_y_array):
                    atom.sigma_y = sigma_y
            if 'rotation' in sublattice_set.keys():
                rotation_array = sublattice_set['rotation'][:]
                for atom, rotation in zip(sublattice.atom_list,
                                          rotation_array):
                    atom.rotation = rotation
####
            if 'amplitude_max_intensity' in sublattice_set.keys():
                amplitude_max_intensity_array = sublattice_set[
                    'amplitude_max_intensity'][:]
                for atom, amplitude_max_intensity in zip(
                        sublattice.atom_list, amplitude_max_intensity_array):
                    atom.amplitude_max_intensity = amplitude_max_intensity

            if 'amplitude_mean_intensity' in sublattice_set.keys():
                amplitude_mean_intensity_array = sublattice_set[
                    'amplitude_mean_intensity'][:]
                for atom, amplitude_mean_intensity in zip(
                        sublattice.atom_list, amplitude_mean_intensity_array):
                    atom.amplitude_mean_intensity = amplitude_mean_intensity

            if 'amplitude_min_intensity' in sublattice_set.keys():
                amplitude_min_intensity_array = sublattice_set[
                    'amplitude_min_intensity'][:]
                for atom, amplitude_min_intensity in zip(
                        sublattice.atom_list, amplitude_min_intensity_array):
                    atom.amplitude_min_intensity = amplitude_min_intensity

            if 'amplitude_total_intensity' in sublattice_set.keys():
                amplitude_total_intensity_array = sublattice_set[
                    'amplitude_total_intensity'][:]
                for atom, amplitude_total_intensity in zip(
                        sublattice.atom_list, amplitude_total_intensity_array):
                    atom.amplitude_total_intensity = amplitude_total_intensity

            if 'elements' in sublattice_set.keys():
                elements_array = sublattice_set['elements'][:]
                for atom, elements in zip(sublattice.atom_list,
                                          elements_array):
                    atom.elements = elements

            if 'z_height' in sublattice_set.keys():
                z_height_array = sublattice_set['z_height'][:]
                # z_height_array_2 = [] # first loop needed because i can't eval() the z_height itself, don't really know why
                # for i in range(0, len(z_height_array)):
                #   z_h = ast.literal_eval(z_height_array[i])
                #  z_height_array_2.append(z_h)
                for atom, z_height in zip(sublattice.atom_list,
                                          z_height_array):
                    atom.z_height = z_height


####
            sublattice.pixel_size = sublattice_set.attrs['pixel_size']

            if 'tag' in sublattice_set.attrs.keys():
                sublattice.name = sublattice_set.attrs['tag']
            elif 'name' in sublattice_set.attrs.keys():
                sublattice.name = sublattice_set.attrs['name']
            else:
                sublattice.name = ''

            if type(sublattice.name) == bytes:
                sublattice.name = sublattice.name.decode()

            sublattice._plot_color = sublattice_set.attrs['plot_color']

            if type(sublattice._plot_color) == bytes:
                sublattice._plot_color = sublattice._plot_color.decode()

            if 'pixel_separation' in sublattice_set.attrs.keys():
                sublattice._pixel_separation = sublattice_set.attrs[
                    'pixel_separation']
            else:
                sublattice._pixel_separation = 0.0

            if construct_zone_axes:
                construct_zone_axes_from_sublattice(sublattice)

            if 'zone_axis_names_byte' in sublattice_set.keys():
                zone_axis_list_byte = sublattice_set.attrs[
                    'zone_axis_names_byte']
                zone_axis_list = []
                for zone_axis_name_byte in zone_axis_list_byte:
                    zone_axis_list.append(zone_axis_name_byte.decode())
                sublattice.zones_axis_average_distances_names = zone_axis_list

            sublattice_list.append(sublattice)

        if group_name == 'image_data0':
            atom_lattice.image0 = h5f[group_name][:]
            atom_lattice.image = atom_lattice.image0
        if group_name == 'image_data1':
            atom_lattice.image1 = h5f[group_name][:]

    sorted_sublattice_list = []
    if not sublattice_index_list:  # Support for older hdf5 files
        sublattice_index_list = range(len(sublattice_list))
    for sublattice_index in sublattice_index_list:
        sorted_sublattice_list.append(sublattice_list[sublattice_index])
    atom_lattice.sublattice_list.extend(sorted_sublattice_list)
    if 'name' in h5f.attrs.keys():
        atom_lattice.name = h5f.attrs['name']
    elif 'path_name' in h5f.attrs.keys():
        atom_lattice.name = h5f.attrs['path_name']
    if 'pixel_separation' in h5f.attrs.keys():
        atom_lattice._pixel_separation = h5f.attrs['pixel_separation']
    else:
        atom_lattice._pixel_separation = 0.8 / sublattice.pixel_size
    if type(atom_lattice.name) == bytes:
        atom_lattice.name = atom_lattice.name.decode()
    h5f.close()
    return (atom_lattice)
Exemplo n.º 30
0
 def test_input_signal_and_original_image(self):
     s = Signal2D(np.zeros((100, 50)))
     s_orig = Signal2D(np.zeros((100, 50)))
     Sublattice(atom_position_list=self.peaks,
                image=s,
                original_image=s_orig)