def test_plot_contrast_editor_norm(norm): np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot(norm=norm) ceditor = ImageContrastEditor(s._plot.signal_plot) if norm == "log": # test log with negative numbers s2 = s - 5E3 s2.plot(norm=norm) _ = ImageContrastEditor(s._plot.signal_plot) assert ceditor.norm == norm.capitalize()
def test_plot_contrast_editor(gamma, saturated_pixels): np.random.seed(1) data = np.random.random(size=(10, 10, 100, 100)) * 1000 data += np.arange(10 * 10 * 100 * 100).reshape((10, 10, 100, 100)) s = signals.Signal2D(data) s.plot(gamma=gamma, saturated_pixels=saturated_pixels) ceditor = ImageContrastEditor(s._plot.signal_plot) assert ceditor.gamma == gamma assert ceditor.saturated_pixels == saturated_pixels return plt.gcf()
def test_plot_contrast_editor_complex(): s = datasets.example_signals.object_hologram() fft = s.fft(True) fft.plot(True, vmin=None, vmax=None) ceditor = ImageContrastEditor(fft._plot.signal_plot) assert ceditor.bins == 250 np.testing.assert_allclose(ceditor._vmin, fft._plot.signal_plot._vmin) np.testing.assert_allclose(ceditor._vmax, fft._plot.signal_plot._vmax) np.testing.assert_allclose(ceditor._vmin, 1.495977361e+3) np.testing.assert_allclose(ceditor._vmax, 3.568838458887e+17)
def test_plot_contrast_editor(gamma, percentile): np.random.seed(1) data = np.random.random(size=(10, 10, 100, 100)) * 1000 data += np.arange(10 * 10 * 100 * 100).reshape((10, 10, 100, 100)) s = signals.Signal2D(data) s.plot(gamma=gamma, vmin=percentile[0], vmax=percentile[1]) ceditor = ImageContrastEditor(s._plot.signal_plot) assert ceditor.gamma == gamma assert ceditor.vmin_percentile == float(percentile[0].split("th")[0]) assert ceditor.vmax_percentile == float(percentile[1].split("th")[0]) return plt.gcf()
def test_change_navigation_coordinate(self): s = self.s s.plot(vmin='10th', vmax='99th') ceditor = ImageContrastEditor(s._plot.signal_plot) np.testing.assert_allclose(ceditor._vmin, 9.9) np.testing.assert_allclose(ceditor._vmax, 98.01) s.axes_manager.indices = (1, 1) np.testing.assert_allclose(ceditor._vmin, 409.9) np.testing.assert_allclose(ceditor._vmax, 498.01)
def test_reset_span_selector(self): s = self.s s.plot(vmin='10th', vmax='99th') ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.set_initial((20, 90)) try: ceditor.update_span_selector() ax_image = s._plot.signal_plot.ax.images[0] np.testing.assert_allclose(ax_image.norm.vmin, 20) np.testing.assert_allclose(ax_image.norm.vmax, 90) ceditor._reset_span_selector() np.testing.assert_allclose(ax_image.norm.vmin, 9.9) np.testing.assert_allclose(ax_image.norm.vmax, 98.01) except TypeError as e: # Failure sometimes seen with pytest-xdist (parallel tests) # Message: `TypeError: restore_region() argument 1 must be matplotlib.backends._backend_agg.BufferRegion, not None` # See e.g. https://github.com/hyperspy/hyperspy/issues/1688 # for a similar issue. Currently unclear what solution is. pytest.skip(f"Skipping reset_span_selector test due to {e}")
def test_change_navigation_coordinate(self): s = self.s s.plot(vmin='10th', vmax='99th') ceditor = ImageContrastEditor(s._plot.signal_plot) np.testing.assert_allclose(ceditor._vmin, 9.9) np.testing.assert_allclose(ceditor._vmax, 98.01) try: # Convenience to be able to run test on systems using backends # supporting blit s.axes_manager.indices = (1, 1) except TypeError: pass np.testing.assert_allclose(ceditor._vmin, 409.9) np.testing.assert_allclose(ceditor._vmax, 498.01)
def test_vmin_vmax_changed(self): s = self.s s.plot(vmin='0th', vmax='100th') ceditor = ImageContrastEditor(s._plot.signal_plot) np.testing.assert_allclose(ceditor._vmin, 0.0) np.testing.assert_allclose(ceditor._vmax, 99.0) ceditor._vmin_percentile_changed(0, 10) ceditor._vmax_percentile_changed(100, 99) np.testing.assert_allclose(ceditor._vmin, 9.9) np.testing.assert_allclose(ceditor._vmax, 98.01)
def test_reset_span_selector(self): s = self.s s.plot(vmin='10th', vmax='99th') ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (20, 90) ceditor._update_image_contrast() ax_image = s._plot.signal_plot.ax.images[0] np.testing.assert_allclose(ax_image.norm.vmin, 20) np.testing.assert_allclose(ax_image.norm.vmax, 90) ceditor._clear_span_selector() assert not ceditor.span_selector.visible np.testing.assert_allclose(ax_image.norm.vmin, 20) np.testing.assert_allclose(ax_image.norm.vmax, 90) ceditor._update_image_contrast() np.testing.assert_allclose(ax_image.norm.vmin, 9.9) np.testing.assert_allclose(ax_image.norm.vmax, 98.01)
def test_close_vmin_vmax(): data = np.random.random(10*10*10).reshape([10]*3) s = hs.signals.Signal2D(data) s.plot() image_plot = s._plot.signal_plot display_range = (0.6, 0.9) ceditor = ImageContrastEditor(image_plot) # Simulate selecting a range on the histogram ceditor.span_selector_switch(True) ceditor.span_selector.set_initial = (0, 1) ceditor.span_selector.range = display_range plt.pause(0.001) # in case, interactive backend is used ceditor.update_span_selector_traits() # Need to use auto=False to pick up the current display when closing ceditor.auto = False ceditor.close() assert image_plot.vmin == display_range[0] assert image_plot.vmax == display_range[1]
def test_plot_constrast_editor_reset(): np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor._update_image_contrast() vmin, vmax = 2.1143748173448866, 10988.568946171192 np.testing.assert_allclose(ceditor._vmin, vmin) np.testing.assert_allclose(ceditor._vmax, vmax) np.testing.assert_allclose(ceditor._get_current_range(), (3E3, 5E3)) ceditor.reset() assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible np.testing.assert_allclose(ceditor._get_current_range(), (vmin, vmax)) np.testing.assert_allclose(ceditor.image._vmin, vmin) np.testing.assert_allclose(ceditor.image._vmax, vmax)
def test_reset_vmin_vmax(self): s = self.s s.plot(vmin='10th', vmax='99th') ceditor = ImageContrastEditor(s._plot.signal_plot) np.testing.assert_allclose(ceditor._vmin, 9.9) np.testing.assert_allclose(ceditor._vmax, 98.01) ceditor._vmin = 20 ceditor._vmax = 90 ceditor._reset_original_settings() np.testing.assert_allclose(ceditor._vmin, 9.9) np.testing.assert_allclose(ceditor._vmax, 98.01)
def test_reset_span_selector(self): s = self.s s.plot(vmin='10th', vmax='99th') ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.set_initial((20, 90)) ceditor.update_span_selector() ax_image = s._plot.signal_plot.ax.images[0] assert_allclose(ax_image.norm.vmin, 20) assert_allclose(ax_image.norm.vmax, 90) ceditor._reset_span_selector() assert_allclose(ax_image.norm.vmin, 9.9) assert_allclose(ax_image.norm.vmax, 98.01)
def test_image_contrast_tool(): s = hs.signals.Signal2D(np.random.random(10000).reshape((100, 100))) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.gui(**KWARGS) vmin = 5 ceditor.vmin_percentile = vmin assert ceditor.vmin_percentile == vmin vmax = 95 ceditor.vmax_percentile = vmax assert ceditor.vmax_percentile == vmax auto = False ceditor.auto = auto assert ceditor.auto == auto for norm in ['Linear', 'Power', 'Log', 'Symlog']: ceditor.norm = norm assert ceditor.norm == norm
def test_close_vmin_vmax(): data = np.random.random(10 * 10 * 10).reshape([10] * 3) s = hs.signals.Signal2D(data) s.plot() image_plot = s._plot.signal_plot display_range = (0.6, 0.9) ceditor = ImageContrastEditor(image_plot) # Simulate selecting a range on the histogram ceditor.span_selector.extents = display_range plt.pause(0.001) # in case, interactive backend is used ceditor._update_image_contrast() # Need to use auto=False to pick up the current display when closing ceditor.auto = False ceditor.close() assert (image_plot.vmin, image_plot.vmax) == display_range
def test_plot_constrast_editor_apply(): np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor._update_image_contrast() image_vmin_vmax = ceditor.image._vmin, ceditor.image._vmax ceditor.apply() assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible np.testing.assert_allclose( (ceditor.image._vmin, ceditor.image._vmax), image_vmin_vmax, )
def test_plot_constrast_editor_auto_indices_changed(): np.random.seed(1) data = np.random.random(size=(10, 10, 100, 100)) * 1000 data += np.arange(10 * 10 * 100 * 100).reshape((10, 10, 100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor.update_span_selector_traits() s.axes_manager.indices = (0, 1) # auto is None by default, the span selector need to be removed: assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible ref_value = (100045.29840954322, 110988.10581608873) np.testing.assert_allclose(ceditor._get_current_range(), ref_value) # Change auto to False ceditor.auto = False s.axes_manager.indices = (0, 2) # vmin, vmax shouldn't have changed np.testing.assert_allclose(ceditor._get_current_range(), ref_value)
def test_vmin_vmax_changed(self): s = self.s s.plot(vmin='0th', vmax='100th') ceditor = ImageContrastEditor(s._plot.signal_plot) np.testing.assert_allclose(ceditor._vmin, 0.0) np.testing.assert_allclose(ceditor._vmax, 99.0) try: # Convenience to be able to run test on systems using backends # supporting blit ceditor._vmin_percentile_changed(0, 10) except TypeError: pass try: # Convenience to be able to run test on systems using backends # supporting blit ceditor._vmax_percentile_changed(100, 99) except TypeError: pass np.testing.assert_allclose(ceditor._vmin, 9.9) np.testing.assert_allclose(ceditor._vmax, 98.01)
def gui_adjust_contrast(self, display=True, toolkit=None): ceditor = ImageContrastEditor(self) return ceditor.gui(display=display, toolkit=toolkit)
def gui_adjust_contrast(self, display=True, toolkit=None): if self._is_rgb: raise NotImplementedError( "Constrast adjustment of RGB images is not implemented") ceditor = ImageContrastEditor(self) return ceditor.gui(display=display, toolkit=toolkit)
def test_constrast_editor(self): # To get this test to work, matplotlib backend needs to set to 'Agg' np.random.seed(1) im = hs.signals.Signal2D(np.random.random((32, 32))) im.plot() ceditor = ImageContrastEditor(im._plot.signal_plot) ceditor.ax.figure.canvas.draw_idle() wd = ceditor.gui(**KWARGS)["ipywidgets"]["wdict"] assert wd["linthresh"].layout.display == "none" # not visible assert wd["linscale"].layout.display == "none" # not visible assert wd["gamma"].layout.display == "none" # not visible wd["bins"].value = 50 assert ceditor.bins == 50 wd["norm"].value = 'Log' assert ceditor.norm == 'Log' assert wd["linthresh"].layout.display == "none" # not visible assert wd["linscale"].layout.display == "none" # not visible wd["norm"].value = 'Symlog' assert ceditor.norm == 'Symlog' assert wd["linthresh"].layout.display == "" # visible assert wd["linscale"].layout.display == "" # visible assert wd["linthresh"].value == 0.01 # default value assert wd["linscale"].value == 0.1 # default value wd["linthresh"].value = 0.1 assert ceditor.linthresh == 0.1 wd["linscale"].value = 0.2 assert ceditor.linscale == 0.2 wd["norm"].value = 'Linear' percentile = [1.0, 99.0] wd["percentile"].value = percentile assert ceditor.vmin_percentile == percentile[0] assert ceditor.vmax_percentile == percentile[1] assert im._plot.signal_plot.vmin == f'{percentile[0]}th' assert im._plot.signal_plot.vmax == f'{percentile[1]}th' wd["norm"].value = 'Power' assert ceditor.norm == 'Power' assert wd["gamma"].layout.display == "" # visible assert wd["gamma"].value == 1.0 # default value wd["gamma"].value = 0.1 assert ceditor.gamma == 0.1 assert wd["auto"].value is True # default value wd["auto"].value = False assert ceditor.auto is False wd["left"].value = 0.2 assert ceditor.ss_left_value == 0.2 wd["right"].value = 0.5 assert ceditor.ss_right_value == 0.5 # Setting the span selector programmatically from the widgets will # need to be implemented properly wd["apply_button"]._click_handlers(wd["apply_button"]) # Trigger it # assert im._plot.signal_plot.vmin == 0.2 # assert im._plot.signal_plot.vmax == 0.5 # Reset to default values wd["reset_button"]._click_handlers(wd["reset_button"]) # Trigger it assert im._plot.signal_plot.vmin == '0.0th' assert im._plot.signal_plot.vmax == '100.0th'
def test_plot_constrast_editor_setting_changed(): # Test that changing setting works np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor.update_span_selector_traits() np.testing.assert_allclose(ceditor.ss_left_value, 3E3) np.testing.assert_allclose(ceditor.ss_right_value, 5E3) assert ceditor.auto # Do a cycle to trigger traits changed ceditor.auto = False assert not ceditor.auto ceditor.auto = True # reset and clear span selector assert ceditor.auto assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible assert not ceditor.line.line.get_visible() ceditor.span_selector.extents = (3E3, 5E3) ceditor.span_selector.set_visible(True) ceditor.update_line() assert ceditor._is_selector_visible assert ceditor.line.line.get_visible() assert ceditor.bins == 24 assert ceditor.line.axis.shape == (ceditor.bins, ) ceditor.bins = 50 assert ceditor.bins == 50 assert ceditor.line.axis.shape == (ceditor.bins, ) # test other parameters ceditor.linthresh = 0.1 assert ceditor.image.linthresh == 0.1 ceditor.linscale = 0.5 assert ceditor.image.linscale == 0.5