-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
38 deletions.
There are no files selected for viewing
92 changes: 72 additions & 20 deletions
92
jdaviz/configs/imviz/plugins/rotate_image/rotate_image.py
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 |
---|---|---|
@@ -1,40 +1,92 @@ | ||
from traitlets import Bool, observe | ||
import numpy as np | ||
from astropy.nddata import NDData | ||
from astropy.wcs import WCS | ||
from traitlets import Any, Bool, observe | ||
|
||
from jdaviz.core.custom_traitlets import FloatHandleEmpty | ||
from jdaviz.configs.imviz.wcs_utils import rotate_wcs | ||
from jdaviz.core.registries import tray_registry | ||
from jdaviz.core.template_mixin import TemplateMixin | ||
from jdaviz.core.template_mixin import TemplateMixin, ViewerSelectMixin, DatasetSelectMixin | ||
|
||
__all__ = ['RotateImageSimple'] | ||
|
||
|
||
@tray_registry('imviz-rotate-image', label="Simple Image Rotation") | ||
class RotateImageSimple(TemplateMixin): | ||
class RotateImageSimple(TemplateMixin, ViewerSelectMixin, DatasetSelectMixin): | ||
template_file = __file__, "rotate_image.vue" | ||
|
||
rotate_mode_on = Bool(False).tag(sync=True) | ||
angle = FloatHandleEmpty(0).tag(sync=True) | ||
angle = Any(0).tag(sync=True) | ||
|
||
@observe('angle') | ||
def vue_rotate_image(*args, **kwargs): | ||
pass # TODO | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._theta = 0 | ||
|
||
# Dummy array to go with the rotated WCS, modified as needed later. | ||
w_ref = WCS({'CTYPE1': 'RA---TAN', 'CTYPE2': 'DEC--TAN', | ||
'CUNIT1': 'deg', 'CUNIT2': 'deg', | ||
'CRPIX1': 1, 'CRPIX2': 1, | ||
'NAXIS1': 3, 'NAXIS2': 3}) | ||
self._ndd_ref_label = '_simple_rotated_wcs_ref' | ||
self._ndd_ref = NDData(np.zeros((3, 3), dtype=np.uint8), wcs=w_ref, | ||
meta={'Plugin': 'Simple Image Rotation'}) | ||
|
||
def _handle_compass_zoom_box(self): | ||
# Hide zoom box in Compass because it is all screwy. | ||
hide_zoom = self.rotate_mode_on and not np.allclose(self._theta, 0) | ||
|
||
# Create a small dummy image | ||
for viewer in self.app._viewer_store.values(): | ||
viewer._compass_show_zoom = not hide_zoom | ||
# Force redraw if the compass is visible. | ||
if viewer.compass is not None and viewer.compass.plugin_opened: | ||
viewer.on_limits_change() | ||
|
||
# Create a fake WCS with desired orientation | ||
@observe('rotate_mode_on') | ||
def vue_toggle_on_off(self, *args, **kwargs): | ||
# It is hard to keep track what the previous reference data was or | ||
# if it is still valid, so just brute force here. | ||
if not self.rotate_mode_on: | ||
for vid in self.app.get_viewer_ids(): | ||
# This is silent no-op if it is not in that viewer. | ||
self.app.remove_data_from_viewer(vid, self._ndd_ref_label) | ||
|
||
self._handle_compass_zoom_box() | ||
|
||
# FIXME: How to wait till user has stopped typing? | ||
@observe('angle') | ||
def vue_rotate_image(self, *args, **kwargs): | ||
try: | ||
self._theta = float(self.angle) | ||
except Exception: | ||
return | ||
|
||
# Add it into data collection | ||
w_data = self.dataset.selected_dc_item.coords | ||
if w_data is None: # Nothing to do | ||
return | ||
|
||
# Make it reference. Remember which one as old reference. | ||
# If no API to make something reference, remove everything, readd | ||
# with it first, and relink. | ||
# Adjust the fake WCS to data with desired orientation | ||
w_new = rotate_wcs(self._ndd_ref.wcs, self._theta) | ||
|
||
# When angle changes, is updating Data.coords with new WCS good enough? | ||
# Center to selected data. | ||
# FIXME (GWCS): https://github.com/spacetelescope/gwcs/issues/408 | ||
if isinstance(w_data, WCS): | ||
w_new.wcs.crval = w_data.wcs.crval | ||
w_new.wcs.set() | ||
|
||
# Find a way to hide this image from Data dropdown, blink, etc | ||
# Add it into data collection or just modify if already there. | ||
if self._ndd_ref_label in self.app.data_collection.labels: | ||
self.app.data_collection[self._ndd_ref_label].coords = w_new | ||
else: | ||
viewer = self.app.get_viewer(self.viewer_selected) | ||
# FIXME: Need to build native Data object here. | ||
self.app.add_data(self._ndd_ref, data_label=self._ndd_ref_label, notify_done=False) | ||
self.app.add_data_to_viewer(viewer, self._ndd_ref_label, clear_other_data=False) | ||
|
||
# When toggle off, discard this from data_collection and restore old reference. | ||
# Make it a reference. | ||
if viewer.state.reference_data.label != self._ndd_ref_label: | ||
viewer.state.reference_data = self.app.data_collection[self._ndd_ref_label] | ||
|
||
# Have compass hide zoom box when rotate_mode_on is True | ||
# and un-hide it when False. | ||
self._handle_compass_zoom_box() | ||
|
||
# Write tests. | ||
# TODO: Find a way to hide this image from Data dropdown, blink, etc | ||
# TODO: Manual testing. | ||
# TODO: Write tests. |
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