Skip to content

Commit

Permalink
Minor fixes for optimal extraction support
Browse files Browse the repository at this point in the history
  • Loading branch information
melanieclarke committed Nov 21, 2024
1 parent 6868aa9 commit de314db
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions jwst/extract_1d/extract1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def build_coef_matrix(image, profiles_2d=None, profile_bg=None,
variance weighting, these should be the square root of the inverse
variance. If not supplied, unit weights will be used.
bkg_order : int
order : int
Polynomial order for fitting to each column of background.
Default 0 (uniform background).
Expand Down Expand Up @@ -403,24 +403,29 @@ def extract1d(image, profiles_2d, variance_rn, variance_phnoise, variance_flat,

# Number of contributing pixels at each wavelength for each source.

npixels = np.sum(pixwgt[..., -nobjects:], axis=1).T
npixels = np.sum(pixwgt[..., -nobjects:] != 0, axis=1).T

Check warning on line 406 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L406

Added line #L406 was not covered by tests

if order > -1:
bkg_2d = np.sum(coefs[:, np.newaxis, :order + 1] * coefmatrix[..., :order + 1], axis=-1).T

Check warning on line 409 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L408-L409

Added lines #L408 - L409 were not covered by tests

# Variances for each object (discard variances for background here)

var_rn = np.sum(pixwgt[..., -nobjects:] ** 2 * variance_rn.T[:, :, np.newaxis], axis=1).T
var_phnoise = np.sum(pixwgt[..., -nobjects:] ** 2 * variance_phnoise.T[:, :, np.newaxis], axis=1).T
var_flat = np.sum(pixwgt[..., -nobjects:] ** 2 * variance_flat.T[:, :, np.newaxis], axis=1).T
var_rn = np.nansum(pixwgt[..., -nobjects:] ** 2 * variance_rn.T[:, :, np.newaxis], axis=1).T
var_phnoise = np.nansum(pixwgt[..., -nobjects:] ** 2 * variance_phnoise.T[:, :, np.newaxis], axis=1).T
var_flat = np.nansum(pixwgt[..., -nobjects:] ** 2 * variance_flat.T[:, :, np.newaxis], axis=1).T

Check warning on line 415 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L413-L415

Added lines #L413 - L415 were not covered by tests

# Computing a background contribution to the noise is harder in a joint fit.
# Here, I am computing the weighting coefficients I would have without a background.
# I then compute those variances, and subtract them from the actual variances.

if order > -1:

Check warning on line 421 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L421

Added line #L421 was not covered by tests
wgt_nobkg = [profiles_2d[i] * weights / np.sum(profiles_2d[i] ** 2 * weights, axis=0) for i in
range(nobjects)]

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning, message="invalid value")

Check warning on line 424 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L423-L424

Added lines #L423 - L424 were not covered by tests

wgt_nobkg = [profiles_2d[i] * weights / np.sum(profiles_2d[i] ** 2 * weights, axis=0)

Check warning on line 426 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L426

Added line #L426 was not covered by tests
for i in range(nobjects)]

bkg = np.array([np.sum(wgt_nobkg[i] * bkg_2d, axis=0) for i in range(nobjects)])

Check warning on line 429 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L429

Added line #L429 was not covered by tests

var_bkg_rn = np.array([var_rn[i] - np.sum(wgt_nobkg[i] ** 2 * variance_rn, axis=0)

Check warning on line 431 in jwst/extract_1d/extract1d.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/extract1d.py#L431

Added line #L431 was not covered by tests
Expand Down

0 comments on commit de314db

Please sign in to comment.