Skip to content

Commit

Permalink
Merge pull request #2493 from astrofrog/fix-invalid-units
Browse files Browse the repository at this point in the history
Make sure that we don't do any unit parsing if the original and target unit are the same
  • Loading branch information
astrofrog authored May 16, 2024
2 parents f70505f + 0fcd1c4 commit 5e92185
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 6 additions & 6 deletions glue/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions glue/viewers/image/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5e92185

Please sign in to comment.