Skip to content

Commit c594308

Browse files
authored
Merge pull request #140 from pllim/rename-offset-by
New offset_by with new API
2 parents ace5abb + 9587099 commit c594308

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

astrowidgets/core.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
# THIRD-PARTY
88
import numpy as np
9+
from astropy import units as u
910
from astropy.coordinates import SkyCoord
1011
from astropy.io import fits
1112
from astropy.table import Table, vstack
13+
from astropy.utils.decorators import deprecated
1214

1315
# Jupyter widgets
1416
import ipywidgets as ipyw
@@ -343,11 +345,14 @@ def center_on(self, point):
343345
else:
344346
self._viewer.set_pan(*(np.asarray(point) - self._pixel_offset))
345347

348+
@deprecated('0.3', alternative='offset_by')
346349
def offset_to(self, dx, dy, skycoord_offset=False):
347350
"""
348351
Move the center to a point that is given offset
349352
away from the current center.
350353
354+
.. note:: This is deprecated. Use :meth:`offset_by`.
355+
351356
Parameters
352357
----------
353358
dx, dy : float
@@ -367,6 +372,28 @@ def offset_to(self, dx, dy, skycoord_offset=False):
367372
pan_x, pan_y = self._viewer.get_pan(coord=coord)
368373
self._viewer.set_pan(pan_x + dx, pan_y + dy, coord=coord)
369374

375+
def offset_by(self, dx, dy):
376+
"""
377+
Move the center to a point that is given offset
378+
away from the current center.
379+
380+
Parameters
381+
----------
382+
dx, dy : float or `~astropy.unit.Quantity`
383+
Offset value. Without a unit, assumed to be pixel offsets.
384+
If a unit is attached, offset by pixel or sky is assumed from
385+
the unit.
386+
387+
"""
388+
dx_val, dx_coord = _offset_is_pixel_or_sky(dx)
389+
dy_val, dy_coord = _offset_is_pixel_or_sky(dy)
390+
391+
if dx_coord != dy_coord:
392+
raise ValueError(f'dx is of type {dx_coord} but dy is of type {dy_coord}')
393+
394+
pan_x, pan_y = self._viewer.get_pan(coord=dx_coord)
395+
self._viewer.set_pan(pan_x + dx_val, pan_y + dy_val, coord=dx_coord)
396+
370397
@property
371398
def zoom_level(self):
372399
"""
@@ -955,3 +982,18 @@ def save(self, filename):
955982
# to write that out to a file.
956983
with open(filename, 'wb') as f:
957984
f.write(self._jup_img.value)
985+
986+
987+
def _offset_is_pixel_or_sky(x):
988+
if isinstance(x, u.Quantity):
989+
if x.unit in (u.dimensionless_unscaled, u.pix):
990+
coord = 'data'
991+
val = x.value
992+
else:
993+
coord = 'wcs'
994+
val = x.to_value(u.deg)
995+
else:
996+
coord = 'data'
997+
val = x
998+
999+
return val, coord

astrowidgets/tests/test_api.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import pytest
44

5+
from astropy import units as u
56
from astropy.io import fits
67
from astropy.nddata import NDData
78
from astropy.table import Table
9+
from astropy.utils.exceptions import AstropyDeprecationWarning
810

911
from ginga.ColorDist import ColorDistBase
1012

@@ -42,7 +44,26 @@ def test_offset_to():
4244
image = ImageWidget()
4345
dx = 10
4446
dy = 10
45-
image.offset_to(dx, dy)
47+
with pytest.warns(AstropyDeprecationWarning):
48+
image.offset_to(dx, dy)
49+
50+
51+
def test_offset_by():
52+
image = ImageWidget()
53+
54+
# Pixels
55+
image.offset_by(10, 10)
56+
image.offset_by(10 * u.pix, 10 * u.dimensionless_unscaled)
57+
image.offset_by(10, 10 * u.pix)
58+
59+
# Sky
60+
image.offset_by(1 * u.arcsec, 0.001 * u.deg)
61+
62+
with pytest.raises(u.UnitConversionError):
63+
image.offset_by(1 * u.arcsec, 1 * u.AA)
64+
65+
with pytest.raises(ValueError, match='but dy is of type'):
66+
image.offset_by(1 * u.arcsec, 1)
4667

4768

4869
def test_zoom_level():
@@ -271,10 +292,10 @@ def test_scroll_pan():
271292
assert image.scroll_pan is val
272293

273294

274-
def test_save():
295+
def test_save(tmp_path):
275296
image = ImageWidget()
276297
filename = 'woot.png'
277-
image.save(filename)
298+
image.save(tmp_path / filename)
278299

279300

280301
def test_width_height():

example_notebooks/ginga_widget.ipynb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
"outputs": [],
186186
"source": [
187187
"# Programmatically offset w.r.t. current center\n",
188-
"w.offset_to(10, 10)"
188+
"w.offset_by(10, 10)"
189189
]
190190
},
191191
{
@@ -212,12 +212,14 @@
212212
"metadata": {},
213213
"outputs": [],
214214
"source": [
215+
"from astropy import units as u\n",
216+
"\n",
215217
"# Change the values if needed.\n",
216-
"deg_offset = 0.001\n",
218+
"deg_offset = 0.001 * u.deg\n",
217219
"\n",
218220
"# Programmatically offset (in degrees) w.r.t.\n",
219221
"# SkyCoord center\n",
220-
"w.offset_to(deg_offset, deg_offset, skycoord_offset=True)"
222+
"w.offset_by(deg_offset, deg_offset)"
221223
]
222224
},
223225
{

0 commit comments

Comments
 (0)