Skip to content

Commit

Permalink
Merge pull request #2232 from astrofrog/avoid-unecessary-wcs-update
Browse files Browse the repository at this point in the history
Prevent resetting of WCS in image if data does not change
  • Loading branch information
astrofrog authored Sep 16, 2021
2 parents bb0da1c + 41a51a3 commit 3ea1ec6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Full changelog
==============

v1.2.2 (unreleased)
-------------------

* Prevent resetting of image viewer limits when adding a dataset to the
viewer. [#2232]

v1.2.1 (2021-08-24)
-------------------

Expand Down
28 changes: 28 additions & 0 deletions glue/viewers/image/qt/tests/test_data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,34 @@ def test_legend(self):

assert to_hex(handles[1].get_facecolor()) == viewer_state.layers[1].color

def test_limit_resetting(self):

# Test to make sure that the limits only change if the reference data
# is changed

self.viewer.state.aspect = 'auto'

self.viewer.add_data(self.image1)

self.viewer.state.x_min = 0.2
self.viewer.state.x_max = 0.4
self.viewer.state.y_min = 0.3
self.viewer.state.y_max = 0.5

self.viewer.add_data(self.image2)

assert self.viewer.state.x_min == 0.2
assert self.viewer.state.x_max == 0.4
assert self.viewer.state.y_min == 0.3
assert self.viewer.state.y_max == 0.5

self.viewer.state.reference_data = self.image2

assert self.viewer.state.x_min == -0.5
assert self.viewer.state.x_max == 1.5
assert self.viewer.state.y_min == -0.5
assert self.viewer.state.y_max == 1.5


class TestSessions(object):

Expand Down
12 changes: 9 additions & 3 deletions glue/viewers/image/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def setup_callbacks(self):
self.state.add_callback('x_att', self._set_wcs)
self.state.add_callback('y_att', self._set_wcs)
self.state.add_callback('slices', self._on_slice_change)
self.state.add_callback('reference_data', self._set_wcs)
self.state.add_callback('reference_data', self._set_wcs, echo_old=True)
self.axes._composite = CompositeArray()
self.axes._composite_image = imshow(self.axes, self.axes._composite, aspect='auto',
origin='lower', interpolation='nearest')
Expand Down Expand Up @@ -88,13 +88,19 @@ def add_data(self, data):

def _on_slice_change(self, event=None):
if self._changing_slice_requires_wcs_update:
self._set_wcs(event=event, relim=False)
self._set_wcs(relim=False)

def _set_wcs(self, event=None, relim=True):
def _set_wcs(self, before=None, after=None, relim=True):

if self.state.x_att is None or self.state.y_att is None or self.state.reference_data is None:
return

# A callback event for reference_data is triggered if the choices change
# but the actual selection doesn't - so we avoid resetting the WCS in
# this case.
if after is not None and before is after:
return

ref_coords = getattr(self.state.reference_data, 'coords', None)

if ref_coords is None or isinstance(ref_coords, LegacyCoordinates):
Expand Down

0 comments on commit 3ea1ec6

Please sign in to comment.