Skip to content

Commit

Permalink
Add Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mwregan2 committed Dec 27, 2023
1 parent b6114da commit 563a0f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/stcal/ramp_fitting/ols_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,8 +1156,7 @@ def ramp_fit_compute_variances(ramp_data, gain_2d, readnoise_2d, fit_slopes_ans,
# Suppress harmless arithmetic warnings for now
warnings.filterwarnings("ignore", ".*invalid value.*", RuntimeWarning)
warnings.filterwarnings("ignore", ".*divide by zero.*", RuntimeWarning)
var_p4[num_int, :, rlo:rhi, :] = den_p3 * (med_rates[rlo:rhi, :] +
avg_dark_current)
var_p4[num_int, :, rlo:rhi, :] = (den_p3 * med_rates[rlo:rhi, :]) + avg_dark_current

# Find the segment variance due to read noise and convert back to DN
var_r4[num_int, :, rlo:rhi, :] = num_r3 * den_r3 / gain_sect**2
Expand Down Expand Up @@ -1197,7 +1196,10 @@ def ramp_fit_compute_variances(ramp_data, gain_2d, readnoise_2d, fit_slopes_ans,
var_p3[:, med_rates <= 0.0] = 0.0
warnings.resetwarnings()

var_p4[num_int, :, med_rates <= 0.0] = 0.0
# For pixels with zero or negative rates set the Poisson variance to the
# dark current rate.
var_p4[num_int, :, med_rates <= 0.0] = avg_dark_current

var_both4[num_int, :, :, :] = var_r4[num_int, :, :, :] + var_p4[num_int, :, :, :]
inv_var_both4[num_int, :, :, :] = 1.0 / var_both4[num_int, :, :, :]

Expand Down
23 changes: 23 additions & 0 deletions tests/test_ramp_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from stcal.ramp_fitting.ramp_fit import ramp_fit_data
from stcal.ramp_fitting.ramp_fit_class import RampData
from stcal.ramp_fitting.utils import compute_num_slices
from stcal.ramp_fitting.ols_fit import ramp_fit_compute_variances

DELIM = "=" * 70

Expand Down Expand Up @@ -1466,6 +1467,28 @@ def test_compute_num_slices():
n_rows = 9
assert compute_num_slices("21", n_rows, max_available_cores) == 9

def test_ramp_fit_dark_current_variance():
nints, ngroups, nrows, ncols = 1, 5, 2, 2
rnval, gval = 10.0, 5.0
frame_time, nframes, groupgap = 10, 1, 0
var = rnval, gval
tm = frame_time, nframes, groupgap
dims = nints, ngroups, nrows, ncols
ramp, gain, rnoise = create_blank_ramp_data(dims, var, tm)
#Test for zero dark rate
avg_dark_current = 0.0
med_rates = np.zeros(shape=(2, 2))
fit_slopes_ans = (1, 0, 0, 0, 0, 1, 0, 0, 0, med_rates)
var_p3, var_r3, var_p4, var_r4, var_both4, var_both3, inv_var_both4, s_inv_var_p3, \
s_inv_var_r3, s_inv_var_both3 = ramp_fit_compute_variances(ramp, gain, rnoise, fit_slopes_ans, avg_dark_current)

assert(var_p4[0, 0, 0, 0] == 0.0)

#Test for a dark rate of 1.5 e-/sec
avg_dark_current = 1.5
var_p3, var_r3, var_p4, var_r4, var_both4, var_both3, inv_var_both4, s_inv_var_p3, \
s_inv_var_r3, s_inv_var_both3 = ramp_fit_compute_variances(ramp, gain, rnoise, fit_slopes_ans, avg_dark_current)
assert(var_p4[0, 0, 0, 0] == 1.5)

# -----------------------------------------------------------------------------
# Set up functions
Expand Down

0 comments on commit 563a0f1

Please sign in to comment.