From befe1c1855bd5ddacf9412b05666005b77a4f0b2 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 19 Dec 2020 18:49:20 +0000 Subject: [PATCH 1/5] Check initialisation value SpanROI. --- hyperspy/roi.py | 6 +++--- hyperspy/tests/utils/test_roi.py | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/hyperspy/roi.py b/hyperspy/roi.py index f2b3433500..95c6ad9f15 100644 --- a/hyperspy/roi.py +++ b/hyperspy/roi.py @@ -702,16 +702,16 @@ class SpanROI(BaseInteractiveROI): _ndim = 1 def __init__(self, left, right): - super(SpanROI, self).__init__() + super().__init__() self._bounds_check = True # Use reponsibly! + if left >= right: + raise ValueError("`left` must be smaller than `right`.") self.left, self.right = left, right def __getitem__(self, *args, **kwargs): _tuple = (self.left, self.right) return _tuple.__getitem__(*args, **kwargs) - - def is_valid(self): return (t.Undefined not in (self.left, self.right) and self.right >= self.left) diff --git a/hyperspy/tests/utils/test_roi.py b/hyperspy/tests/utils/test_roi.py index 45bfa263f7..dfbf935a6c 100644 --- a/hyperspy/tests/utils/test_roi.py +++ b/hyperspy/tests/utils/test_roi.py @@ -104,6 +104,12 @@ def test_point2d_getitem(self): r = Point2DROI(1, 2) assert tuple(r) == (1, 2) + def test_span_roi_init(self): + with pytest.raises(ValueError): + SpanROI(30, 15) + with pytest.raises(ValueError): + SpanROI(15, 15) + def test_span_spectrum_nav(self): s = self.s_s r = SpanROI(15, 30) From b3856dc807f79eabcad46d2cf884360137072824 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Tue, 2 Feb 2021 16:27:35 +0000 Subject: [PATCH 2/5] Add entry to changelog --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 25dd887eae..57b5386cfe 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -13,6 +13,8 @@ RELEASE_next_patch (Unreleased) * Pass keyword argument to the image IO plugins (`#2627 `_) * Drop support for numpy<1.16, in line with NEP 29 and fix protochip reader for numpy 1.20 (`#2616 `_) * Run test suite against upstream dependencies (numpy, scipy, scikit-learn and scikit-image) (`#2616 `_) +* Improve error message when initialising SpanROI with left >= right (`#2604 `_) + Changelog ********* From 7253110e9b3c4ab62e0fc3347fbfc5d5b3fb29e1 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Tue, 2 Feb 2021 20:39:11 +0000 Subject: [PATCH 3/5] Add values in error message and fix typo in comments. --- hyperspy/roi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hyperspy/roi.py b/hyperspy/roi.py index 95c6ad9f15..0ec6e11336 100644 --- a/hyperspy/roi.py +++ b/hyperspy/roi.py @@ -703,9 +703,9 @@ class SpanROI(BaseInteractiveROI): def __init__(self, left, right): super().__init__() - self._bounds_check = True # Use reponsibly! + self._bounds_check = True # Use responsibly! if left >= right: - raise ValueError("`left` must be smaller than `right`.") + raise ValueError(f"`left` ({left}) must be smaller than `right` ({right}).") self.left, self.right = left, right def __getitem__(self, *args, **kwargs): From e4d9634b2bc4af1ecb786cd64f453ba07fd00f2c Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Tue, 2 Feb 2021 20:42:03 +0000 Subject: [PATCH 4/5] Use raw string to avoid "invalid escape sequence" deprecation warning. --- hyperspy/utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hyperspy/utils/__init__.py b/hyperspy/utils/__init__.py index a7407de172..a713d1f444 100644 --- a/hyperspy/utils/__init__.py +++ b/hyperspy/utils/__init__.py @@ -48,7 +48,7 @@ def print_known_signal_types(): - """Print all known `signal_type`\s + r"""Print all known `signal_type`\s This includes `signal_type`\s from all installed packages that extend HyperSpy. From 32b04d4596e20d4765d04646340814539f51cc20 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Tue, 2 Feb 2021 21:03:11 +0000 Subject: [PATCH 5/5] Fix invalid escape sequence deprecation warning in various docstrings. --- hyperspy/misc/label_position.py | 5 ++--- hyperspy/ui_registry.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/hyperspy/misc/label_position.py b/hyperspy/misc/label_position.py index 2aba307abf..ed5a260bdc 100644 --- a/hyperspy/misc/label_position.py +++ b/hyperspy/misc/label_position.py @@ -219,9 +219,8 @@ def _text_parser(self, text_edge): element, subshell = text_edge.split('_') if subshell[-1].isdigit(): - formatted = element+' '+'$\mathregular{'+subshell[0]+'_'+\ - subshell[-1]+'}$' + formatted = f"{element} {subshell[0]}$_{subshell[-1]}$" else: - formatted = element+' '+'$\mathregular{'+subshell[0]+'}$' + formatted = f"{element} {subshell[0]}" return formatted diff --git a/hyperspy/ui_registry.py b/hyperspy/ui_registry.py index 86566818c0..72207be9db 100644 --- a/hyperspy/ui_registry.py +++ b/hyperspy/ui_registry.py @@ -18,7 +18,7 @@ """Registry of user interface widgets. -Format {"tool_key" : {"toolkit" : }} +Format {"tool_key" : {"toolkit" : }} The ``tool_key`` is defined by the "model function" to which the widget provides and user interface. That function gets the widget function from this registry