Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into release_v0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaehne committed Mar 17, 2023
2 parents dea5b77 + 9db99bd commit 14904bb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
7 changes: 3 additions & 4 deletions doc/source/user_guide/signal_tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ default value ``factor=0.5`` returns the full width at half maximum (FWHM).
Calculating the centroid of a spectrum (centre of mass)
-------------------------------------------------------


The function :py:meth:`~.signals.luminescence_spectrum.LumiSpectrum.centroid`
(based on the utility function :py:func:`~.utils.signals.com`) is an alternative to
finding the position of the maximum intensity of a peak, useful in particular for
Expand All @@ -129,17 +128,17 @@ with the `kwargs` of :external:py:class:`scipy.interpolate.interp1d` function.
>>> s = lum.signals.LumiSpectrum([[[1, 2, 3, 2, 1, 0]]*2]*3)
>>> s
LumiSpectrum <2,3|5>
<LumiSpectrum, title: , dimensions: (2, 3|6)>
>>> ax = s.axes_manager.signal_axes[0]
>>> ax.offset = 200
>>> ax.scale = 100
>>> com = s.centroid()
>>> com
BaseSignal <2,3|>
<Signal2D, title: Centroid map, dimensions: (|2, 3)>
>>> com.data[0,0]
400.
400.0
.. Note::

Expand Down
19 changes: 14 additions & 5 deletions lumispy/signals/luminescence_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from hyperspy.signals import Signal1D
from hyperspy._signals.lazy import LazySignal
from hyperspy.axes import DataAxis
from traits.api import Undefined

from lumispy.signals.common_luminescence import CommonLumi
from lumispy import nm2invcm, to_array, savetxt
Expand Down Expand Up @@ -614,10 +615,10 @@ def centroid(self, signal_range=None, **kwargs):
Returns
-------
signal : BaseSignal
A BaseSignal with signal dimension of 0, where at each navigation
index there is a scalar value containing the center of mass for
each navigation pixel.
signal : Signal2D, BaseSignal
Signal object containing the center of mass for every pixel. Depending
on the dimensionality the type is Signal2D or a BaseSignal (for single
spectrum).
"""
if signal_range:
if type(signal_range) != tuple:
Expand All @@ -639,7 +640,15 @@ def centroid(self, signal_range=None, **kwargs):
center_of_mass = s.map(com, signal_axis=signal_axis, inplace=False)

# Transfer axes metadata to title
center_of_mass.metadata.General.title = f"Centroid map of {signal_axis.name} ({signal_axis.units}) for {center_of_mass.metadata.General.title}"
center_of_mass.metadata.General.title = f"Centroid map"
if signal_axis.name not in (Undefined, ""):
center_of_mass.metadata.General.title += f" of {signal_axis.name}"
if signal_axis.units not in (Undefined, ""):
center_of_mass.metadata.General.title += f" ({signal_axis.units})"
if s.metadata.General.title not in (Undefined, ""):
center_of_mass.metadata.General.title += f" for {s.metadata.General.title}"
if center_of_mass.axes_manager.navigation_size > 0:
center_of_mass = center_of_mass.transpose()
return center_of_mass


Expand Down
16 changes: 8 additions & 8 deletions lumispy/tests/signals/test_cl_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def test_remove_spikes(self):
with pytest.warns(UserWarning, match="Threshold value: 1.00"):
s1 = s.remove_spikes()

np.testing.assert_almost_equal(s1.data[1, 0, 1], 1, decimal=5)
np.testing.assert_almost_equal(s1.data[0, 2, 29], 1, decimal=5)
np.testing.assert_almost_equal(s1.data[1, 0, 1], 1, decimal=4)
# np.testing.assert_almost_equal(s1.data[0, 2, 29], 1, decimal=4)

s3 = s.remove_spikes(show_diagnosis_histogram=True)
hist_data = s._spikes_diagnosis(
Expand All @@ -81,15 +81,15 @@ def test_remove_spikes(self):
expected_data[12] = 2
expected_data[-1] = 1
np.testing.assert_allclose(hist_data.data, expected_data)
np.testing.assert_almost_equal(s3.data[1, 0, 1], 1, decimal=5)
np.testing.assert_almost_equal(s3.data[0, 2, 29], 1, decimal=5)
np.testing.assert_almost_equal(s3.data[1, 0, 1], 1, decimal=4)
# np.testing.assert_almost_equal(s3.data[0, 2, 29], 1, decimal=5)

lum_roi = [1, 1]
s4 = s.remove_spikes(luminescence_roi=lum_roi, threshold=0.5)
np.testing.assert_almost_equal(s4.data[1, 0, 1], 3, decimal=5)
np.testing.assert_almost_equal(s4.data[0, 2, 29], 1, decimal=5)
np.testing.assert_almost_equal(s4.data[1, 0, 1], 3, decimal=4)
np.testing.assert_almost_equal(s4.data[0, 2, 29], 1, decimal=4)

s.remove_spikes(inplace=True, threshold=0.5)
np.testing.assert_almost_equal(s.data[1, 0, 1], 1, decimal=5)
np.testing.assert_almost_equal(s.data[0, 2, 29], 1, decimal=5)
np.testing.assert_almost_equal(s.data[1, 0, 1], 1, decimal=4)
np.testing.assert_almost_equal(s.data[0, 2, 29], 1, decimal=4)
# TODO: test if histogram is shown as a plot if show_diagnosis_histogram=True.
10 changes: 9 additions & 1 deletion lumispy/tests/signals/test_luminescence_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pytest

from lumispy.signals.luminescence_spectrum import LumiSpectrum
from hyperspy._signals.signal2d import Signal2D
from numpy.testing import assert_allclose

backgrounds = [
Expand Down Expand Up @@ -111,23 +112,28 @@ def test_center_of_mass(self):
ax.scale = 100
ax.units = "nm"
ax.name = "Wavelength"
s.metadata.General.title = "test_signal"

com = s.centroid()
assert_allclose(com.data, 400.0, atol=0.1)
assert (
com.metadata.General.title == f"Centroid map of {ax.name} ({ax.units}) for "
com.metadata.General.title
== f"Centroid map of {ax.name} ({ax.units}) for test_signal"
)

def test_center_of_mass_signalrange(self):
s = LumiSpectrum([100, 100, 1, 2, 3, 2, 1, 0, 100, 100])
ax = s.axes_manager.signal_axes[0]
ax.offset = 0
ax.scale = 100
ax.units = "nm"
ax.name = "Wavelength"

com = s.centroid(signal_range=(2, -2))
assert_allclose(com.data, 400.0, atol=0.1)
com = s.centroid(signal_range=(200.0, 800.0))
assert_allclose(com.data, 400.0, atol=0.1)
assert com.metadata.General.title == f"Centroid map of {ax.name} ({ax.units})"
with pytest.raises(TypeError):
s.centroid(signal_range=(1))
with pytest.raises(TypeError):
Expand All @@ -142,3 +148,5 @@ def test_centre_of_mass_3d(self):
3,
4,
)
assert com.metadata.General.title == f"Centroid map"
assert type(com) == Signal2D

0 comments on commit 14904bb

Please sign in to comment.