Ejemplo n.º 1
0
 def test_fit_single_ellipse_to_signal_rotation(self):
     rot_list = [
         -np.pi / 16,
         -np.pi / 8,
         -np.pi / 4,
         -np.pi / 2,
         -0.1,
         0.1,
         np.pi / 16,
         np.pi / 8,
         np.pi / 4,
         np.pi / 2,
         np.pi + 0.1,
         np.pi * 2 + 0.1,
         np.pi * 2.5,
         np.pi * 3 + 0.1,
         np.pi * 3.2,
     ]
     for rot in rot_list:
         s = Diffraction2D(np.zeros((200, 200)))
         s.axes_manager[0].offset, s.axes_manager[1].offset = -100, -100
         xx, yy = np.meshgrid(s.axes_manager[0].axis,
                              s.axes_manager[1].axis)
         s.data += mdtd._get_elliptical_ring(xx,
                                             yy,
                                             0,
                                             0,
                                             70,
                                             60,
                                             rot,
                                             lw_r=1)
         output = ra.fit_single_ellipse_to_signal(s, (50, 80),
                                                  angleN=10,
                                                  show_progressbar=False)
         output_rot = output[5] % np.pi
         assert approx(output_rot, abs=0.1) == (rot % np.pi)
     for rot in rot_list:
         s = Diffraction2D(np.zeros((200, 200)))
         s.axes_manager[0].offset, s.axes_manager[1].offset = -100, -100
         xx, yy = np.meshgrid(s.axes_manager[0].axis,
                              s.axes_manager[1].axis)
         s.data += mdtd._get_elliptical_ring(xx,
                                             yy,
                                             0,
                                             0,
                                             60,
                                             70,
                                             rot,
                                             lw_r=1)
         output = ra.fit_single_ellipse_to_signal(s, (50, 80),
                                                  angleN=10,
                                                  show_progressbar=False)
         output_rot = (output[5] + np.pi / 2) % np.pi
         assert approx(output_rot, abs=0.1) == (rot % np.pi)
Ejemplo n.º 2
0
    def test_create(self):
        array0 = np.zeros(shape=(10, 10, 10, 10))
        s0 = Diffraction2D(array0)
        assert array0.shape == s0.axes_manager.shape

        # This should fail due to Diffraction2D inheriting
        # signal2D, i.e. the data has to be at least
        # 2-dimensions
        with pytest.raises(ValueError):
            Diffraction2D(np.zeros(10))

        array1 = np.zeros(shape=(10, 10))
        s1 = Diffraction2D(array1)
        assert array1.shape == s1.axes_manager.shape
Ejemplo n.º 3
0
    def test_axes_manager_copy(self):
        s = Diffraction2D(np.random.randint(100, size=(5, 5, 20, 20)))
        ax_sa = s.axes_manager.signal_axes
        ax_na = s.axes_manager.navigation_axes
        ax_sa[0].name, ax_sa[1].name = "Detector x", "Detector y"
        ax_sa[0].scale, ax_sa[1].scale = 0.2, 0.2
        ax_sa[0].offset, ax_sa[1].offset = 10, 20
        ax_sa[0].units, ax_sa[1].units = "mrad", "mrad"
        ax_na[0].name, ax_na[1].name = "Probe x", "Probe y"
        ax_na[0].scale, ax_na[1].scale = 35, 35
        ax_na[0].offset, ax_na[1].offset = 54, 12
        ax_na[0].units, ax_na[1].units = "nm", "nm"
        s_temp = s.template_match_disk()
        assert s.data.shape == s_temp.data.shape
        ax_sa_t = s_temp.axes_manager.signal_axes
        ax_na_t = s_temp.axes_manager.navigation_axes
        assert ax_sa[0].name == ax_sa_t[0].name
        assert ax_sa[1].name == ax_sa_t[1].name
        assert ax_sa[0].scale == ax_sa_t[0].scale
        assert ax_sa[1].scale == ax_sa_t[1].scale
        assert ax_sa[0].offset == ax_sa_t[0].offset
        assert ax_sa[1].offset == ax_sa_t[1].offset
        assert ax_sa[0].units == ax_sa_t[0].units
        assert ax_sa[1].units == ax_sa_t[1].units

        assert ax_na[0].name == ax_na_t[0].name
        assert ax_na[1].name == ax_na_t[1].name
        assert ax_na[0].scale == ax_na_t[0].scale
        assert ax_na[1].scale == ax_na_t[1].scale
        assert ax_na[0].offset == ax_na_t[0].offset
        assert ax_na[1].offset == ax_na_t[1].offset
        assert ax_na[0].units == ax_na_t[0].units
        assert ax_na[1].units == ax_na_t[1].units
Ejemplo n.º 4
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.º 5
0
 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
Ejemplo n.º 6
0
 def test_simple(self):
     peak_dicts = {}
     peak_dicts["centre"] = randint(124, 132, size=(3, 4, 10, 2))
     peak_dicts["rest"] = randint(204, 212, size=(3, 4, 5, 2))
     peak_dicts["none"] = randint(10, 13, size=(3, 4, 2, 2))
     s = Diffraction2D(np.zeros((3, 4, 256, 256)))
     ct._add_peak_dicts_to_signal(s, peak_dicts)
Ejemplo n.º 7
0
 def test_size(self):
     size = 17
     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, size=size)
     marker = list(s.metadata.Markers)[0][1]
     assert marker.get_data_position("size") == size
Ejemplo n.º 8
0
    def test_get_angle_sector_mask_radial_average1(self):
        x, y = 4.5, 4.5
        array = np.zeros((10, 10, 10, 10))
        array[:, :, 0:5, 0:5] = 1
        centre_x_array = np.ones_like(array) * x
        centre_y_array = np.ones_like(array) * y
        s = Diffraction2D(array)
        s.axes_manager.signal_axes[0].offset = -x
        s.axes_manager.signal_axes[1].offset = -y
        mask0 = s.angular_mask(0.0, 0.5 * np.pi)
        s_r0 = s.radial_average(centre_x=centre_x_array,
                                centre_y=centre_y_array,
                                mask_array=mask0)
        assert np.all(s_r0.isig[0:6].data == 1.0)

        mask1 = s.angular_mask(0, np.pi)
        s_r1 = s.radial_average(centre_x=centre_x_array,
                                centre_y=centre_y_array,
                                mask_array=mask1)
        assert np.all(s_r1.isig[0:6].data == 0.5)

        mask2 = s.angular_mask(0.0, 2 * np.pi)
        s_r2 = s.radial_average(centre_x=centre_x_array,
                                centre_y=centre_y_array,
                                mask_array=mask2)
        assert np.all(s_r2.isig[0:6].data == 0.25)

        mask3 = s.angular_mask(np.pi, 2 * np.pi)
        s_r3 = s.radial_average(centre_x=centre_x_array,
                                centre_y=centre_y_array,
                                mask_array=mask3)
        assert np.all(s_r3.data == 0.0)
Ejemplo n.º 9
0
 def test_dask_input(self):
     s = Diffraction2D(np.zeros((2, 3, 20, 20)))
     peak_array = da.zeros((2, 3, 10, 2), chunks=(1, 1, 10, 2))
     with pytest.raises(ValueError):
         s.add_peak_array_as_markers(peak_array)
     peak_array_computed = peak_array.compute()
     s.add_peak_array_as_markers(peak_array_computed)
Ejemplo n.º 10
0
def test_vi_generator_get_virtual_images_from_mesh(nav_shape):
    n = np.prod(nav_shape) * 32**2
    s = Diffraction2D(np.arange(n).reshape((*nav_shape, 32, 32)))
    vi_generator = VirtualImageGenerator(s)

    with pytest.raises(ValueError):
        vi_generator.get_virtual_images_from_mesh()

    for axis in s.axes_manager.signal_axes:
        axis.scale = 0.1
        axis.offset = -1.6
        axis.units = "1/nm"

    vi_generator.set_ROI_mesh(0.5, 0.6, 1.4)
    vi = vi_generator.get_virtual_images_from_mesh()
    vi_nav_axis = vi.axes_manager.navigation_axes[0]
    assert vi_nav_axis.size == 21
    assert vi_nav_axis.name == "ROI index"
    assert vi_nav_axis.scale == 1.0
    assert len(vi_generator.roi_list) == 21

    vi_generator.set_ROI_mesh(0.5, 0.6, 1.0)
    assert len(vi_generator.roi_list) == 11

    vi = vi_generator.get_virtual_images_from_mesh(normalize=True)
Ejemplo n.º 11
0
 def test_scale_offset(self):
     s = Diffraction2D(np.zeros((50, 60)))
     axis = s.axes_manager[-1]
     axis.scale = 0.5
     axis.offset = 6
     value = mt._pixel_to_scaled_value(axis, 4.5)
     assert value == 8.25
