Ejemplo n.º 1
0
    def test_compare_lazy_and_nonlazy(self):
        y, x = np.mgrid[75:83:9j, 85:95:11j]
        s = mdtd.generate_4d_data(
            probe_size_x=11,
            probe_size_y=9,
            ring_x=None,
            image_size_x=160,
            image_size_y=140,
            disk_x=x,
            disk_y=y,
            disk_r=40,
            disk_I=20,
            blur=True,
            blur_sigma=1,
            downscale=False,
        )
        s_lazy = LazyDiffraction2D(
            da.from_array(s.data, chunks=(1, 1, 140, 160)))
        s_com = s.center_of_mass()
        s_lazy_com = s_lazy.center_of_mass()
        np.testing.assert_equal(s_com.data, s_lazy_com.data)

        com_nav_extent = s_com.axes_manager.navigation_extent
        lazy_com_nav_extent = s_lazy_com.axes_manager.navigation_extent
        assert com_nav_extent == lazy_com_nav_extent

        com_sig_extent = s_com.axes_manager.signal_extent
        lazy_com_sig_extent = s_lazy_com.axes_manager.signal_extent
        assert com_sig_extent == lazy_com_sig_extent
Ejemplo n.º 2
0
 def test_correct_disk_x_y_and_radius_random(self):
     x, y, px, py = 56, 48, 4, 5
     x, y = randint(45, 55, size=(py, px)), randint(45, 55, size=(py, px))
     r = randint(20, 40, size=(py, px))
     s = mdtd.generate_4d_data(
         probe_size_x=px,
         probe_size_y=py,
         image_size_x=120,
         image_size_y=100,
         disk_x=x,
         disk_y=y,
         disk_r=5,
         disk_I=20,
         ring_x=x,
         ring_y=y,
         ring_r=r,
         ring_I=5,
         blur=True,
         downscale=False,
     )
     dask_array = da.from_array(s.data, chunks=(4, 4, 50, 50))
     s = LazyDiffraction2D(dask_array)
     s_com = s.center_of_mass()
     s_r = s.radial_average(centre_x=s_com.inav[0].data,
                            centre_y=s_com.inav[1].data)
     s_r = s_r.isig[15:]  # Do not include the disk
     r -= 15  # Need to shift the radius, due to not including the disk
     assert (s_r.data.argmax(axis=-1) == r).all()
Ejemplo n.º 3
0
 def test_nav_1(self):
     data_shape = (5, 40, 40)
     array0 = da.ones(shape=data_shape, chunks=(5, 5, 5))
     s0 = LazyDiffraction2D(array0)
     s0_r = s0.radial_average()
     assert s0_r.axes_manager.navigation_shape == data_shape[:1]
     assert (s0_r.data[:, :-1] == 1).all()
Ejemplo n.º 4
0
    def test_simple(self):
        array0 = da.ones(shape=(10, 10, 40, 40), chunks=(5, 5, 5, 5))
        s0 = LazyDiffraction2D(array0)
        s0_r = s0.radial_average()
        assert (s0_r.data[:, :, :-1] == 1).all()

        data_shape = 2, 2, 11, 11
        array1 = np.zeros(data_shape)
        array1[:, :, 5, 5] = 1
        dask_array = da.from_array(array1, chunks=(1, 1, 1, 1))
        s1 = LazyDiffraction2D(dask_array)
        s1.axes_manager.signal_axes[0].offset = -5
        s1.axes_manager.signal_axes[1].offset = -5
        s1_r = s1.radial_average()
        assert np.all(s1_r.data[:, :, 0] == 1)
        assert np.all(s1_r.data[:, :, 1:] == 0)
Ejemplo n.º 5
0
def get_hot_pixel_signal(lazy=False):
    """Get Diffraction2D signal with a disk in the middle.

    Has 4 pixels with value equal to 50000, to simulate hot pixels.

    Example
    -------
    >>> s = pxm.dummy_data.get_hot_pixel_signal()

    Lazy signal

    >>> s_lazy = pxm.dummy_data.get_hot_pixel_signal(lazy=True)

    """
    data = mdtd.MakeTestData(size_x=128, size_y=128, default=False, blur=True)
    data.add_disk(64, 64, r=30, intensity=10000)
    s = data.signal
    s.change_dtype("int64")
    s.data += gaussian_filter(s.data, sigma=50)
    s.data[76, 4] = 50000
    s.data[12, 102] = 50000
    s.data[32, 10] = 50000
    s.data[120, 61] = 50000
    if lazy:
        s = LazyDiffraction2D(s)
        s.data = da.from_array(s.data, chunks=(64, 64))
    else:
        s = Diffraction2D(s)
    return s
