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_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 _add_xray_lines_markers(self, xray_lines): """ Add marker on a spec.plot() with the name of the selected X-ray lines Parameters ---------- xray_lines: list of string A valid list of X-ray lines """ line_energy = [] intensity = [] for xray_line in xray_lines: element, line = utils_eds._get_element_and_line(xray_line) line_energy.append(self._get_line_energy(xray_line)) relative_factor = elements_db[element][ 'Atomic_properties']['Xray_lines'][line]['weight'] a_eng = self._get_line_energy(element + '_' + line[0] + 'a') intensity.append(self.isig[a_eng].data * relative_factor) for i in range(len(line_energy)): line = markers.vertical_line_segment( x=line_energy[i], y1=None, y2=intensity[i] * 0.8) self.add_marker(line) text = markers.text( x=line_energy[i], y=intensity[i] * 1.1, text=xray_lines[i], rotation=90) self.add_marker(text)
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_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 add_xray_lines_markers(self, xray_lines): """ Add marker on a spec.plot() with the name of the selected X-ray lines Parameters ---------- xray_lines: list of string A valid list of X-ray lines """ line_energy = [] intensity = [] for xray_line in xray_lines: element, line = utils_eds._get_element_and_line(xray_line) line_energy.append(self._get_line_energy(xray_line)) relative_factor = elements_db[element][ 'Atomic_properties']['Xray_lines'][line]['weight'] a_eng = self._get_line_energy(element + '_' + line[0] + 'a') intensity.append(self.isig[a_eng].data * relative_factor) for i in range(len(line_energy)): line = markers.vertical_line_segment( x=line_energy[i], y1=None, y2=intensity[i] * 0.8) self.add_marker(line) text = markers.text( x=line_energy[i], y=intensity[i] * 1.1, text=xray_lines[i], rotation=90) self.add_marker(text) self._xray_markers[xray_lines[i]] = [line, text] line.events.closed.connect(self._xray_marker_closed) text.events.closed.connect(self._xray_marker_closed)
def test_vertical_horizontal_line_segment_with_None(): m0 = markers.horizontal_line_segment(None, None, 1) m1 = markers.vertical_line_segment(1, None, None) w = 2 h = 3 s = Signal2D(np.arange(w * h).reshape(h, w)) s.add_marker(m0) s.add_marker(m1) np.testing.assert_allclose(m0.marker.get_segments()[0], [[-0.5, 1.],[w - 0.5, 1.]]) np.testing.assert_allclose(m1.marker.get_segments()[0], [[1., h - 0.5],[1., -0.5]])
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_plot_line_markers(): im = Signal2D(np.arange(100 * 100).reshape((100, 100))) m0 = markers.vertical_line_segment(x=20, y1=30, y2=70, linewidth=4, color='red', linestyle='dotted') im.add_marker(m0) m1 = markers.horizontal_line_segment(x1=30, x2=20, y=80, linewidth=8, color='blue', linestyle='-') im.add_marker(m1) m2 = markers.vertical_line(50, linewidth=12, color='green') im.add_marker(m2) m3 = markers.horizontal_line(50, linewidth=10, color='yellow') im.add_marker(m3) return im
def test_save_load_vertical_line_segment_marker(self, tmp_path, file): filename = tmp_path / file s = self.s x, y1, y2 = 2, 1, 3 color = 'white' linewidth = 4.2 name = "vertical_line_segment_test" m = markers.vertical_line_segment( x=x, 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_vertical_line_segment_marker(self): x, y1, y2 = 2, 1, 3 color = 'white' linewidth = 4.2 name = "vertical_line_segment_test" s = Signal2D(np.arange(100).reshape(10, 10)) m = markers.vertical_line_segment( x=x, 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_vertical_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 get_markers(self, labels): '''Get the markers (vertical line segment and text box) for labelling the edges Parameters ---------- labels : dictionary A dictionary with the labels as keys and their energies as values. E.g. for EELS edges it could be {'Mn_L2': 651.0, 'Cr_L3': 575.0}. Returns ------- vls : list A list contains HyperSpy's vertical line segment marker txs : list A list contains HyperSpy's text marker ''' xytext = self._get_textbox_pos(labels) vls = [] txs = [] for xyt in xytext: vl = markers.vertical_line_segment(x=xyt[0], y1=xyt[1], y2=xyt[2], color=xyt[4]) tx = markers.text(x=xyt[0], y=xyt[1], text=self._text_parser(xyt[3]), color=xyt[4], **self.edge_label_style) vl.events.closed.connect(self.signal._edge_marker_closed) tx.events.closed.connect(self.signal._edge_marker_closed) vl.auto_update = True tx.auto_update = True vls.append(vl) txs.append(tx) return vls, txs
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 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)