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-3784 Fix Pixel replace and cube_build output file names #8979

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

Conversation

jemorrison
Copy link
Collaborator

@jemorrison jemorrison commented Nov 25, 2024

Resolves JP-3784

Closes #8904

This PR addresses fixes the names of the output from pixel_replacement when called using a ModelContainer. In addition, it fixes the output names of cube_build when a single file is input to cube_build.

Tasks

  • request a review from someone specific, to avoid making the maintainers review every PR
  • add a build milestone, i.e. Build 11.3 (use the latest build if not sure)
  • Does this PR change user-facing code / API? (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)
    • update or add relevant tests
    • update relevant docstrings and / or docs/ page
    • start a regression test and include a link to the running job (click here for instructions)
      • Do truth files need to be updated ("okified")?
        • after the reviewer has approved these changes, run okify_regtests to update the truth files
  • if a JIRA ticket exists, make sure it is resolved properly
news fragment change types...
  • changes/<PR#>.general.rst: infrastructure or miscellaneous change
  • changes/<PR#>.docs.rst
  • changes/<PR#>.stpipe.rst
  • changes/<PR#>.datamodels.rst
  • changes/<PR#>.scripts.rst
  • changes/<PR#>.fits_generator.rst
  • changes/<PR#>.set_telescope_pointing.rst
  • changes/<PR#>.pipeline.rst

stage 1

  • changes/<PR#>.group_scale.rst
  • changes/<PR#>.dq_init.rst
  • changes/<PR#>.emicorr.rst
  • changes/<PR#>.saturation.rst
  • changes/<PR#>.ipc.rst
  • changes/<PR#>.firstframe.rst
  • changes/<PR#>.lastframe.rst
  • changes/<PR#>.reset.rst
  • changes/<PR#>.superbias.rst
  • changes/<PR#>.refpix.rst
  • changes/<PR#>.linearity.rst
  • changes/<PR#>.rscd.rst
  • changes/<PR#>.persistence.rst
  • changes/<PR#>.dark_current.rst
  • changes/<PR#>.charge_migration.rst
  • changes/<PR#>.jump.rst
  • changes/<PR#>.clean_flicker_noise.rst
  • changes/<PR#>.ramp_fitting.rst
  • changes/<PR#>.gain_scale.rst

stage 2

  • changes/<PR#>.assign_wcs.rst
  • changes/<PR#>.badpix_selfcal.rst
  • changes/<PR#>.msaflagopen.rst
  • changes/<PR#>.nsclean.rst
  • changes/<PR#>.imprint.rst
  • changes/<PR#>.background.rst
  • changes/<PR#>.extract_2d.rst
  • changes/<PR#>.master_background.rst
  • changes/<PR#>.wavecorr.rst
  • changes/<PR#>.srctype.rst
  • changes/<PR#>.straylight.rst
  • changes/<PR#>.wfss_contam.rst
  • changes/<PR#>.flatfield.rst
  • changes/<PR#>.fringe.rst
  • changes/<PR#>.pathloss.rst
  • changes/<PR#>.barshadow.rst
  • changes/<PR#>.photom.rst
  • changes/<PR#>.pixel_replace.rst
  • changes/<PR#>.resample_spec.rst
  • changes/<PR#>.residual_fringe.rst
  • changes/<PR#>.cube_build.rst
  • changes/<PR#>.extract_1d.rst
  • changes/<PR#>.resample.rst

stage 3

  • changes/<PR#>.assign_mtwcs.rst
  • changes/<PR#>.mrs_imatch.rst
  • changes/<PR#>.tweakreg.rst
  • changes/<PR#>.skymatch.rst
  • changes/<PR#>.exp_to_source.rst
  • changes/<PR#>.outlier_detection.rst
  • changes/<PR#>.tso_photometry.rst
  • changes/<PR#>.stack_refs.rst
  • changes/<PR#>.align_refs.rst
  • changes/<PR#>.klip.rst
  • changes/<PR#>.spectral_leak.rst
  • changes/<PR#>.source_catalog.rst
  • changes/<PR#>.combine_1d.rst
  • changes/<PR#>.ami.rst

other

  • changes/<PR#>.wfs_combine.rst
  • changes/<PR#>.white_light.rst
  • changes/<PR#>.cube_skymatch.rst
  • changes/<PR#>.engdb_tools.rst
  • changes/<PR#>.guider_cds.rst

@jemorrison jemorrison requested a review from a team as a code owner November 25, 2024 20:25
@jemorrison jemorrison changed the title JP-3784Pixel replace output JP-3784 Fix Pixel replace and cube_build output file names Nov 25, 2024
Copy link

codecov bot commented Nov 25, 2024

Codecov Report

Attention: Patch coverage is 14.28571% with 18 lines in your changes missing coverage. Please review.

Project coverage is 64.51%. Comparing base (208e7ce) to head (5756f31).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
jwst/pixel_replace/pixel_replace_step.py 10.00% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8979      +/-   ##
==========================================
- Coverage   64.53%   64.51%   -0.03%     
==========================================
  Files         375      375              
  Lines       38728    38744      +16     
==========================================
+ Hits        24993    24995       +2     
- Misses      13735    13749      +14     

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

@jemorrison
Copy link
Collaborator Author

@melanieclarke @emolter
As I am writing a unit test for this step I have come across something I want to check with you guys.
After running pixel_replace on a single file the output data has 'pixelreplacestep' in the output.meta.filename.
But if I run it on a container the returned output is the input cal file. BUT if I write the data out with save_results=True. The correct filename (with pixelreplacestep) is on disk.
I am testing this:
file = []
file.append('jw01267003001_02105_00001_mirifushort_cal.fits')
file.append('jw01267003001_02105_00002_mirifushort_cal.fits')
pixel_replace = pixel_replace_step.PixelReplaceStep()
container = ModelContainer(file)
pixel_replace.call(container, save_results=True, skip=False)
for c in container:
print(c.meta.filename)
Here is a snap shot of running the step on a container. The information in the log show the correct filenames are created but the print statement shows the return value in meta.filename is the cal file

024-11-27 07:10:50,422 - stpipe.PixelReplaceStep - INFO - Saved model in jw01267003001_02105_00001_mirifushort_pixelreplacestep.fits
2024-11-27 07:10:51,059 - stpipe.PixelReplaceStep - INFO - Saved model in jw01267003001_02105_00002_mirifushort_pixelreplacestep.fits
2024-11-27 07:10:51,060 - stpipe.PixelReplaceStep - INFO - Step PixelReplaceStep done
2024-11-27 07:10:51,060 - stpipe - INFO - Results used jwst version: 1.16.1.dev237+g59ef24d68

jw01267003001_02105_00001_mirifushort_cal.fits
jw01267003001_02105_00002_mirifushort_cal.fits

So the step is working just fine but I can not seem to access the output filenames when it is called from a container.
Any suggestions ? Frankly it works so that is a win. I just wanted the filenames to have the pixelreplacestep in the name so I could have a unit test

@jemorrison
Copy link
Collaborator Author

@melanieclarke I got this step to work by including the 'invariant_filenames' routine that is from cal spec3 and it manages filenames. I just copied the routine and suck in pixel_replace_step.py. I tried to import it from:
from jwst.pipeline.calwebb_spec3 import invariant_filename

But that gets circular and I get an error because calwebb_spec3 is importing pixel_replace.
I can leave it as it is - but maybe you have a better idea rather than having duplicate code.

@emolter
Copy link
Collaborator

emolter commented Nov 27, 2024

So the step is working just fine but I can not seem to access the output filenames when it is called from a container.
Any suggestions ? Frankly it works so that is a win. I just wanted the filenames to have the pixelreplacestep in the name so I could have a unit test

The invariant_filename wrapper appears to ensure that model.meta.filename does NOT change when step.save_model is called. I'm not convinced that having _pixelreplacestep in model.meta.filename is the right thing to do, because isn't that just outdated again once the container is passed to the next step?

I didn't review this in detail yet but I don't see anything wrong with the current solution.

