Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update Mosviz parser doc and fix NIRSpec auto-load #2146

Merged
merged 11 commits into from
Apr 19, 2023

Conversation

pllim
Copy link
Contributor

@pllim pllim commented Apr 11, 2023

Description

This pull request is to update Mosviz doc on importing data, and also enable NIRSpec auto loader to take a single image.

And then the scope expanded and now it also fixes #2144

Change log entry

  • Is a change log needed? If yes, is it added to CHANGES.rst? If you want to avoid merge conflicts,
    list the proposed change log here for review and add to CHANGES.rst before merge. If no, maintainer
    should add a no-changelog-entry-needed label.

Checklist for package maintainer(s)

This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.

  • Are two approvals required? Branch protection rule does not check for the second approval. If a second approval is not necessary, please apply the trivial label.
  • Do the proposed changes actually accomplish desired goals? Also manually run the affected example notebooks, if necessary.
  • Do the proposed changes follow the STScI Style Guides?
  • Are tests added/updated as required? If so, do they follow the STScI Style Guides? Not sure how to add tests that don't require downloading a bunch of remote data files. Can we not?
  • Are docs added/updated as required? If so, do they follow the STScI Style Guides?
  • Did the CI pass? If not, are the failures related?
  • Is a milestone set? Set this to bugfix milestone if this is a bug fix and needs to be released ASAP; otherwise, set this to the next major release milestone.
  • After merge, any internal documentations need updating (e.g., JIRA, Innerspace)? 🐱 🐱 🐱

@pllim pllim added the documentation Explanation of code and concepts label Apr 11, 2023
@pllim pllim added this to the 3.5 milestone Apr 11, 2023
@codecov
Copy link

codecov bot commented Apr 12, 2023

Codecov Report

Patch coverage: 80.00% and project coverage change: -0.11 ⚠️

Comparison is base (10adfb6) 91.61% compared to head (0362037) 91.50%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2146      +/-   ##
==========================================
- Coverage   91.61%   91.50%   -0.11%     
==========================================
  Files         147      147              
  Lines       16098    16142      +44     
==========================================
+ Hits        14748    14771      +23     
- Misses       1350     1371      +21     
Impacted Files Coverage Δ
jdaviz/configs/mosviz/tests/test_parsers.py 99.09% <ø> (-0.04%) ⬇️
jdaviz/configs/mosviz/plugins/viewers.py 87.58% <52.94%> (-1.31%) ⬇️
jdaviz/configs/mosviz/plugins/parsers.py 89.27% <73.33%> (-0.56%) ⬇️
jdaviz/configs/mosviz/helper.py 85.11% <82.75%> (-2.25%) ⬇️
.../configs/cubeviz/plugins/slice/tests/test_slice.py 100.00% <100.00%> (ø)
jdaviz/configs/mosviz/tests/test_data_loading.py 100.00% <100.00%> (ø)
jdaviz/configs/mosviz/tests/test_helper.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@pllim pllim marked this pull request as ready for review April 12, 2023 01:04
@camipacifici
Copy link
Contributor

Thank you very much. This fixes my original problem.
I see three additional problems here:

  • the slit overlay is likely wrong (and this is probably not a jdaviz problem...lets just ignore this for now)
  • the table is returning Unspecified for all the sources under the RA and Dec columns
  • the position where the image viewer is zooming to is different from the position given by the SRCRA and SRCDEC keywords. I am not 100% positive that these keywords are the right ones, but maybe this can be double checked with the NIRISS parser that I am sure is correct.

@camipacifici
Copy link
Contributor

Would be nice to fix the image loader for this one too:
#2144

@pllim pllim force-pushed the mosviz-loading-doc branch from 17b9b5e to c3f00ab Compare April 12, 2023 20:54
@pllim
Copy link
Contributor Author

pllim commented Apr 12, 2023

the table is returning Unspecified for all the sources under the RA and Dec columns

For some reason, it is trying to look for RA and Dec on the image, not spectra. Not sure why. I added a fix.

the position where the image viewer is zooming to is different from the position given by the SRCRA and SRCDEC keywords

Hard for me to test using the cutout off the 2GB image you provided. If this is still a problem, please be more specific on what the correct answer should be. In NIRISS, I see that the parser grab RA and Dec off a special ECSV catalog that NIRSpec does not have, so I cannot just use the NIRISS logic. And are you sure this is not the same problem as S_REGION? These are all populated by the pipeline.

Would be nice to fix the image loader

I also added a fix for this. Please test your use case in #2144 with this PR too.

