-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* xarray wrapper for `gaussian_kernel` * get the xarray API for gaussian to work properly * make the `reshape` calls compatible with `dask` * manually create the grid object * partially allow dask arrays * attach coordinates to the kernel's dimensions * refactor `gaussian_kernel` to outsource the computation of weights * compute explicitly * configure `ruff` to disallow relative imports * move `hc.kernels.xarray` to `hc.xarray.kernels` * basic implementation of `convolve` for `Dataset` objects * docstring for `convolve` * expose `convolve` and the kernels module in the top-level `xarray` module * use the accessor to fetch the grid info object * move the rolling mean kernel into the function itself * rename the tested module to `np_kernels` * return the new cell ids from the kernel functions * use the cell ids returned by the low-level kernel function as input cell ids * check that the kernels are constructed properly with the xarray API * fix the cell ids * check that full-sphere convolution kernels still work * add back the extended deadline, `numba` might take a while to compile * support rings greater than 127 Not sure if we ever would need those, though. * manually construct the `DataArray` that wraps the sparse matrix * make `dim` a keyword-only parameter * add padding parameters to `convolve` * pad the input if it is not a global map * add a simple wrapper for the padding function * properly use the padding function * refactor * attach the ring to the kernel's metadata * actually get the padding to work * `pre-commit` * expose `pad`
- Loading branch information
Showing
12 changed files
with
430 additions
and
51 deletions.
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
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
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,3 @@ | ||
from healpix_convolution.xarray import kernels # noqa: F401 | ||
from healpix_convolution.xarray.convolution import convolve # noqa: F401 | ||
from healpix_convolution.xarray.padding import pad # noqa: F401 |
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,67 @@ | ||
import xarray as xr | ||
|
||
from healpix_convolution.xarray.padding import pad | ||
|
||
|
||
def convolve( | ||
ds, kernel, *, dim="cells", mode: str = "constant", constant_value: int | float = 0 | ||
): | ||
"""convolve data on a DGGS grid | ||
Parameters | ||
---------- | ||
ds : xarray.Dataset | ||
The input data. Must be compatible with `xdggs`. | ||
kernel : xarray.DataArray | ||
The sparse kernel matrix. | ||
dim : str, default: "cells" | ||
The input dimension name. Will also be the output dimension name. | ||
mode : str, default: "constant" | ||
Mode used to pad the array before convolving. Only used for regional maps. See | ||
:py:func:`pad` for a list of available modes. | ||
constant_values : int or float, default: 0 | ||
The constant value to pad with when mode is ``"constant"``. | ||
Returns | ||
------- | ||
convolved : xarray.Dataset | ||
The result of the convolution. | ||
See Also | ||
-------- | ||
healpix_convolution.xarray.pad | ||
healpix_convolution.pad | ||
""" | ||
if ds.chunksizes: | ||
kernel = kernel.chunk() | ||
|
||
def _convolve(arr, weights): | ||
src_dims = ["input_cells"] | ||
|
||
if not set(src_dims).issubset(arr.dims): | ||
return arr | ||
|
||
return xr.dot( | ||
# drop all input coords, as those would most likely be broadcast | ||
arr.variable, | ||
weights, | ||
# This dimension will be "contracted" | ||
# or summed over after multiplying by the weights | ||
dims=src_dims, | ||
) | ||
|
||
if ds.sizes["cells"] != kernel.sizes["input_cells"]: | ||
padder = pad( | ||
ds["cell_ids"], | ||
ring=kernel.attrs["ring"], | ||
mode=mode, | ||
constant_value=constant_value, | ||
) | ||
ds = padder.apply(ds) | ||
|
||
return ( | ||
ds.rename_dims({dim: "input_cells"}) | ||
.map(_convolve, weights=kernel) | ||
.rename_dims({"output_cells": dim}) | ||
.rename_vars({"output_cell_ids": "cell_ids"}) | ||
) |
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 @@ | ||
from healpix_convolution.xarray.kernels.gaussian import gaussian_kernel # noqa: F401 |
Oops, something went wrong.