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-3669: Updating the C Extension to do CHARGELOSS Read Noise Recalculations #275

Merged
merged 36 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5c42172
Adding chargeless handler stub.
kmacdonald-stsci Jul 17, 2024
096d3ab
Updating debugging function.
kmacdonald-stsci Jul 17, 2024
4ac254d
Setting up chargeloss test for ramp fitting.
kmacdonald-stsci Jul 17, 2024
29549b0
Updating git ignore file to ignore swp files.
kmacdonald-stsci Jul 17, 2024
554e892
Changed variables in segment computation for ramp fitting in order to…
kmacdonald-stsci Jul 17, 2024
8f37c80
Updating ramp fitting tests in preparation for the chargeloss flag.
kmacdonald-stsci Jul 17, 2024
bdfd518
Generalized the ramp fitting computation of an integration into segme…
kmacdonald-stsci Jul 17, 2024
373d6d7
Refactoring segment read noise calculation for ramp fitting C extension.
kmacdonald-stsci Jul 18, 2024
ff85dda
Refactoring the length 1 group segment computation for ramp fitting.
kmacdonald-stsci Jul 18, 2024
6e4beff
Adding the chargeloss read noise re-computation to the C extension in…
kmacdonald-stsci Jul 18, 2024
6f9dfc1
Skipping chargeloss test in ramp fitting for now.
kmacdonald-stsci Jul 18, 2024
01fee1a
Forcing C extension usage in ramp fitting during testing, but comment…
kmacdonald-stsci Jul 18, 2024
8c4bc4a
Adding some debugging to the ramp fit chargeloss test.
kmacdonald-stsci Jul 19, 2024
641847e
Updating and expanding comments for the new code in the ramp fitting …
kmacdonald-stsci Jul 19, 2024
3cee7cb
Adding a copy of the original GDQ array to the RampData class.
kmacdonald-stsci Jul 25, 2024
30a4744
Adding a copy of the original GDQ array to the RampData class.
kmacdonald-stsci Jul 25, 2024
0e3d5f8
Adding a copy of the original GDQ array to the RampData class.
kmacdonald-stsci Jul 25, 2024
7a43c11
Debugging the problems found in a small number of CHARGELOSS recomput…
kmacdonald-stsci Jul 25, 2024
20a4108
Updating the search for CHARGELOSS flags in the group DQ array during…
kmacdonald-stsci Jul 25, 2024
1e1b8e7
Updating the ramp fitting C extension to properly recompute the read …
kmacdonald-stsci Jul 25, 2024
186d2a2
Updated testing to prevent segfault.
kmacdonald-stsci Jul 25, 2024
95589ab
Rebase prep commit.
kmacdonald-stsci Jul 26, 2024
8f56fe6
Removing debugging statements.
kmacdonald-stsci Jul 26, 2024
ed36550
Removing debugging statements.
kmacdonald-stsci Jul 26, 2024
a94568e
Updating the change log.
kmacdonald-stsci Jul 26, 2024
82b90c8
Removing debugging flag so the OLS algorithm can be selected.
kmacdonald-stsci Jul 30, 2024
a00f4cd
Updating the changelog based on code review feedback.
kmacdonald-stsci Aug 12, 2024
dc40a82
Updating the creation of a copy of the original group DQ array to be …
kmacdonald-stsci Aug 12, 2024
6ebd9e0
Adding missing doctring for new method parameter.
kmacdonald-stsci Aug 12, 2024
3b44cd0
Correcting misspelling in comments.
kmacdonald-stsci Aug 12, 2024
d24f865
Updating ramp fitting tests.
kmacdonald-stsci Sep 3, 2024
bd53ce8
Adding PR fragment to changes for the change log.
kmacdonald-stsci Sep 5, 2024
b7dc9a8
Adding CHARGELOSS keyword to DQ flags for testing.
kmacdonald-stsci Sep 6, 2024
e893cf1
Making changes handling CHARGELOSS flagging for RomanCal.
kmacdonald-stsci Sep 9, 2024
2ee4a85
Updating test_cext_chargeloss.
kmacdonald-stsci Sep 10, 2024
d85f859
Merge branch 'main' into jp_3669
tapastro Sep 10, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ __pycache__/
*$py.class
*~

# Temp files
*.*.swp

# C extensions
*.so

