Skip to content
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

Imviz: astrowidget API for center_on and offset_to #687

Merged
merged 6 commits into from
Jun 25, 2021

Conversation

pllim
Copy link
Contributor

@pllim pllim commented Jun 16, 2021

Implement some of #631 and #632.

Out of scope: Unable to use this API on a tiled viewers; i.e., it only works on the first viewer that the app opened. See #643

Screenshot 2021-06-18 163551

@pllim pllim added this to the Imviz 1.0 milestone Jun 16, 2021
@pllim pllim requested a review from astrofrog June 16, 2021 21:59
@github-actions github-actions bot added the imviz label Jun 16, 2021
@pllim pllim mentioned this pull request Jun 16, 2021
14 tasks
@codecov
Copy link

codecov bot commented Jun 16, 2021

Codecov Report

Merging #687 (7039327) into main (ccabe72) will increase coverage by 0.66%.
The diff coverage is 81.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #687      +/-   ##
==========================================
+ Coverage   60.13%   60.80%   +0.66%     
==========================================
  Files          65       65              
  Lines        4099     4156      +57     
==========================================
+ Hits         2465     2527      +62     
+ Misses       1634     1629       -5     
Impacted Files Coverage Δ
jdaviz/configs/imviz/plugins/tools.py 37.62% <0.00%> (-1.00%) ⬇️
jdaviz/configs/imviz/plugins/viewers.py 40.74% <0.00%> (+13.10%) ⬆️
jdaviz/configs/imviz/helper.py 95.55% <94.23%> (+0.81%) ⬆️
jdaviz/configs/imviz/plugins/parsers.py 97.80% <0.00%> (+0.54%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ccabe72...7039327. Read the comment docs.

jdaviz/configs/imviz/helper.py Outdated Show resolved Hide resolved
@pllim pllim force-pushed the astrowidgets-quack-center-offset branch from 30f1543 to e1e8906 Compare June 17, 2021 21:40
@@ -2,13 +2,15 @@
"cells": [
{
"cell_type": "markdown",
"id": "17b4af6f",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't me. Jupyter did this after I upgraded. 🤷

@pllim pllim force-pushed the astrowidgets-quack-center-offset branch from e1e8906 to a96032d Compare June 18, 2021 20:38
new_sky_cen = sky_cen.__class__(
SkyOffsetFrame(dx, dy, origin=sky_cen.frame).transform_to(sky_cen))
else:
new_sky_cen = sky_cen.spherical_offsets_by(dx, dy)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This was added in astropy/astropy#11635

"metadata": {},
"outputs": [],
"source": [
"from astropy.utils.data import download_file\n",
"\n",
"gc_2mass_j = download_file('https://www.astropy.org/astropy-data/galactic_center/gc_2mass_j.fits', cache=True)\n",
"gc_msx_e = download_file('https://www.astropy.org/astropy-data/galactic_center/gc_msx_e.fits', cache=True)"
"acs_47tuc_1 = download_file('https://mast.stsci.edu/api/v0.1/Download/file?uri=mast:HST/product/jbqf03gjq_flc.fits', cache=True)\n",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my sanity, I am replacing the example images with real HST/ACS data. Everyone loves 47 Tuc, right? Or is it just calibration team? 😉

"metadata": {},
"outputs": [],
"source": [
"viewer.state.layers[0].percentile = 99"
"viewer.state.layers[0].percentile = 95"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also for my sanity. Things are easier to see with 95th percentile.

@pllim pllim force-pushed the astrowidgets-quack-center-offset branch from a96032d to c910d1d Compare June 18, 2021 21:31
@pllim pllim marked this pull request as ready for review June 18, 2021 21:32

class TestAstrowidgetsAPI:
@pytest.fixture(autouse=True)
def setup_class(self, imviz_app):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This is basically the same test setup in #630 . When we get back to the other PR, will have to see how to consolidate this to avoid code duplication.

Copy link
Collaborator

@astrofrog astrofrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of comments but looks good otherwise!


if isinstance(point, SkyCoord):
i_top = get_top_layer_index(viewer)
image = viewer.layers[i_top].layer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should probably use image = viewer.reference_data as that is the one which is used to define the pixel space

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion does not seem to work: AttributeError: 'ImvizImageView' object has no attribute 'reference_data'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

viewer.state.reference_data exists but it causes my test to fail:

        # Blink to the one without WCS
        self.viewer.blink_once()
    
        with pytest.raises(AttributeError, match='does not have a valid WCS'):
>           self.imviz.center_on(sky)
E           Failed: DID NOT RAISE <class 'AttributeError'>

jdaviz/configs/imviz/tests/test_astrowidgets_api.py:83: Failed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears reference_data does not keep track of which layer is actually visible to the user after blinking.


if skycoord_offset:
i_top = get_top_layer_index(viewer)
image = viewer.layers[i_top].layer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

@rosteen
Copy link
Collaborator

rosteen commented Jun 25, 2021

Looks pretty good, the only thing I think might be good to add is some checking to see if the center_on input is within the bounds of the image. Inputting pixel coordinates beyond the bounds of the image centers there and just shows no data, which I guess is fine. Trying to center_on with sky coordinates that are outside the image, on the other hand, leads to a long stack trace:

---------------------------------------------------------------------------
NoConvergence                             Traceback (most recent call last)
<ipython-input-25-3978131487f0> in <module>
      3 
      4 sky = SkyCoord('01h24m07.33s -71d52m50.71s')
----> 5 imviz.center_on(sky)

~/projects/jdaviz/jdaviz/configs/imviz/helper.py in center_on(self, point)
    109             image = viewer.layers[i_top].layer
    110             if hasattr(image, 'coords') and isinstance(image.coords, BaseHighLevelWCS):
--> 111                 pix = image.coords.world_to_pixel(point)  # 0-indexed X, Y
    112             else:
    113                 raise AttributeError(f'{image.label} does not have a valid WCS')

~/opt/anaconda3/envs/viz_dev/lib/python3.9/site-packages/astropy/wcs/wcsapi/high_level_api.py in world_to_pixel(self, *world_objects)
    230 
    231         # Finally we convert to pixel coordinates
--> 232         pixel = self.low_level_wcs.world_to_pixel_values(*world)
    233 
    234         return pixel

~/opt/anaconda3/envs/viz_dev/lib/python3.9/site-packages/astropy/wcs/wcsapi/fitswcs.py in world_to_pixel_values(self, *world_arrays)
    315 
    316     def world_to_pixel_values(self, *world_arrays):
--> 317         pixel = self.all_world2pix(*world_arrays, 0)
    318         return pixel[0] if self.pixel_n_dim == 1 else tuple(pixel)
    319 

~/opt/anaconda3/envs/viz_dev/lib/python3.9/site-packages/astropy/wcs/wcs.py in all_world2pix(self, tolerance, maxiter, adaptive, detect_divergence, quiet, *args, **kwargs)
   1884             raise ValueError("No basic WCS settings were created.")
   1885 
-> 1886         return self._array_converter(
   1887             lambda *args, **kwargs:
   1888             self._all_world2pix(

~/opt/anaconda3/envs/viz_dev/lib/python3.9/site-packages/astropy/wcs/wcs.py in _array_converter(self, func, sky, ra_dec_order, *args)
   1334                     "a 1-D array for each axis, followed by an origin.")
   1335 
-> 1336             return _return_list_of_arrays(axes, origin)
   1337 
   1338         raise TypeError(

~/opt/anaconda3/envs/viz_dev/lib/python3.9/site-packages/astropy/wcs/wcs.py in _return_list_of_arrays(axes, origin)
   1288             if ra_dec_order and sky == 'input':
   1289                 xy = self._denormalize_sky(xy)
-> 1290             output = func(xy, origin)
   1291             if ra_dec_order and sky == 'output':
   1292                 output = self._normalize_sky(output)

~/opt/anaconda3/envs/viz_dev/lib/python3.9/site-packages/astropy/wcs/wcs.py in <lambda>(*args, **kwargs)
   1886         return self._array_converter(
   1887             lambda *args, **kwargs:
-> 1888             self._all_world2pix(
   1889                 *args, tolerance=tolerance, maxiter=maxiter,
   1890                 adaptive=adaptive, detect_divergence=detect_divergence,

~/opt/anaconda3/envs/viz_dev/lib/python3.9/site-packages/astropy/wcs/wcs.py in _all_world2pix(self, world, origin, tolerance, maxiter, adaptive, detect_divergence, quiet)
   1868                     slow_conv=ind, divergent=None)
   1869             else:
-> 1870                 raise NoConvergence(
   1871                     "'WCS.all_world2pix' failed to "
   1872                     "converge to the requested accuracy.\n"

NoConvergence: 'WCS.all_world2pix' failed to converge to the requested accuracy.
After 2 iterations, the solution is diverging at least for one input point.

Perhaps it would be useful to check whether the sky coordinates input are within the image bounds, and if not raise a more informative/concise error that you can't center on sky coordinates outside the image.

@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

@rosteen , I think @mcara said this error only happens if image is distorted...? Which image did you use? Thanks for smoking this out!

@rosteen
Copy link
Collaborator

rosteen commented Jun 25, 2021

That's with acs_47tuc_1 from the Imviz example notebook.

@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

Ah, yes. FLC is not corrected for distortion. 😬

@pllim pllim force-pushed the astrowidgets-quack-center-offset branch from c910d1d to 2650711 Compare June 25, 2021 16:14
@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

@rosteen , is this better?

Screenshot 2021-06-25 121305

@rosteen
Copy link
Collaborator

rosteen commented Jun 25, 2021

I like capturing it that way much better than the long stack trace in the notebook. Approving, since making this work with more viewers needs to happen after some other work.

@ojustino
Copy link
Contributor

Just did. No more error printouts, but the viewer still whites out and trying to recenter on an in-image location doesn't have a visible effect.

@pllim pllim force-pushed the astrowidgets-quack-center-offset branch from d077314 to 7039327 Compare June 25, 2021 20:29
@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

Added one more check. Is this a satisfactory solution to the error you encounter, @ojustino ?

Screenshot 2021-06-25 162821

@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

The CI failure smells like upstream problem in Actions:

  Error: fatal: unable to access 'https://github.com/spacetelescope/jdaviz/': The requested URL returned error: 500
  The process '/usr/bin/git' failed with exit code 128

@ojustino
Copy link
Contributor

@pllim With this commit, center_on triggers the new dropdown message and blocks execution if I try SkyCoords or pixels that are off-screen but do exist "on-image."

It is not blocking SkyCoords that are off-screen and off-image, and that case still causes the white out and does not trigger the dropdown message.

@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

@ojustino , at this point... please point me to that image you are using, and provide me with the calls that (1) block execution, and (2) white out.

Thanks for the very thorough checks!

Copy link
Contributor

@ibusko ibusko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to run the demo notebook and exercise all cells that have the centering and offseting operations. The image seemed to behave in the way we expect.

@ojustino
Copy link
Contributor

Perhaps the problems are with my environment, then? In any case, these are my images:

cygA1 = download_file('https://hla.stsci.edu/cgi-bin/getdata.cgi?dataset=hst_05368_01_wfpc2_f555w_pc_drz.fits', cache=True)
cygA2 = download_file('https://hla.stsci.edu/cgi-bin/getdata.cgi?dataset=hst_05368_01_wfpc2_f555w_wf_drz.fits', cache=True)

This is the offending code:

from astropy.coordinates import SkyCoord
sky = SkyCoord('09h59m26.02s 40d44m17.92s')

I also tried the notebook's original 47 Tuc images and get the same unintended blocking behavior in the "off-screen, on-image" case, though the "off-screen, off-image" case is blocked as intended. I should also note that anything "on-screen and on-image" has been working fine.

@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

What does "off-screen, on-image" mean?

@ojustino
Copy link
Contributor

A set of coordinates that has data in the image but is not currently visible in the viewer. An example would be if I'm zoomed in at one corner and want to center on a location on the opposite side of the image.

@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

A set of coordinates that has data in the image but is not currently visible in the viewer.

Oh... In this case, perhaps changing x/y_min/max of the layer does not actually load the data that is currently not displayed. If you know how to force data loading, please let me know. Otherwise, here is my proposal:

  1. Let me know if 7039327 needs to be reverted or not.
  2. Once (1) is resolved, we merge and open new issue about the "off-screen, on-image" use case. This way, we can wait for SME, while I can also continue onto Imviz: astrowidgets markers API #699 while we wait till they become available.

@ojustino
Copy link
Contributor

That's an answer I don't know.

  1. I think 7039327 is a good idea for avoiding the whiteout scenario, so I'm fine with a merge, especially since other developers didn't encounter any issues.
  2. I'm fine with migrating this problem into a new issue.

@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

@astrofrog , if you feel strongly about using i_top to grab the top visible layer, we can handle it in a follow-up PR. Thanks!

@pllim pllim dismissed astrofrog’s stale review June 25, 2021 21:19

Follow up next time

@pllim pllim merged commit e45e503 into spacetelescope:main Jun 25, 2021
@pllim pllim deleted the astrowidgets-quack-center-offset branch June 25, 2021 21:19
@pllim
Copy link
Contributor Author

pllim commented Jun 25, 2021

Thank you for the very thorough reviews, everyone!

@ojustino , I opened #704 as a follow-up issue. Please correct me if I got anything wrong there. Thanks again!

@eteq
Copy link
Contributor

eteq commented Jun 30, 2021

Post-hoc review: this looks great, except for one thing: I think it should be offset_by isntead of offset_to! Will create a follow-on issue to capture that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants