Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug iterate signal with markers #1968

25 changes: 24 additions & 1 deletion hyperspy/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3948,12 +3948,35 @@ def get_current_signal(self, auto_title=True, auto_filename=True):
<Signal2D, title: (2, 1), dimensions: (32, 32)>

"""

metadata = self.metadata.deepcopy()

# Check if marker update
if metadata.has_item('Markers'):
marker_name_list = metadata.Markers.keys()
markers_dict = metadata.Markers.__dict__
for marker_name in marker_name_list:
marker = markers_dict[marker_name]['_dtb_value_']
if marker.auto_update:
marker.axes_manager = self.axes_manager
key_dict = {}
for key in marker.data.dtype.names:
key_dict[key] = marker.get_data_position(key)
marker.set_data(**key_dict)

cs = self.__class__(
self(),
axes=self.axes_manager._get_signal_axes_dicts(),
metadata=self.metadata.as_dictionary(),
metadata=metadata.as_dictionary(),
attributes={'_lazy': False})

if cs.metadata.has_item('Markers'):
temp_marker_dict = cs.metadata.Markers.as_dictionary()
markers_dict = markers_metadata_dict_to_markers(
temp_marker_dict,
cs.axes_manager)
cs.metadata.Markers = markers_dict

if auto_filename is True and self.tmp_parameters.has_item('filename'):
cs.tmp_parameters.filename = (self.tmp_parameters.filename +
'_' +
Expand Down
42 changes: 40 additions & 2 deletions hyperspy/tests/drawing/test_plot_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pytest

from hyperspy.misc.test_utils import update_close_figure, sanitize_dict
from hyperspy.signals import Signal2D, Signal1D
from hyperspy.signals import Signal2D, Signal1D, BaseSignal
from hyperspy.utils import markers, stack
from hyperspy.drawing.marker import dict2marker
from hyperspy.datasets.example_signals import EDS_TEM_Spectrum
Expand Down Expand Up @@ -472,4 +472,42 @@ def test_plot_eds_lines():
s.plot(True)
s.axes_manager.navigation_axes[0].index = 1
return s._plot.signal_plot.figure


@update_close_figure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this?

Copy link
Contributor Author

@AEljarrat AEljarrat Aug 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you refer to the import BaseSignal? I use a basesignal to test in test_iterate_markers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meand @update_close_figure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is needed to close the figure that is created when add_marker is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, it seems it is not needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the purpose of this in other tests is to test if closing the plot closes the markers without issue. Having it here may make this test fail if that gets broken what may be confusing. However, @ericpre should know better.

def test_iterate_markers():
from skimage.feature import peak_local_max
import scipy.misc
ims = BaseSignal(scipy.misc.face()).as_signal2D([1,2])
index = np.array([peak_local_max(im.data, min_distance=100,
num_peaks=4) for im in ims])
# Add multiple markers
for i in range(4):
xs = index[:, i, 1]
ys = index[:, i, 0]
m = markers.point(x=xs, y=ys, color='red')
ims.add_marker(m, plot_marker=True, permanent=True)
m = markers.text(x=10+xs, y=10+ys, text=str(i), color='k')
ims.add_marker(m, plot_marker=True, permanent=True)
xs = index[:, :, 1]
ys = index[:, :, 0]
m = markers.rectangle(np.min(xs, 1),
np.min(ys, 1),
np.max(xs, 1),
np.max(ys, 1),
color='green')
ims.add_marker(m, plot_marker=True, permanent=True)

for im in ims:
m_original = ims.metadata.Markers
m_iterated = im.metadata.Markers
for key in m_original.keys():
mo = m_original[key]
mi = m_iterated[key]
assert mo.__class__.__name__ == mi.__class__.__name__
assert mo.name == mi.name
assert mo.get_data_position('x1') == mi.get_data_position('x1')
assert mo.get_data_position('y1') == mi.get_data_position('y1')
assert mo.get_data_position('text') == mi.get_data_position('text')
assert mo.marker_properties['color'] == \
mi.marker_properties['color']
return ims
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test compares the markers from the sub-signal ims with the markers from the original signal im. It should check if the markers are replicated correctly when the signal is iterated into sub-signals; for im in ims.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but, why does it have to return ims?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups! certainly not. I will remove that together with the update_close_figure.

Thanks for reviewing!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, the update_close_figure decorator is to check that the plots are closing without raising any error. It doesn't seem to be required here because this should be covered elsewhere.