forked from spacetelescope/jdaviz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
that still does not work
- Loading branch information
Showing
13 changed files
with
780 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .rotate_image import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import numpy as np | ||
from traitlets import Any | ||
|
||
from jdaviz.configs.imviz.wcs_utils import generate_rotated_wcs | ||
from jdaviz.core.events import SnackbarMessage | ||
from jdaviz.core.registries import tray_registry | ||
from jdaviz.core.template_mixin import PluginTemplateMixin, DatasetSelectMixin | ||
|
||
__all__ = ['RotateImageSimple'] | ||
|
||
|
||
@tray_registry('imviz-rotate-image', label="Simple Image Rotation") | ||
class RotateImageSimple(PluginTemplateMixin, DatasetSelectMixin): | ||
template_file = __file__, "rotate_image.vue" | ||
|
||
angle = Any(0).tag(sync=True) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._orig_wcs_key = '_orig_wcs' | ||
self._theta = 0 # degrees, clockwise | ||
|
||
def vue_rotate_image(self, *args, **kwargs): | ||
# We only grab the value here to avoid constantly updating as | ||
# user is still entering or updating the value. | ||
try: | ||
self._theta = float(self.angle) | ||
except Exception: | ||
return | ||
|
||
w_data = self.dataset.selected_dc_item.coords | ||
if w_data is None: # Nothing to do | ||
return | ||
|
||
try: | ||
# Adjust the fake WCS to data with desired orientation. | ||
w_shape = self.dataset.selected_dc_item.shape | ||
w_in = generate_rotated_wcs(self._theta, shape=w_shape) | ||
|
||
# TODO: How to make this more robust? | ||
# Match with selected data. | ||
sky0 = w_data.pixel_to_world(0, 0) | ||
sky1 = w_data.pixel_to_world(w_shape[1] * 0.5, w_shape[0] * 0.5) | ||
avg_cdelt = (abs(sky1.ra.deg - sky0.ra.deg) + abs(sky1.dec.deg - sky0.dec.deg)) * 0.5 | ||
w_in.wcs.crval = np.array([sky1.ra.deg, sky1.dec.deg]) | ||
w_in.wcs.cdelt = np.array([-avg_cdelt, avg_cdelt]) | ||
w_in.wcs.set() | ||
|
||
# FIXME: This does not work -- We did not rotate the data. | ||
# Store the original WCS, if not already. | ||
if self._orig_wcs_key not in self.dataset.selected_dc_item.meta: | ||
self.dataset.selected_dc_item.meta[self._orig_wcs_key] = self.dataset.selected_dc_item.coords # noqa: E501 | ||
|
||
# Update the WCS. | ||
self.dataset.selected_dc_item.coords = w_in | ||
|
||
except Exception as err: # pragma: no cover | ||
self.hub.broadcast(SnackbarMessage( | ||
f"Image rotation failed: {repr(err)}", color='error', sender=self)) | ||
|
||
def vue_reset_image(self, *args, **kwargs): | ||
w_data = self.dataset.selected_dc_item.coords | ||
if w_data is None: # Nothing to do | ||
return | ||
|
||
try: | ||
if self._orig_wcs_key in self.dataset.selected_dc_item.meta: | ||
self.dataset.selected_dc_item.coords = self.dataset.selected_dc_item.meta[self._orig_wcs_key] # noqa: E501 | ||
|
||
except Exception as err: # pragma: no cover | ||
self.hub.broadcast(SnackbarMessage( | ||
f"Image rotation reset failed: {repr(err)}", color='error', sender=self)) |
32 changes: 32 additions & 0 deletions
32
jdaviz/configs/imviz/plugins/rotate_image/rotate_image.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<template> | ||
<j-tray-plugin> | ||
<v-row> | ||
<j-docs-link :link="'https://jdaviz.readthedocs.io/en/'+vdocs+'/'+config+'/plugins.html#simple-image-rotation'">Rotate image N-up/E-left.</j-docs-link> | ||
</v-row> | ||
|
||
<plugin-dataset-select | ||
:items="dataset_items" | ||
:selected.sync="dataset_selected" | ||
:show_if_single_entry="false" | ||
label="Data" | ||
hint="Select the data to rotate." | ||
/> | ||
|
||
<v-row> | ||
<v-col> | ||
<v-text-field | ||
v-model="angle" | ||
type="number" | ||
label="Angle" | ||
hint="Rotation angle of N-axis in degrees clockwise (0 is N-up)" | ||
></v-text-field> | ||
</v-col> | ||
</v-row> | ||
|
||
<v-row justify="end"> | ||
<v-btn color="primary" text @click="rotate_image">Rotate</v-btn> | ||
<v-btn color="primary" text @click="reset_image">Reset</v-btn> | ||
</v-row> | ||
|
||
</j-tray-plugin> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import gwcs | ||
import numpy as np | ||
from astropy import units as u | ||
from astropy.coordinates import ICRS | ||
from astropy.modeling import models | ||
from astropy.nddata import NDData | ||
from astropy.wcs import WCS | ||
from gwcs import coordinate_frames as cf | ||
from numpy.testing import assert_allclose | ||
|
||
|
||
def test_simple_image_rotation_plugin(imviz_helper): | ||
# Mimic interactive mode where viewer is displayed. | ||
imviz_helper.default_viewer.shape = (100, 100) | ||
imviz_helper.default_viewer.state._set_axes_aspect_ratio(1) | ||
|
||
a = np.zeros((10, 8)) | ||
a[0, 0] = 1 # Bright corner for sanity check. | ||
|
||
# Adapted from HST/ACS FITS WCS without the distortion. | ||
w_fits = WCS({'WCSAXES': 2, 'NAXIS1': 8, 'NAXIS2': 10, | ||
'CRPIX1': 5.0, 'CRPIX2': 5.0, | ||
'PC1_1': -1.14852e-05, 'PC1_2': 7.01477e-06, | ||
'PC2_1': 7.75765e-06, 'PC2_2': 1.20927e-05, | ||
'CDELT1': 1.0, 'CDELT2': 1.0, | ||
'CUNIT1': 'deg', 'CUNIT2': 'deg', | ||
'CTYPE1': 'RA---TAN', 'CTYPE2': 'DEC--TAN', | ||
'CRVAL1': 3.581704851882, 'CRVAL2': -30.39197867265, | ||
'LONPOLE': 180.0, 'LATPOLE': -30.39197867265, | ||
'MJDREF': 0.0, 'RADESYS': 'ICRS'}) | ||
|
||
# Adapted from GWCS example. | ||
shift_by_crpix = models.Shift(-(5 - 1) * u.pix) & models.Shift(-(5 - 1) * u.pix) | ||
matrix = np.array([[1.290551569736E-05, 5.9525007864732E-06], | ||
[5.0226382102765E-06, -1.2644844123757E-05]]) | ||
rotation = models.AffineTransformation2D(matrix * u.deg, translation=[0, 0] * u.deg) | ||
rotation.input_units_equivalencies = {"x": u.pixel_scale(1 * (u.deg / u.pix)), | ||
"y": u.pixel_scale(1 * (u.deg / u.pix))} | ||
rotation.inverse = models.AffineTransformation2D(np.linalg.inv(matrix) * u.pix, | ||
translation=[0, 0] * u.pix) | ||
rotation.inverse.input_units_equivalencies = {"x": u.pixel_scale(1 * (u.pix / u.deg)), | ||
"y": u.pixel_scale(1 * (u.pix / u.deg))} | ||
tan = models.Pix2Sky_TAN() | ||
celestial_rotation = models.RotateNative2Celestial( | ||
3.581704851882 * u.deg, -30.39197867265 * u.deg, 180 * u.deg) | ||
det2sky = shift_by_crpix | rotation | tan | celestial_rotation | ||
det2sky.name = "linear_transform" | ||
detector_frame = cf.Frame2D(name="detector", axes_names=("x", "y"), unit=(u.pix, u.pix)) | ||
sky_frame = cf.CelestialFrame(reference_frame=ICRS(), name='icrs', unit=(u.deg, u.deg)) | ||
pipeline = [(detector_frame, det2sky), (sky_frame, None)] | ||
w_gwcs = gwcs.WCS(pipeline) | ||
|
||
# Load data into Imviz. | ||
imviz_helper.load_data(NDData(a, wcs=w_fits, unit='electron/s'), data_label='fits_wcs') | ||
imviz_helper.load_data(NDData(a, wcs=w_gwcs), data_label='gwcs') | ||
imviz_helper.load_data(a, data_label='no_wcs') | ||
|
||
plg = imviz_helper.plugins['Simple Image Rotation']._obj | ||
plg.plugin_opened = True | ||
|
||
# Select data. | ||
assert plg.dataset.selected == 'fits_wcs[DATA]' | ||
|
||
# Rotate with default settings. | ||
plg.vue_rotate_image() | ||
assert_allclose(plg._theta, 0) | ||
assert plg.dataset.labels == ['fits_wcs[DATA]', 'gwcs[DATA]', 'no_wcs'] | ||
|
||
# The zoom box is now a rotated rombus. | ||
# no_wcs is the same as fits_wcs because they are linked by pixel, but gwcs is different. | ||
fits_wcs_zoom_limits = imviz_helper.default_viewer._get_zoom_limits( | ||
imviz_helper.app.data_collection['fits_wcs[DATA]']) | ||
assert_allclose(fits_wcs_zoom_limits, ((-0.37873422, 5.62910616), (3.42315751, 11.85389963), | ||
(13.52329142, 5.37451188), (9.72139968, -0.85028158))) | ||
assert_allclose(fits_wcs_zoom_limits, imviz_helper.default_viewer._get_zoom_limits( | ||
imviz_helper.app.data_collection['no_wcs'])) | ||
assert_allclose(imviz_helper.default_viewer._get_zoom_limits('gwcs[DATA]'), ( | ||
(7.60196643, 6.55912761), (10.83179746, -0.44341285), | ||
(0.25848145, -4.64322363), (-2.97134959, 2.35931683))) | ||
|
||
# TODO: Test the plugin. | ||
# 2. Try positive angle | ||
# 3. Add another viewer. | ||
# 4. Try negative angle on second viewer. | ||
# 5. Cross-test with Compass/Zoom-box, Line Profile, Coord Info? | ||
# 6. Untoggle and check state. | ||
# 7. Retoggle and check state. | ||
# 8. Make sure we also cover engine code not mentioned above but changed in the PR. | ||
|
||
# Second test function: Load just data without WCS, toggle on, should be no-op. | ||
|
||
# Also test function to make sure invalid angle does not crash plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Copyright (c) 2010-2019 CNRS / Centre de Recherche Astrophysique de Lyon | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its contributors | ||
may be used to endorse or promote products derived from this software without | ||
specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Oops, something went wrong.