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

JP-3482: Load units from FITS default header keywords directly #238

Closed
wants to merge 4 commits into from

Conversation

tapastro
Copy link
Collaborator

@tapastro tapastro commented Dec 4, 2023

Resolves JP-3482

Closes [spacetelescope/jwst#8109]

This PR addresses a lack of specified units in the spec/spectable jwst schemas, resulting in metadata not propagating through a save/load cycle. Additionally, loading a spectrum into a datamodel will not have any unit strings accessible. This feels like hacky workaround, but does generate unit strings in the spec_table. Looking forward to expert review 🙂

Checklist

  • added entry in CHANGES.rst (either in Bug Fixes or Changes to API)
  • updated relevant tests
  • updated relevant documentation
  • updated relevant milestone(s)
  • added relevant label(s)

@tapastro tapastro added bug Something isn't working jwst datamodels labels Dec 4, 2023
@tapastro tapastro added this to the 1.8.5 milestone Dec 4, 2023
@tapastro tapastro requested a review from a team as a code owner December 4, 2023 20:56
Copy link

codecov bot commented Dec 4, 2023

Codecov Report

Attention: 11 lines in your changes are missing coverage. Please review.

Comparison is base (12a4c3a) 64.71% compared to head (e5dfeb8) 64.62%.

Files Patch % Lines
src/stdatamodels/jwst/datamodels/multispec.py 35.29% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #238      +/-   ##
==========================================
- Coverage   64.71%   64.62%   -0.09%     
==========================================
  Files         102      102              
  Lines        5668     5685      +17     
==========================================
+ Hits         3668     3674       +6     
- Misses       2000     2011      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -57,3 +61,17 @@ def __init__(self, init=None, **kwargs):
return

super(MultiSpecModel, self).__init__(init=init, **kwargs)

try:
if isinstance(init, fits.hdu.hdulist.HDUList):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I like the idea of hardwiring FITS-isms into our datamodels. Kind of breaks the paradigm of making them disk format independent. What happens if someone in the future ever wants to store a MultiSpecModel in an ASDF file, instead of FITS?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally this would/should be handled at a lower level somewhere down in the actual datamodels code and/or its interface to fitsio.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree in principle, and hopefully @braingram has a better idea of how to accomplish this. But I don't see a method defined that connects a schema-defined table with unit designations, and I'm unsure of how to even start that. Even just saving an existing MultiSpecModel from fits to asdf, then loading the asdf gives different data structure types for the table - the asdf file returns <class 'asdf.tags.core.ndarray.NDArrayType'> with no column attributes, while the fits version returns <class 'astropy.io.fits.fitsrec.FITS_rec'>. I don't know enough about the asdf ndarray to understand if it allows units, but the FITS_rec does allow this, with the downside that it stores that data in the FITS keywords...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FWIW, @nden pointed me to the unit schemas in asdf that may or may not be used in Roman datamodels, but I don't know if/how they're being implemented for tables.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for putting this together. I should hopefully have a chance to look into this PR tomorrow in more depth. One question I have is: does make sense to put the units in the schema? There are a few other schemas that have units (here's one example). I am not sure if or how the units in the schema are used. It might be possible to have stdatamodels use the units in the schema during write to define the TUINT headers and on read to fill in the units from the schema (perhaps instead of or only if there are no TUNIT headers).

Copy link
Collaborator Author

@tapastro tapastro Dec 5, 2023

Choose a reason for hiding this comment

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

The units here are variable, which makes me think that example would be hard to mimic - I tried to pull the unit schema from asdf into the schema directory here and enter something like:

- name: WAVELENGTH
  datatype: float64
  unit:
    - ref: unit.schema

in the spectable.schema.yaml file, but from what I can tell, datamodels does not have any method of storing/validating attributes of a table/array beyond name and datatype.

And no rush, I know you're out of the office today! Just leaving a tag in your log to look into tomorrow - thanks for taking a look!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! I wasn't aware these were variable (and agree this makes adding them to the schema not an option).

It appears that the _cast that occurs during reading a datamodel from a fits file loses the units. I think the changes here should fix this specific issue: master...braingram:stdatamodels:fitsrec_units

I opened an issue to try to organize and document the current state of unit support and hopefully come up with a plan: #240

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should I close this PR and have a new one for your fixes, or should I just commit those into this PR and remove my edits?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It may be easiest if I open a new one. I sent the link to @jemorrison and if the new PR doesn't fix the issue (but this one does) then I'll close the new PR.

@braingram braingram mentioned this pull request Dec 6, 2023
5 tasks
@hbushouse
Copy link
Collaborator

Is this being superseded by #243 and hence can be closed?

@braingram
Copy link
Collaborator

I believe the open PR #242 supersedes this (I'm not sure if the test PR #243 fixes the issue here and is more an example of one way to define units to dicuss in issue #240). @tapastro does #242 look good to you?

@braingram
Copy link
Collaborator

Not enough coffee yet this morning... I clicked merge on #242 and then realized I hadn't yet heard back on my last comment. @tapastro does #242 solve the issue and if so are you ok with closing this PR?

@tapastro
Copy link
Collaborator Author

This has definitely been superseded and can be closed!

@tapastro tapastro closed this Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working jwst datamodels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants