def test_navigator_kwarg(self): s = Diffraction2D( np.random.randint(0, 256, (8, 9, 10, 30), dtype=np.uint8)) plt.ion() # To make plotting non-blocking s_nav = Diffraction2D(np.zeros((8, 9))) s.plot(navigator=s_nav) plt.close("all")
def test_wrong_navigator_shape_kwarg(self): s = Diffraction2D( np.random.randint(0, 256, (8, 9, 10, 30), dtype=np.uint8)) plt.ion() # To make plotting non-blocking s_nav = Diffraction2D(np.zeros((2, 19))) s._navigator_probe = s_nav with pytest.raises(ValueError): s.plot()
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)
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
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()
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()
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
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()
def diffraction_pattern_for_azimuthal(self): """ Two diffraction patterns with easy to see radial profiles, wrapped in Diffraction2D <2|8,8> """ dp = Diffraction2D(np.zeros((2, 8, 8))) dp.data[0] = np.array([[0., 0., 2., 2., 2., 2., 0., 0.], [0., 2., 3., 3., 3., 3., 2., 0.], [2., 3., 3., 4., 4., 3., 3., 2.], [2., 3., 4., 5., 5., 4., 3., 2.], [2., 3., 4., 5., 5., 4., 3., 2.], [2., 3., 3., 4., 4., 3., 3., 2.], [0., 2., 3., 3., 3., 3., 2., 0.], [0., 0., 2., 2., 2., 2., 0., 0.]]) dp.data[1] = np.array([[0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.]]) return dp
def test_simple(self): s = Diffraction2D(np.zeros((2, 3, 100, 100))) peak_array = np.empty((2, 3), dtype=np.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)
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)
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
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)
def axes_test_dp(self): """ Two diffraction patterns with easy to see radial profiles, wrapped in Diffraction2D <2,2|3,3> """ dp = Diffraction2D(np.zeros((2, 2, 3, 3))) return dp
def dp_for_azimuthal(): """ Two diffraction patterns with easy to see radial profiles, wrapped in Diffraction2D <2|8,8> """ dp = Diffraction2D(np.zeros((2, 8, 8))) dp.data[0] = np.array([ [0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 2.0, 3.0, 3.0, 3.0, 3.0, 2.0, 0.0], [2.0, 3.0, 3.0, 4.0, 4.0, 3.0, 3.0, 2.0], [2.0, 3.0, 4.0, 5.0, 5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 4.0, 5.0, 5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 4.0, 4.0, 3.0, 3.0, 2.0], [0.0, 2.0, 3.0, 3.0, 3.0, 3.0, 2.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 0.0, 0.0], ]) dp.data[1] = np.array([ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], ]) return dp
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(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] if af < bf: rf += math.pi / 2 af, bf = bf, af assert approx((xcf, ycf, af, bf, rf), abs=0.1) == [xc, yc, a, b, r] 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
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)
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
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
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 = ps.dummy_data.get_hot_pixel_signal() Lazy signal >>> s_lazy = ps.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
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
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
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"
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()
def ones(self): ones_diff = Diffraction2D(data=np.ones(shape=(10, 10))) ones_diff.axes_manager.signal_axes[0].scale = 0.1 ones_diff.axes_manager.signal_axes[1].scale = 0.1 ones_diff.axes_manager.signal_axes[0].name = "kx" ones_diff.axes_manager.signal_axes[1].name = "ky" ones_diff.unit = "2th_deg" return ones_diff
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
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()
def test_several_markers_different_peak_array_size(self): peak_array = np.empty((2, 3), dtype=np.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
def test_color(self): s = Diffraction2D(np.zeros((2, 3, 100, 100))) peak_array = np.empty((2, 3), dtype=np.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"
def test_dead_pixel_camera_edge(self): s = Diffraction2D(np.ones((10, 10, 10, 10))) s.data[:, :, 0, 5] = 0 s.data[:, :, 2, 0] = 0 s.data[:, :, 2, -1] = 0 s.data[:, :, -1, 7] = 0 s_orig = s.deepcopy() pst.find_and_remove_dead_pixels(s) assert (s.data == s_orig.data).all()