Skip to content

Commit

Permalink
Testing short ramp that should be switched to OLS_C.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmacdonald-stsci committed Sep 10, 2024
1 parent 905ae93 commit 0ef30be
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 78 deletions.
5 changes: 2 additions & 3 deletions src/stcal/ramp_fitting/likely_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

DELIM = "=" * 80
SQRT2 = 1.41421356
LIKELY_MIN_NGROUPS = 4

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

BUFSIZE = 1024 * 300000 # 300Mb cache size for data section

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

Expand Down Expand Up @@ -58,7 +57,7 @@ def likely_ramp_fit(ramp_data, readnoise_2d, gain_2d):

nints, ngroups, nrows, ncols = ramp_data.data.shape

if ngroups < 4:
if ngroups < LIKELY_MIN_NGROUPS:
raise ValueError("Likelihood fit requires at least 4 groups.")

readtimes = get_readtimes(ramp_data)
Expand Down
16 changes: 11 additions & 5 deletions src/stcal/ramp_fitting/ramp_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ def ramp_fit(
# data models.
ramp_data = create_ramp_fit_class(model, dqflags, suppress_one_group)

if algorithm.upper() == "OLS_C":
ramp_data.run_c_code = True

return ramp_fit_data(
ramp_data, buffsize, save_opt, readnoise_2d, gain_2d, algorithm, weighting, max_cores, dqflags
)
Expand Down Expand Up @@ -248,13 +245,22 @@ def ramp_fit_data(
# For the LIKELY algorithm, due to the jump detection portion of the code
# a minimum of a four group ramp is needed.
ngroups = ramp_data.data.shape[1]
likely_min_ngroups = 4
if algorithm.upper() == "LIKELY" and ngroups < likely_fit.LIKELY_MIN_NGROUPS:
log.info(f"When selecting the LIKELY ramp fitting algorithm the"
" ngroups needs to be a minimum of {likely_fit.LIKELY_MIN_NGROUPS},"
" but ngroups = {ngroups}. Due to this, the ramp fitting algorithm"
" is being changed to OLS_C")
algorithm = "OLS_C"

if algorithm.upper() == "OLS_C":
ramp_data.run_c_code = True

if algorithm.upper() == "GLS":
image_info, integ_info, gls_opt_info = gls_fit.gls_ramp_fit(
ramp_data, buffsize, save_opt, readnoise_2d, gain_2d, max_cores
)
opt_info = None
elif algorithm.upper() == "LIKELY" and ngroups >= likely_min_ngroups:
elif algorithm.upper() == "LIKELY" and ngroups >= likely_fit.LIKELY_MIN_NGROUPS:
image_info, integ_info, opt_info = likely_fit.likely_ramp_fit(
ramp_data, readnoise_2d, gain_2d
)
Expand Down
100 changes: 30 additions & 70 deletions tests/test_ramp_fitting_likely_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,73 +25,6 @@
DELIM = "-" * 70


def setup_inputs(dims, gain, rnoise, group_time, frame_time):
"""
Creates test data for testing. All ramp data is zero.
Parameters
----------
dims: tuple
Four dimensions (nints, ngroups, nrows, ncols)
gain: float
Gain noise
rnoise: float
Read noise
group_time: float
Group time
frame_time: float
Frame time
Return
------
ramp_class: RampClass
A RampClass with all zero data.
gain: ndarray
A 2-D array for gain noise for each pixel.
rnoise: ndarray
A 2-D array for read noise for each pixel.
"""
nints, ngroups, nrows, ncols = dims

ramp_class = ramp_fit_class.RampData() # Create class

# Create zero arrays according to dimensions
data = np.zeros(shape=(nints, ngroups, nrows, ncols), dtype=np.float32)
err = np.ones(shape=(nints, ngroups, nrows, ncols), dtype=np.float32)
groupdq = np.zeros(shape=(nints, ngroups, nrows, ncols), dtype=np.uint8)
pixeldq = np.zeros(shape=(nrows, ncols), dtype=np.uint32)
dark_current = np.zeros(shape=(nrows, ncols), dtype=np.float32)


# Set class arrays
ramp_class.set_arrays(data, err, groupdq, pixeldq, average_dark_current=dark_current)

# Set class meta
ramp_class.set_meta(
name="MIRI",
frame_time=frame_time,
group_time=group_time,
groupgap=0,
nframes=1,
drop_frames1=0,
)

# Set class data quality flags
ramp_class.set_dqflags(test_dq_flags)

# Set noise arrays
gain = np.ones(shape=(nrows, ncols), dtype=np.float64) * gain
rnoise = np.full((nrows, ncols), rnoise, dtype=np.float32)

return ramp_class, gain, rnoise


def create_blank_ramp_data(dims, var, tm):
"""
Create empty RampData classes, as well as gain and read noise arrays,
Expand Down Expand Up @@ -120,8 +53,8 @@ def create_blank_ramp_data(dims, var, tm):
)
ramp_data.set_dqflags(test_dq_flags)

gain = np.ones(shape=(nrows, ncols), dtype=np.float64) * gval
rnoise = np.ones(shape=(nrows, ncols), dtype=np.float64) * rnval
gain = np.ones(shape=(nrows, ncols), dtype=np.float32) * gval
rnoise = np.ones(shape=(nrows, ncols), dtype=np.float32) * rnval

return ramp_data, gain, rnoise

Expand Down Expand Up @@ -494,7 +427,7 @@ def test_short_group_ramp(nframes):
data1 = cube1[0][0, 0, 0]
diff = abs(data - data1)
assert diff < tol
dbg_print_slope_slope1(slopes, slopes1, (0, 0))
# dbg_print_slope_slope1(slopes, slopes1, (0, 0))


def data_small_good_groups():
Expand Down Expand Up @@ -603,6 +536,33 @@ def test_jump_detect():
assert dq[1, 1] == JMP


def test_too_few_groups(caplog):
"""
Ensure
"""
nints, ngroups, nrows, ncols = 1, 3, 1, 1
rnval, gval = 10.0, 5.0
frame_time, nframes, groupgap = 10.736, 4, 1

dims = nints, ngroups, nrows, ncols
var = rnval, gval
tm = frame_time, nframes, groupgap

ramp_data, gain2d, rnoise2d = create_blank_ramp_data(dims, var, tm)

# Create a simple linear ramp.
ramp = np.array(list(range(ngroups))) * 20 + 10
ramp_data.data[0, :, 0, 0] = ramp

save_opt, algo, ncores = False, "LIKELY", "none"
slopes, cube, ols_opt, gls_opt = ramp_fit_data(
ramp_data, 512, save_opt, rnoise2d, gain2d, algo, "optimal", ncores, test_dq_flags
)

expected_log = "ramp fitting algorithm is being changed to OLS_C"
assert expected_log in caplog.text


# -----------------------------------------------------------------
# DEBUG
# -----------------------------------------------------------------
Expand Down

0 comments on commit 0ef30be

Please sign in to comment.