From 0fcd1c453973ecd55ad54203a1046071dd1a811e Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Thu, 16 May 2024 14:25:22 +0100 Subject: [PATCH] Make sure that we don't do any unit parsing if the original and target unit are the same --- glue/core/units.py | 12 ++++++------ glue/viewers/image/tests/test_state.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/glue/core/units.py b/glue/core/units.py index 576772f6e..8090a4888 100644 --- a/glue/core/units.py +++ b/glue/core/units.py @@ -27,19 +27,19 @@ def to_unit(self, data, cid, values, target_units): if target_units is None: return values original_units = self._get_units(data, cid) - if original_units: - return self.converter_helper.to_unit(data, cid, values, original_units, target_units) - else: + if original_units == target_units or not original_units: return values + else: + return self.converter_helper.to_unit(data, cid, values, original_units, target_units) def to_native(self, data, cid, values, original_units): if original_units is None: return values target_units = self._get_units(data, cid) - if target_units: - return self.converter_helper.to_unit(data, cid, values, original_units, target_units) - else: + if original_units == target_units or not target_units: return values + else: + return self.converter_helper.to_unit(data, cid, values, original_units, target_units) def _get_units(self, data, cid): data = data.data if isinstance(data, Subset) else data diff --git a/glue/viewers/image/tests/test_state.py b/glue/viewers/image/tests/test_state.py index ce88efb0c..5ae4be992 100644 --- a/glue/viewers/image/tests/test_state.py +++ b/glue/viewers/image/tests/test_state.py @@ -458,3 +458,16 @@ def test_stretch_global(): assert layer_state.v_min == 49.95 assert layer_state.v_max == 949.05 + + +def test_attribute_units_invalid(): + + # Regression test for a bug that caused a crash if a dataset had an + # unrecognized unit + + viewer_state = ImageViewerState() + + data = Data(x=np.arange(100).reshape((10, 10))) + data.get_component('x').units = 'banana' + + ImageLayerState(layer=data, viewer_state=viewer_state)