Skip to content

Commit 8b96c2d

Browse files
committed
Update: filtering of DIC standard uncertainty
1 parent f7545e5 commit 8b96c2d

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

kaleidoscope/algorithms/filter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
from numbers import Real
9+
from typing import Literal
910

1011
from dask import array as da
1112
from dask_image.ndfilters import gaussian_filter
@@ -69,14 +70,20 @@ class Median(Algorithm):
6970

7071
@override
7172
def apply_to(
72-
self, data: da.Array, *, dims: tuple[str, ...], size: int = 3
73+
self,
74+
data: da.Array,
75+
*,
76+
dims: tuple[str, ...],
77+
size: int = 3,
78+
mode: Literal["constant", "wrap"] = "constant",
7379
) -> da.Array:
7480
"""
7581
Applies a lateral median filter to data.
7682
7783
:param data: The data.
7884
:param dims: The dimension names.
7985
:param size: The size of the uniform kernel (pixels).
86+
:param mode: How to extend the image.
8087
:return: The filtered data.
8188
"""
8289
sizes = [
@@ -86,8 +93,8 @@ def apply_to(
8693
m = da.isnan(data)
8794
v = da.where(m, 0.0, data)
8895
w = da.where(m, 0.0, 1.0)
89-
v = median_filter(v, sizes, mode="constant")
90-
w = median_filter(w, sizes, mode="constant")
96+
v = median_filter(v, sizes, mode=mode)
97+
w = median_filter(w, sizes, mode=mode)
9198
return da.where(m, data, v / w)
9299

93100
@property

kaleidoscope/config/config.collect.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
"long_name": "standard uncertainty of Dissolved Inorganic Carbon in seawater"
1010
},
1111
"filter": {
12-
"kind": "Gaussian",
13-
"fwhm": 1.0
12+
"kind": "median",
13+
"size": 3,
14+
"mode": "wrap"
1415
}
1516
},
1617
"DOC": {
1718
"attrs": {
1819
"long_name": "standard uncertainty of estimated DOC"
1920
},
2021
"filter": {
21-
"kind": "Gaussian",
22+
"kind": "gaussian",
2223
"fwhm": 4.0
2324
}
2425
},
@@ -27,7 +28,7 @@
2728
"long_name": "standard uncertainty of PIC"
2829
},
2930
"filter": {
30-
"kind": "Gaussian",
31+
"kind": "gaussian",
3132
"fwhm": 4.0
3233
}
3334
},
@@ -36,7 +37,7 @@
3637
"long_name": "standard uncertainty of POC"
3738
},
3839
"filter": {
39-
"kind": "Gaussian",
40+
"kind": "gaussian",
4041
"fwhm": 4.0
4142
}
4243
},
@@ -45,7 +46,7 @@
4546
"long_name": "standard uncertainty of Phytoplankton Carbon concentration in seawater"
4647
},
4748
"filter": {
48-
"kind": "Gaussian",
49+
"kind": "gaussian",
4950
"fwhm": 4.0
5051
}
5152
},
@@ -54,7 +55,7 @@
5455
"long_name": "standard uncertainty of Phytoplankton Primary Production"
5556
},
5657
"filter": {
57-
"kind": "Gaussian",
58+
"kind": "gaussian",
5859
"fwhm": 4.0
5960
}
6061
},
@@ -64,7 +65,7 @@
6465
"description": "standard uncertainty of Predicted DOC values"
6566
},
6667
"filter": {
67-
"kind": "Gaussian",
68+
"kind": "gaussian",
6869
"fwhm": 4.0
6970
}
7071
}

kaleidoscope/operators/collectop.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from ..algorithms.codec import decode
1818
from ..algorithms.codec import encode
1919
from ..algorithms.filter import Gaussian
20+
from ..algorithms.filter import Median
21+
from ..algorithms.filter import Uniform
2022
from ..interface.logging import Logging
2123
from ..interface.operator import Operator
2224
from ..logger import get_logger
@@ -28,9 +30,27 @@ def _filter(x: da.Array, dims: tuple, filter_config: dict) -> da.Array:
2830
fluctuations caused by finite sampling of error probability
2931
density functions.
3032
"""
31-
return Gaussian(dtype=x.dtype, m=x.ndim, n=x.ndim).apply_to(
32-
x, dims=dims, fwhm=filter_config.get("fwhm", 1.0)
33-
)
33+
match filter_config.get("kind", "_"):
34+
case "gaussian":
35+
return Gaussian(dtype=x.dtype, m=x.ndim, n=x.ndim).apply_to(
36+
x, dims=dims, fwhm=filter_config.get("fwhm", 4.0)
37+
)
38+
case "median":
39+
# noinspection PyTypeChecker
40+
return Median(dtype=x.dtype, m=x.ndim, n=x.ndim).apply_to(
41+
x,
42+
dims=dims,
43+
size=filter_config.get("size", 3),
44+
mode=filter_config.get("mode", "constant"),
45+
)
46+
case "uniform":
47+
return Uniform(dtype=x.dtype, m=x.ndim, n=x.ndim).apply_to(
48+
x, dims=dims, size=filter_config.get("size", 3)
49+
)
50+
case _:
51+
return Uniform(dtype=x.dtype, m=x.ndim, n=x.ndim).apply_to(
52+
x, dims=dims, size=3
53+
)
3454

3555

3656
def _std(
@@ -50,7 +70,7 @@ def _std(
5070
"""
5171
return da.sqrt(
5272
_filter(_mse(x[1:] - x[:1]), dims, filter_config)
53-
if filtered
73+
if filtered and filter_config
5474
else _mse(x[1:] - x[:1])
5575
)
5676

0 commit comments

Comments
 (0)