From 5f42c3edb02812def73968e138cf29262e4068f2 Mon Sep 17 00:00:00 2001 From: HanHsuanWu <112028355+HanHsuanWu@users.noreply.github.com> Date: Tue, 31 Oct 2023 01:09:09 -0700 Subject: [PATCH 1/7] Flexible cross-correlation alignment --- exspy/signals/eels.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/exspy/signals/eels.py b/exspy/signals/eels.py index 822c70924..4ac1dea00 100644 --- a/exspy/signals/eels.py +++ b/exspy/signals/eels.py @@ -326,6 +326,8 @@ def align_zero_loss_peak( also_align=[], print_stats=True, subpixel=True, + left=-3.0, + right=3.0, mask=None, signal_range=None, show_progressbar=None, @@ -335,7 +337,8 @@ def align_zero_loss_peak( """Align the zero-loss peak. This function first aligns the spectra using the result of - `estimate_zero_loss_peak_centre` and afterward, if subpixel is True, + `estimate_zero_loss_peak_centre` which finds the maximum in the + given energy range, then if subpixel is True, proceeds to align with subpixel accuracy using `align1D`. The offset is automatically correct if `calibrate` is True. @@ -355,6 +358,12 @@ def align_zero_loss_peak( subpixel : bool If True, perform the alignment with subpixel accuracy using cross-correlation. + left : float + When subpixel is True, left is the start of energy range used + in cross-correlation in whichever unit the energy axis is in. + right : float + When subpixel is True, right is the end of energy range used + in cross-correlation in whichever unit the energy axis is in. mask : Signal1D of bool data type or bool array. It must have signal_dimension = 0 and navigation_shape equal to the shape of the current signal. Where mask is True the shift is @@ -450,7 +459,7 @@ def estimate_zero_loss_peak_centre(s, mask, signal_range): if subpixel is False: return - left, right = -3.0, 3.0 + left, right = left, right if calibrate is False: left += mean_ right += mean_ From b85a69ddf2fb8b983091bdeb955e879a7c464988 Mon Sep 17 00:00:00 2001 From: HanHsuanWu <112028355+HanHsuanWu@users.noreply.github.com> Date: Thu, 2 Nov 2023 01:27:01 -0700 Subject: [PATCH 2/7] Update eels.py --- exspy/signals/eels.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/exspy/signals/eels.py b/exspy/signals/eels.py index 4ac1dea00..e9d8263bd 100644 --- a/exspy/signals/eels.py +++ b/exspy/signals/eels.py @@ -326,8 +326,8 @@ def align_zero_loss_peak( also_align=[], print_stats=True, subpixel=True, - left=-3.0, - right=3.0, + start=-3.0, + end=3.0, mask=None, signal_range=None, show_progressbar=None, @@ -358,11 +358,11 @@ def align_zero_loss_peak( subpixel : bool If True, perform the alignment with subpixel accuracy using cross-correlation. - left : float - When subpixel is True, left is the start of energy range used + start : float + When subpixel is True, this variable is the start of energy range used in cross-correlation in whichever unit the energy axis is in. - right : float - When subpixel is True, right is the end of energy range used + end : float + When subpixel is True, this variable is the end of energy range used in cross-correlation in whichever unit the energy axis is in. mask : Signal1D of bool data type or bool array. It must have signal_dimension = 0 and navigation_shape equal to @@ -459,26 +459,26 @@ def estimate_zero_loss_peak_centre(s, mask, signal_range): if subpixel is False: return - left, right = left, right + if calibrate is False: - left += mean_ - right += mean_ + float(start) += mean_ + float(end) += mean_ - left = ( - left - if left > self.axes_manager[-1].axis[0] + start = ( + start + if start > self.axes_manager[-1].axis[0] else self.axes_manager[-1].axis[0] ) - right = ( - right - if right < self.axes_manager[-1].axis[-1] + end = ( + end + if end < self.axes_manager[-1].axis[-1] else self.axes_manager[-1].axis[-1] ) if self.axes_manager.navigation_size > 1: self.align1D( - left, - right, + start , + end , also_align=also_align, show_progressbar=show_progressbar, mask=mask, From f0af38c925c6ec12d002ede2759df9d717d07ce5 Mon Sep 17 00:00:00 2001 From: HanHsuanWu Date: Thu, 9 Nov 2023 19:02:47 -0800 Subject: [PATCH 3/7] added test cases for float and int inputs --- exspy/signals/eels.py | 8 ++++++-- exspy/tests/signals/test_eels.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/exspy/signals/eels.py b/exspy/signals/eels.py index e9d8263bd..832a5e50d 100644 --- a/exspy/signals/eels.py +++ b/exspy/signals/eels.py @@ -460,9 +460,13 @@ def estimate_zero_loss_peak_centre(s, mask, signal_range): if subpixel is False: return + #Enforce start and end as float + start = float(start) + end = float(end) + if calibrate is False: - float(start) += mean_ - float(end) += mean_ + start += mean_ + end += mean_ start = ( start diff --git a/exspy/tests/signals/test_eels.py b/exspy/tests/signals/test_eels.py index e0c80add0..c6b0ac3f0 100644 --- a/exspy/tests/signals/test_eels.py +++ b/exspy/tests/signals/test_eels.py @@ -193,6 +193,20 @@ def test_align_zero_loss_peak_crop_false(self): s.align_zero_loss_peak(crop=False, print_stats=False) assert original_size == s.axes_manager.signal_axes[0].size + def test_align_zero_loss_peak_start_end_float(self): + s = self.signal + s.align_zero_loss_peak(subpixel=True, start=-2., end= 2.) + #Check if start and end arguments work + assert s.data.mean() == 0 + assert s.data.std() == 0 + + def test_align_zero_loss_peak_start_end_int(self): + s = self.signal + s.align_zero_loss_peak(subpixel=True, start=-2, end= 2) + #Check if start and end arguments work + assert s.data.mean() == 0 + assert s.data.std() == 0 + @lazifyTestClass class TestSpikesRemovalToolZLP: From cfc2615d3804eb15270a9b758351b1a15965a947 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Fri, 17 Nov 2023 20:44:08 +0000 Subject: [PATCH 4/7] Add coverage settings --- pyproject.toml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 21fb67d88..f92a4d8b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,6 +93,14 @@ force-exclude = ''' | exspy/misc/elements.py ''' +[tool.coverage.run] +branch = true +source = ["hyperspy"] +omit = ["hyperspy/tests/*"] + +[tool.coverage.report] +precision = 2 + [tool.pytest.ini_options] addopts = "-ra -n auto --dist loadfile" testpaths = ["exspy/tests", ] From b923c88708559cbaf69b35ae83084606a54d66b1 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Fri, 17 Nov 2023 21:03:49 +0000 Subject: [PATCH 5/7] Fix tests and formatting --- exspy/signals/eels.py | 24 ++++++++++++------------ exspy/tests/signals/test_eels.py | 20 ++++++++++++-------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/exspy/signals/eels.py b/exspy/signals/eels.py index 832a5e50d..0c55dae4e 100644 --- a/exspy/signals/eels.py +++ b/exspy/signals/eels.py @@ -337,7 +337,7 @@ def align_zero_loss_peak( """Align the zero-loss peak. This function first aligns the spectra using the result of - `estimate_zero_loss_peak_centre` which finds the maximum in the + `estimate_zero_loss_peak_centre` which finds the maximum in the given energy range, then if subpixel is True, proceeds to align with subpixel accuracy using `align1D`. The offset is automatically correct if `calibrate` is True. @@ -359,10 +359,10 @@ def align_zero_loss_peak( If True, perform the alignment with subpixel accuracy using cross-correlation. start : float - When subpixel is True, this variable is the start of energy range used + When subpixel is True, this variable is the start of energy range used in cross-correlation in whichever unit the energy axis is in. end : float - When subpixel is True, this variable is the end of energy range used + When subpixel is True, this variable is the end of energy range used in cross-correlation in whichever unit the energy axis is in. mask : Signal1D of bool data type or bool array. It must have signal_dimension = 0 and navigation_shape equal to @@ -459,30 +459,30 @@ def estimate_zero_loss_peak_centre(s, mask, signal_range): if subpixel is False: return - - #Enforce start and end as float + + # Enforce start and end as float start = float(start) end = float(end) if calibrate is False: - start += mean_ - end += mean_ + start += mean_ + end += mean_ start = ( start if start > self.axes_manager[-1].axis[0] else self.axes_manager[-1].axis[0] ) - end = ( - end - if end < self.axes_manager[-1].axis[-1] + end = ( + end + if end < self.axes_manager[-1].axis[-1] else self.axes_manager[-1].axis[-1] ) if self.axes_manager.navigation_size > 1: self.align1D( - start , - end , + start, + end, also_align=also_align, show_progressbar=show_progressbar, mask=mask, diff --git a/exspy/tests/signals/test_eels.py b/exspy/tests/signals/test_eels.py index c6b0ac3f0..87524d152 100644 --- a/exspy/tests/signals/test_eels.py +++ b/exspy/tests/signals/test_eels.py @@ -195,17 +195,21 @@ def test_align_zero_loss_peak_crop_false(self): def test_align_zero_loss_peak_start_end_float(self): s = self.signal - s.align_zero_loss_peak(subpixel=True, start=-2., end= 2.) - #Check if start and end arguments work - assert s.data.mean() == 0 - assert s.data.std() == 0 + s.axes_manager[-1].offset = -2 + s.align_zero_loss_peak(subpixel=True, start=-2.0, end=2.0) + zlpc = s.estimate_zero_loss_peak_centre() + # Check if start and end arguments work + assert zlpc.data.mean() == 0 + assert zlpc.data.std() == 0 def test_align_zero_loss_peak_start_end_int(self): s = self.signal - s.align_zero_loss_peak(subpixel=True, start=-2, end= 2) - #Check if start and end arguments work - assert s.data.mean() == 0 - assert s.data.std() == 0 + s.axes_manager[-1].offset = -2 + s.align_zero_loss_peak(subpixel=True, start=-2, end=2) + zlpc = s.estimate_zero_loss_peak_centre() + # Check if start and end arguments work + assert zlpc.data.mean() == 0 + assert zlpc.data.std() == 0 @lazifyTestClass From 19498c614cf241ab881f7edcf9fb032a13120138 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Fri, 17 Nov 2023 21:11:25 +0000 Subject: [PATCH 6/7] Add changelog entry --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index c075fced1..97ecc327c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,7 @@ https://holospy.readthedocs.io/en/latest/changes.html 0.1.dev0 (UNRELEASED) ===================== +- Enable ``signal_range`` arguments when using ``subpixel=True`` in :py:meth:`~.signals.EELSSpectrum.align_zero_loss_peak` (`#7 `_) - Support for tabulated :ref:`Generalised Oscillator Strengths (GOS) ` using the `GOSH `_ open file format. By default, a freely usable dataset is downloaded from `doi:10.5281/zenodo.7645765 `_ From 1ab68eac376482a27e04c8822fc288b743c81110 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Fri, 17 Nov 2023 21:43:26 +0000 Subject: [PATCH 7/7] Use `signal_range` instead of adding new arguments --- exspy/signals/eels.py | 12 +----------- exspy/tests/signals/test_eels.py | 16 +++++----------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/exspy/signals/eels.py b/exspy/signals/eels.py index 0c55dae4e..036cce7c9 100644 --- a/exspy/signals/eels.py +++ b/exspy/signals/eels.py @@ -326,8 +326,6 @@ def align_zero_loss_peak( also_align=[], print_stats=True, subpixel=True, - start=-3.0, - end=3.0, mask=None, signal_range=None, show_progressbar=None, @@ -358,12 +356,6 @@ def align_zero_loss_peak( subpixel : bool If True, perform the alignment with subpixel accuracy using cross-correlation. - start : float - When subpixel is True, this variable is the start of energy range used - in cross-correlation in whichever unit the energy axis is in. - end : float - When subpixel is True, this variable is the end of energy range used - in cross-correlation in whichever unit the energy axis is in. mask : Signal1D of bool data type or bool array. It must have signal_dimension = 0 and navigation_shape equal to the shape of the current signal. Where mask is True the shift is @@ -460,9 +452,7 @@ def estimate_zero_loss_peak_centre(s, mask, signal_range): if subpixel is False: return - # Enforce start and end as float - start = float(start) - end = float(end) + start, end = signal_range or (-3.0, 3.0) if calibrate is False: start += mean_ diff --git a/exspy/tests/signals/test_eels.py b/exspy/tests/signals/test_eels.py index 87524d152..98f54c709 100644 --- a/exspy/tests/signals/test_eels.py +++ b/exspy/tests/signals/test_eels.py @@ -193,19 +193,13 @@ def test_align_zero_loss_peak_crop_false(self): s.align_zero_loss_peak(crop=False, print_stats=False) assert original_size == s.axes_manager.signal_axes[0].size - def test_align_zero_loss_peak_start_end_float(self): + @pytest.mark.parametrize("signal_range", ((-2.0, 2.0), (0, 40), "roi")) + def test_align_zero_loss_peak_start_end_float(self, signal_range): s = self.signal + if signal_range == "roi": + signal_range = hs.roi.SpanROI(-3, 3) s.axes_manager[-1].offset = -2 - s.align_zero_loss_peak(subpixel=True, start=-2.0, end=2.0) - zlpc = s.estimate_zero_loss_peak_centre() - # Check if start and end arguments work - assert zlpc.data.mean() == 0 - assert zlpc.data.std() == 0 - - def test_align_zero_loss_peak_start_end_int(self): - s = self.signal - s.axes_manager[-1].offset = -2 - s.align_zero_loss_peak(subpixel=True, start=-2, end=2) + s.align_zero_loss_peak(subpixel=True, signal_range=signal_range) zlpc = s.estimate_zero_loss_peak_centre() # Check if start and end arguments work assert zlpc.data.mean() == 0