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
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_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_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
def _get_ellipse_markers( ellipse_array, inlier_array=None, peak_array=None, nr=20, signal_axes=None, color_ellipse="blue", linewidth=1, linestyle="solid", color_inlier="blue", color_outlier="red", point_size=20, ): marker_list = _get_ellipse_marker_list_from_ellipse_array( ellipse_array, nr=nr, signal_axes=signal_axes, color=color_ellipse, linewidth=linewidth, linestyle=linestyle, ) if inlier_array is not None: inlier_parray, outlier_parray = _get_inlier_outlier_peak_arrays( peak_array, inlier_array) marker_in_list = mt._get_4d_points_marker_list(inlier_parray, signal_axes=signal_axes, color=color_inlier, size=point_size) marker_out_list = mt._get_4d_points_marker_list( outlier_parray, signal_axes=signal_axes, color=color_outlier, size=point_size, ) marker_list.extend(marker_in_list) marker_list.extend(marker_out_list) return marker_list
def test_simple(self): peak_array = np.empty((2, 3), dtype=object) peak_array[0, 0] = [[2, 4]] peak_array[0, 1] = [[8, 2]] peak_array[0, 2] = [[1, 8]] peak_array[1, 0] = [[3, 1]] peak_array[1, 1] = [[9, 1]] peak_array[1, 2] = [[6, 3]] 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") mt._add_permanent_markers_to_signal(s, marker_list) assert len(marker_list) == 1 marker = marker_list[0] assert marker.marker_properties["color"] == "red" s.plot() for iy, ix in np.ndindex(peak_array.shape[:2]): peak = peak_array[iy, ix] s.axes_manager.indices = (ix, iy) assert marker.get_data_position("x1") == peak[0][1] assert marker.get_data_position("y1") == peak[0][0]
def test_peak_finding_to_marker(): data = np.zeros(shape=(3, 2, 10, 12)) data[0, 0, 2, 7] = 1 data[0, 1, 7, 3] = 1 data[1, 0, 4, 6] = 1 data[1, 1, 2, 3] = 1 data[2, 0, 3, 6] = 1 data[2, 1, 2, 2] = 1 s = Diffraction2D(data) peak_array = s.find_peaks_lazy(min_sigma=0.1, max_sigma=2, threshold=0.01, lazy_result=False) marker_list = mt._get_4d_points_marker_list(peak_array, s.axes_manager.signal_axes) assert len(marker_list) == 1 marker = marker_list[0] mt._add_permanent_markers_to_signal(s, marker_list) s.plot() for ix, iy in s.axes_manager: px, py = marker.get_data_position("x1"), marker.get_data_position("y1") value = s.inav[ix, iy].isig[int(px), int(py)].data[0] assert value == 1.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
def _sorted_cluster_dict_to_marker_list( sorted_cluster_dict, signal_axes=None, color_centre="blue", color_rest="red", color_none="green", size=20, ): """Make a list of markers with different colors from a sorted cluster dict Parameters ---------- sorted_cluster_dict : dict dict with clusters sorted into 'centre', 'rest' and 'none' lists. signal_axes : HyperSpy axes_manager, optional color_centre, color_rest, color_none : string, optional Color of the markers. Default 'blue', 'red' and 'green'. size : scalar, optional Size of the markers. Returns ------- marker_list : list of HyperSpy markers Examples -------- >>> from numpy.random import randint >>> sorted_cluster_dict = {} >>> sorted_cluster_dict['centre'] = randint(10, size=(3, 4, 10, 2)) >>> sorted_cluster_dict['rest'] = randint(50, 60, size=(3, 4, 10, 2)) >>> sorted_cluster_dict['none'] = randint(90, size=(3, 4, 2, 2)) >>> import pyxem.utils.cluster_tools as ct >>> marker_list = ct._sorted_cluster_dict_to_marker_list( ... sorted_cluster_dict) >>> import pyxem.utils.marker_tools as mt >>> s = pxm.signals.Diffraction2D(np.random.random((3, 4, 100, 100))) >>> mt._add_permanent_markers_to_signal(s, marker_list) Different colors >>> marker_list = ct._sorted_cluster_dict_to_marker_list( ... sorted_cluster_dict, color_centre='green', color_rest='cyan', ... color_none='purple', size=15) """ marker_list = [] for label, cluster_list in sorted_cluster_dict.items(): if label == "centre": color = color_centre elif label == "rest": color = color_rest elif label == "none": color = color_none else: color = "cyan" temp_markers = mt._get_4d_points_marker_list(cluster_list, signal_axes=signal_axes, color=color, size=size) marker_list.extend(temp_markers) return marker_list