Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that we don't do any unit parsing if the original and target unit are the same #2493

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading