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

RCAL-723 Fix model.meta.filename when saving model #295

Merged
merged 6 commits into from
Dec 14, 2023

Conversation

WilliamJamieson
Copy link
Collaborator

@WilliamJamieson WilliamJamieson commented Nov 24, 2023

Resolves RCAL-723

Closes spacetelescope/romancal#1026

When saving a DataModel to an arbitrary ASDF file, it was noted that the model.meta.filename does not reflect the name of the file it was saved to. This PR makes sure that when saving a DataModel to foo.asdf that Datamodel.meta.filename==foo.asdf in the saved file.

Note that this solves RCAL-723 when running individual steps or outputting all the step results.

Checklist

Copy link

codecov bot commented Nov 24, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c9d6323) 97.07% compared to head (b0ae471) 97.24%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #295      +/-   ##
==========================================
+ Coverage   97.07%   97.24%   +0.17%     
==========================================
  Files          29       29              
  Lines        2526     2538      +12     
==========================================
+ Hits         2452     2468      +16     
+ Misses         74       70       -4     

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

Comment on lines +50 to +63
@contextmanager
def _temporary_update_filename(datamodel, filename):
"""
Context manager to temporarily update the filename of a datamodel so that it
can be saved with that new file name without changing the current model's filename
"""
from roman_datamodels.stnode import Filename

if "meta" in datamodel._instance and "filename" in datamodel._instance.meta:
old_filename = datamodel._instance.meta.filename
datamodel._instance.meta.filename = Filename(filename)

yield
datamodel._instance.meta.filename = old_filename
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This specifically exists so that the DataModel instance being saved via save or to_asdf does not update its filename, the filename is only updated in the newly written ASDF file and the model which can be opened from that.

I chose to do it this way because AsdfFile.write_to is employed by these to_asdf (which is called by save), which writes the AsdfFile object to the new file descriptor passed to it, but it does not update the file descriptor of the AsdfFile object. This means the asdf file wrapped by the DataModel instance is not changing, so the filename of the python object being saved should not be updated.

@PaulHuwe
Copy link
Collaborator

PaulHuwe commented Nov 27, 2023

Could you add a regression test link? I think this may break them (if previous tests were not careful with file names).

@WilliamJamieson
Copy link
Collaborator Author

Could you add a regression test link? I think this may break them (if previous tests were not careful with file names).

You are quite right, it breaks most of the regression tests. See https://plwishmaster.stsci.edu:8081/job/RT/job/Roman-Developers-Pull-Requests/502/. In reviewing them, it looks like all the failures are due to the roman.meta.filename not matching, which points to us not being careful about the names before.

@schlafly
Copy link
Collaborator

Let's discuss what we want here at a Thursday tag up. Honestly, it's not always clear to me what one wants for meta.filename. I guess if you save it you want it to be the filename? But if you pass it through steps should the steps be updating it to what they would save it to, were they to write it out? When I was playing with related things last week I got a bit confused about when meta.filename was used to decide what output filename to use, vs. when command line arguments were used, vs. when file names were constructed automatically from the step names.

But let's do this after the upcoming release.

Copy link
Collaborator

@PaulHuwe PaulHuwe left a comment

Choose a reason for hiding this comment

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

LGTM

@nden
Copy link
Collaborator

nden commented Dec 14, 2023

@WilliamJamieson Feel free to merge once checks pass

@nden nden merged commit da87fdc into spacetelescope:main Dec 14, 2023
16 checks passed
@WilliamJamieson WilliamJamieson deleted the bugfix/meta_filename branch December 15, 2023 14:05
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.

roman_datamodel attribute filename used consistently
5 participants