From 994eb1e81eae6ec485c7809248eafda9c70894df Mon Sep 17 00:00:00 2001 From: Pey Lian Lim <2090236+pllim@users.noreply.github.com> Date: Thu, 1 Jul 2021 13:04:08 -0400 Subject: [PATCH 1/2] New offset_by with new API. Deprecate offset_to with old API. Added tests and updated example notebook. Fixed a test that was writing file into source dir. --- astrowidgets/core.py | 42 ++++++++++++++++++++++++++++ astrowidgets/tests/test_api.py | 27 ++++++++++++++++-- example_notebooks/ginga_widget.ipynb | 4 +-- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/astrowidgets/core.py b/astrowidgets/core.py index 674440e..3e573a8 100644 --- a/astrowidgets/core.py +++ b/astrowidgets/core.py @@ -6,9 +6,11 @@ # THIRD-PARTY import numpy as np +from astropy import units as u from astropy.coordinates import SkyCoord from astropy.io import fits from astropy.table import Table, vstack +from astropy.utils.decorators import deprecated # Jupyter widgets import ipywidgets as ipyw @@ -343,11 +345,14 @@ def center_on(self, point): else: self._viewer.set_pan(*(np.asarray(point) - self._pixel_offset)) + @deprecated('0.3', alternative='offset_by') def offset_to(self, dx, dy, skycoord_offset=False): """ Move the center to a point that is given offset away from the current center. + .. note:: This is deprecated. Use :meth:`offset_by`. + Parameters ---------- dx, dy : float @@ -367,6 +372,28 @@ def offset_to(self, dx, dy, skycoord_offset=False): pan_x, pan_y = self._viewer.get_pan(coord=coord) self._viewer.set_pan(pan_x + dx, pan_y + dy, coord=coord) + def offset_by(self, dx, dy): + """ + Move the center to a point that is given offset + away from the current center. + + Parameters + ---------- + dx, dy : float or `~astropy.unit.Quantity` + Offset value. Without a unit, assumed to be pixel offsets. + If a unit is attached, offset by pixel or sky is assumed from + the unit. + + """ + dx_val, dx_coord = _offset_is_pixel_or_sky(dx) + dy_val, dy_coord = _offset_is_pixel_or_sky(dy) + + if dx_coord != dy_coord: + raise ValueError(f'dx is of type {dx_coord} but dy is of type {dy_coord}') + + pan_x, pan_y = self._viewer.get_pan(coord=dx_coord) + self._viewer.set_pan(pan_x + dx_val, pan_y + dy_val, coord=dx_coord) + @property def zoom_level(self): """ @@ -955,3 +982,18 @@ def save(self, filename): # to write that out to a file. with open(filename, 'wb') as f: f.write(self._jup_img.value) + + +def _offset_is_pixel_or_sky(x): + if isinstance(x, u.Quantity): + if x.unit in (u.dimensionless_unscaled, u.pix): + coord = 'data' + val = x.value + else: + coord = 'wcs' + val = x.to_value(u.deg) + else: + coord = 'data' + val = x + + return val, coord diff --git a/astrowidgets/tests/test_api.py b/astrowidgets/tests/test_api.py index 396aff1..37113e5 100644 --- a/astrowidgets/tests/test_api.py +++ b/astrowidgets/tests/test_api.py @@ -2,9 +2,11 @@ import pytest +from astropy import units as u from astropy.io import fits from astropy.nddata import NDData from astropy.table import Table +from astropy.utils.exceptions import AstropyDeprecationWarning from ginga.ColorDist import ColorDistBase @@ -42,7 +44,26 @@ def test_offset_to(): image = ImageWidget() dx = 10 dy = 10 - image.offset_to(dx, dy) + with pytest.warns(AstropyDeprecationWarning): + image.offset_to(dx, dy) + + +def test_offset_by(): + image = ImageWidget() + + # Pixels + image.offset_by(10, 10) + image.offset_by(10 * u.pix, 10 * u.dimensionless_unscaled) + image.offset_by(10, 10 * u.pix) + + # Sky + image.offset_by(1 * u.arcsec, 0.001 * u.deg) + + with pytest.raises(u.UnitConversionError): + image.offset_by(1 * u.arcsec, 1 * u.AA) + + with pytest.raises(ValueError, match='but dy is of type'): + image.offset_by(1 * u.arcsec, 1) def test_zoom_level(): @@ -271,10 +292,10 @@ def test_scroll_pan(): assert image.scroll_pan is val -def test_save(): +def test_save(tmp_path): image = ImageWidget() filename = 'woot.png' - image.save(filename) + image.save(tmp_path / filename) def test_width_height(): diff --git a/example_notebooks/ginga_widget.ipynb b/example_notebooks/ginga_widget.ipynb index de06d26..99fafe8 100644 --- a/example_notebooks/ginga_widget.ipynb +++ b/example_notebooks/ginga_widget.ipynb @@ -185,7 +185,7 @@ "outputs": [], "source": [ "# Programmatically offset w.r.t. current center\n", - "w.offset_to(10, 10)" + "w.offset_by(10, 10)" ] }, { @@ -217,7 +217,7 @@ "\n", "# Programmatically offset (in degrees) w.r.t.\n", "# SkyCoord center\n", - "w.offset_to(deg_offset, deg_offset, skycoord_offset=True)" + "w.offset_by(deg_offset, deg_offset)" ] }, { From 9587099c1bef72c23d43aff8200aa70057f9fc7c Mon Sep 17 00:00:00 2001 From: Pey Lian Lim <2090236+pllim@users.noreply.github.com> Date: Thu, 1 Jul 2021 16:54:53 -0400 Subject: [PATCH 2/2] Fix example notebook --- example_notebooks/ginga_widget.ipynb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/example_notebooks/ginga_widget.ipynb b/example_notebooks/ginga_widget.ipynb index 99fafe8..c422fca 100644 --- a/example_notebooks/ginga_widget.ipynb +++ b/example_notebooks/ginga_widget.ipynb @@ -212,8 +212,10 @@ "metadata": {}, "outputs": [], "source": [ + "from astropy import units as u\n", + "\n", "# Change the values if needed.\n", - "deg_offset = 0.001\n", + "deg_offset = 0.001 * u.deg\n", "\n", "# Programmatically offset (in degrees) w.r.t.\n", "# SkyCoord center\n",