-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
DataArrayCoarsen does not have a map or reduce function #3741
Comments
👍 that would be super useful! |
coarsen is pretty similar to rolling AFAIR so it may not be too hard to implement a |
As a workaround, it's possible to use rolling and .sel to keep only adjacent windows: ds
<xarray.Dataset>
Dimensions: (x: 237, y: 69, z: 2)
Coordinates:
* x (x) int64 0 1 2 3 4 5 6 7 8 ... 228 229 230 231 232 233 234 235 236
* y (y) int64 0 1 2 3 4 5 6 7 8 9 10 ... 59 60 61 62 63 64 65 66 67 68
* z (z) int64 0 1
Data variables:
data2D (x, y) float64 dask.array<chunksize=(102, 42), meta=np.ndarray>
data3D (x, y, z) float64 dask.array<chunksize=(102, 42, 2), meta=np.ndarray>
# window size
window = {'x' : 51, 'y' : 21}
# window dims, prefixed by 'k_'
window_dims = {k: "k_%s" % k for k in window.keys()}
# dataset, with new dims as window. .sel drop sliding windows, to keep only adjacent ones.
ds_win = ds.rolling(window,center=True).construct(window_dims).sel(
{k: slice(window[k]//2,None,window[k]) for k in window.keys()})
<xarray.Dataset>
Dimensions: (k_x: 51, k_y: 21, x: 5, y: 3, z: 2)
Coordinates:
* x (x) int64 25 76 127 178 229
* y (y) int64 10 31 52
* z (z) int64 0 1
Dimensions without coordinates: k_x, k_y
Data variables:
data2D (x, y, k_x, k_y) float64 dask.array<chunksize=(2, 2, 51, 21), meta=np.ndarray>
data3D (x, y, z, k_x, k_y) float64 dask.array<chunksize=(2, 2, 2, 51, 21), meta=np.ndarray>
# now, use reduce on a standard dataset, using window k_dims as dimensions
ds_red = ds_win.reduce(np.mean,dim=window_dims.values())
<xarray.Dataset>
Dimensions: (x: 5, y: 3, z: 2)
Coordinates:
* x (x) int64 25 76 127 178 229
* y (y) int64 10 31 52
* z (z) int64 0 1
Data variables:
data2D (x, y) float64 dask.array<chunksize=(2, 2), meta=np.ndarray>
data3D (x, y, z) float64 dask.array<chunksize=(2, 2, 2), meta=np.ndarray> Note that i was unable to use |
5 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to count unique samples when resampling to a square kilometre from a 5x5m input grid. I'd like to be able to apply the
Dask.array.unique()
function withreturn_counts=True
to give me a new dimension with the original integer values and their counts.In order to resample along spatial dimensions I assume I need to use
.coarsen()
, unfortunately thecore.rolling.DataArrayCoarsen
object does not yet implement either a.map()
or.reduce()
function for applying an arbitrary function when coarsening.MCVE Code Sample
outputs;
AttributeError: 'DataArrayCoarsen' object has no attribute 'map'
N.B.
core.groupby.DataArrayGroupBy
has both.map()
and.reduce()
whilecore.rolling.DataArrayRolling
has.reduce()
. Would it make sense for all three to have the same interface?Output of
xr.show_versions()
xarray: 0.15.0
pandas: 1.0.0
numpy: 1.18.1
scipy: 1.4.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.2
cfgrib: None
iris: None
bottleneck: None
dask: 2.10.1
distributed: 2.10.0
matplotlib: 3.1.2
cartopy: None
seaborn: None
numbagg: None
setuptools: 40.8.0
pip: 19.0.3
conda: None
pytest: 5.3.5
IPython: 7.11.1
sphinx: None
The text was updated successfully, but these errors were encountered: