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

AL-875: Add memory saving options to compute_weight_threshold sigma_clip call #312

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion src/stcal/outlier_detection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ def compute_weight_threshold(weight, maskpt):
weight_masked = np.ma.array(weight, mask=np.logical_or(
mask_zero_weight, mask_nans))
# Sigma-clip the unmasked data
weight_masked = sigma_clip(weight_masked, sigma=3, maxiters=5)
weight_masked = sigma_clip(weight_masked,
sigma=3,
maxiters=5,
masked=False,
copy=False,
)
mean_weight = np.mean(weight_masked)
# Mask pixels where weight falls below maskpt percent
weight_threshold = mean_weight * maskpt
Expand Down
18 changes: 18 additions & 0 deletions tests/outlier_detection/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
reproject,
medfilt,
)
from stcal.testing_helpers import MemoryThreshold


@pytest.mark.parametrize("shape,diff", [
Expand Down Expand Up @@ -72,6 +73,23 @@ def test_compute_weight_threshold_zeros():
np.testing.assert_allclose(result, 21)


def test_compute_weight_threshold_memory():
"""Test that weight threshold function modifies
the weight array in place"""
arr = np.zeros([500, 500], dtype=np.float32)
arr[:250, :250] = 42
arr[10,10] = 0
arr[-10,-10] = np.nan

# buffer to account for memory overhead needs to be small enough
# to ensure that the array was not copied
fractional_memory_buffer = 1.9
expected_mem = int(arr.nbytes*fractional_memory_buffer)
with MemoryThreshold(str(expected_mem) + " B"):
result = compute_weight_threshold(arr, 0.5)
np.testing.assert_allclose(result, 21)


def test_flag_crs():
sci = np.zeros((10, 10), dtype=np.float32)
err = np.ones_like(sci)
Expand Down
Loading