Skip to content

Commit

Permalink
Clean up essentially duplicated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed Oct 6, 2023
1 parent d138d6a commit c55eb6c
Showing 1 changed file with 41 additions and 51 deletions.
92 changes: 41 additions & 51 deletions tests/test_jump_cas22.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,75 +323,40 @@ def detector_data(ramp_data):

return resultants, read_noise, read_pattern

@pytest.mark.parametrize("use_jump", [True, False])
def test_fit_ramps_array_outputs(detector_data, use_jump):
"""
Test that the array outputs line up with the dictionary output
"""
resultants, read_noise, read_pattern = detector_data
dq = np.zeros(resultants.shape, dtype=np.int32)

fits, parameters, variances, _ = fit_ramps(
resultants, dq, read_noise, ROMAN_READ_TIME, read_pattern, use_jump=use_jump
)

for fit, par, var in zip(fits, parameters, variances):
assert par[Parameter.intercept] == 0
assert par[Parameter.slope] == fit['average']['slope']

assert var[Variance.read_var] == fit['average']['read_var']
assert var[Variance.poisson_var] == fit['average']['poisson_var']
assert var[Variance.total_var] == np.float32(
fit['average']['read_var'] + fit['average']['poisson_var']
)


@pytest.mark.parametrize("use_jump", [True, False])
def test_fit_ramps_no_dq(detector_data, use_jump):
"""
Test fitting ramps with no dq flags set on data which has no jumps
Since no jumps are simulated in the data, jump detection shouldn't pick
up any jumps.
"""
resultants, read_noise, read_pattern = detector_data
dq = np.zeros(resultants.shape, dtype=np.int32)

fits, *_ = fit_ramps(resultants, dq, read_noise, ROMAN_READ_TIME, read_pattern, use_jump=use_jump)
assert len(fits) == N_PIXELS # sanity check that a fit is output for each pixel

# Check that the chi2 for the resulting fit relative to the assumed flux is ~1
chi2 = 0
for fit in fits:
assert len(fit['fits']) == 1 # only one fit per pixel since no dq/jump

total_var = fit['average']['read_var'] + fit['average']['poisson_var']
chi2 += (fit['average']['slope'] - FLUX)**2 / total_var

chi2 /= N_PIXELS

assert np.abs(chi2 - 1) < CHI2_TOL


@pytest.mark.parametrize("use_jump", [True, False])
def test_fit_ramps_dq(detector_data, use_jump):
@pytest.mark.parametrize("use_dq", [True, False])
def test_fit_ramps(detector_data, use_jump, use_dq):
"""
Test fitting ramps with dq flags set
Test fitting ramps
Since no jumps are simulated in the data, jump detection shouldn't pick
up any jumps.
"""
resultants, read_noise, read_pattern = detector_data
dq = (RNG.uniform(size=resultants.shape) > 1).astype(np.int32)
dq = (
(RNG.uniform(size=resultants.shape) > 1).astype(np.int32) if use_dq else
np.zeros(resultants.shape, dtype=np.int32)
)

# only use okay ramps
# ramps passing the below criterion have at least two adjacent valid reads
# i.e., we can make a measurement from them.
okay = np.sum((dq[1:, :] == 0) & (dq[:-1, :] == 0), axis=0) != 0
assert okay.dtype == bool

# Note that for use_dq = False, okay == True for all ramps, so we perform
# a sanity check that the above criterion is correct
if not use_dq:
assert okay.all()

fits, *_ = fit_ramps(resultants, dq, read_noise, ROMAN_READ_TIME, read_pattern, use_jump=use_jump)
assert len(fits) == N_PIXELS # sanity check that a fit is output for each pixel

chi2 = 0
for fit, use in zip(fits, okay):
if not use_dq:
assert len(fit['fits']) == 1 # only one fit per pixel since no dq/jump in this case

if use:
# Add okay ramps to chi2
total_var = fit['average']['read_var'] + fit['average']['poisson_var']
Expand All @@ -402,10 +367,35 @@ def test_fit_ramps_dq(detector_data, use_jump):
assert fit['average']['read_var'] == 0
assert fit['average']['poisson_var'] == 0

Check warning on line 368 in tests/test_jump_cas22.py

View check run for this annotation

Codecov / codecov/patch

tests/test_jump_cas22.py#L366-L368

Added lines #L366 - L368 were not covered by tests

assert use_dq # sanity check that this branch is only encountered when use_dq = True

Check warning on line 370 in tests/test_jump_cas22.py

View check run for this annotation

Codecov / codecov/patch

tests/test_jump_cas22.py#L370

Added line #L370 was not covered by tests

chi2 /= np.sum(okay)
assert np.abs(chi2 - 1) < CHI2_TOL


@pytest.mark.parametrize("use_jump", [True, False])
def test_fit_ramps_array_outputs(detector_data, use_jump):
"""
Test that the array outputs line up with the dictionary output
"""
resultants, read_noise, read_pattern = detector_data
dq = np.zeros(resultants.shape, dtype=np.int32)

fits, parameters, variances, _ = fit_ramps(
resultants, dq, read_noise, ROMAN_READ_TIME, read_pattern, use_jump=use_jump
)

for fit, par, var in zip(fits, parameters, variances):
assert par[Parameter.intercept] == 0
assert par[Parameter.slope] == fit['average']['slope']

assert var[Variance.read_var] == fit['average']['read_var']
assert var[Variance.poisson_var] == fit['average']['poisson_var']
assert var[Variance.total_var] == np.float32(
fit['average']['read_var'] + fit['average']['poisson_var']
)


@pytest.fixture(scope="module")
def jump_data(detector_data):
"""
Expand Down

0 comments on commit c55eb6c

Please sign in to comment.