Skip to content

Commit

Permalink
Fix weighting of convolve
Browse files Browse the repository at this point in the history
  • Loading branch information
stefraynaud committed Jun 27, 2024
1 parent f9a3a92 commit 8515ddd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Deprecations

Bug fixes
---------
- Fix weighting of :func:`xoa.filter.convolve`.
- Fix :func:`xoa.grid.to_rect` raising of errors and handling of missing data in coordinates.
- Fix string formatting in :func:`xoa.filter.tidal_filter` warning.

Expand Down
25 changes: 14 additions & 11 deletions xoa/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
3,
1,
],
"godin":[
"godin": [
1,
3,
6,
Expand Down Expand Up @@ -172,7 +172,7 @@
3,
1,
],
"mean":[
"mean": [
1,
1,
1,
Expand All @@ -198,8 +198,7 @@
1,
1,
1,
]

],
}


Expand Down Expand Up @@ -538,7 +537,11 @@ def generate_kernel(
if isotropic is True:
isotropic = data.dims
elif not set(isotropic).issubset(data.dims):
raise XoaError("invalid dimensions for isotropic keyword")
raise XoaError(
"Invalid dimensions for isotropic keyword: {}".format(
set(isotropic) - set(data.dims)
)
)

# Split into consistant isotropic and orthogonal kernels
isokernels_sizes = {}
Expand All @@ -562,7 +565,7 @@ def generate_kernel(
and not np.isscalar(isokernel)
and not np.allclose(kn, isokernel)
):
raise XoaError("Inconsistant 1d kernels for building " "isotropic kernel")
raise XoaError("Inconsistant 1d kernels for building isotropic kernel")
else:
isokernel = kn
size = kn if np.isscalar(kn) else len(kn)
Expand Down Expand Up @@ -604,15 +607,15 @@ def generate_kernel(
elif isinstance(kernel, np.ndarray):
if kernel.ndim > data.ndim:
raise XoaError(
"too many dimensions for your numpy kernel: {} > {}".format(kernel.dim, data.ndim)
"Too many dimensions for your numpy kernel: {} > {}".format(kernel.dim, data.ndim)
)
dims = data.dims[-kernel.ndim :]

# Data array
if not isinstance(kernel, xr.DataArray):
kernel = xr.DataArray(kernel, dims=dims)
elif not set(kernel.dims).issubset(set(data.dims)):
raise XoaError(f"kernel dimensions {kernel.dims} " f"are not a subset of {data.dims}")
raise XoaError(f"Kernel dimensions {kernel.dims} are not a subset of {data.dims}")

# Finalize
kernel = kernel.astype(data.dtype)
Expand Down Expand Up @@ -684,7 +687,7 @@ def _convolve_(data, kernel, normalize, na_thres, axis=None):

# Convolutions
cdata = convolve_func(data, kernel, cval=0.0, **kwc)
weights = convolve_func((~bad).astype('i'), kernel, cval=0, **kwc)
weights = convolve_func((~bad).astype('d'), kernel, cval=0, **kwc)
# weights = np.clip(weights, 0, kernel.sum())

# Weigthing and masking
Expand Down Expand Up @@ -854,7 +857,7 @@ def erode_mask(data, until=1, kernel=None):
else:
mask = until
if not set(mask.dims).issubset(data.dims):
raise XoaError('mask dims must be a subset of data dims')
raise XoaError('Mask dims must be a subset of data dims')
mask = xcoords.transpose(mask, data, mode="compat")

# Filter
Expand Down Expand Up @@ -915,7 +918,7 @@ def erode_coast(data, until=1, kernel=None, xdim=None, ydim=None):
if isinstance(kernel, xr.DataArray):
assert xdim in kernel.dims, f"kernel must have dimension: {xdim}"
assert ydim in kernel.dims, f"kernel must have dimension: {ydim}"
elif kernel is None or kernel == "shapiro":
elif kernel is None or (isinstance(kernel, str) and kernel == "shapiro"):
kernel = shapiro_kernel((ydim, xdim))

# Mask array
Expand Down

0 comments on commit 8515ddd

Please sign in to comment.