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

Renamed the kernel_shape argument of ndfilters.trimmed_mean_filter() to size. #7

Merged
merged 2 commits into from
May 26, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ pip install ndfilters
The only filter currently implemented is a [trimmed mean filter](https://ndfilters.readthedocs.io/en/latest/_autosummary/ndfilters.trimmed_mean_filter.html#ndfilters.trimmed_mean_filter).
This filter ignores a given portion of the dataset before calculating the mean at each pixel.

![trimmed mean filter](https://ndfilters.readthedocs.io/en/latest/_images/ndfilters.trimmed_mean_filter_0_1.png)
![trimmed mean filter](https://ndfilters.readthedocs.io/en/latest/_images/ndfilters.trimmed_mean_filter_0_2.png)
27 changes: 13 additions & 14 deletions ndfilters/_tests/test_trimmed_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
)
@pytest.mark.parametrize(
argnames="kernel_shape",
argnames="size",
argvalues=[2, (3,), (3, 4), (3, 4, 5)],
)
@pytest.mark.parametrize(
Expand All @@ -33,7 +33,7 @@
@pytest.mark.parametrize("proportion", [0.25, 0.45])
def test_trimmed_mean_filter(
array: np.ndarray,
kernel_shape: int | tuple[int, ...],
size: int | tuple[int, ...],
axis: None | int | tuple[int, ...],
proportion: float,
):
Expand All @@ -48,43 +48,42 @@ def test_trimmed_mean_filter(
with pytest.raises(np.AxisError):
ndfilters.trimmed_mean_filter(
array=array,
kernel_shape=kernel_shape,
size=size,
proportion=proportion,
axis=axis,
)
return

kernel_shape_normalized = (
(kernel_shape,) * len(axis_normalized)
if isinstance(kernel_shape, int)
else kernel_shape
)
if isinstance(size, int):
size_normalized = (size,) * len(axis_normalized)
else:
size_normalized = size

if len(kernel_shape_normalized) != len(axis_normalized):
if len(size_normalized) != len(axis_normalized):
with pytest.raises(ValueError):
ndfilters.trimmed_mean_filter(
array=array,
kernel_shape=kernel_shape,
size=size,
proportion=proportion,
axis=axis,
)
return

result = ndfilters.trimmed_mean_filter(
array=array,
kernel_shape=kernel_shape,
size=size,
proportion=proportion,
axis=axis,
)

kernel_shape_scipy = [1] * array.ndim
size_scipy = [1] * array.ndim
for i, ax in enumerate(axis_normalized):
kernel_shape_scipy[ax] = kernel_shape_normalized[i]
size_scipy[ax] = size_normalized[i]

expected = scipy.ndimage.generic_filter(
input=array,
function=scipy.stats.trim_mean,
size=kernel_shape_scipy,
size=size_scipy,
mode="mirror",
extra_keywords=dict(proportiontocut=proportion),
)
Expand Down
36 changes: 18 additions & 18 deletions ndfilters/_trimmed_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def trimmed_mean_filter(
array: np.ndarray,
kernel_shape: int | tuple[int, ...],
size: int | tuple[int, ...],
proportion: float = 0.25,
axis: None | int | tuple[int, ...] = None,
) -> np.ndarray:
Expand All @@ -19,7 +19,7 @@ def trimmed_mean_filter(
----------
array
The input array to be filtered
kernel_shape
size
The shape of the kernel over which the trimmed mean will be calculated.
proportion
The proportion to cut from the top and bottom of the distribution.
Expand All @@ -41,7 +41,7 @@ def trimmed_mean_filter(
import ndfilters

img = scipy.datasets.ascent()
img_filtered = ndfilters.trimmed_mean_filter(img, kernel_shape=21)
img_filtered = ndfilters.trimmed_mean_filter(img, size=21)

fig, axs = plt.subplots(ncols=2, sharex=True, sharey=True)
axs[0].set_title("original image");
Expand All @@ -55,15 +55,15 @@ def trimmed_mean_filter(
axis = tuple(range(array.ndim))
axis = np.core.numeric.normalize_axis_tuple(axis, ndim=array.ndim)

if isinstance(kernel_shape, int):
kernel_shape = (kernel_shape,) * len(axis)
if isinstance(size, int):
size = (size,) * len(axis)
else:
if len(kernel_shape) != len(axis):
if len(size) != len(axis):
raise ValueError(
f"`kernel_shape` should have the same number of elements, "
f"{len(kernel_shape)}, as `axis`, {len(axis)}"
f"`size` should have the same number of elements, "
f"{len(size)}, as `axis`, {len(axis)}"
)
kernel_shape = tuple(np.array(kernel_shape)[np.argsort(axis)])
size = tuple(np.array(size)[np.argsort(axis)])

shape_orthogonal = tuple(
1 if ax in axis else array.shape[ax] for ax in range(array.ndim)
Expand All @@ -81,19 +81,19 @@ def trimmed_mean_filter(
if len(axis) == 1:
result[index] = _mean_trimmed_1d(
array=array[index],
kernel_shape=kernel_shape,
size=size,
proportion=proportion,
)
elif len(axis) == 2:
result[index] = _mean_trimmed_2d(
array=array[index],
kernel_shape=kernel_shape,
size=size,
proportion=proportion,
)
elif len(axis) == 3:
result[index] = _mean_trimmed_3d(
array=array[index],
kernel_shape=kernel_shape,
size=size,
proportion=proportion,
)
else:
Expand All @@ -107,14 +107,14 @@ def trimmed_mean_filter(
@numba.njit(parallel=True)
def _mean_trimmed_1d(
array: np.ndarray,
kernel_shape: tuple[int],
size: tuple[int],
proportion: float,
):
result = np.empty_like(array)

(array_shape_x,) = array.shape

(kernel_shape_x,) = kernel_shape
(kernel_shape_x,) = size
kernel_size = kernel_shape_x

for ix in numba.prange(array_shape_x):
Expand Down Expand Up @@ -164,14 +164,14 @@ def _mean_trimmed_1d(
@numba.njit(parallel=True)
def _mean_trimmed_2d(
array: np.ndarray,
kernel_shape: tuple[int, int],
size: tuple[int, int],
proportion: float,
):
result = np.empty_like(array)

array_shape_x, array_shape_y = array.shape

kernel_shape_x, kernel_shape_y = kernel_shape
kernel_shape_x, kernel_shape_y = size
kernel_size = kernel_shape_x * kernel_shape_y

for ix in numba.prange(array_shape_x):
Expand Down Expand Up @@ -233,14 +233,14 @@ def _mean_trimmed_2d(
@numba.njit(parallel=True)
def _mean_trimmed_3d(
array: np.ndarray,
kernel_shape: tuple[int, int, int],
size: tuple[int, int, int],
proportion: float,
):
result = np.empty_like(array)

array_shape_x, array_shape_y, array_shape_z = array.shape

kernel_shape_x, kernel_shape_y, kernel_shape_z = kernel_shape
kernel_shape_x, kernel_shape_y, kernel_shape_z = size
kernel_size = kernel_shape_x * kernel_shape_y * kernel_shape_z

for ix in numba.prange(array_shape_x):
Expand Down
Loading