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

ENH: Cubeviz metadata viewer #1325

Merged
merged 7 commits into from
May 19, 2022
Merged

Conversation

pllim
Copy link
Contributor

@pllim pllim commented May 17, 2022

Description

This pull request is to parse metadata for Cubeviz and show it in Metadata Viewer plugin.

Screenshot 2022-05-17 121952

When there is primary header:

Fixes #1324

TODO

  • Decide what to do with Primary header in multi-extension FITS.
  • Add some metadata parsing checks to parser tests.

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?
  • 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 change log needed? If yes, is it added to CHANGES.rst?
  • Is a milestone set?
  • After merge, any internal documentations need updating (e.g., JIRA, Innerspace)?

@pllim pllim added this to the 2.6 milestone May 17, 2022
@codecov
Copy link

codecov bot commented May 17, 2022

Codecov Report

Merging #1325 (07270be) into main (806c94d) will increase coverage by 1.02%.
The diff coverage is 85.60%.

@@            Coverage Diff             @@
##             main    #1325      +/-   ##
==========================================
+ Coverage   83.75%   84.77%   +1.02%     
==========================================
  Files          91       91              
  Lines        7761     7836      +75     
==========================================
+ Hits         6500     6643     +143     
+ Misses       1261     1193      -68     
Impacted Files Coverage Δ
jdaviz/configs/cubeviz/plugins/parsers.py 56.29% <55.00%> (-1.57%) ⬇️
jdaviz/configs/mosviz/plugins/parsers.py 90.42% <82.35%> (-0.57%) ⬇️
...default/plugins/metadata_viewer/metadata_viewer.py 93.40% <88.67%> (-0.83%) ⬇️
...imviz/plugins/aper_phot_simple/aper_phot_simple.py 91.55% <100.00%> (+0.08%) ⬆️
jdaviz/configs/imviz/plugins/parsers.py 100.00% <100.00%> (ø)
jdaviz/configs/specviz/plugins/parsers.py 89.02% <100.00%> (+0.41%) ⬆️
jdaviz/configs/specviz2d/plugins/parsers.py 87.09% <100.00%> (+50.05%) ⬆️
jdaviz/utils.py 92.10% <100.00%> (+1.78%) ⬆️
...configs/default/plugins/data_tools/file_chooser.py 69.14% <0.00%> (+3.42%) ⬆️
...s/default/plugins/model_fitting/fitting_backend.py 93.02% <0.00%> (+15.11%) ⬆️
... and 2 more

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 806c94d...07270be. Read the comment docs.

for badkey in ('COMMENT', 'HISTORY', ''):
# Some FITS keywords cause "# ipykernel cannot clean for JSON" messages.
# Also, we want to hide internal metadata that starts with underscore.
badkeys = ['COMMENT', 'HISTORY', ''] + [k for k in d if k.startswith('_')]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kecnry , I think we talked about automatically hiding "hidden" metadata? This would do it for you.

'S_REGION' not in data.meta):
data.meta['S_REGION'] = data.meta['header']['S_REGION']
# Make metadata layout conform with other viz.
if 'header' in data.meta:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With this change, we should no longer worry about meta[somekey] vs meta['header'][somekey].

