Esempio n. 1
0
 def test_deprecated_method(self):
     s = Diffraction2D(np.zeros((2, 2, 10, 10)))
     with pytest.raises(Exception):
         s.angular_slice_radial_integration()
Esempio n. 2
0
 def test_non_lazy(self):
     s = Diffraction2D(np.random.randint(100, size=(5, 5, 20, 20)))
     s_template = s.template_match_disk(lazy_result=False)
     assert s.data.shape == s_template.data.shape
     assert s._lazy is False
Esempio n. 3
0
 def test_navigation_signal_plot(self):
     s = Diffraction2D(np.zeros(shape=(3, 4, 6, 10)))
     s_nav = Signal2D(np.zeros((3, 4)))
     s.navigation_signal = s_nav
     s.plot()
Esempio n. 4
0
 def test_scale(self):
     s = Diffraction2D(np.zeros((50, 60)))
     axis = s.axes_manager[-1]
     axis.scale = 0.5
     value = mt._pixel_to_scaled_value(axis, 4.5)
     assert value == 2.25
Esempio n. 5
0
 def test_0d_nav_dims(self):
     s = Diffraction2D(np.zeros((100, 100)))
     peak_array = np.random.randint(20, 80, size=(1, 1, 100, 2))
     s.add_peak_array_as_markers(peak_array)
     marker = list(s.metadata.Markers)[0][1]
     assert marker.data["x1"].shape == ()
Esempio n. 6
0
 def test_no_change(self, shape):
     s = Diffraction2D(np.zeros(shape))
     s1 = s.threshold_and_mask()
     assert (s.data == s1.data).all()
 def setup_method(self, method):
     # Navigation dimension of the diffraction patterns
     diffraction_pattern = Diffraction2D(np.arange(2000).reshape(4, 5, 10, 10))
     virtual_image_generator = VirtualImageGenerator(diffraction_pattern)
     self.virtual_image_generator = virtual_image_generator
     self.diffraction_pattern = diffraction_pattern
Esempio n. 8
0
 def test_wrong_input(self):
     s = Diffraction2D(np.random.randint(100, size=(5, 5, 20, 20)))
     with pytest.raises(ValueError):
         s.template_match_ring(r_inner=5, r_outer=3)
     with pytest.raises(ValueError):
         s.template_match_ring(r_inner=3, r_outer=3)
 def test_different_shape(self):
     array = np.ones(shape=(7, 9, 30, 40))
     s = Diffraction2D(array)
     s_r = s.radial_average()
     assert (s_r.data[:, :, :-2] == 1).all()
 def test_deprecated_method(self):
     s = Diffraction2D(np.zeros((2, 2, 5, 5)))
     with pytest.raises(Exception):
         s.radial_integration()
Esempio n. 11
0
 def test_several_markers(self):
     peak_array = np.zeros(shape=(3, 2, 3, 2))
     s = Diffraction2D(np.zeros(shape=(3, 2, 10, 10)))
     marker_list = mt._get_4d_points_marker_list(peak_array,
                                                 s.axes_manager.signal_axes)
     assert len(marker_list) == 3
Esempio n. 12
0
 def test_inside(self, line):
     s = Diffraction2D(np.zeros((2, 3, 50, 50), dtype=np.uint16))
     signal_axes = s.axes_manager.signal_axes
     assert mt._check_line_segment_inside(signal_axes, line)
Esempio n. 13
0
 def test_offset(self):
     s = Diffraction2D(np.zeros((50, 60)))
     axis = s.axes_manager[-1]
     axis.offset = 6
     value = mt._pixel_to_scaled_value(axis, 4.5)
     assert value == 10.5
Esempio n. 14
0
 def test_different_dimensions(self, nav_dims):
     shape = list(np.random.randint(2, 6, size=nav_dims))
     shape.extend([50, 50])
     s = Diffraction2D(np.random.random(size=shape))
     st = s.template_match_disk(disk_r=4, lazy_result=False)
     assert st.data.shape == tuple(shape)
Esempio n. 15
0
 def test_decomposition_class_assignment(self, diffraction_pattern):
     s = Diffraction2D(diffraction_pattern)
     s = s.as_lazy()
     s.decomposition()
     assert isinstance(s, LazyDiffraction2D)
Esempio n. 16
0
 def test_simple(self):
     s = Diffraction2D(np.random.randint(100, size=(5, 5, 20, 20)))
     s_template = s.template_match_ring(r_inner=3, r_outer=5)
     assert s.data.shape == s_template.data.shape
     assert s_template._lazy is True
