hyperspy icon indicating copy to clipboard operation
hyperspy copied to clipboard

Line2DROI does not work with stacks

Open opens21 opened this issue 2 years ago • 3 comments

Describe the bug

I have a signal2D stack (filtered_stack) with dimensions 16 x (412, 412) where I cannot apply Line2DROI directly getting the error message below. Instead I have to split up the stack, which is very inefficient for larger data sets and I lose the capability to handle directly the whole stack for example for plotting.

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Cell In[52], line 6
      3 filtered_stack.inav[0].plot(norm='log', cmap = 'inferno', vmin = 0.000002, vmax = 0.005, scalebar_color='red',title ='')
      5 line_roi = hs.roi.Line2DROI(280, 730, 330, 220, 5)
----> 6 roi0 = line_roi.interactive(filtered_stack.inav[0])
      7 ax2 = hs.plot.plot_spectra([roi0])

File /usr/lib/python3.11/site-packages/hyperspy/roi.py:430, in BaseInteractiveROI.interactive(self, signal, navigation_signal, out, color, snap, **kwargs)
    428 if navigation_signal is not None:
    429     if navigation_signal not in self.signal_map:
--> 430         self.add_widget(navigation_signal, color=color, snap=snap,
    431                         axes=kwargs.get("axes", None))
    432 if (self.update not in
    433         signal.axes_manager.events.any_axis_changed.connected):
    434     signal.axes_manager.events.any_axis_changed.connect(
    435         self.update,
    436         [])

File /usr/lib/python3.11/site-packages/hyperspy/roi.py:513, in BaseInteractiveROI.add_widget(self, signal, axes, widget, color, snap, **kwargs)
    511 if widget.ax is None:
    512     if signal._plot is None or signal._plot.signal_plot is None:
--> 513         raise Exception(
    514             f"{repr(signal)} does not have an active plot. Plot the "
    515             "signal before calling this method.")
    517     ax = _get_mpl_ax(signal._plot, axes)
    518     widget.set_mpl_ax(ax)

Exception: <Signal2D, title: Stack of Time Series at 200, dimensions: (|412, 412)> does not have an active plot. Plot the signal before calling this method.

This is the executed code:

filtered_stack.inav[0].plot()
line_roi = hs.roi.Line2DROI(280, 730, 330, 220, 5)
roi0 = line_roi.interactive(filtered_stack.inav[0])
ax2 = hs.plot.plot_spectra([roi0])

If I use instead this code, it works, I lose however the ability to work with the whole stack, which can be very handy, especially for plotting:

qq = hs.signals.Signal2D(filtered_stack.inav[0])
qq.plot()
line_roi = hs.roi.Line2DROI(170, 280, 210, 280, 30)
roi0 = line_roi.interactive(qq)
ax2 = hs.plot.plot_spectra([roi0])

Expected behavior

Interactive plotting should work with individual sets of a stack as well.

Minimal example which does not work (basically I pick one element of the stack):

import numpy as np
import hyperspy.api as hs
import matplotlib.pyplot as plt
%matplotlib qt

images = [np.random.random((100, 100)) for _ in range(10)]

# Convert each numpy array to a HyperSpy signal
signals = [hs.signals.Signal2D(img) for img in images]

# Stack the signals into a single signal
stacked = hs.stack(signals)

stacked.inav[0].plot()
line_roi = hs.roi.Line2DROI(10, 10, 30, 30, 5)
roi0 = line_roi.interactive(stacked.inav[0])
ax2 = hs.plot.plot_spectra([roi0])

Minimal example which does work (without stack):

iimport numpy as np
import hyperspy.api as hs
import matplotlib.pyplot as plt
%matplotlib qt

images = [np.random.random((100, 100)) for _ in range(10)]

# Convert each numpy array to a HyperSpy signal
signals = [hs.signals.Signal2D(img) for img in images]

# Stack the signals into a single signal
stacked = hs.stack(signals)

qq = stacked.inav[0]
qq.plot()
line_roi = hs.roi.Line2DROI(10, 10, 30, 30, 5)
roi0 = line_roi.interactive(qq)
ax2 = hs.plot.plot_spectra([roi0])

opens21 avatar Dec 14 '23 12:12 opens21

Can you provide a minimum reproducible example? It needs to be self-contained, otherwise it is ambiguous.

ericpre avatar Dec 14 '23 13:12 ericpre

Can you provide a minimum reproducible example? It needs to be self-contained, otherwise it is ambiguous.

Thanks, @ericpre I added it to the original post.

opens21 avatar Dec 14 '23 13:12 opens21