def _test_plot_rectange_markers(): # Create test image 100x100 pixels: im = Signal2D(np.arange(100).reshape([10, 10])) # Add four line markers: m1 = markers.line_segment( x1=2, y1=2, x2=7, y2=2, color='red', linewidth=3) m2 = markers.line_segment( x1=2, y1=2, x2=2, y2=7, color='red', linewidth=3) m3 = markers.line_segment( x1=2, y1=7, x2=7, y2=7, color='red', linewidth=3) m4 = markers.line_segment( x1=7, y1=2, x2=7, y2=7, color='red', linewidth=3) # Add rectangle marker at same position: m = markers.rectangle(x1=2, x2=7, y1=2, y2=7, linewidth=4, color='blue', ls='dotted') # Plot image and add markers to img: im.plot() im.add_marker(m) im.add_marker(m1) im.add_marker(m2) im.add_marker(m3) im.add_marker(m4) return im
def _get_marker_list(ellipse_parameters, x_list=None, y_list=None, name=None, r_scale=0.05): xC, yC, semi_len0, semi_len1, rot, ecce = _get_ellipse_parameters( ellipse_parameters) xx, yy = _get_ellipse_from_parameters(xC, yC, semi_len0, semi_len1, rot, r_scale=r_scale) marker_list = [] if x_list is not None: for x, y in zip(x_list, y_list): point_marker = point(x, y, color="red") if name is not None: point_marker.name = name + "_" + point_marker.name marker_list.append(point_marker) for i in range(len(xx)): if i == (len(xx) - 1): line = line_segment(xx[i], yy[i], xx[0], yy[0], color="green") else: line = line_segment(xx[i], yy[i], xx[i + 1], yy[i + 1], color="green") if name is not None: line.name = name + "_" + line.name marker_list.append(line) return marker_list
def get_line_segment_list(lines: Union[list, np.ndarray], **kwargs) -> list: """Return a list of line segment markers. Parameters ---------- lines On the form [[x00, y00, x01, y01], [x10, y10, x11, y11], ...]. kwargs Keyword arguments allowed by :func:`matplotlib.pyplot.axvline`. Returns ------- marker_list : list List of :class:`hyperspy.drawing._markers.line_segment.LineSegment`. """ lines = np.asarray(lines) if lines.ndim == 1: # No navigation shape, one line lines = lines[np.newaxis, ...] marker_list = [] for i in range(lines.shape[-2]): # Iterate over bands if not np.allclose(lines[..., i, :], np.nan, equal_nan=True): x1 = lines[..., i, 0] y1 = lines[..., i, 1] x2 = lines[..., i, 2] y2 = lines[..., i, 3] marker_list.append( line_segment(x1=x1, y1=y1, x2=x2, y2=y2, **kwargs)) return marker_list
def test_plot_markers_mpl_options(): # check if required parameters are shown in repr _test_plot_markers_repr(markers.arrow(10, 20, 30, 40), ['x1', 'y1', 'x2', 'y2', 'edgecolor', 'arrowstyle']) _test_plot_markers_repr(markers.ellipse(10, 20, 30, 40, color='red'), ['x', 'y', 'width', 'height', 'edgecolor']) _test_plot_markers_repr(markers.horizontal_line(10), ['y', 'color']) _test_plot_markers_repr(markers.horizontal_line_segment(10, 20, 30), ['x1', 'x2', 'y', 'color']) _test_plot_markers_repr(markers.line_segment(10, 20, 30,40), ['x1', 'x2', 'y1', 'y2', 'color']) _test_plot_markers_repr(markers.point(10, 20), ['x', 'x', 'color', 'size']) m = markers.rectangle(10, 20, 30, 40, color='red') _test_plot_markers_repr(m, ['edgecolor']) # check if 'color' property is converted to 'edgecolor' assert 'color' not in m.marker_properties assert 'edgecolor' in m.marker_properties assert m.marker_properties['edgecolor'] == 'red' _test_plot_markers_repr(markers.text(10,20,"test"), ['x', 'y', 'text', 'color']) _test_plot_markers_repr(markers.vertical_line(10), ['x', 'color']) m = markers.vertical_line_segment(10, 20, 30) _test_plot_markers_repr(m,['x', 'y1', 'y2', 'color'])
def test_save_load_permanent_marker_all_types(self): x1, y1, x2, y2 = 5, 2, 1, 8 s = Signal2D(np.arange(100).reshape(10, 10)) m0_list = [ markers.point(x=x1, y=y1), markers.horizontal_line(y=y1), markers.horizontal_line_segment(x1=x1, x2=x2, y=y1), markers.line_segment(x1=x1, x2=x2, y1=y1, y2=y2), markers.rectangle(x1=x1, x2=x2, y1=y1, y2=y2), markers.text(x=x1, y=y1, text="test"), markers.vertical_line(x=x1), markers.vertical_line_segment(x=x1, y1=y1, y2=y2), ] for m in m0_list: s.add_marker(m, permanent=True) with tempfile.TemporaryDirectory() as tmp: filename = tmp + '/testallmarkersfile.hdf5' s.save(filename) s1 = load(filename) markers_dict = s1.metadata.Markers m0_dict_list = [] m1_dict_list = [] for m in m0_list: m0_dict_list.append(san_dict(m._to_dictionary())) m1_dict_list.append( san_dict(markers_dict.get_item(m.name)._to_dictionary())) assert len(list(s1.metadata.Markers)) == 8 for m0_dict, m1_dict in zip(m0_dict_list, m1_dict_list): assert m0_dict == m1_dict
def test_add_several_permanent_markers(self): s = Signal1D(np.arange(10)) m_point = markers.point(x=5, y=5) m_line = markers.line_segment(x1=5, x2=10, y1=5, y2=10) m_vline = markers.vertical_line(x=5) m_vline_segment = markers.vertical_line_segment(x=4, y1=3, y2=6) m_hline = markers.horizontal_line(y=5) m_hline_segment = markers.horizontal_line_segment(x1=1, x2=9, y=5) m_rect = markers.rectangle(x1=1, x2=3, y1=5, y2=10) m_text = markers.text(x=1, y=5, text="test") m_arrow = markers.arrow(x1=4, y1=5, x2=6, y2=6, arrowstyle='<->') m_ellipse = markers.ellipse(x=10, y=11, width=4, height=6) s.add_marker(m_point, permanent=True) s.add_marker(m_line, permanent=True) s.add_marker(m_vline, permanent=True) s.add_marker(m_vline_segment, permanent=True) s.add_marker(m_hline, permanent=True) s.add_marker(m_hline_segment, permanent=True) s.add_marker(m_rect, permanent=True) s.add_marker(m_text, permanent=True) s.add_marker(m_arrow, permanent=True) s.add_marker(m_ellipse, permanent=True) assert len(list(s.metadata.Markers)) == 10 with pytest.raises(ValueError): s.add_marker(m_rect, permanent=True)
def get_line_segment_list(lines: Union[list, np.ndarray], **kwargs) -> list: """Return a list of line segment markers. Parameters ---------- lines : On the form [[x00, y00, x01, y01], [x10, y10, x11, y11], ...]. kwargs : Keyword arguments allowed by :class:`matplotlib.pyplot.axvline`. Returns ------- marker_list : List of :class:`hyperspy.utils.markers.line_segment`. """ lines = np.asarray(lines) if lines.ndim == 1: lines = lines[np.newaxis, ...] marker_list = [] for i in range(lines.shape[-2]): # Iterate over bands # TODO: Exclude np.nan bands (not visible in that pattern) x1 = lines[..., i, 0] y1 = lines[..., i, 1] x2 = lines[..., i, 2] y2 = lines[..., i, 3] marker_list.append(line_segment(x1=x1, y1=y1, x2=x2, y2=y2, **kwargs)) return marker_list
def test_save_load_permanent_marker_all_types(self, tmp_path, file): filename = tmp_path / file s = self.s x1, y1, x2, y2 = 5, 2, 1, 8 m0_list = [ markers.point(x=x1, y=y1), markers.horizontal_line(y=y1), markers.horizontal_line_segment(x1=x1, x2=x2, y=y1), markers.line_segment(x1=x1, x2=x2, y1=y1, y2=y2), markers.rectangle(x1=x1, x2=x2, y1=y1, y2=y2), markers.text(x=x1, y=y1, text="test"), markers.vertical_line(x=x1), markers.vertical_line_segment(x=x1, y1=y1, y2=y2), ] for m in m0_list: s.add_marker(m, permanent=True) s.save(filename) s1 = load(filename) markers_dict = s1.metadata.Markers m0_dict_list = [] m1_dict_list = [] for m in m0_list: m0_dict_list.append(san_dict(m._to_dictionary())) m1_dict_list.append( san_dict(markers_dict.get_item(m.name)._to_dictionary())) assert len(list(s1.metadata.Markers)) == 8 for m0_dict, m1_dict in zip(m0_dict_list, m1_dict_list): assert m0_dict == m1_dict
def test_get_data(self): s = Image(np.zeros([3, 2, 2])) m = markers.line_segment(x1=range(3), x2=range(3), y1=1.3, y2=1.5) m.axes_manager = s.axes_manager nose.tools.assert_true(m.get_data_position('x1') == 0) nose.tools.assert_true(m.get_data_position('y1') == 1.3) s.axes_manager[0].index = 2 nose.tools.assert_true(m.get_data_position('x1') == 2) nose.tools.assert_true(m.get_data_position('y1') == 1.3)
def test_get_data_array(self): s = Image(np.zeros([2, 2, 2, 2])) m = markers.line_segment(x1=[[1.1, 1.2], [1.3, 1.4]], x2=1.1, y1=1.3, y2=1.5) m.axes_manager = s.axes_manager nose.tools.assert_true(m.get_data_position('x1') == 1.1) s.axes_manager[0].index = 1 nose.tools.assert_true(m.get_data_position('x1') == 1.2) s.axes_manager[1].index = 1 nose.tools.assert_true(m.get_data_position('x1') == 1.4)
def test_get_data_array(self): s = Signal2D(np.zeros([2, 2, 2, 2])) m = markers.line_segment(x1=[[1.1, 1.2], [1.3, 1.4]], x2=1.1, y1=1.3, y2=1.5) m.axes_manager = s.axes_manager assert m.get_data_position('x1') == 1.1 s.axes_manager[0].index = 1 assert m.get_data_position('x1') == 1.2 s.axes_manager[1].index = 1 assert m.get_data_position('x1') == 1.4
def test_get_data(self): s = Signal2D(np.zeros([3, 2, 2])) m = markers.line_segment(x1=list(range(3)), x2=list(range(3)), y1=1.3, y2=1.5) m.axes_manager = s.axes_manager nose.tools.assert_equal(m.get_data_position('x1'), 0) nose.tools.assert_equal(m.get_data_position('y1'), 1.3) s.axes_manager[0].index = 2 nose.tools.assert_equal(m.get_data_position('x1'), 2) nose.tools.assert_equal(m.get_data_position('y1'), 1.3)
def test_get_data(self): s = Signal2D(np.zeros([3, 2, 2])) m = markers.line_segment(x1=list(range(3)), x2=list(range(3)), y1=1.3, y2=1.5) m.axes_manager = s.axes_manager assert m.get_data_position('x1') == 0 assert m.get_data_position('y1') == 1.3 s.axes_manager[0].index = 2 assert m.get_data_position('x1') == 2 assert m.get_data_position('y1') == 1.3
def test_dict2marker(self): m_point0 = markers.point(x=5, y=5) m_point1 = markers.point(x=(5, 10), y=(1, 5)) m_line = markers.line_segment(x1=5, x2=10, y1=5, y2=10) m_vline = markers.vertical_line(x=5) m_vline_segment = markers.vertical_line_segment(x=4, y1=3, y2=6) m_hline = markers.horizontal_line(y=5) m_hline_segment = markers.horizontal_line_segment(x1=1, x2=9, y=5) m_rect = markers.rectangle(x1=1, x2=3, y1=5, y2=10) m_text = markers.text(x=1, y=5, text="test") m_point0_new = dict2marker(m_point0._to_dictionary(), m_point0.name) m_point1_new = dict2marker(m_point1._to_dictionary(), m_point1.name) m_line_new = dict2marker(m_line._to_dictionary(), m_line.name) m_vline_new = dict2marker(m_vline._to_dictionary(), m_vline.name) m_vline_segment_new = dict2marker( m_vline_segment._to_dictionary(), m_vline_segment.name) m_hline_new = dict2marker(m_hline._to_dictionary(), m_hline.name) m_hline_segment_new = dict2marker( m_hline_segment._to_dictionary(), m_hline_segment.name) m_rect_new = dict2marker(m_rect._to_dictionary(), m_rect.name) m_text_new = dict2marker(m_text._to_dictionary(), m_text.name) m_point0_dict = sanitize_dict(m_point0._to_dictionary()) m_point1_dict = sanitize_dict(m_point1._to_dictionary()) m_line_dict = sanitize_dict(m_line._to_dictionary()) m_vline_dict = sanitize_dict(m_vline._to_dictionary()) m_vline_segment_dict = sanitize_dict(m_vline_segment._to_dictionary()) m_hline_dict = sanitize_dict(m_hline._to_dictionary()) m_hline_segment_dict = sanitize_dict(m_hline_segment._to_dictionary()) m_rect_dict = sanitize_dict(m_rect._to_dictionary()) m_text_dict = sanitize_dict(m_text._to_dictionary()) m_point0_new_dict = sanitize_dict(m_point0_new._to_dictionary()) m_point1_new_dict = sanitize_dict(m_point1_new._to_dictionary()) m_line_new_dict = sanitize_dict(m_line_new._to_dictionary()) m_vline_new_dict = sanitize_dict(m_vline_new._to_dictionary()) m_vline_segment_new_dict = sanitize_dict( m_vline_segment_new._to_dictionary()) m_hline_new_dict = sanitize_dict(m_hline_new._to_dictionary()) m_hline_segment_new_dict = sanitize_dict( m_hline_segment_new._to_dictionary()) m_rect_new_dict = sanitize_dict(m_rect_new._to_dictionary()) m_text_new_dict = sanitize_dict(m_text_new._to_dictionary()) assert m_point0_dict == m_point0_new_dict assert m_point1_dict == m_point1_new_dict assert m_line_dict == m_line_new_dict assert m_vline_dict == m_vline_new_dict assert m_vline_segment_dict == m_vline_segment_new_dict assert m_hline_dict == m_hline_new_dict assert m_hline_segment_dict == m_hline_segment_new_dict assert m_rect_dict == m_rect_new_dict assert m_text_dict == m_text_new_dict
def test_markers_auto_update(): # test data for fixed marker pos = [1, 2, 3, 4] # test data for auto_update marker pos_list = np.array([[1, 3, 5], [2, 4, 6]]) pos_2d = [pos_list + pos[i] for i in range(4)] s = Signal2D(np.arange(2 * 3 * 4 * 5).reshape(2, 3, 4, 5)) marker_list = [] for _auto_update, _pos in [(False, pos), (True, pos_2d)]: _markers = [ markers.vertical_line(_pos[0]), markers.horizontal_line(_pos[0]), markers.vertical_line_segment(*(_pos[0:3])), markers.horizontal_line_segment(*(_pos[0:3])), markers.rectangle(*_pos), markers.ellipse(*_pos), markers.arrow(*_pos), markers.line_segment(*_pos), markers.point(_pos[0], _pos[1]), markers.text(_pos[0], _pos[1], "test"), ] for marker in _markers: assert marker.auto_update is _auto_update marker_list += _markers assert len(marker_list) == 20 s.add_marker(marker_list) for iy, temp_marker_list in enumerate(pos_list): for ix, value in enumerate(temp_marker_list): s.axes_manager.indices = (ix, iy) for marker in marker_list: _xy = [marker.get_data_position(ax) for ax in ('x1','y1','x2','y2')] if marker.auto_update is False: _xy2 = pos else: _xy2 = [pos_2d[i][iy, ix] for i in range(4)] _name = marker.name if marker.auto_update is False: if _name == 'vertical_line': assert _xy2[0] == _xy[0] elif _name == 'horizontal_line': assert _xy2[0] == _xy[1] elif _name == 'vertical_line_segment': assert _xy2[0:3] == [_xy[i] for i in (0,1,3)] elif _name == 'horizontal_line_segment': assert _xy2[0:3] == [_xy[i] for i in (0,2,1)] elif _name in ('rectangle', 'ellipse', 'arrow', 'line_segment'): assert _xy2 == _xy elif _name in ('point', 'text'): assert _xy2[0:2] == _xy[0:2] else: raise ValueError('Unknown marker : ' + _name)
def test_save_load_line_segment_marker(self, tmp_path, file): filename = tmp_path / file s = self.s x1, x2, y1, y2 = 1, 9, 4, 7 color = 'cyan' linewidth = 0.7 name = "line_segment_test" m = markers.line_segment( x1=x1, x2=x2, y1=y1, y2=y2, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary())
def test_save_load_line_segment_marker(self): x1, x2, y1, y2 = 1, 9, 4, 7 color = 'cyan' linewidth = 0.7 name = "line_segment_test" s = Signal2D(np.arange(100).reshape(10, 10)) m = markers.line_segment( x1=x1, x2=x2, y1=y1, y2=y2, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) with tempfile.TemporaryDirectory() as tmp: filename = tmp + '/test_save_line_segment_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary())
def test_add_several_permanent_markers(self, mpl_cleanup): s = Signal1D(np.arange(10)) m_point = markers.point(x=5, y=5) m_line = markers.line_segment(x1=5, x2=10, y1=5, y2=10) m_vline = markers.vertical_line(x=5) m_vline_segment = markers.vertical_line_segment(x=4, y1=3, y2=6) m_hline = markers.horizontal_line(y=5) m_hline_segment = markers.horizontal_line_segment(x1=1, x2=9, y=5) m_rect = markers.rectangle(x1=1, x2=3, y1=5, y2=10) m_text = markers.text(x=1, y=5, text="test") s.add_marker(m_point, permanent=True) s.add_marker(m_line, permanent=True) s.add_marker(m_vline, permanent=True) s.add_marker(m_vline_segment, permanent=True) s.add_marker(m_hline, permanent=True) s.add_marker(m_hline_segment, permanent=True) s.add_marker(m_rect, permanent=True) s.add_marker(m_text, permanent=True) assert len(list(s.metadata.Markers)) == 8 with pytest.raises(ValueError): s.add_marker(m_rect, permanent=True)
def test_add_several_permanent_markers(self): s = Signal1D(np.arange(10)) m_point = markers.point(x=5, y=5) m_line = markers.line_segment(x1=5, x2=10, y1=5, y2=10) m_vline = markers.vertical_line(x=5) m_vline_segment = markers.vertical_line_segment(x=4, y1=3, y2=6) m_hline = markers.horizontal_line(y=5) m_hline_segment = markers.horizontal_line_segment(x1=1, x2=9, y=5) m_rect = markers.rectangle(x1=1, x2=3, y1=5, y2=10) m_text = markers.text(x=1, y=5, text="test") s.add_marker(m_point, permanent=True) s.add_marker(m_line, permanent=True) s.add_marker(m_vline, permanent=True) s.add_marker(m_vline_segment, permanent=True) s.add_marker(m_hline, permanent=True) s.add_marker(m_hline_segment, permanent=True) s.add_marker(m_rect, permanent=True) s.add_marker(m_text, permanent=True) assert len(list(s.metadata.Markers)) == 8 with pytest.raises(ValueError): s.add_marker(m_rect, permanent=True)
def regression(self, roi, signal=None, axes=None): if signal is None: f = self.reg_tool.widget.ax.figure window = f.canvas.parent() sw = window.property('hyperspyUI.SignalWrapper') if sw is None: return signal = sw.signal sig_axes = signal.axes_manager._axes if axes is None: axes = self.reg_tool.axes else: axes = sig_axes[axes] i_ax = sig_axes.index(axes[0]) slices = list(roi._make_slices(sig_axes, axes)) for i, a in enumerate(sig_axes): if i != i_ax: slices[i] = a.index y = signal.data[slices] x = axes[0].axis[slices[i_ax]] # TODO: If in signal dim, iterate through navigation space reg = stats.linregress(x, y) x1, x2 = np.min(x), np.max(x) y1, y2 = np.array([x1, x2]) * reg[0] + reg[1] m_l = line_segment(x1, y1, x2, y2) signal.add_marker(m_l) m_t = text((x2+x1)*0.5, (y2+y1)*0.5, "y = %.4gx + %.4g, R=%.4g" % reg[0:3]) signal.add_marker(m_t) self.record_code("signal = ui.get_selected_signal()") self.record_code("axes = " + str(tuple([sig_axes.index(a) for a in axes]))) self.record_code("roi = utils.roi." + str(roi)) self.record_code("<p>.regression(roi, signal, axes)") self.reg_tool.cancel() # Turn off functionality as we are finished
def regression(self, roi, signal=None, axes=None): if signal is None: f = self.reg_tool.widget.ax.figure window = f.canvas.parent() sw = window.property('hyperspyUI.SignalWrapper') if sw is None: return signal = sw.signal sig_axes = signal.axes_manager._axes if axes is None: axes = self.reg_tool.axes else: axes = sig_axes[axes] i_ax = sig_axes.index(axes[0]) slices = list(roi._make_slices(sig_axes, axes)) for i, a in enumerate(sig_axes): if i != i_ax: slices[i] = a.index y = signal.data[slices] x = axes[0].axis[slices[i_ax]] # TODO: If in signal dim, iterate through navigation space reg = stats.linregress(x, y) x1, x2 = np.min(x), np.max(x) y1, y2 = np.array([x1, x2]) * reg[0] + reg[1] m_l = line_segment(x1, y1, x2, y2) signal.add_marker(m_l) m_t = text((x2 + x1) * 0.5, (y2 + y1) * 0.5, "y = %.4gx + %.4g, R=%.4g" % reg[0:3]) signal.add_marker(m_t) self.record_code("signal = ui.get_selected_signal()") self.record_code("axes = " + str(tuple([sig_axes.index(a) for a in axes]))) self.record_code("roi = utils.roi." + str(roi)) self.record_code("<p>.regression(roi, signal, axes)") self.reg_tool.cancel() # Turn off functionality as we are finished
def _get_2d_line_segment_list(lines_list, signal_axes=None, color="red", linewidth=1, linestyle="solid"): """Get a list of 2d dimensional line segments markers. The markers will be displayed on the signal dimensions. Parameters ---------- lines_list : list In form [[x01, y01, x02, y02], [x11, y11, x12, y12], ...] signal_axes : HyperSpy axes_manager object color : string, optional Color of point marker. Default 'red'. linewidth : scalar, optional Default 2 linestyle : string, optional Default 'solid' Returns ------- marker_list : list of HyperSpy marker objects """ marker_list = [] for x1, y1, x2, y2 in lines_list: marker = hm.line_segment(x1, y1, x2, y2, color=color, linewidth=linewidth, linestyle=linestyle) marker_list.append(marker) return marker_list
def test_plot_markers_zorder(reversed_order): s = Signal2D(np.full((10,10),20)) s.axes_manager[0].name='x' s.axes_manager[0].scale=10 s.axes_manager[1].scale=10 s.plot() marker_list = [ markers.rectangle(35, 45, 65, 75, edgecolor="yellow", facecolor="cyan", zorder=3), markers.text(10, 20, "Text", color="white", size=30, zorder=4), markers.ellipse(40, 60, 30, 25, edgecolor='white', facecolor='red', linewidth=4, zorder=8), markers.arrow(10, 10, 50, 50, arrowstyle='<|-|>', edgecolor='white', facecolor='red', linewidth=1, shrinkA=2, shrinkB=2, zorder=8.5), markers.arrow(10, 15, 50, 60, arrowstyle='<->', edgecolor='red', facecolor='red', linewidth=3, shrinkA=2, shrinkB=2, zorder=2.8), markers.rectangle(10, 20, 60, 70, edgecolor="red", facecolor="green", fill=True, zorder=2.7), markers.text(50, 60, "Text", color="white", backgroundcolor="blue", size=40, zorder=6.6), markers.ellipse(70, 40, 30, 25, edgecolor='blue', facecolor='red', fill=True, linewidth=4, zorder=7.5), markers.line_segment(50, 10, 40, 80, color='cyan', linewidth=3, zorder=3.2), ] if reversed_order: marker_list.reverse() s.add_marker(marker_list) return s._plot.signal_plot.figure
def _add_background_windows_markers(self, windows_position): """ Plot the background windows associated with each X-ray lines. For X-ray lines, a black line links the left and right window with the average value in each window. Parameters ---------- windows_position: 2D array of float The position of the windows in energy. Each line corresponds to a X-ray lines. In a line, the two first value corresponds to the limit of the left window and the two last values corresponds to the limit of the right window. See also -------- estimate_background_windows, get_lines_intensity """ self._add_vertical_lines_groups(windows_position) ax = self.axes_manager.signal_axes[0] for bw in windows_position: # TODO: test to prevent slicing bug. To be reomved when fixed if ax.value2index(bw[0]) == ax.value2index(bw[1]): y1 = self.isig[bw[0]].data else: y1 = self.isig[bw[0]:bw[1]].mean(-1).data if ax.value2index(bw[2]) == ax.value2index(bw[3]): y2 = self.isig[bw[2]].data else: y2 = self.isig[bw[2]:bw[3]].mean(-1).data line = markers.line_segment( x1=(bw[0] + bw[1]) / 2., x2=(bw[2] + bw[3]) / 2., y1=y1, y2=y2, color='black') self.add_marker(line)
def _get_4d_line_segment_list(lines_array, signal_axes=None, color="red", linewidth=1, linestyle="solid"): """Get a list of 4 dimensional line segments markers. The markers will be displayed on the signal dimensions. Parameters ---------- lines_array : 4D NumPy array signal_axes : HyperSpy axes_manager object color : string, optional Color of point marker. Default 'red'. linewidth : scalar, optional Default 2 linestyle : string, optional Default 'solid' Returns ------- marker_list : list of HyperSpy marker objects """ max_lines = 0 for ix, iy in np.ndindex(lines_array.shape[:2]): lines_list = lines_array[ix, iy] if lines_list is not None: n_lines = len(lines_list) if n_lines > max_lines: max_lines = n_lines marker_array_shape = (lines_array.shape[0], lines_array.shape[1], max_lines) marker_x1_array = np.ones(marker_array_shape) * -1000 marker_y1_array = np.ones(marker_array_shape) * -1000 marker_x2_array = np.ones(marker_array_shape) * -1000 marker_y2_array = np.ones(marker_array_shape) * -1000 for ix, iy in np.ndindex(marker_x1_array.shape[:2]): lines_list = lines_array[ix, iy] if lines_list is not None: for i_p, line in enumerate(lines_list): if signal_axes is None: marker_x1_array[ix, iy, i_p] = line[1] marker_y1_array[ix, iy, i_p] = line[0] marker_x2_array[ix, iy, i_p] = line[3] marker_y2_array[ix, iy, i_p] = line[2] else: if _check_line_segment_inside(signal_axes, line): sa0iv = signal_axes[0].index2value sa1iv = signal_axes[1].index2value marker_x1_array[ix, iy, i_p] = sa0iv(int(line[1])) marker_y1_array[ix, iy, i_p] = sa1iv(int(line[0])) marker_x2_array[ix, iy, i_p] = sa0iv(int(line[3])) marker_y2_array[ix, iy, i_p] = sa1iv(int(line[2])) marker_list = [] for i_p in range(max_lines): marker = hm.line_segment( marker_x1_array[..., i_p], marker_y1_array[..., i_p], marker_x2_array[..., i_p], marker_y2_array[..., i_p], color=color, linewidth=linewidth, linestyle=linestyle, ) marker_list.append(marker) return marker_list