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-3664: Fix pixel replace numpy 2.0 issues #9004

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes/9004.pixel_replace.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Change from the default BFGS algorithm to Nelder-Mead when calling scipy.minimize
within the fit_profile approach topixel replacement in order to fix numpy 2.0
compatibility issues. Additionally, add safety catch to ensure that pixel replacement
profile fitting doesn't attempt to scale based on noise.
26 changes: 20 additions & 6 deletions jwst/pixel_replace/pixel_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,27 @@
# Cut out valid neighboring profiles
adjacent_condition = self.custom_slice(dispaxis, valid_adjacent_inds)
profile_data = model.data[adjacent_condition]
profile_err = model.err[adjacent_condition]

# Mask out bad pixels
invalid_condition = (model.dq[adjacent_condition] & self.DO_NOT_USE).astype(bool)
profile_data[invalid_condition] = np.nan
profile_err[invalid_condition] = np.nan

# Add additional cut to pull only from region with valid data
# for convenience (may not be necessary)
region_condition = self.custom_slice(3 - dispaxis, range(*profile_cut))
profile_data = profile_data[region_condition]
profile_snr = np.abs(profile_data / profile_err[region_condition])

# Normalize profile data
# TODO: check on signs here - absolute max sometimes picks up
# large negative outliers
profile_norm_scale = np.nanmax(np.abs(profile_data), axis=(dispaxis - 1), keepdims=True)
# If profile data has SNR < 5 everywhere just use unity scaling
# (so we don't normalize to noise)
if (np.nanmax(profile_snr) < 5):
profile_norm_scale[:] = 1.0
normalized = profile_data / profile_norm_scale

# Get corresponding error and variance data and scale and mask to match
Expand Down Expand Up @@ -377,12 +384,19 @@
norm_current = min_current / np.max(min_current)

# Scale median profile to current profile with bad pixel - minimize mse?
# TODO: check on signs here - absolute max sometimes picks up
# large negative outliers
norm_scale = minimize(self.profile_mse, x0=np.abs(np.nanmax(norm_current)),
args=(min_median, norm_current)).x

scale = np.max(min_current)
# Only do this scaling if we didn't default to all-unity scaling above,
# and require input values below 1e20 so that we don't overflow the
# minimization routine with extremely bad noise.
if ((np.nanmedian(profile_norm_scale) != 1.0) & (np.nanmax(np.abs(min_median)) < 1e20)
& (np.nanmax(np.abs(norm_current)) < 1e20)):
# TODO: check on signs here - absolute max sometimes picks up
# large negative outliers
norm_scale = minimize(self.profile_mse, x0=np.abs(np.nanmax(norm_current)),

Check warning on line 394 in jwst/pixel_replace/pixel_replace.py

View check run for this annotation

Codecov / codecov/patch

jwst/pixel_replace/pixel_replace.py#L394

Added line #L394 was not covered by tests
args=(np.abs(min_median), np.abs(norm_current)), method='Nelder-Mead').x
scale = np.max(min_current)

Check warning on line 396 in jwst/pixel_replace/pixel_replace.py

View check run for this annotation

Codecov / codecov/patch

jwst/pixel_replace/pixel_replace.py#L396

Added line #L396 was not covered by tests
else:
norm_scale = 1.0
scale = 1.0

# Replace pixels that are do-not-use but not non-science
current_dq = model.dq[current_condition][range(*profile_cut)]
Expand Down
Loading