Skip to content

Commit

Permalink
Merge pull request #1710 from ericpre/improve_triggering_event_when_s…
Browse files Browse the repository at this point in the history
…etting_axes_manager_indices

Trigger events only once when settings axes manager indices
  • Loading branch information
to266 committed Oct 29, 2017
2 parents 635ec73 + 17b8d43 commit 2ca6aa8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
20 changes: 16 additions & 4 deletions hyperspy/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,14 @@ def coordinates(self, coordinates):
"The number of coordinates must be equal to the "
"navigation dimension that is %i" %
self.navigation_dimension)
for value, axis in zip(coordinates, self.navigation_axes):
axis.value = value
changes = False
with self.events.indices_changed.suppress():
for value, axis in zip(coordinates, self.navigation_axes):
changes = changes or (axis.value != value)
axis.value = value
# Trigger only if the indices are changed
if changes:
self.events.indices_changed.trigger(obj=self)

@property
def indices(self):
Expand Down Expand Up @@ -1118,8 +1124,14 @@ def indices(self, indices):
"The number of indices must be equal to the "
"navigation dimension that is %i" %
self.navigation_dimension)
for index, axis in zip(indices, self.navigation_axes):
axis.index = index
changes = False
with self.events.indices_changed.suppress():
for index, axis in zip(indices, self.navigation_axes):
changes = changes or (axis.index != index)
axis.index = index
# Trigger only if the indices are changed
if changes:
self.events.indices_changed.trigger(obj=self)

def _get_axis_attribute_values(self, attr):
return [getattr(axis, attr) for axis in self._axes]
Expand Down
49 changes: 48 additions & 1 deletion hyperspy/tests/axes/test_axes_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from unittest import mock

from hyperspy.axes import DataAxis, AxesManager
from hyperspy.axes import AxesManager
from hyperspy.signals import BaseSignal, Signal1D, Signal2D
from numpy import arange

Expand Down Expand Up @@ -216,3 +216,50 @@ def test_changing_scale_offset(self):
signal_axis1.low_value, signal_axis1.high_value,
)
assert signal_extent == s.axes_manager.signal_extent


def test_setting_indices_coordinates():
s = Signal1D(arange(1000).reshape(10, 10, 10))

m = mock.Mock()
s.axes_manager.events.indices_changed.connect(m, [])

# both indices are changed but the event is triggered only once
s.axes_manager.indices = (5, 5)
assert s.axes_manager.indices == (5, 5)
assert m.call_count == 1

# indices not changed, so the event is not triggered
s.axes_manager.indices == (5, 5)
assert s.axes_manager.indices == (5, 5)
assert m.call_count == 1

# both indices changed again, call only once
s.axes_manager.indices = (2, 3)
assert s.axes_manager.indices == (2, 3)
assert m.call_count == 2

# single index changed, call only once
s.axes_manager.indices = (2, 2)
assert s.axes_manager.indices == (2, 2)
assert m.call_count == 3

# both coordinates are changed but the event is triggered only once
s.axes_manager.coordinates = (5, 5)
assert s.axes_manager.coordinates == (5, 5)
assert m.call_count == 4

# coordinates not changed, so the event is not triggered
s.axes_manager.indices == (5, 5)
assert s.axes_manager.indices == (5, 5)
assert m.call_count == 4

# both coordinates changed again, call only once
s.axes_manager.coordinates = (2, 3)
assert s.axes_manager.coordinates == (2, 3)
assert m.call_count == 5

# single coordinate changed, call only once
s.axes_manager.indices = (2, 2)
assert s.axes_manager.indices == (2, 2)
assert m.call_count == 6

0 comments on commit 2ca6aa8

Please sign in to comment.