forked from pytroll/satpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pytroll#2521 from mraspaud/feature_median_filter
Add a median filter modifier
- Loading branch information
Showing
6 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ dependencies: | |
- xarray!=2022.9.0 | ||
- dask | ||
- distributed | ||
- dask-image | ||
- donfig | ||
- appdirs | ||
- toolz | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Tests for image filters.""" | ||
import logging | ||
|
||
import xarray as xr | ||
|
||
from satpy.modifiers import ModifierBase | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Median(ModifierBase): | ||
"""Apply a median filter to the band.""" | ||
|
||
def __init__(self, median_filter_params, **kwargs): | ||
"""Create the instance. | ||
Args: | ||
median_filter_params: The arguments to pass to dask-image's median_filter function. For example, {size: 3} | ||
makes give the median filter a kernel of size 3. | ||
""" | ||
self.median_filter_params = median_filter_params | ||
super().__init__(**kwargs) | ||
|
||
def __call__(self, arrays, **info): | ||
"""Get the median filtered band.""" | ||
from dask_image.ndfilters import median_filter | ||
|
||
data = arrays[0] | ||
logger.debug(f"Apply median filtering with parameters {self.median_filter_params}.") | ||
res = xr.DataArray(median_filter(data.data, **self.median_filter_params), | ||
dims=data.dims, attrs=data.attrs, coords=data.coords) | ||
self.apply_modifier_info(data, res) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Implementation of some image filters.""" | ||
|
||
import logging | ||
|
||
import dask.array as da | ||
import numpy as np | ||
import xarray as xr | ||
|
||
from satpy.modifiers.filters import Median | ||
|
||
|
||
def test_median(caplog): | ||
"""Test the median filter modifier.""" | ||
caplog.set_level(logging.DEBUG) | ||
dims = "y", "x" | ||
coordinates = dict(x=np.arange(6), y=np.arange(6)) | ||
attrs = dict(units="K") | ||
median_filter_params = dict(size=3) | ||
name = "median_filter" | ||
median_filter = Median(median_filter_params, name=name) | ||
array = xr.DataArray(da.arange(36).reshape((6, 6)), coords=coordinates, dims=dims, attrs=attrs) | ||
res = median_filter([array]) | ||
filtered_array = np.array([[1, 2, 3, 4, 5, 5], | ||
[6, 7, 8, 9, 10, 11], | ||
[12, 13, 14, 15, 16, 17], | ||
[18, 19, 20, 21, 22, 23], | ||
[24, 25, 26, 27, 28, 29], | ||
[30, 30, 31, 32, 33, 34]]) | ||
np.testing.assert_allclose(res, filtered_array) | ||
assert res.dims == dims | ||
assert attrs.items() <= res.attrs.items() | ||
assert res.attrs["name"] == name | ||
np.testing.assert_equal(res.coords["x"], coordinates["x"]) | ||
np.testing.assert_equal(res.coords["y"], coordinates["y"]) | ||
assert "Apply median filtering with parameters {'size': 3}" in caplog.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters