From 5a5d23a85334e171afdd52200e7baca8bda158f4 Mon Sep 17 00:00:00 2001 From: "Pey Lian Lim (Github)" <2090236+pllim@users.noreply.github.com> Date: Wed, 9 Nov 2022 13:38:08 -0500 Subject: [PATCH] Fix GUI update and dithered use case --- .../default/plugins/subset_plugin/subset_plugin.py | 13 ++++++++++++- jdaviz/configs/imviz/plugins/viewers.py | 13 ++++++++++--- .../concepts/imviz_edit_subset_programmatic.ipynb | 9 +++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/jdaviz/configs/default/plugins/subset_plugin/subset_plugin.py b/jdaviz/configs/default/plugins/subset_plugin/subset_plugin.py index e042ddff5c..454675f38d 100644 --- a/jdaviz/configs/default/plugins/subset_plugin/subset_plugin.py +++ b/jdaviz/configs/default/plugins/subset_plugin/subset_plugin.py @@ -241,7 +241,13 @@ def vue_recenter_subset(self, *args): comp_data = comp.data phot_aperstats = ApertureStats(comp_data, aperture) - self.set_center(phot_aperstats.centroid, update=False) + # Centroid was calculated in selected data, which might or might not be + # the reference data. However, Subset is always defined w.r.t. + # the reference data, so we need to convert back. + viewer = self.app._jdaviz_helper.default_viewer + x, y, _ = viewer._get_real_xy( + data, phot_aperstats.xcentroid, phot_aperstats.ycentroid, reverse=True) + self.set_center((x, y), update=False) def get_center(self): # Composite region cannot be edited. @@ -304,6 +310,11 @@ def set_center(self, new_cen, update=False): if update: self.vue_update_subset() + else: + # Force UI to update on browser without changing the subset. + tmp = self.subset_definitions + self.subset_definitions = [] + self.subset_definitions = tmp # List of JSON-like dict is nice for front-end but a pain to look up, # so we use these helper functions. diff --git a/jdaviz/configs/imviz/plugins/viewers.py b/jdaviz/configs/imviz/plugins/viewers.py index 96ab2a8d44..caaca890de 100644 --- a/jdaviz/configs/imviz/plugins/viewers.py +++ b/jdaviz/configs/imviz/plugins/viewers.py @@ -220,12 +220,15 @@ def on_limits_change(self, *args): return self.set_compass(self.state.layers[i].layer) - def _get_real_xy(self, image, x, y): + def _get_real_xy(self, image, x, y, reverse=False): """Return real (X, Y) position and status in case of dithering. ``coords_status`` is for ``self.label_mouseover`` coords handling only. When `True`, it sets the coords, otherwise it resets. + ``reverse=True`` is only for internal roundtripping (e.g., centroiding + in Subset Tools plugin). Never use this for coordinates display panel. + """ if data_has_valid_wcs(image): # Convert these to a SkyCoord via WCS - note that for other datasets @@ -235,8 +238,12 @@ def _get_real_xy(self, image, x, y): # Convert X,Y from reference data to the one we are actually seeing. # world_to_pixel return scalar ndarray that we need to convert to float. if self.get_link_type(image.label) == 'wcs': - x, y = list(map(float, image.coords.world_to_pixel( - self.state.reference_data.coords.pixel_to_world(x, y)))) + if not reverse: + x, y = list(map(float, image.coords.world_to_pixel( + self.state.reference_data.coords.pixel_to_world(x, y)))) + else: + x, y = list(map(float, self.state.reference_data.coords.world_to_pixel( + image.coords.pixel_to_world(x, y)))) coords_status = True except Exception: coords_status = False diff --git a/notebooks/concepts/imviz_edit_subset_programmatic.ipynb b/notebooks/concepts/imviz_edit_subset_programmatic.ipynb index 7bbe607597..f968529226 100644 --- a/notebooks/concepts/imviz_edit_subset_programmatic.ipynb +++ b/notebooks/concepts/imviz_edit_subset_programmatic.ipynb @@ -1,5 +1,13 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "e23ea362", + "metadata": {}, + "source": [ + "This concept notebook shows how you can move spatial subsets around in Imviz programmatically." + ] + }, { "cell_type": "code", "execution_count": null, @@ -111,6 +119,7 @@ " cur_x, cur_y = plg.get_center()\n", " plg.set_center((cur_x + 5, cur_y), update=True)\n", "\n", + " # We slow it down so you can see, otherwise this will complete too fast.\n", " time.sleep(0.5)\n", " \n", " plg.subset_selected = 'Subset 2'\n",