Skip to content

New offset_by with new API #140

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

Merged
merged 2 commits into from
Jul 2, 2021
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
42 changes: 42 additions & 0 deletions astrowidgets/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
27 changes: 24 additions & 3 deletions astrowidgets/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand Down
8 changes: 5 additions & 3 deletions example_notebooks/ginga_widget.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"outputs": [],
"source": [
"# Programmatically offset w.r.t. current center\n",
"w.offset_to(10, 10)"
"w.offset_by(10, 10)"
]
},
{
Expand All @@ -212,12 +212,14 @@
"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",
"w.offset_to(deg_offset, deg_offset, skycoord_offset=True)"
"w.offset_by(deg_offset, deg_offset)"
]
},
{
Expand Down