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

Add miri lrs spec wcs model #393

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

jemorrison
Copy link
Contributor

@jemorrison jemorrison commented Feb 3, 2025

Helps Resolve JP-3848

This PR replaces #382. There we too many conflicts to resolve easily after the code style changes were made.

Closes #

Corresponding jwst PR: spacetelescope/jwst#9193

This PR adds a datamodel of the reference file, specwcs, for MIRI LRS data. It can be used for fixed slit or slitless data. The reference file for fixed slit contains the V2,V3 corners of the slit. These values are not in the slitless reference file and default to None when the reference files are read in using the data models

Tasks

  • update or add relevant tests
  • update relevant docstrings and / or docs/ page
  • Does this PR change any API used downstream? (if not, label with no-changelog-entry-needed)
    • write news fragment(s) in changes/: echo "changed something" > changes/<PR#>.<changetype>.rst (see below for change types)
    • run jwst regression tests with this branch installed ("git+https://github.com/<fork>/stdatamodels@<branch>")
news fragment change types...
  • changes/<PR#>.feature.rst: new feature
  • changes/<PR#>.bugfix.rst: fixes an issue
  • changes/<PR#>.doc.rst: documentation change
  • changes/<PR#>.removal.rst: deprecation or removal of public API
  • changes/<PR#>.misc.rst: infrastructure or miscellaneous change

@jemorrison jemorrison requested a review from a team as a code owner February 3, 2025 22:34
Copy link

codecov bot commented Feb 3, 2025

Codecov Report

Attention: Patch coverage is 70.83333% with 14 lines in your changes missing coverage. Please review.

Project coverage is 78.28%. Comparing base (4e83608) to head (9c0fae9).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
src/stdatamodels/jwst/datamodels/wcs_ref_models.py 70.83% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #393      +/-   ##
==========================================
+ Coverage   78.16%   78.28%   +0.11%     
==========================================
  Files         115      110       -5     
  Lines        5144     5158      +14     
==========================================
+ Hits         4021     4038      +17     
+ Misses       1123     1120       -3     

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

@jemorrison
Copy link
Contributor Author

@braingram This PR is ready for another review

@jemorrison
Copy link
Contributor Author

This is ready to be merged so we can get the new reference file in CRDS

@braingram
Copy link
Collaborator

Would you fix the failing style check?
https://github.com/spacetelescope/stdatamodels/actions/runs/13332329197/job/37239256929?pr=393

I think a few comments/suggestions were lost when this new PR was opened.