Ejemplo n.º 6
0
 def test_5d_data_compute(self):
     dask_array = da.random.random((2, 3, 4, 10, 15),
                                   chunks=(1, 1, 1, 10, 15))
     s = LazyDiffraction2D(dask_array)
     s.compute()
     assert s.__class__ == Diffraction2D
     assert dask_array.shape == s.data.shape
Ejemplo n.º 7
0
    def test_lazy(self):
        y, x = np.mgrid[75:83:9j, 85:95:11j]
        s = mdtd.generate_4d_data(
            probe_size_x=11,
            probe_size_y=9,
            ring_x=None,
            image_size_x=160,
            image_size_y=140,
            disk_x=x,
            disk_y=y,
            disk_r=40,
            disk_I=20,
            blur=True,
            blur_sigma=1,
            downscale=False,
        )
        s_lazy = LazyDiffraction2D(
            da.from_array(s.data, chunks=(1, 1, 140, 160)))
        s_lazy_com = s_lazy.center_of_mass()
        np.testing.assert_allclose(s_lazy_com.inav[0].data, x)
        np.testing.assert_allclose(s_lazy_com.inav[1].data, y)

        s_lazy_1d = s_lazy.inav[0]
        s_lazy_1d_com = s_lazy_1d.center_of_mass()
        np.testing.assert_allclose(s_lazy_1d_com.inav[0].data, x[:, 0])
        np.testing.assert_allclose(s_lazy_1d_com.inav[1].data, y[:, 0])

        s_lazy_0d = s_lazy.inav[0, 0]
        s_lazy_0d_com = s_lazy_0d.center_of_mass()
        np.testing.assert_allclose(s_lazy_0d_com.inav[0].data, x[0, 0])
        np.testing.assert_allclose(s_lazy_0d_com.inav[1].data, y[0, 0])
Ejemplo n.º 8
0
 def test_disk_radius(self):
     s = LazyDiffraction2D(
         da.random.randint(100, size=(5, 5, 30, 30), chunks=(1, 1, 10, 10)))
     s_template0 = s.template_match_disk(disk_r=2, lazy_result=False)
     s_template1 = s.template_match_disk(disk_r=4, lazy_result=False)
     assert s.data.shape == s_template0.data.shape
     assert s.data.shape == s_template1.data.shape
     assert not (s_template0.data == s_template1.data).all()
Ejemplo n.º 9
0
 def test_big_value(self):
     data_shape = (5, 40, 40)
     big_value = 50000000
     array0 = np.ones(shape=data_shape) * big_value
     dask_array = da.from_array(array0, chunks=(2, 10, 10))
     s0 = LazyDiffraction2D(dask_array)
     s0_r = s0.radial_average()
     assert s0_r.axes_manager.navigation_shape == data_shape[:1]
     assert (s0_r.data[:, :-1] == big_value).all()
Ejemplo n.º 10
0
    def test_rotate_diffraction_keep_shape(self):
        shape = (7, 5, 4, 15)
        s = Diffraction2D(np.zeros(shape))
        s_rot = s.rotate_diffraction(angle=45)
        assert s.axes_manager.shape == s_rot.axes_manager.shape

        s_lazy = LazyDiffraction2D(da.zeros(shape, chunks=(1, 1, 1, 1)))
        s_rot_lazy = s_lazy.rotate_diffraction(angle=45)
        assert s_lazy.axes_manager.shape == s_rot_lazy.axes_manager.shape
Ejemplo n.º 11
0
 def test_lazy(self):
     data = np.zeros((10, 10, 30, 30))
     x, y = 20, 10
     data[:, :, y, x] += 1
     data = da.from_array(data, chunks=(5, 5, 5, 5))
     s = LazyDiffraction2D(data)
     shift_x, shift_y = 4, 3
     s_shift = s.shift_diffraction(shift_x=shift_x, shift_y=shift_y)
     s_shift.compute()
     assert s_shift.data[0, 0, y - shift_y, x - shift_x] == 1
     s_shift.data[:, :, y - shift_y, x - shift_x] = 0
     assert s_shift.data.sum() == 0