For unit testing, instead of checking the model.meta.filename, could you check the actual name of the files written to disk? See e.g. this outlier detection unit test as an example - it just uses assert path.isfile(filename). Make sure you pass in the tmp_cwd fixture like this test does, too.

@melanieclarke
Copy link
Collaborator

But that gets circular and I get an error because calwebb_spec3 is importing pixel_replace.
I can leave it as it is - but maybe you have a better idea rather than having duplicate code.

We could move that function to a library and import it in both places - maybe in lib/pipe_utils.py. I'm not sure I understand why that's working, though.

@jemorrison jemorrison added this to the Build 11.2 milestone Nov 27, 2024
@emolter
Copy link
Collaborator

emolter commented Nov 27, 2024

We could move that function to a library and import it in both places - maybe in lib/pipe_utils.py.

At risk of nit-picking, since invariant_filename wraps an stpipe.step method, would somewhere within jwst.stpipe be a good place for it?

@melanieclarke
Copy link
Collaborator

Testing locally, it looks like this solution does work for the case we were trying to fix, where container is made from a list of filenames:
PixelReplaceStep.call(container, save_results=True, skip=False)

But it now un-fixes the calwebb_spec3 case. Calling like this:
strun calwebb_spec3 s3.json --steps.pixel_replace.skip=False --steps.pixel_replace.save_results=True
the output from pixel_replace is correct, but the final products look like this:
_g395h-f290lp_s3d.fits and _g395h-f290lp_x1d.fits.

@jemorrison
Copy link
Collaborator Author

@melanieclarke @emolter I do have a question about a unit test that saves the output to disk. Is that allowed ? It writes out files to the test directory ?

@melanieclarke I need to understand your test better. In your s3.json file what is the output name set to ?

I have a set of tests (in a Jupyter notebook) that I have developed to test the various filenames of pixel_replace and cube_build. When I run calspec3 and save the pixel_replace output. ALL the filenames have the correct format.

spec3_asn = 'test.json'
spec3 = Spec3Pipeline()
result = spec3.call(spec3_asn,
steps={ 'outlier_detection':{'skip':True},
'pixel_replace':{'skip': False, 'save_results': True},
'cube_build':{'save_results':True},
'extract_1d':{'skip': True}
},
save_results=True)

in the association the name is set to 'test_miri' and there are 4 input files.
The output from pixel_replace is:
024-11-27 11:37:44,322 - stpipe.Spec3Pipeline.pixel_replace - INFO - Saved model in jw01267003001_02105_00002_mirifushort_ctest_pixel_replace.fits
2024-11-27 11:37:44,765 - stpipe.Spec3Pipeline.pixel_replace - INFO - Saved model in jw01267003001_02105_00001_mirifushort_ctest_pixel_replace.fits
2024-11-27 11:37:45,482 - stpipe.Spec3Pipeline.pixel_replace - INFO - Saved model in jw01267003001_02105_00004_mirifushort_ctest_pixel_replace.fits
2024-11-27 11:37:45,936 - stpipe.Spec3Pipeline.pixel_replace - INFO - Saved model in jw01267003001_02105_00003_mirifushort_ctest_pixel_replace.fits

and the output from cube_build is:
2024-11-27 11:38:17,804 - stpipe.Spec3Pipeline.spectral_leak - INFO - Saved model in test_miri_ch2-short_x1d.fits
2024-11-27 11:38:17,914 - stpipe.Spec3Pipeline.spectral_leak - INFO - Saved model in test_miri_ch1-short_x1d.fits

@jemorrison
Copy link
Collaborator Author

@melanieclarke Forget last message. I was on a wrong branch. Let me check what is going on with cube_build and I will update

@emolter
Copy link
Collaborator

emolter commented Nov 27, 2024

@emolter I do have a question about a unit test that saves the output to disk. Is that allowed ? It writes out files to the test directory ?

If you're passing in e.g. the tmp_cwd fixture to the test, it will make its own temporary directory where it writes out files. After the test ran, you should not see any new files in the test directory.

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

Successfully merging this pull request may close these issues.

PixelReplaceStep returns strange output filenames from save_results=True
3 participants