Would you also add a unit test (re: #382 (comment))?

Also:

Finally if this is a reference model mapped to a CRDS reference file the test_schema_against_crds file could use an update (to use the new reference model):

Is there an example file yet for this reference model? If so, would you share it so I can test this PR?

@braingram braingram requested a review from nden February 17, 2025 15:29
@braingram
Copy link
Collaborator

Also requesting @nden as a reviewer to follow up on comments from #382

---
$schema: "http://stsci.edu/schemas/asdf/asdf-schema-1.1.0"
id: "http://stsci.edu/schemas/jwst_datamodel/specwcs_miri_lrs.schema"
title: MIRI LRS Spec Schema
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
title: MIRI LRS Spec Schema
title: MIRI LRS Fixed Slit Specwcs Schema

- $ref: keyword_exptype.schema
- $ref: keyword_readpatt.schema
- $ref: keyword_filter.schema
- $ref: keyword_band.schema
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are band and readpatt really necessary?

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 don't think they are needed

type: number
title: y coord of ref position of MIRIM_SLITLESS
default: pixels
fits_keyword: IMYSLTL
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this schema going to be used for SLITLESS mode? Do we expect a slitless mode reference file?

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 schema is supposed to work for both FIX slit and slitless. The slitless data does not need the v2/v3 vertices just the FIXED slit case

class MiriLRSSpecwcsModel(ReferenceFileModel):
"""
A model for a reference file of type "specwcs" for MIRI LRS Slit.
The model is for the specwcs for LRS Fixed Slit and LRSSlitless
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we expect an LRS slitless reference file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nden Let me clarify as understand it there is a reference file for FIXED slit and a reference file for SLITLESS. But Both of these files are read in with the same datamodel. The existing slitless reference on CRDS will be used.
A new reference file for FIXED SLIT LRS will have the v2,v3 vertices.
Do you see any problem with this. I tested reading in the new reference file for LRS FIXED slit and the existing slitless reference file using this 1 datamodel model and they are read in correctly

def populate_meta(self):
self.meta.instrument.name = "MIRI"
self.meta.instrument.detector = "MIRIMAGE"
self.meta.reftype = self.reftype
Copy link
Collaborator

Choose a reason for hiding this comment

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

This needs more keywords to define a LRS fixed slit mode - filter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added self.meta.instrument.filter = 'P750L'

@jemorrison
Copy link
Contributor Author

@braingram I have an example reference file. How should I share it with you ?

@braingram
Copy link
Collaborator

@braingram I have an example reference file. How should I share it with you ?

Would you upload it to the linked jira ticket? The link in the description is broken, would you fix that as well?

@jemorrison
Copy link
Contributor Author

@braingram
The reference file is already on the JIRI ticket: https://jira.stsci.edu/browse/JP-3848
Click on the first attached file jwst_miri_specwcs_2025jan17.fits
I just downloaded it so you should be able to get it.

@jemorrison
Copy link
Contributor Author

@braingram Sorry I am not tracking.
Looking at the other specwcs reference models: NIRCAMGrismModel, NIRISSGrismModel.
These models are not defined in test_schema_against_cdrs.py
I looked at how they are used in the pipeline:
with NIRCAMGrismModel(reference_files['specwcs'])
with NIRCAMGrismModel(reference_files['specwcs'])

This is how I am using the MIRILRS one:
refmodel = MiriLRSSpecwcsModel(reference_files['specwcs'])

So do I need it in test_schema_against_crds.py ?

@jemorrison
Copy link
Contributor Author

@braingram I added a unit test for this new model. I wrote this to test it was working as expected - I am not sure if it is what you want for a unit test.
Let me know if I need to change it

@braingram
Copy link
Collaborator

@braingram Sorry I am not tracking. Looking at the other specwcs reference models: NIRCAMGrismModel, NIRISSGrismModel. These models are not defined in test_schema_against_cdrs.py I looked at how they are used in the pipeline: with NIRCAMGrismModel(reference_files['specwcs']) with NIRCAMGrismModel(reference_files['specwcs'])

This is how I am using the MIRILRS one: refmodel = MiriLRSSpecwcsModel(reference_files['specwcs'])

So do I need it in test_schema_against_crds.py ?

Yeah I would say so. I believe @tapastro wrote the test originally. I think it's a bug (or lack of coverage) that NIRCAMGrismModel isn't covered but I'd say let's not extend that bug to the new model added in this PR.

@jemorrison
Copy link
Contributor Author

@braingram Sorry again
This is obviously wrong because it fails. But what should it be
it seems the format is ref_type: model

"specwcs": dm.SpecwcsModel, dm.MiriLRSSpecwcsModel,
this does not work either:
"specwcs": dm.SpecwcsModel,
"specwcs": dm.MiriLRSSpecwcsModel,

@jemorrison
Copy link
Contributor Author

jemorrison commented Feb 19, 2025

@braingram Maybe I need to do something like cubepar_model_map does and then add it to
ref_to_multiples_dict ?
Then I can all the NIRCAM and NIRSSS ones too ?

@jemorrison jemorrison requested a review from nden February 20, 2025 16:50
@tapastro
Copy link
Collaborator

@jemorrison It looks like your recent updates got you most of the way there - there was a typo in the NRC_WFSS exposure type, and once that is added it uncovered a bug in the NIRCam grism model that this test was designed to catch - so I added the schema reference to fix it. We'll see if tests pass now!

Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

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

Thanks for making the test updates and pointing me to the example file.

I left a few comments about:

  • adding a call to validate in the unit test
  • clarification of the default schema values
  • a comment/question about the reftype in the example file causing validation failure

try:
assert self.meta.instrument.name == "MIRI"
assert self.meta.instrument.detector == "MIRIMAGE"
assert self.meta.reftype == self.reftype
Copy link
Collaborator

Choose a reason for hiding this comment

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

This fails with the example file which has SPECWCS instead of specwcs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so is this really a failure of the reference file not having the correction retype ?
Is it ok to do this to account for that problem
assert self.meta.reftype == self.reftype.lower()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I have been using the example reference file and this model to read in the data. So how does it fail for you ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

What I ran was:

>> import stdatamodels.jwst.datamodels as dm
>> m = dm.MiriLRSSpecwcsModel("jwst_miri_specwcs_2025jan17.fits")
>> m.validate()
<ipython-input-5-6a8bfee86cf8>:1: ValidationWarning: Traceback (most recent call last):
  File "/Users/bgraham/projects/src/stdatamodels/src/stdatamodels/jwst/datamodels/wcs_ref_models.py", line 500, in validate
    assert self.meta.reftype == self.reftype
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

  m.validate()

This might just be an issue with the example file. Presumably the model will be used to generate a new reference model. In that case reftype will match (since it's set in populate_meta). However if I make a blank reference model it also fails to validate:

>>> m = dm.MiriLRSSpecwcsModel()
>>> m.validate()
/Users/bgraham/projects/src/stdatamodels/src/stdatamodels/jwst/datamodels/reference.py:41: ValidationWarning: Model.meta is missing values for ['description', 'author', 'pedigree', 'useafter']
  self.print_err(f"Model.meta is missing values for {to_fix}")

@tapastro is that expected? I'm not that familiar with how these reference models are used.

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 am not sure the reference model will be used to create the reference file. It is being created in IDL.
So my test is probably not as complete as yours
from stdatamodels.jwst import datamodels
file = 'jwst_miri_specwcs_2025jan17.fits'
model = datamodels.MiriLRSSpecwcsModel(file)

that seems to work fine
When I added this line to validate MiriLRSSpecwcs function in wcs_ref_models.py assert self.meta.reftype.lower() == self.reftype
It now validates - is that cheating ?

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 that might be a crds question. @stscieisenhamer are there any special requirements for reference file reftype (are they case sensitive)?

Copy link
Contributor

@drlaw1558 drlaw1558 Feb 26, 2025

Choose a reason for hiding this comment

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

Looking at a number of other reference files currently in CRDS it looks like those too have capitalized reftype values; e.g., APCORR, CUBEPAR, GAIN, etc.

@jemorrison
Copy link
Contributor Author

@tapastro @braingram I am still getting an error when I run pytest test_schema_against_crds.py
I am getting an error related to --no-crds
How am I supposed to run this test. I have been just typing

pytest test_schema_against_crds.py

@tapastro
Copy link
Collaborator

@tapastro @braingram I am still getting an error when I run pytest test_schema_against_crds.py I am getting an error related to --no-crds How am I supposed to run this test. I have been just typing

pytest test_schema_against_crds.py

I had the same issue - stdatamodels has a test configuration file in the base test directory that doesn't get picked up if you run these JWST tests in their local directory. If cd to the stdatamodels base directory and run something like pytest -k selectors, it should pick up the config option.

@jemorrison
Copy link
Contributor Author

@tapastro running pytest -k selectors did the trick. Now I can get test_schema_against_crds.py

@braingram I think all the changes have been made that are requested. We still need to determine about the reftype term and if it matters if it is SPECWCS or specwcs. I will ask at standup, but I can read the reference file and read in the contents so it works as expected and the retype in the reference file is SPECWCS so it seems fine to me

Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

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

These suggestions update the titles to follow the convention of having the unit in square brackets at the start of the title. This convention is followed by many of the existing keywords (see the core schema for some examples) and I suggest that we follow it for new keywords.

@braingram
Copy link
Collaborator

Thanks. The changes pushed in 823ee64 don't match the suggestions. They contain yaml syntax errors that are causing the CI failures and the failure you're seeing. There are also still "unit" keywords left in the table datatype that I suggested to remove.

@jemorrison
Copy link
Contributor Author

@braingram Ok thanks I missed the yaml errors and I misunderstood about units. I think I may finally have it.
Thanks for your help with this.

Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

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

Thanks for all the updates to this. The changes look good to me.

Would you run the regression tests with this PR?

Otherwise I'll leave the approval up to @nden

@jemorrison
Copy link
Contributor Author

@braingram To run the regression tests on datamodels do I just select the JWST Calibration Pipeline workflow and set the git repo the spacetelescope/datamodels. Or is running a regression test on the jwst branch I have made that uses this datamodel model sufficient to test this ?
I have a first run at https://github.com/spacetelescope/RegressionTests/actions/runs/13533041283 - but I will try a second one because there are failures not related to this PR.

@braingram
Copy link
Collaborator

@braingram To run the regression tests on datamodels do I just select the JWST Calibration Pipeline workflow and set the git repo the spacetelescope/datamodels. Or is running a regression test on the jwst branch I have made that uses this datamodel model sufficient to test this ? I have a first run at https://github.com/spacetelescope/RegressionTests/actions/runs/13533041283 - but I will try a second one because there are failures not related to this PR.

I think the most useful run for this PR would be using spacetelescope/jwst (jwst main) and providing a dependency override to install the source branch for this PR git+https://github.com/jemorrison/stdatamodels.git@MIRI_LRS_Specwcs. If that passes we'd know that merging this PR wouldn't break jwst main. Would you start that run? Let me know if you have any questions or if there's anything I can do to help.

@jemorrison
Copy link
Contributor Author

@braingram results of regression test:
https://github.com/spacetelescope/RegressionTests/actions/runs/13549832932

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

Successfully merging this pull request may close these issues.

5 participants