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-3593: Saturation step should increase flagging for group 3 saturation #8499

Closed
wants to merge 26 commits into from

Conversation

penaguerrero
Copy link
Contributor

@penaguerrero penaguerrero commented May 21, 2024

Resolves JP-3593

Closes #8413

This PR adds a check for saturation bias in group 2 for nframes > 1.

Checklist for PR authors (skip items if you don't have permissions or they are not applicable)

  • added entry in CHANGES.rst within the relevant release section
  • updated or added relevant tests
  • updated relevant documentation
  • added relevant milestone
  • added relevant label(s)
  • ran regression tests, post a link to the Jenkins job below.
    How to run regression tests on a PR
  • All comments are resolved
  • Make sure the JIRA ticket is resolved properly

@penaguerrero penaguerrero added this to the Build 11.0 milestone May 21, 2024
@penaguerrero penaguerrero requested a review from a team as a code owner May 21, 2024 20:00
Copy link

codecov bot commented May 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.56%. Comparing base (23fb5cd) to head (be2b053).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8499      +/-   ##
==========================================
+ Coverage   59.54%   59.56%   +0.01%     
==========================================
  Files         391      391              
  Lines       39292    39310      +18     
==========================================
+ Hits        23396    23414      +18     
  Misses      15896    15896              

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

@hbushouse
Copy link
Collaborator

@penaguerrero Now that this update has been moved over to the stcal repo, can this PR be closed?

@hbushouse
Copy link
Collaborator

@penaguerrero What's the relationship between this PR and spacetelescope/stcal#259, which implements the saturation check in the part of the code that resides in stcal? Can this PR be closed, because it's no longer necessary? Or will we need at least some minimal change in the top-level saturation_step module here in jwst to add anything like a new step parameter?

@penaguerrero penaguerrero changed the title WIP - JP-3593: Saturation step should increase flagging for group 3 saturation JP-3593: Saturation step should increase flagging for group 3 saturation Jun 3, 2024
@penaguerrero
Copy link
Contributor Author

The PR is necessary because due to the changes on stcal, which adds code to an already present parameter, the jwst side needs to now use that parameter. This PR makes use of this parameter as well as modifying the tests.


# Identify groups that are saturated in the third group
dq_temp = x_irs2.from_irs2(groupdq[ints, 2, :, :], irs2_mask, detector)
gp3mask = np.where(dq_temp & SATURATED, True, False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code won't work here; it's looking for where SATURATED is set in groupdq for group==2, but the flags aren't passed from flag_temp into groupdq until after this code block executes. Likewise for the stcal PR.

if group == 2:
# Identify groups which we wouldn't expect to saturate by the third group,
# on the basis of the first group
mask = sci_temp[ints, 0, ...] / np.mean(read_pattern[0]) * read_pattern[2][-1] < sat_thresh
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this will work; sci_temp as defined above is 2048x2048, so accessing sci_temp[ints,0,...] will just return a single element [0,0] of the array.

mask = sci_temp[ints, 0, ...] / np.mean(read_pattern[0]) * read_pattern[2][-1] < sat_thresh

# Identify groups with suspiciously large values in the second group
mask &= sci_temp[ints, 1, ...] > sat_thresh / len(read_pattern[1])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Likewise here, this will just return element [0,1] of the 2d sci_temp array.

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 we also need something to grow this mask by n_pix_grow_sat using the adjacency_sat routine. For pixels bordering pixels that saturate in group 3, the saturation flag is set too. Those pixels might not be intrinsically bright enough to trigger the 'suspiciously large' criterion, but will likewise be affected by the CR and restricted to only fitting a ramp to groups 1+2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@drlaw1558 thank you for the comments! I'm committing the changes soon. Can I please ask you to take look again when they are in on this branch as well?


# now, flag any pixels that border saturated pixels
if n_pix_grow_sat > 0:
mask = adjacency_sat(mask, SATURATED, n_pix_grow_sat)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we can call it like this; 'mask' is an array of True/False whereas adjacency_sat expects input of actual dq flag values. It should work though to say
if n_pix_grow_sat > 0:
dq_temp = adjacency_sat(dq_temp, SATURATED, n_pix_grow_sat)
right after line 188 before the call to x_irs2.to_irs2

# Flag the 2nd group for the pixels passing that gauntlet
dq_temp = x_irs2.from_irs2(groupdq[ints, 1, :, :], irs2_mask, detector)
dq_temp[mask] |= SATURATED
x_irs2.to_irs2(flagarray, dq_temp, irs2_mask, detector)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this is being passed back correctly; we're populating flagarray, but flagarray only gets put into the groupdq array a few lines later for group==2 whereas we're trying to set saturation bits for group==1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@drlaw1558 I made some changes and I believe you were correct and now it sets the flags ok, can you verify please?

mask &= gp3mask

# Flag the 2nd group for the pixels passing that gauntlet
dq_temp = x_irs2.from_irs2(groupdq[ints, 1, :, :], irs2_mask, detector)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nearly there, but I think this line is problematic. Since it's using the existing groupdq array, it's going to be growing ALL pixels flagged as SATURATED, not just those that passed the gauntlet above, so things in group1 will get grown twice.

I think this should be possible to deal with by replacing
dq_temp = x_irs2.from_irs2(groupdq[ints, 1, :, :], irs2_mask, detector)
with
dq_temp = np.zeros_like(mask).astype(int)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suppose it would probably be better form though to explicitly cast the dq_temp to be the same dtype as groupdq.

@drlaw1558
Copy link
Collaborator

Hm, I'm also seeing some unintended consequences on NIRISS data from this PR. Defining the read_patt is activating dilution_factor code in stcal that wasn't being used before, resulting in larger-than-expected flagging. Digging into this further.

@drlaw1558
Copy link
Collaborator

Ok, I'm inclined to say that we should remove the changes around line 73-76 in this jwst PR, and not merge the stcal PR at this time. This should limit the changes to affecting NIRSpec IRS2 data only for the time being, and fix the immediate issue while giving us more time to study what's going on with the NIRISS data.

@hbushouse hbushouse modified the milestones: Build 11.0, Build 11.1 Jun 25, 2024
@penaguerrero
Copy link
Contributor Author

closing this PR as a new branch is dealing with the issues found for NIRISS and NIRCam data https://github.com/drlaw1558/stcal/tree/jp3593_take2.

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.

Saturation step should increase flagging for group 3 saturation
3 participants