-
Notifications
You must be signed in to change notification settings - Fork 75
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
Rotate image in Imviz #1340
Closed
Closed
Rotate image in Imviz #1340
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
101 changes: 101 additions & 0 deletions
101
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import numpy as np | ||
from astropy.nddata import NDData | ||
from traitlets import Any, Bool, observe | ||
|
||
from jdaviz.configs.imviz.helper import link_image_data | ||
from jdaviz.configs.imviz.plugins.parsers import _nddata_to_glue_data | ||
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 TemplateMixin, ViewerSelectMixin, DatasetSelectMixin | ||
|
||
__all__ = ['RotateImageSimple'] | ||
|
||
|
||
# TODO: Refactor to use Kyle's CSS rotation? | ||
# TODO: Maybe we do not need DataSelectMixin. | ||
@tray_registry('imviz-rotate-image', label="Simple Image Rotation") | ||
class RotateImageSimple(TemplateMixin, ViewerSelectMixin, DatasetSelectMixin): | ||
template_file = __file__, "rotate_image.vue" | ||
|
||
rotate_mode_on = Bool(False).tag(sync=True) | ||
angle = Any(0).tag(sync=True) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._theta = 0 # degrees, clockwise | ||
|
||
# Dummy array to go with the rotated WCS, modified as needed later. | ||
ndd = NDData(np.zeros((2, 2), dtype=np.uint8), wcs=generate_rotated_wcs(0), | ||
meta={'Plugin': 'Simple Image Rotation'}) | ||
for cur_data, cur_label in _nddata_to_glue_data(ndd, '_simple_rotated_wcs_ref'): | ||
self._data_ref = cur_data | ||
self._data_ref_label = cur_label | ||
break # Just the DATA | ||
|
||
def _update_all_viewers(self): | ||
for viewer in self.app._viewer_store.values(): | ||
viewer.update() # Force viewer to update. | ||
|
||
@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. | ||
try: | ||
self.app.remove_data_from_viewer(vid, self._data_ref_label) | ||
except Exception: # nosec B110 | ||
pass | ||
|
||
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_in = generate_rotated_wcs(self._theta) | ||
|
||
# TODO: How to make this more robust? | ||
# Match with selected data. | ||
w_shape = self.dataset.selected_dc_item.shape | ||
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() | ||
|
||
# Add it into data collection if not already there. | ||
if self._data_ref_label not in self.app.data_collection.labels: | ||
self.app.add_data(self._data_ref, data_label=self._data_ref_label, | ||
notify_done=False) | ||
|
||
# Update the WCS. | ||
self.app.data_collection[self._data_ref_label].coords = w_in | ||
|
||
# Force link by WCS if not already. Otherwise, no point in rotating WCS. | ||
# Default settings is enough since we already said distortions are ignored. | ||
link_image_data(self.app, link_type='wcs') | ||
|
||
# Make it a reference. | ||
self.app.add_data_to_viewer(self.viewer_selected, self._data_ref_label, | ||
clear_other_data=False) | ||
viewer = self.app.get_viewer(self.viewer_selected) | ||
if viewer.state.reference_data.label != self._data_ref_label: | ||
viewer.state.reference_data = self.app.data_collection[self._data_ref_label] | ||
|
||
# Force all viewers to update. | ||
self._update_all_viewers() | ||
except Exception as err: # pragma: no cover | ||
self.hub.broadcast(SnackbarMessage( | ||
f"Image rotation failed: {repr(err)}", color='error', sender=self)) |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
<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> | ||
|
||
<v-row> | ||
<v-switch | ||
label="Enable celestial axes rotation" | ||
hint="Toggle rotation mode on or off" | ||
v-model="rotate_mode_on" | ||
persistent-hint> | ||
</v-switch> | ||
</v-row> | ||
|
||
<div v-if="rotate_mode_on"> | ||
<plugin-viewer-select | ||
:items="viewer_items" | ||
:selected.sync="viewer_selected" | ||
label="Viewer" | ||
hint="Select viewer." | ||
/> | ||
|
||
<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-row> | ||
</div> | ||
</j-tray-plugin> | ||
</template> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used to have this in the app somewhere but maybe it was removed in one of the refactoring. @kecnry , I know your pet peeve is the reference vs ID stuff, so I want to make sure you are okay with this change.