Esempio n. 17
0
 def get_signal(self):
     s = Diffraction2D(self.get_diffraction_test_image())
     return s
Esempio n. 18
0
 def test_5d_data_as_lazy(self):
     data = np.random.random((2, 3, 4, 10, 15))
     s = Diffraction2D(data)
     s_lazy = s.as_lazy()
     assert s_lazy.__class__ == LazyDiffraction2D
     assert data.shape == s_lazy.data.shape
Esempio n. 19
0
 def get_signal(self):
     s = Diffraction2D(self.data)
     return s
Esempio n. 20
0
 def test_wrong_input_dimensions(self):
     s = Diffraction2D(np.zeros((2, 5, 5)))
     with pytest.raises(ValueError):
         s.add_ellipse_array_as_markers(ellipse_array=None)
Esempio n. 21
0
 def to_signal(self):
     self.signal = Diffraction2D(self.z_blurred)
     self.signal.axes_manager[0].scale = self.scale
     self.signal.axes_manager[1].scale = self.scale
Esempio n. 22
0
 def test_simple_plot(self):
     s = Diffraction2D(np.zeros(shape=(3, 4, 6, 10)))
     s.plot()
Esempio n. 23
0
def generate_4d_data(
    probe_size_x=10,
    probe_size_y=10,
    image_size_x=50,
    image_size_y=50,
    disk_x=25,
    disk_y=25,
    disk_r=5,
    disk_I=20,
    ring_x=25,
    ring_y=25,
    ring_r=20,
    ring_I=6,
    ring_lw=0,
    ring_e_x=None,
    ring_e_y=25,
    ring_e_semi_len0=15,
    ring_e_semi_len1=15,
    ring_e_r=0,
    ring_e_I=6,
    ring_e_lw=1,
    blur=True,
    blur_sigma=1,
    downscale=True,
    add_noise=False,
    noise_amplitude=1,
    lazy=False,
    lazy_chunks=None,
    show_progressbar=True,
):
    """Generate a test dataset containing a disk and diffraction ring.

    Useful for checking that radial average algorithms are working
    properly.

    The centre, intensity and radius position of the ring and disk can vary
    as a function of probe position, through the disk_x, disk_y, disk_r,
    disk_I, ring_x, ring_y, ring_r and ring_I arguments.
    In addition, the line width of the ring can be varied with ring_lw.

    There is also an elliptical ring, which can be added separately
    to the circular ring. This elliptical ring uses the ring_e_*
    arguments. It is disabled by default.

    The ring can be deactivated by setting ring_x=None.
    The disk can be deactivated by setting disk_x=None.
    The elliptical ring can be deactivated by setting ring_e_x=None.

    Parameters
    ----------
    probe_size_x, probe_size_y : int, default 10
        Size of the navigation dimension.
    image_size_x, image_size_y : int, default 50
        Size of the signal dimension.
    disk_x, disk_y : int or NumPy 2D-array, default 20
        Centre position of the disk. Either integer or NumPy 2-D array.
        See examples on how to make them the correct size.
        To deactivate the disk, set disk_x=None.
    disk_r : int or NumPy 2D-array, default 5
        Radius of the disk. Either integer or NumPy 2-D array.
        See examples on how to make it the correct size.
    disk_I : int or NumPy 2D-array, default 20
        Intensity of the disk, for each of the pixels.
        So if I=30, the each pixel in the disk will have a value of 30.
        Note, this value will change if blur=True or downscale=True.
    ring_x, ring_y : int or NumPy 2D-array, default 20
        Centre position of the ring. Either integer or NumPy 2-D array.
        See examples on how to make them the correct size.
        To deactivate the ring, set ring_x=None.
    ring_r : int or NumPy 2D-array, default 20
        Radius of the ring. Either integer or NumPy 2-D array.
        See examples on how to make it the correct size.
    ring_I : int or NumPy 2D-array, default 6
        Intensity of the ring, for each of the pixels.
        So if I=5, each pixel in the ring will have a value of 5.
        Note, this value will change if blur=True or downscale=True.
    ring_lw : int or NumPy 2D-array, default 0
        Line width of the ring. If ring_lw=1, the line will be 3 pixels wide.
        If ring_lw=2, the line will be 5 pixels wide.
    ring_e_x, ring_e_y : int or NumPy 2D-array, default 20
        Centre position of the elliptical ring. Either integer or
        NumPy 2-D array. See examples on how to make them the correct size.
        To deactivate the ring, set ring_x=None (which is the default).
    ring_e_semi_len0, ring_e_semi_len1 : int or NumPy 2D-array, default 15
        Semi lengths of the elliptical ring. Either integer or NumPy 2-D
        arrays. See examples on how to make it the correct size.
    ring_e_I : int or NumPy 2D-array, default 6
        Intensity of the elliptical ring, for each of the pixels.
        So if I=5, each pixel in the ring will have a value of 5.
        Note, this value will change if blur=True or downscale=True.
    ring_e_r : int or NumPy 2D-array, default 0
        Rotation of the elliptical ring, in radians.
    ring_e_lw : int or NumPy 2D-array, default 0
        Line width of the ring. If ring_lw=1, the line will be 3 pixels wide.
        If ring_lw=2, the line will be 5 pixels wide.
    blur : bool, default True
        If True, do a Gaussian blur of the disk.
    blur_sigma : int, default 1
        Sigma of the Gaussian blurring, if blur is True.
    downscale : bool, default True
        If True, use upscaling (then downscaling) to anti-alise the disk.
    add_noise : bool, default False
        Add Gaussian random noise.
    noise_amplitude : float, default 1
        The amplitude of the noise, if add_noise is True.
    lazy : bool, default False
        If True, the signal will be lazy
    lazy_chunks : tuple, optional
        Used if lazy is True, default (10, 10, 10, 10).

    Returns
    -------
    signal : HyperSpy Signal2D
        Signal with 2 navigation dimensions and 2 signal dimensions.

    Examples
    --------
    >>> from pyxem.dummy_data import make_diffraction_test_data as mdtd
    >>> s = mdtd.generate_4d_data(show_progressbar=False)
    >>> s.plot()

    Using more arguments

    >>> s = mdtd.generate_4d_data(probe_size_x=20, probe_size_y=30,
    ...         image_size_x=50, image_size_y=90,
    ...         disk_x=30, disk_y=70, disk_r=9, disk_I=30,
    ...         ring_x=35, ring_y=65, ring_r=20, ring_I=10,
    ...         blur=False, downscale=False, show_progressbar=False)

    Adding some Gaussian random noise

    >>> s = mdtd.generate_4d_data(add_noise=True, noise_amplitude=3,
    ...         show_progressbar=False)

    Different centre positions for each probe position.
    Note the size=(20, 10), and probe_x=10, probe_y=20: size=(y, x).

    >>> import numpy as np
    >>> disk_x = np.random.randint(5, 35, size=(20, 10))
    >>> disk_y = np.random.randint(5, 45, size=(20, 10))
    >>> disk_I = np.random.randint(50, 100, size=(20, 10))
    >>> ring_x = np.random.randint(5, 35, size=(20, 10))
    >>> ring_y = np.random.randint(5, 45, size=(20, 10))
    >>> ring_r = np.random.randint(10, 15, size=(20, 10))
    >>> ring_I = np.random.randint(1, 30, size=(20, 10))
    >>> ring_lw = np.random.randint(1, 5, size=(20, 10))
    >>> s = mdtd.generate_4d_data(probe_size_x=10, probe_size_y=20,
    ...         image_size_x=40, image_size_y=50, disk_x=disk_x, disk_y=disk_y,
    ...         disk_I=disk_I, ring_x=ring_x, ring_y=ring_y, ring_r=ring_r,
    ...         ring_I=ring_I, ring_lw=ring_lw, show_progressbar=False)

    Do not plot the disk

    >>> s = mdtd.generate_4d_data(disk_x=None, show_progressbar=False)

    Do not plot the ring

    >>> s = mdtd.generate_4d_data(ring_x=None, show_progressbar=False)

    Plot only an elliptical ring

    >>> from numpy.random import randint, random
    >>> s = mdtd.generate_4d_data(
    ...        probe_size_x=10, probe_size_y=10,
    ...        disk_x=None, ring_x=None,
    ...        ring_e_x=randint(20, 30, (10, 10)),
    ...        ring_e_y=randint(20, 30, (10, 10)),
    ...        ring_e_semi_len0=randint(10, 20, (10, 10)),
    ...        ring_e_semi_len1=randint(10, 20, (10, 10)),
    ...        ring_e_r=random((10, 10))*np.pi,
    ...        ring_e_lw=randint(1, 3, (10, 10)))

    """
    if disk_x is None:
        plot_disk = False
    else:
        plot_disk = True
        if not isiterable(disk_x):
            disk_x = np.ones((probe_size_y, probe_size_x)) * disk_x
    if not isiterable(disk_y):
        disk_y = np.ones((probe_size_y, probe_size_x)) * disk_y
    if not isiterable(disk_r):
        disk_r = np.ones((probe_size_y, probe_size_x)) * disk_r
    if not isiterable(disk_I):
        disk_I = np.ones((probe_size_y, probe_size_x)) * disk_I

    if ring_x is None:
        plot_ring = False
    else:
        plot_ring = True
        if not isiterable(ring_x):
            ring_x = np.ones((probe_size_y, probe_size_x)) * ring_x
    if not isiterable(ring_y):
        ring_y = np.ones((probe_size_y, probe_size_x)) * ring_y
    if not isiterable(ring_r):
        ring_r = np.ones((probe_size_y, probe_size_x)) * ring_r
    if not isiterable(ring_I):
        ring_I = np.ones((probe_size_y, probe_size_x)) * ring_I
    if not isiterable(ring_lw):
        ring_lw = np.ones((probe_size_y, probe_size_x)) * ring_lw

    if ring_e_x is None:
        plot_ring_e = False
    else:
        plot_ring_e = True
        if not isiterable(ring_e_x):
            ring_e_x = np.ones((probe_size_y, probe_size_x)) * ring_e_x
    if not isiterable(ring_e_y):
        ring_e_y = np.ones((probe_size_y, probe_size_x)) * ring_e_y
    if not isiterable(ring_e_semi_len0):
        ring_e_semi_len0 = np.ones((probe_size_y, probe_size_x)) * ring_e_semi_len0
    if not isiterable(ring_e_semi_len1):
        ring_e_semi_len1 = np.ones((probe_size_y, probe_size_x)) * ring_e_semi_len1
    if not isiterable(ring_e_I):
        ring_e_I = np.ones((probe_size_y, probe_size_x)) * ring_e_I
    if not isiterable(ring_e_lw):
        ring_e_lw = np.ones((probe_size_y, probe_size_x)) * ring_e_lw
    if not isiterable(ring_e_r):
        ring_e_r = np.ones((probe_size_y, probe_size_x)) * ring_e_r

    signal_shape = (probe_size_y, probe_size_x, image_size_y, image_size_x)
    s = Diffraction2D(np.zeros(shape=signal_shape))
    for i in tqdm(s, desc="Make test data", disable=not show_progressbar):
        index = s.axes_manager.indices[::-1]
        test_data = MakeTestData(
            size_x=image_size_x,
            size_y=image_size_y,
            default=False,
            blur=blur,
            blur_sigma=blur_sigma,
            downscale=downscale,
        )
        if plot_disk:
            dx, dy, dr = disk_x[index], disk_y[index], disk_r[index]
            dI = disk_I[index]
            test_data.add_disk(dx, dy, dr, intensity=dI)
        if plot_ring:
            rx, ry, rr = ring_x[index], ring_y[index], ring_r[index]
            rI, rLW = ring_I[index], ring_lw[index]
            test_data.add_ring(rx, ry, rr, intensity=rI, lw_pix=rLW)
        if plot_ring_e:
            rex, rey = ring_e_x[index], ring_e_y[index]
            resl0, resl1 = ring_e_semi_len0[index], ring_e_semi_len1[index]
            reI, reLW, rer = ring_e_I[index], ring_e_lw[index], ring_e_r[index]
            test_data.add_ring_ellipse(
                x0=rex,
                y0=rey,
                semi_len0=resl0,
                semi_len1=resl1,
                rotation=rer,
                intensity=reI,
                lw_r=reLW,
            )
        s.data[index][:] = test_data.signal.data[:]
        if add_noise:
            s.data[index][:] += (
                np.random.random(size=(image_size_y, image_size_x)) * noise_amplitude
            )
    s.axes_manager.indices = [0] * s.axes_manager.navigation_dimension
    if lazy:
        if lazy_chunks is None:
            lazy_chunks = 10, 10, 10, 10
        data_lazy = da.from_array(s.data, lazy_chunks)
        s = LazyDiffraction2D(data_lazy)
    return s
Esempio n. 24
0
 def test_navigation_signal_plot_argument(self):
     s = Diffraction2D(np.zeros(shape=(3, 4, 6, 10)))
     s_nav = Signal2D(np.zeros((3, 4)))
     s.plot(navigator=s_nav)
Esempio n. 25
0
 def test_simple(self):
     peak_array = np.zeros(shape=(3, 2, 3, 2))
     s = Diffraction2D(np.zeros(shape=(3, 2, 10, 10)))
     mt.add_peak_array_to_signal_as_markers(s, peak_array)
     assert len(s.metadata.Markers) == 3