Skip to content

Commit

Permalink
Merge pull request hyperspy#2734 from ericpre/fix_disconnection_remov…
Browse files Browse the repository at this point in the history
…e_background

Fix disconnecting events when closing figure and remove_background is active
  • Loading branch information
francisco-dlp authored May 10, 2021
2 parents 74c4cd4 + 9c54568 commit 4713edc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
13 changes: 9 additions & 4 deletions hyperspy/signal_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ class BackgroundRemoval(SpanSelectorInSignal1D):
def __init__(self, signal, background_type='Power law', polynomial_order=2,
fast=True, plot_remainder=True, zero_fill=False,
show_progressbar=None, model=None):
super(BackgroundRemoval, self).__init__(signal)
super().__init__(signal)
# setting the polynomial order will change the backgroud_type to
# polynomial, so we set it before setting the background type
self.bg_line = None
Expand All @@ -1248,7 +1248,7 @@ def __init__(self, signal, background_type='Power law', polynomial_order=2,
self.fast = fast
self.plot_remainder = plot_remainder
if plot_remainder:
# When plotting the remainder on the right hand side axis, we
# When plotting the remainder on the right hand side axis, we
# adjust the layout here to avoid doing it later to avoid
# corrupting the background when using blitting
figure = signal._plot.signal_plot.figure
Expand All @@ -1270,6 +1270,10 @@ def __init__(self, signal, background_type='Power law', polynomial_order=2,
self.set_background_estimator()

self.signal.axes_manager.events.indices_changed.connect(self._fit, [])
# This is also disconnected when disabling the span selector but we
# disconnect also when closing the figure, because in this case,
# `on_disabling_span_selector` will not be called.
self.signal._plot.signal_plot.events.closed.connect(self.disconnect, [])

def on_disabling_span_selector(self):
# Disconnect event
Expand Down Expand Up @@ -1424,8 +1428,9 @@ def apply(self):

def disconnect(self):
axes_manager = self.signal.axes_manager
if self._fit in axes_manager.events.indices_changed.connected:
axes_manager.events.indices_changed.disconnect(self._fit)
for f in [self._fit, self.model._on_navigating]:
if f in axes_manager.events.indices_changed.connected:
axes_manager.events.indices_changed.disconnect(f)


def _get_background_estimator(background_type, polynomial_order=1):
Expand Down
28 changes: 28 additions & 0 deletions hyperspy/tests/drawing/test_plot_signal_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ def test_plot_BackgroundRemoval():
return br.signal._plot.signal_plot.figure


def test_plot_BackgroundRemoval_close_figure():
s = signals.Signal1D(np.arange(1000).reshape(10, 100))
br = BackgroundRemoval(s, background_type='Gaussian')
signal_plot = s._plot.signal_plot

assert len(signal_plot.events.closed.connected) == 5
assert len(s.axes_manager.events.indices_changed.connected) == 4
s._plot.close()
assert not br._fit in s.axes_manager.events.indices_changed.connected
assert not br.disconnect in signal_plot.events.closed.connected


def test_plot_BackgroundRemoval_close_tool():
s = signals.Signal1D(np.arange(1000).reshape(10, 100))
br = BackgroundRemoval(s, background_type='Gaussian')
br.span_selector.set_initial((20, 40))
br.span_selector.onmove_callback()
br.span_selector_changed()
signal_plot = s._plot.signal_plot

assert len(signal_plot.events.closed.connected) == 5
assert len(s.axes_manager.events.indices_changed.connected) == 4
br.on_disabling_span_selector()
assert not br._fit in s.axes_manager.events.indices_changed.connected
s._plot.close()
assert not br.disconnect in signal_plot.events.closed.connected


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR,
tolerance=DEFAULT_TOL, style=STYLE_PYTEST_MPL)
@pytest.mark.parametrize("gamma", (0.7, 1.2))
Expand Down

0 comments on commit 4713edc

Please sign in to comment.