Expand Down
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ General

- Add TweakReg submodule. [#267]

ramp_fitting
~~~~~~~~~~~~

- Move the CHARGELOSS read noise variance recalculation from the JWST step
code to the C extension to simplify the code and improve performance.[#275]

zacharyburnett marked this conversation as resolved.
Show resolved Hide resolved
Changes to API
--------------

Expand Down
2 changes: 2 additions & 0 deletions changes/275.general.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ramp_fitting] Moving the read noise recalculation due to CHARGELOSS flagging from
the JWST ramp fit step code into the STCAL ramp fit C-extension.
7 changes: 7 additions & 0 deletions src/stcal/ramp_fitting/ols_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@
data = ramp_data.data
err = ramp_data.err
groupdq = ramp_data.groupdq
orig_gdq = ramp_data.orig_gdq

n_int, ngroups, nrows, ncols = data.shape

Expand Down Expand Up @@ -949,6 +950,8 @@
if num_bad_slices > 0:
data = data[:, num_bad_slices:, :, :]
err = err[:, num_bad_slices:, :, :]
if orig_gdq is not None:
orig_gdq = orig_gdq[:, num_bad_slices:, :, :]

Check warning on line 954 in src/stcal/ramp_fitting/ols_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ols_fit.py#L954

Added line #L954 was not covered by tests

log.info("Number of leading groups that are flagged as DO_NOT_USE: %s", num_bad_slices)

Expand All @@ -968,6 +971,8 @@
data = data[:, :-1, :, :]
err = err[:, :-1, :, :]
groupdq = groupdq[:, :-1, :, :]
if orig_gdq is not None:
orig_gdq = orig_gdq[:, :-1, :, :]

Check warning on line 975 in src/stcal/ramp_fitting/ols_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ols_fit.py#L975

Added line #L975 was not covered by tests

log.info("MIRI dataset has all pixels in the final group flagged as DO_NOT_USE.")

Expand All @@ -981,6 +986,8 @@
ramp_data.data = data
ramp_data.err = err
ramp_data.groupdq = groupdq
if orig_gdq is not None:
ramp_data.orig_gdq = orig_gdq

Check warning on line 990 in src/stcal/ramp_fitting/ols_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ols_fit.py#L990

Added line #L990 was not covered by tests

return True

Expand Down
20 changes: 17 additions & 3 deletions src/stcal/ramp_fitting/ramp_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
BUFSIZE = 1024 * 300000 # 300Mb cache size for data section


def create_ramp_fit_class(model, dqflags=None, suppress_one_group=False):
def create_ramp_fit_class(model, algorithm, dqflags=None, suppress_one_group=False):
"""
Create an internal ramp fit class from a data model.

Expand Down Expand Up @@ -58,11 +58,24 @@
else:
dark_current_array = model.average_dark_current

orig_gdq = None
if algorithm.upper() == "OLS_C":
wh_chargeloss = np.where(np.bitwise_and(model.groupdq.astype(np.uint32), dqflags['CHARGELOSS']))
if len(wh_chargeloss[0]) > 0:
orig_gdq = model.groupdq.copy()
del wh_chargeloss

Check warning on line 66 in src/stcal/ramp_fitting/ramp_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ramp_fit.py#L61-L66

Added lines #L61 - L66 were not covered by tests

if isinstance(model.data, u.Quantity):
ramp_data.set_arrays(model.data.value, model.err.value, model.groupdq,
model.pixeldq, dark_current_array)
else:
ramp_data.set_arrays(model.data, model.err, model.groupdq, model.pixeldq, dark_current_array)
ramp_data.set_arrays(

Check warning on line 72 in src/stcal/ramp_fitting/ramp_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ramp_fit.py#L72

Added line #L72 was not covered by tests
model.data,
model.err,
model.groupdq,
model.pixeldq,
dark_current_array,
orig_gdq)

# Attribute may not be supported by all pipelines. Default is NoneType.
drop_frames1 = model.meta.exposure.drop_frames1 if hasattr(model, "drop_frames1") else None
Expand All @@ -78,6 +91,7 @@
if "zero_frame" in model.meta.exposure and model.meta.exposure.zero_frame:
ramp_data.zeroframe = model.zeroframe

ramp_data.algorithm = algorithm

Check warning on line 94 in src/stcal/ramp_fitting/ramp_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ramp_fit.py#L94

Added line #L94 was not covered by tests
ramp_data.set_dqflags(dqflags)
ramp_data.start_row = 0
ramp_data.num_rows = ramp_data.data.shape[2]
Expand Down Expand Up @@ -170,7 +184,7 @@
# Create an instance of the internal ramp class, using only values needed
# for ramp fitting from the to remove further ramp fitting dependence on
# data models.
ramp_data = create_ramp_fit_class(model, dqflags, suppress_one_group)
ramp_data = create_ramp_fit_class(model, algorithm, dqflags, suppress_one_group)

Check warning on line 187 in src/stcal/ramp_fitting/ramp_fit.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ramp_fit.py#L187

Added line #L187 was not covered by tests

if algorithm.upper() == "OLS_C":
ramp_data.run_c_code = True
Expand Down
28 changes: 27 additions & 1 deletion src/stcal/ramp_fitting/ramp_fit_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
self.pixeldq = None
self.average_dark_current = None

# Needed for CHARGELOSS recomputation
self.orig_gdq = None
self.algorithm = None

# Meta information
self.instrument_name = None

Expand All @@ -25,6 +29,7 @@
self.flags_saturated = None
self.flags_no_gain_val = None
self.flags_unreliable_slope = None
self.flags_chargeloss = None

# ZEROFRAME
self.zframe_mat = None
Expand All @@ -41,13 +46,15 @@

# C code debugging switch.
self.run_c_code = False
self.run_chargeloss = True
# self.run_chargeloss = False

self.one_groups_locs = None # One good group locations.
self.one_groups_time = None # Time to use for one good group ramps.

self.current_integ = -1

def set_arrays(self, data, err, groupdq, pixeldq, average_dark_current):
def set_arrays(self, data, err, groupdq, pixeldq, average_dark_current, orig_gdq=None):
kmacdonald-stsci marked this conversation as resolved.
Show resolved Hide resolved
"""
Set the arrays needed for ramp fitting.

Expand All @@ -72,6 +79,11 @@
average_dark_current : ndarray (float32)
2-D array containing the average dark current. It has
dimensions (nrows, ncols)

orig_gdq : ndarray
4-D array containing a copy of the original group DQ array. Since
the group DQ array can be modified during ramp fitting, this keeps
around the original group DQ flags passed to ramp fitting.
"""
# Get arrays from the data model
self.data = data
Expand All @@ -80,6 +92,8 @@
self.pixeldq = pixeldq
self.average_dark_current = average_dark_current

self.orig_gdq = orig_gdq

def set_meta(self, name, frame_time, group_time, groupgap, nframes, drop_frames1=None):
"""
Set the metainformation needed for ramp fitting.
Expand Down Expand Up @@ -131,6 +145,8 @@
self.flags_saturated = dqflags["SATURATED"]
self.flags_no_gain_val = dqflags["NO_GAIN_VALUE"]
self.flags_unreliable_slope = dqflags["UNRELIABLE_SLOPE"]
if self.algorithm is not None and self.algorithm.upper() == "OLS_C":
self.flags_chargeloss = dqflags["CHARGELOSS"]

Check warning on line 149 in src/stcal/ramp_fitting/ramp_fit_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ramp_fit_class.py#L149

Added line #L149 was not covered by tests

def dbg_print_types(self):
# Arrays from the data model
Expand Down Expand Up @@ -200,6 +216,16 @@
# print(f" err :\n{self.err[:, :, row, col]}")
# print(f" pixeldq :\n{self.pixeldq[row, col]}")

def dbg_print_info(self):
print(" ")
nints, ngroups, nrows, ncols = self.data.shape
for row in range(nrows):
for col in range(ncols):
print("=" * 80)
print(f"**** Pixel ({row}, {col}) ****")
self.dbg_print_pixel_info(row, col)
print("=" * 80)

Check warning on line 227 in src/stcal/ramp_fitting/ramp_fit_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/ramp_fitting/ramp_fit_class.py#L220-L227

Added lines #L220 - L227 were not covered by tests

def dbg_write_ramp_data_pix_pre(self, fname, row, col, fd):
fd.write("def create_ramp_data_pixel():\n")
indent = INDENT
Expand Down
Loading
Loading