It seems like the meta['header'] stuff was imposed by specutils, so in the future if we add new parsing logic that loads stuff from specutils, we will need to remember to do this along with the new code. A little messy but seems to work. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Awesome! With this change, the WCS logic for detecting data-type should also probably be updated (either in #1313 if this is merged first, or here if #1313 is merged first).

d.meta.update(meta)
data_list.append(d)
# We do not use the generated labels
data_list = [d for d, _ in get_image_data_iterator(app, hdulist, "Image", ext=None)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Metadata stuff is already taken care of in get_image_data_iterator (Imviz parsing logic).

@@ -24,7 +28,7 @@ def test_load_spectrum1d(mosviz_helper, spectrum1d):

assert isinstance(data[label], Spectrum1D)

with pytest.raises(TypeError):
with pytest.raises(AttributeError):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exception class changed because it now crashes in metadata parsing. I don't think it matters?

@pllim pllim marked this pull request as ready for review May 18, 2022 01:22
@pllim pllim requested a review from havok2063 May 18, 2022 01:22
Copy link
Collaborator

@havok2063 havok2063 left a comment

Choose a reason for hiding this comment

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

I played around with this a bit. I think the toggle is fine. I would suggest two changes.

  1. That the UI remembers the toggled state as users select new extensions from the data dropdown. I see this more as a global toggle rather than a per extension toggle. I'd like to toggle it once for everything and leave it until I toggle it back.
  2. Add in any header comments for each key name, value. Either as a third column or as a hover tooltip on the key name or the row.

@pllim

This comment was marked as resolved.

@havok2063
Copy link
Collaborator

Fair points.

That complicates the logic a little, as not all data would have primary header but I'll see what I can do.

Ahh yeah. I guess in those cases you could fall back to the existing header and de-toggle with a notice, similar to what happens now.

This is a tough one. I don't think astropy.io.fits or Glue can preserve it natively because the metadata becomes key/value paired dictionary. It is not preserved by specutils either when it parses spectra from file. So, I am not sure if this is a realistic request for this PR.

You could time box it and see how hard it is. I think it'd be useful for users to have some sort of description of what the header keys are. I imagine at some point before the data get parsed and passed into the machinery hole, the main HDUList is accessible to extract out the Header cards. But yeah, if it's not easy to pull that out, then it can be out of scope for this PR.

@pllim pllim force-pushed the cubeviz-metadata branch from b557f49 to 17f542c Compare May 18, 2022 21:04
@pllim
Copy link
Contributor Author

pllim commented May 18, 2022

Well, this is more code than I thought I would write for this feature but I think I have addressed your feedback, @havok2063 .

Copy link
Contributor

@javerbukh javerbukh left a comment

Choose a reason for hiding this comment

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

Tested with all the configurations and works great, nice work!

Copy link
Member

@kecnry kecnry left a comment

Choose a reason for hiding this comment

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

Never expected extending the metadata viewer to cubeviz would get this... messy... but its definitely nice to have it across all viztools now. Just a few minor thoughts that you can take or leave, but otherwise I'm happy with merging this and we can iterate on any tweaks as people use it for real. Thanks!

Comment on lines +208 to +211
metadata = standardize_metadata(hdulist[ext].header)
metadata.update(wcs_dict) # To be internally consistent
if hdulist[ext].name != 'PRIMARY' and 'PRIMARY' in hdulist:
metadata[PRIHDR_KEY] = standardize_metadata(hdulist['PRIMARY'].header)
Copy link
Member

Choose a reason for hiding this comment

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

this seems to be a re-used block of code in a few places... could this logic be moved entirely into standardize_metadata or no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, as passing in hdulist into standardize_metadata would make it too specific to FITS, which should stay in the realm of parsers.

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 mean standardize_metadata already checks for fits.Header but you could embed fits.Header in a plain dict, which is what specutils does, but one would/should never put the whole HDUList inside metadata.

Comment on lines +17 to +24
<v-row v-if="has_primary">
<v-switch
label="Show primary header"
hint="Show MEF primary header metadata instead."
v-model="show_primary"
persistent-hint>
</v-switch>
</v-row>
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this is the most-intuitive.... but that can be covered in docs or iterated based on user-feedback (I don't really have any suggestions except a full extension-dropdown, but that might not be any more clear).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In Ginga, we have a checkbox toggle that would mix in primary header keys into the display, together with actual header. Not that that is not messy but no one has complained over there... 🤷

Comment on lines +16 to +18

This plugin allows viewing of any metadata associated with the selected data.

Copy link
Member

Choose a reason for hiding this comment

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

maybe worth a mention explaining the primary switch here?

@havok2063
Copy link
Collaborator

This is looking good. My only comments are about the styling. When the plugin tray size is small, everything gets squished together and word wraps. This improves when the plugin tray is expanded out, but that takes up real estate. Could we tweak the styles here to try to mitigate some of this? Maybe reducing the font size of the values and comments? Or making the header key bold? Something to try to reduce the amount of whitespace/padding and to visually distinguish a header key with the comment underneath when the plugin tray is small. Jenn might have some thoughts for this.

@pllim
Copy link
Contributor Author

pllim commented May 19, 2022

I am really afraid this is going to drag on and then go stale, so I am going to merge and address doc and styling in a follow-up PR (see #1331). Thanks!

@pllim pllim merged commit 12d97b8 into spacetelescope:main May 19, 2022
@pllim pllim deleted the cubeviz-metadata branch May 19, 2022 15:17
kecnry added a commit to kecnry/jdaviz that referenced this pull request May 19, 2022
now that spacetelescope#1325 is merged and rebased

Co-authored-by: P. L. Lim <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cubeviz metadata viewer
4 participants