Ejemplo n.º 12
0
 def test_nav_1(self):
     data_shape = (5, 40, 40)
     array0 = np.ones(shape=data_shape)
     s0 = Diffraction2D(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.º 13
0
    def test_bool_array(self):
        peak_array = np.empty((2, 3), dtype=object)
        bool_array = np.empty((2, 3), dtype=object)
        for ix, iy in np.ndindex(peak_array.shape):
            peak_array[ix, iy] = np.random.randint(9, size=(1, 2))
            bool_array[ix, iy] = np.random.randint(0, 2, size=1, dtype=bool)

        s = Diffraction2D(np.zeros(shape=(2, 3, 10, 10)))
        marker_list = mt._get_4d_points_marker_list(peak_array,
                                                    s.axes_manager.signal_axes,
                                                    color="red",
                                                    bool_array=bool_array)
        mt._add_permanent_markers_to_signal(s, marker_list)
        marker = marker_list[0]
        s.plot()

        for iy, ix in np.ndindex(peak_array.shape[:2]):
            peak = peak_array[iy, ix][0]
            boolean = bool_array[iy, ix][0]
            s.axes_manager.indices = (ix, iy)
            if boolean:
                assert marker.get_data_position("x1") == peak[1]
                assert marker.get_data_position("y1") == peak[0]
            else:
                assert marker.get_data_position("x1") == -1000.0
                assert marker.get_data_position("y1") == -1000.0
Ejemplo n.º 14
0
 def test_nav_0(self):
     data_shape = (40, 40)
     array0 = np.ones(shape=data_shape)
     s0 = Diffraction2D(array0)
     s0_r = s0.radial_average()
     assert s0_r.axes_manager.navigation_dimension == 0
     assert (s0_r.data[:-1] == 1).all()
Ejemplo n.º 15
0
 def test_threshold(self, x, y):
     s = Diffraction2D(np.random.randint(0, 10, size=(10, 10, 10, 10)))
     s.data[:, :, x, y] = 1000000
     s1 = s.threshold_and_mask(threshold=1)
     assert (s1.data[:, :, x, y] == 1.0).all()
     s1.data[:, :, x, y] = 0
     assert (s1.data == 0).all()
Ejemplo n.º 16
0
 def test_color(self):
     color = "blue"
     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, color=color)
     marker = list(s.metadata.Markers)[0][1]
     assert marker.marker_properties["color"] == color
Ejemplo n.º 17
0
 def test_simple(self):
     s = Diffraction2D(np.zeros((2, 3, 100, 100)))
     peak_array = np.empty((2, 3), dtype=object)
     for index in np.ndindex(peak_array.shape):
         islice = np.s_[index]
         peak_array[islice] = np.random.randint(20, 80, (100, 2))
     s.add_peak_array_as_markers(peak_array)
Ejemplo n.º 18
0
 def test_big_value(self):
     data_shape = (5, 40, 40)
     big_value = 50000000
     array0 = np.ones(shape=data_shape) * big_value
     s0 = Diffraction2D(array0)
     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.º 19
0
 def test_inplace(self):
     s = Diffraction2D(np.zeros((10, 10, 30, 40)))
     x, y, shift_x, shift_y = 20, 10, 4, -3
     s.data[:, :, y, x] = 1
     s.shift_diffraction(shift_x=shift_x, shift_y=shift_y, inplace=True)
     assert s.data[0, 0, y - shift_y, x - shift_x] == 1
     s.data[:, :, y - shift_y, x - shift_x] = 0
     assert s.data.sum() == 0
Ejemplo n.º 20
0
 def test_single_shift(self, shift_x, shift_y):
     s = Diffraction2D(np.zeros((10, 10, 30, 40)))
     x, y = 20, 10
     s.data[:, :, y, x] = 1
     s_shift = s.shift_diffraction(shift_x=shift_x, shift_y=shift_y)
     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.º 21
0
 def test_flip_y(self):
     array = np.zeros(shape=(3, 4, 6, 10))
     array[:, :, 3:, :] = 1
     s = Diffraction2D(array)
     assert (s.data[:, :, 3:, :] == 1).all()
     s_flip = s.flip_diffraction_y()
     assert (s_flip.data[:, :, 3:, :] == 0).all()
     assert (s_flip.data[:, :, :3, :] == 1).all()