Ejemplo n.º 12
0
 def test_2d_data_compute(self):
     dask_array = da.random.random((100, 150), chunks=(50, 50))
     s = LazyDiffraction2D(dask_array)
     scale0, scale1, metadata_string = 0.5, 1.5, "test"
     s.axes_manager[0].scale = scale0
     s.axes_manager[1].scale = scale1
     s.metadata.Test = metadata_string
     s.compute()
     assert s.__class__ == Diffraction2D
     assert not hasattr(s.data, "compute")
     assert s.axes_manager[0].scale == scale0
     assert s.axes_manager[1].scale == scale1
     assert s.metadata.Test == metadata_string
     assert dask_array.shape == s.data.shape
Ejemplo n.º 13
0
    def test_lazy_result(self):
        data = da.ones((10, 10, 20, 20), chunks=(10, 10, 10, 10))
        s_lazy = LazyDiffraction2D(data)
        s_lazy_com = s_lazy.center_of_mass(lazy_result=True)
        assert s_lazy_com._lazy
        assert s_lazy_com.axes_manager.signal_shape == (10, 10)

        s_lazy_1d = s_lazy.inav[0]
        s_lazy_1d_com = s_lazy_1d.center_of_mass(lazy_result=True)
        assert s_lazy_1d_com._lazy
        assert s_lazy_1d_com.axes_manager.signal_shape == (10, )

        s_lazy_0d = s_lazy.inav[0, 0]
        s_lazy_0d_com = s_lazy_0d.center_of_mass(lazy_result=True)
        assert s_lazy_0d_com._lazy
        assert s_lazy_0d_com.axes_manager.signal_shape == ()
Ejemplo n.º 14
0
 def test_correct_radius_random(self):
     x, y, px, py = 56, 48, 4, 5
     r = np.random.randint(20, 40, size=(py, px))
     s = mdtd.generate_4d_data(
         probe_size_x=px,
         probe_size_y=py,
         image_size_x=120,
         image_size_y=100,
         disk_I=0,
         ring_x=x,
         ring_y=y,
         ring_r=r,
         ring_I=5,
         blur=True,
         downscale=False,
     )
     dask_array = da.from_array(s.data, chunks=(4, 4, 50, 50))
     s = LazyDiffraction2D(dask_array)
     s.axes_manager.signal_axes[0].offset = -x
     s.axes_manager.signal_axes[1].offset = -y
     s_r = s.radial_average()
     assert (s_r.data.argmax(axis=-1) == r).all()
Ejemplo n.º 15
0
 def test_correct_radius_simple(self):
     x, y, r, px, py = 40, 51, 30, 4, 5
     s = mdtd.generate_4d_data(
         probe_size_x=px,
         probe_size_y=py,
         image_size_x=120,
         image_size_y=100,
         disk_I=0,
         ring_x=x,
         ring_y=y,
         ring_r=r,
         ring_I=5,
         blur=True,
         downscale=False,
     )
     dask_array = da.from_array(s.data, chunks=(4, 4, 50, 50))
     s = LazyDiffraction2D(dask_array)
     s.axes_manager.signal_axes[0].offset = -x
     s.axes_manager.signal_axes[1].offset = -y
     s_r = s.radial_average()
     assert s_r.axes_manager.navigation_shape == (px, py)
     assert (s_r.data.argmax(axis=-1) == 30).all()
Ejemplo n.º 16
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
Ejemplo n.º 17
0
 def test_different_shape(self):
     array = da.ones(shape=(7, 9, 30, 40), chunks=(3, 3, 5, 5))
     s = LazyDiffraction2D(array)
     s_r = s.radial_average()
     assert (s_r.data[:, :, :-2] == 1).all()
Ejemplo n.º 18
0
 def test_lazy_input(self):
     s = LazyDiffraction2D(
         da.random.randint(100, size=(5, 5, 20, 20), chunks=(1, 1, 10, 10)))
     s_template = s.template_match_disk()
     assert s.data.shape == s_template.data.shape
     assert s._lazy is True