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-3677: Add maximum_shower_amplitude parameter to jump step #306

Merged
merged 11 commits into from
Dec 3, 2024

Conversation

drlaw1558
Copy link
Contributor

@drlaw1558 drlaw1558 commented Oct 15, 2024

This PR addresses JP-3677 by adding a maximum_shower_amplitude parameter to the MIRI cosmic ray showers code, and ensuring that any changes in the rate image due to shower flagging are below this amplitude.

See also corresponding jwst PR spacetelescope/jwst#8890

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 regression tests with this branch installed ("git+https://github.com/<fork>/stcal@<branch>")
news fragment change types...
  • changes/<PR#>.apichange.rst: change to public API
  • changes/<PR#>.bugfix.rst: fixes an issue
  • changes/<PR#>.general.rst: infrastructure or miscellaneous change

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

Attention: Patch coverage is 6.89655% with 27 lines in your changes missing coverage. Please review.

Project coverage is 29.56%. Comparing base (26836b8) to head (728b877).
Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
src/stcal/jump/jump.py 6.89% 27 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (26836b8) and HEAD (728b877). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (26836b8) HEAD (728b877)
3 2
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #306       +/-   ##
===========================================
- Coverage   86.68%   29.56%   -57.13%     
===========================================
  Files          49       37       -12     
  Lines        8937     8332      -605     
===========================================
- Hits         7747     2463     -5284     
- Misses       1190     5869     +4679     

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

@drlaw1558
Copy link
Contributor Author

Not sure why I can't request a review, but tagging @kmacdonald-stsci

@ddavis-stsci
Copy link
Collaborator

The romancal regression tests pass
https://github.com/spacetelescope/RegressionTests/actions/runs/11365425316
so we're good with the changes

Copy link
Collaborator

@kmacdonald-stsci kmacdonald-stsci left a comment

Choose a reason for hiding this comment

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

There are portions of the code that are invoked unconditionally, but included in conditionals, which results in two parts of the code that need to be maintained, when there could be a single unconditional call to be maintained.

Also, there are some formatting issues.

@@ -317,6 +320,7 @@ def detect_jumps(
)
log.info("Total snowballs = %i", total_snowballs)
number_extended_events = total_snowballs

if find_showers:
Copy link
Collaborator

Choose a reason for hiding this comment

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

As noted in another PR, the find_showers portion of the code is always invoked in both the if and else portion of the conditional. Since it is always executed unconditionally, it should be moved outside the conditional. This applies to the expand_large_events portion of the code, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

# Approximate pre-shower rates
tempdata = indata[intg,:,:,:].copy()
# Ignore any groups flagged in the original gdq array
tempdata[ingdq[intg,:,:,:] & donotuse_flag != 0] = np.nan
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be combined into a single computation by setting invalid_flags = donotuse_flag | sat_flag | jump_flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

# Ensure that flagging showers didn't change final fluxes by more than the allowed amount
for intg in range(nints):
# Approximate pre-shower rates
tempdata = indata[intg,:,:,:].copy()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add spaces after ,.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

# Approximate pre-shower rates
tempdata = indata[intg,:,:,:].copy()
# Ignore any groups flagged in the original gdq array
tempdata[ingdq[intg,:,:,:] & donotuse_flag != 0] = np.nan
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add spaces after ,.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

# Approximate post-shower rates
tempdata = indata[intg,:,:,:].copy()
# Ignore any groups flagged in the shower gdq array
tempdata[gdq[intg,:,:,:] & donotuse_flag != 0] = np.nan
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add spaces after ,.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

# became NaN or changed by more than the amount reasonable for a real CR shower
diff = np.abs(image1 - image2)
indx = np.where((np.isfinite(diff) == False)|(diff > max_shower_amplitude))
gdq[intg,:,indx[0],indx[1]] = ingdq[intg,:,indx[0],indx[1]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add spaces after ,.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

# Approximate post-shower rates
tempdata = indata[intg,:,:,:].copy()
# Ignore any groups flagged in the shower gdq array
tempdata[gdq[intg,:,:,:] & donotuse_flag != 0] = np.nan
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be combined into a single computation by setting invalid_flags = donotuse_flag | sat_flag | jump_flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -1111,6 +1123,39 @@ def find_faint_extended(
num_grps_masked=num_grps_masked,
max_extended_radius=max_extended_radius
)

# Ensure that flagging showers didn't change final fluxes by more than the allowed amount
for intg in range(nints):
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 loop needed? Is it possible to operate on the full data set, taking advantage of fast numpy looping, instead of slow explicit looping?

Copy link
Contributor Author

@drlaw1558 drlaw1558 Oct 17, 2024

Choose a reason for hiding this comment

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

Here the worry was that calling .copy() on the entire array could be extremely memory intensive for cases where there were a large number of integrations. Looping over the integration is less efficient, but it looks like it still only takes 3 seconds even for a TSO case broken into 18 ints, 30 groups, and 1024x1032 pixels.

Copy link
Collaborator

@kmacdonald-stsci kmacdonald-stsci left a comment

Choose a reason for hiding this comment

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

LGTM

@drlaw1558
Copy link
Contributor Author

Updated to catch additional runtime warnings, and ensure the maximum amplitude is passed in the correct units so that it works for both FAST and SLOW mode data.

@drlaw1558
Copy link
Contributor Author

Added one more minor change based on feedback from @mwregan2 to fix a prior bug with propagating flags to smoothed masks.

Copy link
Collaborator

@tapastro tapastro left a comment

Choose a reason for hiding this comment

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

Change fragment needs renamed from 307 --> 306. Otherwise looks good, will approve pending fix.

@tapastro
Copy link
Collaborator

It looks like a primary result of the regresssion tests is a difference in how reference pixels are labeled - the test output shows the reference pixels now lack the DNU flag. Is this intended behavior?

@drlaw1558
Copy link
Contributor Author

@tapastro Just double checked this for both MIRI MRS and Imaging detectors, and that change is fine. Without running find_showers the reference pixels do not get flagged as DO_NOT_USE by the jump step, and so this change actually makes their treatment more consistent between whether or not the showers algorithm was run.

@tapastro tapastro self-requested a review December 3, 2024 17:04
@tapastro tapastro merged commit 30dd769 into spacetelescope:main Dec 3, 2024
23 of 26 checks passed
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.

5 participants