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-3448: Always flag at least one group when showers are detected #237

Merged
merged 42 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
06387dd
test
mwregan2 Dec 13, 2023
88adb73
fix conflicts
mwregan2 Dec 14, 2023
7a9ceb0
Update jump.py
mwregan2 Dec 14, 2023
4bcd446
Update jump.py
mwregan2 Dec 14, 2023
183305b
Update jump.py
mwregan2 Dec 14, 2023
5d8d6d8
tests2
mwregan2 Dec 14, 2023
c099e12
Update jump.py
mwregan2 Dec 14, 2023
a7f70a4
changes
mwregan2 Dec 14, 2023
5a5db94
Update jump.py
mwregan2 Dec 14, 2023
5ddb6c8
tests
mwregan2 Dec 14, 2023
4569b11
fixes
mwregan2 Dec 14, 2023
f5fed78
changes
mwregan2 Dec 14, 2023
1251f17
Update jump.py
mwregan2 Dec 14, 2023
affff19
Update jump.py
mwregan2 Dec 14, 2023
0fd250a
Update jump.py
mwregan2 Dec 14, 2023
22dab7e
Update jump.py
mwregan2 Dec 14, 2023
0617d35
Update jump.py
mwregan2 Dec 14, 2023
6806a7c
Update jump.py
mwregan2 Dec 14, 2023
5008db3
add tests
mwregan2 Dec 28, 2023
d7f1513
Update test_jump.py
mwregan2 Dec 28, 2023
1d8105b
clean up
mwregan2 Dec 28, 2023
541ceed
Update test_jump.py
mwregan2 Dec 28, 2023
93d3b56
Update test_twopoint_difference.py
mwregan2 Dec 28, 2023
07badf3
Update CHANGES.rst
mwregan2 Dec 28, 2023
28bff55
Update jump.py
mwregan2 Jan 5, 2024
b0f06b8
Update CHANGES.rst
mwregan2 Jan 26, 2024
4f1baf1
Update CHANGES.rst
mwregan2 Jan 26, 2024
7205fdb
Update jump.py
mwregan2 Jan 26, 2024
08ad69e
Merge branch 'test_gdq' of https://github.com/mwregan2/stcal into alw…
mwregan2 Jan 26, 2024
5b15a24
Update src/stcal/jump/jump.py
hbushouse Jan 31, 2024
e4017c3
Update tests/test_jump.py
hbushouse Jan 31, 2024
a3cbe27
Update tests/test_jump.py
hbushouse Jan 31, 2024
e69926a
Update src/stcal/jump/jump.py
hbushouse Jan 31, 2024
07186c4
Update src/stcal/jump/jump.py
hbushouse Jan 31, 2024
304a4b0
Update src/stcal/jump/jump.py
hbushouse Jan 31, 2024
d4b7e0f
Update test_jump.py
mwregan2 Jan 31, 2024
89d7ef5
fixes
mwregan2 Jan 31, 2024
f50aaad
Update test_jump.py
mwregan2 Jan 31, 2024
26274df
Update jump.py
mwregan2 Jan 31, 2024
9ec7676
Update test_jump.py
mwregan2 Jan 31, 2024
037966e
fix num_grps_masked for snowballs
mwregan2 Jan 31, 2024
a62076e
Merge branch 'main' into test_gdq
hbushouse Feb 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
1.5.3 (unreleased)
==================