Ejemplo n.º 22
0
 def test_size(self):
     size = 12
     peak_array = np.zeros(shape=(3, 2, 1, 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,
                                                 size=size)
     assert marker_list[0].get_data_position("size") == size
Ejemplo n.º 23
0
 def test_color(self):
     color = "blue"
     peak_array = np.zeros(shape=(3, 2, 1, 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,
                                                 color=color)
     assert marker_list[0].marker_properties["color"] == "blue"
Ejemplo n.º 24
0
def test_full_ellipse_ransac_processing():
    xf, yf, a, b, r, nt = 100, 115, 45, 35, 0, 15
    data_points = ret.make_ellipse_data_points(xf, yf, a, b, r, nt)
    image = np.zeros(shape=(200, 210), dtype=np.float32)
    for x, y in data_points:
        image[int(round(x)), int(round(y))] = 100
    disk = morphology.disk(5, np.uint16)
    image = convolve2d(image, disk, mode="same")
    data = np.zeros((2, 3, 210, 200), dtype=np.float32)
    data[:, :] = image.T

    s = Diffraction2D(data)
    s_t = s.template_match_disk(disk_r=5)
    peak_array = s_t.find_peaks_lazy(threshold=0.1, lazy_result=False)

    c = math.sqrt(math.pow(a, 2) - math.pow(b, 2))
    xc, yc = xf - c * math.cos(r), yf - c * math.sin(r)

    for iy, ix in np.ndindex(peak_array.shape):
        peaks = peak_array[iy, ix]
        assert len(peaks) == 15
        assert approx(peaks[:, 1].mean(), abs=2) == xc
        assert approx(peaks[:, 0].mean(), abs=2) == yc
        assert approx(peaks[:, 1].max(), abs=2) == xc + a
        assert approx(peaks[:, 0].max(), abs=2) == yc + b
        assert approx(peaks[:, 1].min(), abs=2) == xc - a
        assert approx(peaks[:, 0].min(), abs=2) == yc - b

    ellipse_array, inlier_array = ret.get_ellipse_model_ransac(
        peak_array,
        yf=yf,
        xf=xf,
        rf_lim=15,
        semi_len_min=min(a, b) - 5,
        semi_len_max=max(a, b) + 5,
        semi_len_ratio_lim=5,
        max_trails=50,
        min_samples=10,
    )
    s.add_ellipse_array_as_markers(ellipse_array)

    for iy, ix in np.ndindex(ellipse_array.shape):
        ycf, xcf, bf, af, rf = ellipse_array[iy, ix]
        compare_model_params((xcf, ycf, af, bf, rf), (xc, yc, a, b, r),
                             abs=0.1)
        assert inlier_array[iy, ix].all()

    s.add_ellipse_array_as_markers(ellipse_array)
    x_list, y_list = [], []
    for _, marker in list(s.metadata.Markers):
        x_list.append(marker.data["x1"][()][0][0])
        y_list.append(marker.data["y1"][()][0][0])
    assert approx(np.mean(x_list), abs=1) == xc
    assert approx(np.mean(y_list), abs=1) == yc
    assert approx(np.max(x_list), abs=1) == xc + a
    assert approx(np.max(y_list), abs=1) == yc + b
    assert approx(np.min(x_list), abs=1) == xc - a
    assert approx(np.min(y_list), abs=1) == yc - b
Ejemplo n.º 25
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.º 26
0
 def test_size(self):
     s = Diffraction2D(np.zeros((2, 3, 100, 100)))
     peak_array = np.empty((2, 3), dtype=object)
     for index in np.ndindex(peak_array.shape):
         islice = np.s_[index]
         peak_array[islice] = np.random.randint(20, 80, (100, 2))
     s.add_peak_array_as_markers(peak_array, size=13)
     marker0 = list(s.metadata.Markers)[9][1]
     assert marker0.get_data_position("size") == 13
Ejemplo n.º 27
0
 def test_3d_nav_dims(self):
     s = Diffraction2D(np.zeros((2, 3, 4, 100, 100)))
     peak_array = np.empty((2, 3, 4), dtype=object)
     for index in np.ndindex(peak_array.shape):
         islice = np.s_[index]
         peak_array[islice] = np.random.randint(20, 80, (100, 2))
     s.add_peak_array_as_markers(peak_array)
     marker = list(s.metadata.Markers)[0][1]
     assert marker.data["x1"][()].shape == (2, 3, 4)
Ejemplo n.º 28
0
 def test_color(self):
     s = Diffraction2D(np.zeros((2, 3, 100, 100)))
     peak_array = np.empty((2, 3), dtype=object)
     for index in np.ndindex(peak_array.shape):
         islice = np.s_[index]
         peak_array[islice] = np.random.randint(20, 80, (100, 2))
     s.add_peak_array_as_markers(peak_array, color="blue")
     marker0 = list(s.metadata.Markers)[9][1]
     assert marker0.marker_properties["color"] == "blue"
Ejemplo n.º 29
0
 def test_several_markers_different_peak_array_size(self):
     peak_array = np.empty((2, 3), dtype=object)
     peak_array[0, 0] = [[2, 4], [1, 9]]
     peak_array[0, 1] = [[8, 2]]
     s = Diffraction2D(np.zeros(shape=(2, 3, 10, 10)))
     marker_list = mt._get_4d_points_marker_list(peak_array,
                                                 s.axes_manager.signal_axes,
                                                 color="red")
     assert len(marker_list) == 2
Ejemplo n.º 30
0
 def test_get_angle_sector_mask_simple(self):
     array = np.zeros((10, 10, 10, 10))
     array[:, :, 0:5, 0:5] = 1
     s = Diffraction2D(array)
     s.axes_manager.signal_axes[0].offset = -4.5
     s.axes_manager.signal_axes[1].offset = -4.5
     mask = s.angular_mask(0.0, 0.5 * np.pi)
     assert mask[:, :, 0:5, 0:5].all()
     assert not mask[:, :, 5:, :].any()
     assert not mask[:, :, :, 5:].any()