Skip to content

Commit

Permalink
internalize bug fix and add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen committed Apr 16, 2024
1 parent 1a97304 commit 00f0a17
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
19 changes: 11 additions & 8 deletions jdaviz/configs/imviz/plugins/footprints/footprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def _on_link_type_updated(self, msg=None):
# can be True, but only last fp will display. This ensures all fp(s) display
if not self.is_pixel_linked:
for choice in self.overlay.choices:
self._preset_args_changed(overlay_selected=choice)
if len(self.overlay.choices) > 1:
self._change_overlay(overlay_selected=choice)

def vue_link_by_wcs(self, *args):
# call other plugin so that other options (wcs_use_affine, wcs_use_fallback)
Expand Down Expand Up @@ -320,37 +321,39 @@ def vue_center_on_viewer(self, viewer_ref):
self.center_on_viewer(viewer_ref)

@observe('overlay_selected')
def _change_overlay(self, *args):
def _change_overlay(self, *args, overlay_selected=None):
if not hasattr(self, 'overlay'): # pragma: nocover
# plugin/traitlet startup
return
if self.overlay_selected == '':
# no overlay selected (this can happen when removing all overlays)
return

if self.overlay_selected not in self._overlays:
overlay_selected = overlay_selected if overlay_selected is not None else self.overlay_selected # noqa

if overlay_selected not in self._overlays:
# create new entry with defaults (any defaults not provided here will be carried over
# from the previous selection based on current traitlet values)

# if this is the first overlay, there is a chance the traitlet for color was already set
# to something other than the default, so we want to use that, otherwise for successive
# new overlays, we want to ignore the traitlet and default back to "active" orange.
default_color = '#c75109' if len(self._overlays) else self.color
self._overlays[self.overlay_selected] = {'color': default_color}
self._overlays[overlay_selected] = {'color': default_color}

# similarly, if the user called any import APIs before opening the plugin, we want to
# respect that, but when creating successive overlays, any selection from file/region
# should be cleared for the next selection
if self.preset_selected == 'From File...' and len(self._overlays) > 1:
self._overlays[self.overlay_selected]['from_file'] = ''
self._overlays[self.overlay_selected]['preset'] = self.preset.choices[0]
self._overlays[overlay_selected]['from_file'] = ''
self._overlays[overlay_selected]['preset'] = self.preset.choices[0]

# for the first overlay only, default the position to be centered on the current
# zoom limits of the first selected viewer
if len(self._overlays) == 1 and len(self.viewer.selected):
self.center_on_viewer(self.viewer.selected[0])

fp = self._overlays[self.overlay_selected]
fp = self._overlays[overlay_selected]

# we'll temporarily disable updating the overlays so that we can set all
# traitlets simultaneously (and since we're only updating traitlets to a previously-set
Expand All @@ -371,7 +374,7 @@ def _change_overlay(self, *args):
# dict above.
fp[key] = getattr(self, attr)
self._ignore_traitlet_change = False
self._preset_args_changed()
self._preset_args_changed(overlay_selected=overlay_selected)

def _mark_visible(self, viewer_id, overlay=None):
if not self.is_active:
Expand Down
42 changes: 42 additions & 0 deletions jdaviz/configs/imviz/tests/test_footprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from jdaviz.core.marks import FootprintOverlay
from jdaviz.configs.imviz.plugins.footprints.preset_regions import _all_apertures
from astropy.wcs import WCS

pytest.importorskip("pysiaf")

Expand Down Expand Up @@ -142,3 +143,44 @@ def test_user_api(imviz_helper, image_2d_wcs, tmp_path):
assert plugin._obj.is_active is False
viewer_marks = _get_markers_from_viewer(imviz_helper.default_viewer)
assert viewer_marks[0].visible is False


def test_api_after_linking(imviz_helper):
# custom image to enable visual test in notebook
arr = np.ones(300, 300)
image_2d_wcs = WCS({'CTYPE1': 'RA---TAN', 'CUNIT1': 'deg', 'CDELT1': -0.0002777777778,
'CRPIX1': 1, 'CRVAL1': 337.5202808,
'CTYPE2': 'DEC--TAN', 'CUNIT2': 'deg', 'CDELT2': 0.0002777777778,
'CRPIX2': 1, 'CRVAL2': -20.833333059999998})

viewer = imviz_helper.app.get_viewer_by_id('imviz-0')

ndd = NDData(arr, wcs=image_2d_wcs)
imviz_helper.load_data(ndd)
imviz_helper.load_data(ndd)

plugin = imviz_helper.plugins['Footprints']
with plugin.as_active():

pointing = {'name': ['pt'], 'ra': [337.51], 'dec': [-20.77], 'pa': [0]}
plugin.color = 'green'
plugin.add_overlay(pointing['name'][0])
plugin.ra = pointing['ra'][0]
plugin.dec = pointing['dec'][0]
plugin.pa = pointing['pa'][0]

# when pixel linked are any marks displayed
viewer_marks = _get_markers_from_viewer(viewer)
no_marks_displayed = all(not mark.visible for mark in viewer_marks)
assert no_marks_displayed is True

# link by wcs and retest
imviz_helper.link_data(link_type='wcs')

viewer_marks = _get_markers_from_viewer(viewer)
# distinguish default from custom overlay with color
marks_displayed = any(mark.visible and mark.colors[0] == 'green'
for mark in viewer_marks)

# when wcs linked are any marks displayed
assert marks_displayed is True

0 comments on commit 00f0a17

Please sign in to comment.