Skip to content

Commit

Permalink
Imviz: API for center_on and offset_to in pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim committed Jun 17, 2021
1 parent d8fad81 commit e1e8906
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
54 changes: 54 additions & 0 deletions jdaviz/configs/imviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import re
from copy import deepcopy

from astropy.coordinates import SkyCoord
from echo import delay_callback

from jdaviz.core.helpers import ConfigHelper

__all__ = ['Imviz']
Expand Down Expand Up @@ -79,6 +82,57 @@ def load_data(self, data, parser_reference=None, **kwargs):
self.app.load_data(
data, parser_reference=parser_reference, **kwargs)

def center_on(self, point):
"""Centers the view on a particular point.
Parameters
----------
point : tuple or `~astropy.coordinates.SkyCoord`
If tuple of ``(X, Y)`` is given, it is assumed
to be in data coordinates and 0-indexed.
"""
viewer = self.app.get_viewer("viewer-1")

if isinstance(point, SkyCoord):
raise NotImplementedError
else:
with delay_callback(viewer.state, 'x_min', 'x_max', 'y_min', 'y_max'):
width = viewer.state.x_max - viewer.state.x_min
height = viewer.state.y_max - viewer.state.y_min
viewer.state.x_min = point[0] - (width * 0.5)
viewer.state.y_min = point[1] - (height * 0.5)
viewer.state.x_max = viewer.state.x_min + width
viewer.state.y_max = viewer.state.y_min + height

def offset_to(self, dx, dy, skycoord_offset=False):
"""Move the center to a point that is given offset
away from the current center.
Parameters
----------
dx, dy : float
Offset value. Unit is assumed based on
``skycoord_offset``.
skycoord_offset : bool
If `True`, offset must be given in degrees.
Otherwise, they are in pixel values.
"""
viewer = self.app.get_viewer("viewer-1")

if skycoord_offset:
raise NotImplementedError
else:
with delay_callback(viewer.state, 'x_min', 'x_max', 'y_min', 'y_max'):
width = viewer.state.x_max - viewer.state.x_min
height = viewer.state.y_max - viewer.state.y_min
viewer.state.x_min += dx
viewer.state.y_min += dy
viewer.state.x_max = viewer.state.x_min + width
viewer.state.y_max = viewer.state.y_min + height


def split_filename_with_fits_ext(filename):
"""Split a ``filename[ext]`` input into filename and FITS extension.
Expand Down
55 changes: 54 additions & 1 deletion notebooks/ImvizExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"cells": [
{
"cell_type": "markdown",
"id": "17b4af6f",
"metadata": {},
"source": [
"# Proof of concept of Imviz requirements using glupyter/bqplot"
]
},
{
"cell_type": "markdown",
"id": "3b0f5142",
"metadata": {},
"source": [
"We start off by silencing warnings that can happen when loading data as well as deprecation warnings, for clarity:"
Expand All @@ -17,6 +19,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "a4b3aee3",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -27,6 +30,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "bccca7f4",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -38,6 +42,7 @@
},
{
"cell_type": "markdown",
"id": "0ec7c554",
"metadata": {},
"source": [
"We start off by looking at some of the basic features:"
Expand All @@ -46,6 +51,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "77c97590",
"metadata": {
"scrolled": false
},
Expand Down Expand Up @@ -88,6 +94,7 @@
},
{
"cell_type": "markdown",
"id": "1abb759e",
"metadata": {},
"source": [
"Panning and zooming is possible by showing the viewer toolbar and clicking on the '+'-shaped icon, then dragging around in the image and using scrolling to zoom in and out. To change the stretch and colormap, show the **Layer** options accessible through the last icon in the viewer toolbar.\n",
Expand All @@ -98,6 +105,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d76b335d",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -106,6 +114,7 @@
},
{
"cell_type": "markdown",
"id": "11888383",
"metadata": {},
"source": [
"the colormap:"
Expand All @@ -114,6 +123,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "fec4de61",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -122,6 +132,7 @@
},
{
"cell_type": "markdown",
"id": "def18589",
"metadata": {},
"source": [
"the limits via the percentile option:"
Expand All @@ -130,6 +141,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7c41f17a",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -138,6 +150,7 @@
},
{
"cell_type": "markdown",
"id": "c59d150e",
"metadata": {},
"source": [
"or the limits directly:"
Expand All @@ -146,6 +159,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "2a3ac022",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -155,13 +169,15 @@
},
{
"cell_type": "markdown",
"id": "2f25ccd8",
"metadata": {},
"source": [
"Note also that in the above example there are mouse-over coordinates visible by default."
]
},
{
"cell_type": "markdown",
"id": "5a7848d0",
"metadata": {},
"source": [
"It possible to make selections/regions in images and export these to astropy regions. Click on the viewer toolbar then click on the circular selection tool, and drag and click to select an interesting region on the sky. We can then export this region with:"
Expand All @@ -170,6 +186,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "1e369edd",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -179,6 +196,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "a0385234",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -187,6 +205,7 @@
},
{
"cell_type": "markdown",
"id": "11afa633",
"metadata": {},
"source": [
"Since the region is an astropy region, we can e.g. convert it to a mask:"
Expand All @@ -195,6 +214,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "71e2191b",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -204,6 +224,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0a4e02ef",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -213,15 +234,47 @@
{
"cell_type": "code",
"execution_count": null,
"id": "164a1a40",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(mask.to_image(data.shape), origin='lower')"
]
},
{
"cell_type": "markdown",
"id": "99766ca8",
"metadata": {},
"source": [
"You can also programmatically control the viewer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e908421",
"metadata": {},
"outputs": [],
"source": [
"# Center the image on given pixel position.\n",
"imviz.center_on((10, 20)) # X=10,Y=20 (0-indexed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e68b6306",
"metadata": {},
"outputs": [],
"source": [
"# Move the image with the given pixel offsets.\n",
"imviz.offset_to(-10, 10) # dX, dY"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0f3845a",
"metadata": {},
"outputs": [],
"source": []
Expand All @@ -243,7 +296,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.9.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit e1e8906

Please sign in to comment.