"""
super().load_data(data_obj, parser_reference="mosviz-image-parser",
data_labels=data_labels, share_image=share_image)
self._add_redshift_column()
if add_redshift_column:
self._add_redshift_column()
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: Multiple calls to self._add_redshift_column() in load_data was what causing all the warnings Cami was seeing, so I have to disable this call in sub-calls by default to get rid of the warnings.

Copy link
Contributor

Choose a reason for hiding this comment

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

I confirm I do not see the warnings anymore.

@pllim pllim added bug Something isn't working feature Feature request labels Apr 12, 2023
@camipacifici
Copy link
Contributor

The table is nicely populated, but if I try to load the image I still get the same traceback that ends with
ValueError: The dimensions of component 2D Spectra are incompatible with the dimensions of this data: (50,) vs (1,)

This is the way I am calling it:
mosviz = Mosviz()
mosviz.load_data(spectra_1d="../data/mastDownload/jones_calx1d_files/single/jw02136001001_03137_00001_nrs1_x1d.fits", spectra_2d="../data/mastDownload/jones_s2d_level2/jw02136001001_03137_00001_nrs1_s2d.fits", images="/Users/cpacifici/Dropbox/SINCRONIZZARE/Documents/WORK/CANDELS/hlsp_candels_hst_wfc3_egs-tot-60mas_f160w_v1.0_drz.fits", instrument="nirspec")
mosviz.show()

@pllim pllim force-pushed the mosviz-loading-doc branch from c3f00ab to bfa09bb Compare April 13, 2023 15:20
@pllim
Copy link
Contributor Author

pllim commented Apr 13, 2023

There are just too many ways to do the same thing in Mosviz. Looks like you were using a different logic route. I have pushed a fix.

By the way, you don't have to specify instrument= unless you are passing in a directory=, otherwise it does nothing and just more typing for you.

@pllim pllim force-pushed the mosviz-loading-doc branch from bfa09bb to 334b743 Compare April 13, 2023 15:26
@@ -212,7 +209,7 @@ def test_load_single_image_multi_spec(mosviz_helper, mos_image, spectrum1d, mos_
label_mouseover._viewer_mouse_event(spec2d_viewer,
{'event': 'mousemove', 'domain': {'x': 10, 'y': 100}})
assert label_mouseover.as_text() == ('Pixel x=00010.0 y=00100.0 Value +8.12986e-01', '', '')
assert label_mouseover.icon == 'b'
assert label_mouseover.icon == 'c'
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: I changed the loading order in load_data to fix a bug, hence this change here.

@camipacifici
Copy link
Contributor

The way I was using now works, thank you!
Out of curiosity, what was the way that worked for you before this last fix?

Now, I see another problem. The image viewer is not zooming on SRCRA and SRCDEC (it is quite far off). Do you see the same on your side? I can demo with the big image if you wish. The same is valid for the slit overlay. It is off from the coordinates in S_REGION.

@pllim

This comment was marked as resolved.

@pllim
Copy link
Contributor Author

pllim commented Apr 13, 2023

what was the way that worked for you

I was loading using directory, having moved the image under some sub-dir that it looks for. I did not try to load all 3 components separately and when I did, I could reproduce your problem (and then fix it).

@camipacifici
Copy link
Contributor

Understood. Let me try to find a different image!

@pllim
Copy link
Contributor Author

pllim commented Apr 13, 2023

Hmm I just found the code that zoom on shared image. I forgot this is not Imviz, so maybe that is a real bug, lemme see.

@camipacifici
Copy link
Contributor

You are right!! My mistake, the image is there and the zoom is doing what it is supposed to, i.e. zooming using the number of pixels in the cross dispersion direction in the 2D image. That is apparently too small, but it not a jdaviz problem. Also the fact that the RA/Dec are not in the header is not a jdaviz problem.

For the record, I did not have to put the image under an images directory. I just included it in the folder, like for NIRISS, so docs are also correct.

Sorry for the false alarm! This looks good to me!

@pllim pllim force-pushed the mosviz-loading-doc branch from 80bbca5 to 07f8025 Compare April 17, 2023 19:33
@pllim
Copy link
Contributor Author

pllim commented Apr 17, 2023

Failures are unrelated.

@rosteen
Copy link
Collaborator

rosteen commented Apr 18, 2023

I get a test failure that appears related when I run them locally, from test_nirspec_parser[nirspec]:

>       assert len(mosviz_helper.app.data_collection) == 16
E       AssertionError: assert 11 == 16
E        +  where 11 = len(DataCollection (11 data sets)\n	  0: MOS Table\n	  1: 1D Spectrum 0\n	  2: 1D Spectrum 1\n	  3: 1D Spectrum 2\n	  4: 1D Spe...	  5: 1D Spectrum 4\n	  6: 2D Spectrum 0\n	  7: 2D Spectrum 1\n	  8: 2D Spectrum 2\n	  9: 2D Spectrum 3\n	 10: 2D Spectrum 4)
E        +    where DataCollection (11 data sets)\n	  0: MOS Table\n	  1: 1D Spectrum 0\n	  2: 1D Spectrum 1\n	  3: 1D Spectrum 2\n	  4: 1D Spe...	  5: 1D Spectrum 4\n	  6: 2D Spectrum 0\n	  7: 2D Spectrum 1\n	  8: 2D Spectrum 2\n	  9: 2D Spectrum 3\n	 10: 2D Spectrum 4 = Application(config='mosviz', events=['call_viewer_method', 'close_snackbar_message', 'data_item_remove', 'data_item_un...erflow hidden will prevent the scrollbar from\n   * appearing */\n  overflow: hidden;\n}\n</style>\n'), vdocs='latest').data_collection
E        +      where Application(config='mosviz', events=['call_viewer_method', 'close_snackbar_message', 'data_item_remove', 'data_item_un...erflow hidden will prevent the scrollbar from\n   * appearing */\n  overflow: hidden;\n}\n</style>\n'), vdocs='latest') = <jdaviz.configs.mosviz.helper.Mosviz object at 0x13f810550>.app

configs/mosviz/tests/test_parsers.py:38: AssertionError

I assume that you weren't seeing this locally, any idea what might be going wrong?

@pllim
Copy link
Contributor Author

pllim commented Apr 18, 2023

@rosteen , I seen that before. I think some data was not loaded properly. I thought I fixed that. Are you testing an older version of my branch? Did you try clearing the download cache?

@rosteen
Copy link
Collaborator

rosteen commented Apr 18, 2023

The test passed after I used astropy's clear_download_cache, good call. I'll try to finish reading through the code changes soon.

Copy link
Collaborator

@rosteen rosteen left a comment

Choose a reason for hiding this comment

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

I left a couple small questions and one small suggestion, but otherwise I think this looks good.

docs/mosviz/import_data.rst Outdated Show resolved Hide resolved
def set_plot_axes(self):
self.figure.axes[1].tick_format = None
self.figure.axes[0].tick_format = None
def _mark_targets(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where does this get used, anyway? This is the only instance of the string "_mark_targets" that I see in the repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed it to debug. I wanted to see where the RA and Dec from the table actually are. I feel like we could make it public (in one way or another) in the future but I don't want that headache right now.

It comes "for free" with the Astrowidgets inheritance, but if this bothers you, I can remove it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I see. I don't think it needs to be removed then, but it would be nice to have a comment saying that it's there to help with debugging and isn't actually called internally.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I could see this being useful eventually, why not 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added.

Comment on lines 51 to 53
from astropy.coordinates import SkyCoord
from astropy.table import QTable
Copy link
Collaborator

Choose a reason for hiding this comment

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

Depends on the answer to the question above probably, but maybe move these to the top level? I generally dislike imports in functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does #2146 (comment) help you making a decision on this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If it's only used in debugging it can stay.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The comment doesn't quite give me an answer as to why these imports should be down here. Is there a reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the imports are unnecessary otherwise. If we decide to delete this method one day, it gives a cleaner diff. If you really want the imports to be high up, I can do it too. Lemme know.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Understandable, but isn't this directly against PEP8? https://peps.python.org/pep-0008/#imports

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

Unless those particular imports take a particularly long time, I'd argue following PEP8. I'm personally comfortable with counting this as a value-add rather than a "small debugging thing that might be useful later"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imports moved up.

pllim and others added 8 commits April 18, 2023 16:29
Add change log.

Ignore non-FITS files in NIRSpec glob.

Grab source RA/Dec from 1D spec first.
remove unnecessary ignoring of warnings that are not there.
and disable image viewer axes
Co-authored-by: Ricky O'Steen <39831871+rosteen@users.noreply.github.com>
that it is only for debugging
@pllim pllim force-pushed the mosviz-loading-doc branch from 9500a3c to 50e2dd1 Compare April 18, 2023 20:31
Copy link
Collaborator

@duytnguyendtn duytnguyendtn left a comment

Choose a reason for hiding this comment

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

For whatever reason, I couldn't get to Cami's nirspec example, but the nircam example worked and zoomed well. Code looks good and much more logical. Thanks Pey-Lian! A few other comments

jdaviz/configs/mosviz/helper.py Show resolved Hide resolved
jdaviz/configs/mosviz/helper.py Outdated Show resolved Hide resolved
and rename xh variable
@pllim

This comment was marked as resolved.

pllim added 2 commits April 18, 2023 17:29
so we just blindly ignore, not caring if it shows up or not
@pllim
Copy link
Contributor Author

pllim commented Apr 18, 2023

@rosteen and @duytnguyendtn , is this okay to merge now? Thanks for the reviews!

@pllim pllim merged commit c0e2ed5 into spacetelescope:main Apr 19, 2023
@pllim pllim deleted the mosviz-loading-doc branch April 19, 2023 14:50
javerbukh pushed a commit to javerbukh/jdaviz that referenced this pull request May 4, 2023
* Update Mosviz parser doc and fix NIRSpec auto-load.

Add change log.

Ignore non-FITS files in NIRSpec glob.

Grab source RA/Dec from 1D spec first.

* Update Mosviz example notebook

* Update tests and
remove unnecessary ignoring of warnings that are not there.

* Fix single image loading for mix-and-match

Update change log

* Fix Mosviz image zoom to target
and disable image viewer axes

* BUG: images not returning Path objects

* Fix English typo

Co-authored-by: Ricky O'Steen <39831871+rosteen@users.noreply.github.com>

* Add a note about _mark_targets
that it is only for debugging

* Move local imports up
and rename xh variable

* TST: Warning gone from test_slice

* OMG now warning is back
so we just blindly ignore, not caring if it shows up or not

---------

Co-authored-by: Ricky O'Steen <39831871+rosteen@users.noreply.github.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working documentation Explanation of code and concepts feature Feature request mosviz
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mosviz: nirspec level 2 loader
4 participants