Skip to content

Commit

Permalink
more tests and some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Jul 8, 2024
1 parent 5fd363e commit d1955f9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
Empty file.
95 changes: 95 additions & 0 deletions tests/outlier_detection/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import warnings

import pytest
import numpy as np
import scipy.signal

from stcal.outlier_detection.utils import (
_abs_deriv,
compute_weight_threshold,
medfilt,
)


@pytest.mark.parametrize("shape,diff", [
([5, 5], 100),
([7, 7], 200),
])
def test_abs_deriv(shape, diff):
arr = np.zeros(shape)

Check warning on line 19 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L19

Added line #L19 was not covered by tests
# put diff at the center
np.put(arr, arr.size // 2, diff)

Check warning on line 21 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L21

Added line #L21 was not covered by tests
# since abs_deriv with a single non-zero value is the same as a
# convolution with a 3x3 cross kernel use it to test the result
expected = scipy.signal.convolve2d(arr, [[0, 1, 0], [1, 1, 1], [0, 1, 0]], mode='same')
result = _abs_deriv(arr)
np.testing.assert_allclose(result, expected)

Check warning on line 26 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L24-L26

Added lines #L24 - L26 were not covered by tests


@pytest.mark.parametrize("shape,mean,maskpt,expected", [
([5, 5], 11, 0.5, 5.5),
([5, 5], 11, 0.25, 2.75),
([3, 3, 3], 17, 0.5, 8.5),
])
def test_compute_weight_threshold(shape, mean, maskpt, expected):
arr = np.ones(shape, dtype=np.float32) * mean
result = compute_weight_threshold(arr, maskpt)
np.testing.assert_allclose(result, expected)

Check warning on line 37 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L35-L37

Added lines #L35 - L37 were not covered by tests


def test_compute_weight_threshold_outlier():
"""
Test that a large outlier doesn't bias the threshold
"""
arr = np.ones([7, 7, 7], dtype=np.float32) * 42
arr[3, 3] = 9000
result = compute_weight_threshold(arr, 0.5)
np.testing.assert_allclose(result, 21)

Check warning on line 47 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L44-L47

Added lines #L44 - L47 were not covered by tests


def test_compute_weight_threshold_zeros():
"""
Test that zeros are ignored
"""
arr = np.zeros([10, 10], dtype=np.float32)
arr[:5, :5] = 42
result = compute_weight_threshold(arr, 0.5)
np.testing.assert_allclose(result, 21)

Check warning on line 57 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L54-L57

Added lines #L54 - L57 were not covered by tests


@pytest.mark.parametrize("shape,kern_size", [
([7, 7], [3, 3]),
([7, 7], [3, 1]),
([7, 7], [1, 3]),
([7, 5], [3, 3]),
([5, 7], [3, 3]),
([42, 42], [7, 7]),
([42, 42], [7, 5]),
([42, 42], [5, 7]),
([42, 7, 5], [3, 3, 3]),
([5, 7, 42], [5, 5, 5]),
])
def test_medfilt_against_scipy(shape, kern_size):
arr = np.arange(np.prod(shape), dtype='uint32').reshape(shape)
result = medfilt(arr, kern_size)

Check warning on line 74 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L73-L74

Added lines #L73 - L74 were not covered by tests

# The use of scipy.signal.medfilt is ok here ONLY because the
# input has no nans. See the medfilt docstring
expected = scipy.signal.medfilt(arr, kern_size)

Check warning on line 78 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L78

Added line #L78 was not covered by tests

np.testing.assert_allclose(result, expected)

Check warning on line 80 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L80

Added line #L80 was not covered by tests


@pytest.mark.parametrize("arr,kern_size,expected", [
([2, np.nan, 0], [3], [1, 1, 0]),
([np.nan, np.nan, np.nan], [3], [0, np.nan, 0]),
])
def test_medfilt_nan(arr, kern_size, expected):
with warnings.catch_warnings():
warnings.filterwarnings(

Check warning on line 89 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L88-L89

Added lines #L88 - L89 were not covered by tests
"ignore",
message="All-NaN slice",
category=RuntimeWarning
)
result = medfilt(arr, kern_size)
np.testing.assert_allclose(result, expected)

Check warning on line 95 in tests/outlier_detection/utils.py

View check run for this annotation

Codecov / codecov/patch

tests/outlier_detection/utils.py#L94-L95

Added lines #L94 - L95 were not covered by tests

0 comments on commit d1955f9

Please sign in to comment.