Skip to content

Commit

Permalink
Merge pull request #1447 from pllim/unbreak-my-subset
Browse files Browse the repository at this point in the history
BUG: Simple Aperture Photometry plugin to update when Subset updates
  • Loading branch information
kecnry authored Jul 6, 2022
2 parents 820e9a7 + 6b8e0b9 commit 92a7c47
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ Imviz
- Fixed a bug where some custom colormap added to Imviz is inaccessible
via ``viewer.set_colormap()`` API. [#1440]

- Fixed a bug where Simple Aperture Photometry plugin does not know
an existing Subset has been modified until it is reselected from
the dropdown menu. [#1447]

Mosviz
^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from astropy.modeling.models import Gaussian1D
from astropy.table import QTable
from astropy.time import Time
from glue.core.message import SubsetUpdateMessage
from ipywidgets import widget_serialization
from photutils.aperture import (ApertureStats, CircularAperture, EllipticalAperture,
RectangularAperture)
Expand Down Expand Up @@ -72,6 +73,8 @@ def __init__(self, *args, **kwargs):
self.current_plot_type = self.plot_types[0]
self._fitted_model_name = 'phot_radial_profile'

self.session.hub.subscribe(self, SubsetUpdateMessage, handler=self._on_subset_update)

def reset_results(self):
self.result_available = False
self.results = []
Expand Down Expand Up @@ -131,7 +134,6 @@ def _dataset_selected_changed(self, event={}):
# Update self._selected_subset with the new self._selected_data
# and auto-populate background, if applicable.
self._subset_selected_changed()
self._bg_subset_selected_changed()

def _get_region_from_subset(self, subset):
for subset_grp in self.app.data_collection.subset_groups:
Expand All @@ -144,6 +146,16 @@ def _get_region_from_subset(self, subset):
else:
raise ValueError(f'Subset "{subset}" not found')

def _on_subset_update(self, msg):
if self.dataset_selected == '' or self.subset_selected == '':
return

sbst = msg.subset
if sbst.label == self.subset_selected and sbst.data.label == self.dataset_selected:
self._subset_selected_changed()
elif sbst.label == self.bg_subset_selected and sbst.data.label == self.dataset_selected:
self._bg_subset_selected_changed()

@observe('subset_selected')
def _subset_selected_changed(self, event={}):
subset_selected = event.get('new', self.subset_selected)
Expand Down Expand Up @@ -172,6 +184,9 @@ def _subset_selected_changed(self, event={}):
self.hub.broadcast(SnackbarMessage(
f"Failed to extract {subset_selected}: {repr(e)}", color='error', sender=self))

else:
self._bg_subset_selected_changed()

def _calc_bg_subset_median(self, reg):
# Basically same way image stats are calculated in vue_do_aper_phot()
# except here we only care about one stat for the background.
Expand Down
8 changes: 8 additions & 0 deletions jdaviz/configs/imviz/tests/test_simple_aper_phot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
from astropy import units as u
from astropy.tests.helper import assert_quantity_allclose
from glue.core.edit_subset_mode import ReplaceMode
from numpy.testing import assert_allclose, assert_array_equal
from photutils.aperture import (ApertureStats, CircularAperture, EllipticalAperture,
RectangularAperture, EllipticalAnnulus)
Expand Down Expand Up @@ -249,6 +250,13 @@ def test_annulus_background(imviz_helper):
phot_plugin.bg_annulus_width = 5
assert_allclose(phot_plugin.background_value, 4.894003242594493)

# Move the last created Subset (ellipse) and make sure background updates
imviz_helper.app.session.edit_subset_mode.mode = ReplaceMode
imviz_helper._apply_interactive_region('bqplot:ellipse', (0, 30), (51, 55))
assert_allclose(phot_plugin.bg_annulus_inner_r, 40)
assert_allclose(phot_plugin.bg_annulus_width, 5)
assert_allclose(phot_plugin.background_value, 4.894003)

# Bad annulus should not crash plugin
phot_plugin.bg_annulus_inner_r = -1
assert_allclose(phot_plugin.background_value, 0)
Expand Down

0 comments on commit 92a7c47

Please sign in to comment.