Skip to content

Commit

Permalink
POC: Reproject plugin for Imviz.
Browse files Browse the repository at this point in the history
Added new optional dependency of reproject package.
  • Loading branch information
pllim committed Jan 30, 2023
1 parent f1044f4 commit 3e56b99
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ Cubeviz
Imviz
^^^^^

- New Reproject plugin. [#1949]

Mosviz
^^^^^^

- Reliably retrieves identifier using each datasets' metadata entry. [#1851]

- Improved mouseover info display for spectrum viewer. [#1894]
Expand Down
21 changes: 21 additions & 0 deletions docs/imviz/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ are not stored. To save the current result before submitting a new query, you ca
The table returned from the API above may cover more sources than shown in the currently zoomed-in
portion of the image. Additional steps will be needed to filter out these points, if necessary.

.. _imviz-reproject:

Reproject
=========

.. note:: This plugin requires ``reproject`` to be installed.

.. warning::

This operation is not recommended if the input image has not been
corrected for distortion.

Reprojecting a large image may be resource intensive.

Use the `reproject <https://reproject.readthedocs.io/>`_ package to create a new image
that is the input image reprojected to its optimal celestial WCS.

Choose the desired image from the data selection menu, if applicable.
Then click on the :guilabel:`REPROJECT` button.
If successful, a new reprojected image will be displayed in Imviz.

.. _imviz-export-plot:

Export Plot
Expand Down
1 change: 1 addition & 0 deletions jdaviz/configs/imviz/imviz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tray:
- imviz-line-profile-xy
- imviz-aper-phot-simple
- imviz-catalogs
- imviz-reproject
- g-export-plot
viewer_area:
- container: col
Expand Down
3 changes: 2 additions & 1 deletion jdaviz/configs/imviz/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
from .compass import * # noqa
from .aper_phot_simple import * # noqa
from .line_profile_xy import * # noqa
from .catalogs import * # noqa
from .catalogs import * # noqa
from .reproject import * # noqa
1 change: 1 addition & 0 deletions jdaviz/configs/imviz/plugins/reproject/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .reproject import * # noqa
69 changes: 69 additions & 0 deletions jdaviz/configs/imviz/plugins/reproject/reproject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from traitlets import Bool

from jdaviz.core.events import SnackbarMessage
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import PluginTemplateMixin, DatasetSelectMixin

try:
from reproject import reproject_interp
from reproject.mosaicking import find_optimal_celestial_wcs
except ImportError:
HAS_REPROJECT = False
else:
HAS_REPROJECT = True

__all__ = ['Reproject']


@tray_registry('imviz-reproject', label="Reproject")
class Reproject(PluginTemplateMixin, DatasetSelectMixin):
template_file = __file__, "reproject.vue"

reproject_in_progress = Bool(False).tag(sync=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

if not HAS_REPROJECT:
self.disabled_msg = 'Please install reproject and restart Jdaviz to use this plugin'
return

def vue_do_reproject(self, *args, **kwargs):
if (not HAS_REPROJECT or self.dataset_selected not in self.data_collection
or self.reproject_in_progress):
return

data = self.data_collection[self.dataset_selected]
wcs_in = data.coords
if wcs_in is None:
return

from astropy.nddata import NDData

self.reproject_in_progress = True
try:
# TODO: Support GWCS (https://github.com/astropy/reproject/issues/328)
# Find WCS where North is pointing up.
wcs_out, shape_out = find_optimal_celestial_wcs([(data.shape, wcs_in)], frame='icrs')

# Reproject image to new WCS.
comp = data.get_component(data.main_components[0])
new_arr = reproject_interp((comp.data, wcs_in), wcs_out, shape_out=shape_out,
return_footprint=False)

# TODO: Let user customize new label; have default label not be so ugly.
new_label = f'{self.dataset_selected} (reprojected)'

# Stuff it back into Imviz.
# We don't want to inherit input metadata because it might have wrong (unrotated)
# WCS info in the header metadata.
ndd = NDData(new_arr, wcs=wcs_out)
self.app._jdaviz_helper.load_data(ndd, data_label=new_label)

except Exception as e:
self.hub.broadcast(SnackbarMessage(
f"Failed to reproject {self.dataset_selected}: {repr(e)}",
color='error', sender=self))

finally:
self.reproject_in_progress = False
38 changes: 38 additions & 0 deletions jdaviz/configs/imviz/plugins/reproject/reproject.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<template>
<j-tray-plugin
description='Perform reprojection for a single image.'
:link="'https://jdaviz.readthedocs.io/en/'+vdocs+'/'+config+'/plugins.html#reproject'"
:disabled_msg="disabled_msg"
:popout_button="popout_button">

<plugin-dataset-select
:items="dataset_items"
:selected.sync="dataset_selected"
:show_if_single_entry="false"
label="Data"
hint="Select the data for reprojection."
/>

<div v-if='dataset_selected'>
<v-row justify="end">
<v-btn color="primary" text @click="do_reproject">Reproject</v-btn>
</v-row>
</div>

<div v-if="reproject_in_progress"
class="text-center"
style="grid-area: 1/1;
z-index:2;
margin-left: -24px;
margin-right: -24px;
padding-top: 60px;
background-color: rgb(0 0 0 / 20%)">
<v-progress-circular
indeterminate
color="spinner"
size="50"
width="6"
></v-progress-circular>
</div>
</j-tray-plugin>
</template>
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ test =
docs =
sphinx-rtd-theme
sphinx-astropy
all =
reproject>=0.10

[options.package_data]
jdaviz =
Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ deps =
# The following indicates which extras_require from setup.cfg will be installed
extras =
test
# Uncomment when we have all again in setup.cfg
#alldeps: all
alldeps: all

commands =
devdeps: pip install -U -i https://pypi.anaconda.org/astropy/simple astropy --pre
Expand Down

0 comments on commit 3e56b99

Please sign in to comment.