-
jump
----
- Fix the code to at least always flag the group with the shower and the requested
groups after the primary shower. [#237]

1.5.2 (2023-12-13)
==================
Expand Down
56 changes: 41 additions & 15 deletions src/stcal/jump/jump.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import multiprocessing
import time

import cv2 as cv
import numpy as np
from astropy import stats
from astropy.convolution import Ring2DKernel, convolve
import cv2 as cv
import astropy.stats as stats

from astropy.convolution import Ring2DKernel
from astropy.convolution import convolve


from . import constants
from . import twopoint_difference as twopt
Expand Down Expand Up @@ -471,7 +473,6 @@ def detect_jumps(
data /= gain_2d
err /= gain_2d
readnoise_2d /= gain_2d

# Return the updated data quality arrays
return gdq, pdq, total_primary_crs, number_extended_events, stddev

Expand Down Expand Up @@ -631,7 +632,8 @@ def extend_ellipses(
# For a given DQ plane it will use the list of ellipses to create
# expanded ellipses of pixels with
# the jump flag set.
plane = gdq_cube[intg, grp, :, :]
out_gdq_cube = gdq_cube.copy()
plane = gdq_cube[intg, grp, :, :].copy()
ncols = plane.shape[1]
nrows = plane.shape[0]
image = np.zeros(shape=(nrows, ncols, 3), dtype=np.uint8)
Expand Down Expand Up @@ -671,15 +673,38 @@ def extend_ellipses(
)
jump_ellipse = image[:, :, 2]
ngrps = gdq_cube.shape[1]
last_grp = min(grp + num_grps_masked, ngrps)
last_grp = find_last_grp(grp, ngrps, num_grps_masked)
# This loop will flag the number of groups
for flg_grp in range(grp, last_grp):
sat_pix = np.bitwise_and(gdq_cube[intg, flg_grp, :, :], sat_flag)
saty, satx = np.where(sat_pix == sat_flag)
jump_ellipse[saty, satx] = 0
gdq_cube[intg, flg_grp, :, :] = np.bitwise_or(gdq_cube[intg, flg_grp, :, :], jump_ellipse)
return gdq_cube, num_ellipses
out_gdq_cube[intg, flg_grp, :, :] = np.bitwise_or(gdq_cube[intg, flg_grp, :, :], jump_ellipse)
diff_cube = out_gdq_cube - gdq_cube
return out_gdq_cube, num_ellipses

def find_last_grp(grp, ngrps, num_grps_masked_after):
hbushouse marked this conversation as resolved.
Show resolved Hide resolved
"""
Parameters
----------
grp : int
The location of the shower

ngrps : int
The number of groups in the integration

num_grps_masked_after : int
hbushouse marked this conversation as resolved.
Show resolved Hide resolved
The requested number of groups to be flagged after the shower

Returns
-------
last_grp : int
The index of the last group to flag for the shower

"""
num_grps_masked_after += 1
last_grp = min(grp + num_grps_masked_after, ngrps)
hbushouse marked this conversation as resolved.
Show resolved Hide resolved
return last_grp

def find_circles(dqplane, bitmask, min_area):
# Using an input DQ plane this routine will find the groups of pixels with at least the minimum
Expand Down Expand Up @@ -834,7 +859,7 @@ def find_faint_extended(
Total number of showers detected.

"""
read_noise_2 = readnoise_2d**2
read_noise_2d_sqr = readnoise_2d**2
data = indata.copy()
data[gdq == sat_flag] = np.nan
data[gdq == 1] = np.nan
Expand All @@ -849,12 +874,11 @@ def find_faint_extended(
# calculate sigma for each pixel
if nints <= minimum_sigclip_groups:
median_diffs = np.nanmedian(first_diffs_masked[intg], axis=0)
sigma = np.sqrt(np.abs(median_diffs) + read_noise_2 / nframes)
sigma = np.sqrt(np.abs(median_diffs) + read_noise_2d_sqr / nframes)
# The difference from the median difference for each group
e_jump = first_diffs_masked[intg] - median_diffs[np.newaxis, :, :]
# SNR ratio of each diff.
ratio = np.abs(e_jump) / sigma[np.newaxis, :, :]

# The convolution kernel creation
ring_2D_kernel = Ring2DKernel(inner, outer)
ngrps = data.shape[1]
Expand All @@ -881,14 +905,14 @@ def find_faint_extended(
extended_emission = np.zeros(shape=(nrows, ncols), dtype=np.uint8)
exty, extx = np.where(masked_smoothed_ratio > snr_threshold)
extended_emission[exty, extx] = 1

# find the contours of the extended emission
contours, hierarchy = cv.findContours(extended_emission, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# get the contours that are above the minimum size
bigcontours = [con for con in contours if cv.contourArea(con) > min_shower_area]
# get the minimum enclosing rectangle which is the same as the
# minimum enclosing ellipse
ellipses = [cv.minAreaRect(con) for con in bigcontours]

expand_by_ratio = True
expansion = 1.0
plane = gdq[intg, grp, :, :]
Expand Down Expand Up @@ -938,10 +962,12 @@ def find_faint_extended(
# This is deferred until all showers are detected. because the showers
# can flag future groups and would confuse the detection algorithm if
# we worked on groups that already had some flagged showers.
total_showers = 0
for showers in all_ellipses:
intg = showers[0]
grp = showers[1]
ellipses = showers[2]
total_showers += len(ellipses)
gdq, num = extend_ellipses(
gdq,
intg,
Expand All @@ -952,9 +978,9 @@ def find_faint_extended(
expansion=ellipse_expand,
expand_by_ratio=True,
num_grps_masked=num_grps_masked,
max_extended_radius=max_extended_radius,
max_extended_radius=max_extended_radius
)
return gdq, len(all_ellipses)
return gdq, total_showers


def calc_num_slices(n_rows, max_cores, max_available):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_jump.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
find_faint_extended,
flag_large_events,
point_inside_ellipse,
find_last_grp
)

DQFLAGS = {"JUMP_DET": 4, "SATURATED": 2, "DO_NOT_USE": 1, "GOOD": 0, "NO_GAIN_VALUE": 8}
Expand Down Expand Up @@ -352,3 +353,15 @@
assert calc_num_slices(n_rows, "3/4", max_available_cores) == 1
n_rows = 9
assert calc_num_slices(n_rows, "21", max_available_cores) == 9


def test_find_last_grp():
assert (find_last_grp(grp=5, ngrps=7, num_grps_masked=0) == 6)
assert (find_last_grp(grp=5, ngrps=7, num_grps_masked=2) == 7)
assert (find_last_grp(grp=5, ngrps=7, num_grps_masked=3) == 7)
assert (find_last_grp(grp=5, ngrps=6, num_grps_masked=1) == 6)
assert (find_last_grp(grp=5, ngrps=6, num_grps_masked=0) == 6)
assert (find_last_grp(grp=5, ngrps=6, num_grps_masked=2) == 6)
assert (find_last_grp(grp=5, ngrps=8, num_grps_masked=0) == 6)
assert (find_last_grp(grp=5, ngrps=8, num_grps_masked=1) == 7)
assert (find_last_grp(grp=5, ngrps=8, num_grps_masked=2) == 8)

Check warning on line 367 in tests/test_jump.py

View check run for this annotation

Codecov / codecov/patch

tests/test_jump.py#L360-L367

Added lines #L360 - L367 were not covered by tests
hbushouse marked this conversation as resolved.
Show resolved Hide resolved