LumiSpy / lumispy

Luminescence data analysis with HyperSpy.
https://lumispy.org
GNU General Public License v3.0
25 stars 17 forks source link

`to_eV()` function and non-uniform axis #212

Open StePhanino opened 1 month ago

StePhanino commented 1 month ago

Hi everyone, first of all thanks for the lumispy package.

I'm working with CL data, and most of the time I'm playing with interactive ROIs or new hyperspy functions like the hyperspy.api.plot.plot_roi_map() but non-uniform axes are not supported yet.

Would it be reasonable to expand the to_eV() function to include a conversion option like in the following code?

from lumispy.signals import CLSEMSpectrum

n_points = 1024
wl = np.linspace(300,700,n_points)
sigma = 25
wl_center = 500
gaussian = 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (wl - wl_center)**2 / (2 * sigma**2))
noise = CLSEMSpectrum(np.random.random((10,10,n_points))/1000)
s = CLSEMSpectrum(np.broadcast_to(gaussian, (10,10,n_points))) + noise

s.axes_manager.signal_axes[0].name = 'Wavelength'
s.axes_manager.signal_axes[0].units = 'nm'
s.axes_manager.signal_axes[0].scale = (700-300)/n_points
s.axes_manager.signal_axes[0].offset = 300

s.axes_manager.navigation_axes[0].name = 'X'
s.axes_manager.navigation_axes[0].units = 'nm'
s.axes_manager.navigation_axes[0].scale = 0.1
s.axes_manager.navigation_axes[0].offset = 100

s.axes_manager.navigation_axes[1].name = 'Y'
s.axes_manager.navigation_axes[1].units = 'nm'
s.axes_manager.navigation_axes[1].scale = 0.1
s.axes_manager.navigation_axes[1].offset = 100

s_ev = s.to_eV(inplace=False)
print(type(s_ev.axes_manager[-1]))

import hyperspy.axes as hsax
old_axis = s_ev.axes_manager[-1].axis
new_axis = hsax.DataAxis(axis=old_axis, name='Energy', units='eV')
new_axis.convert_to_uniform_axis()
print(type(new_axis))
test = s_ev.interpolate_on_axis(new_axis, axis='Energy', inplace=False)
test.plot()

hs.plot.plot_spectra([s_ev.inav[5,5], test.inav[5,5]], legend=['Original data', 'Data after uniform + interpolation'])

fig, ax = plt.subplots()
ax.scatter(old_axis, np.ones(len(old_axis)), label='Non uniform original axis')
ax.scatter(new_axis.axis, 2*np.ones(len(old_axis)), label='Uniform new axis')
fig.legend()

thanks a lot

jlaehne commented 1 month ago

Sorry for the delayed response @StePhanino.

Introducing a uniform_axis option that defaults to False would indeed be a nice option to add now that we have both convert_to_uniform_axis for the axis and interpolate_on_axis for the data available.

Would this be something you could implement @pietsjoh?