Getting xarray.apply_ufunc
and np.linalg.pinv
to work with dask.array
#7503
-
I'm having trouble parallelizing import xarray as xr
import numpy as np
from numpy.linalg import pinv
import dask
data = np.random.randn(4, 4, 3, 2)
da = xr.DataArray(data=data, dims=("x", "y", "i", "j"),)
da = da.chunk(x=1, y=1)
da_inv = xr.apply_ufunc(pinv, da,
input_core_dims=[["i", "j"]],
output_core_dims=[["i", "j"]],
exclude_dims=set(("i", "j")),
dask = "parallelized",
) This throws me this error: Traceback (most recent call last):
File "/glade/scratch/tomasc/tracer_inversion2/mwe.py", line 14, in <module>
da_inv = xr.apply_ufunc(pinv, da,
File "/glade/u/home/tomasc/miniconda3/envs/py310/lib/python3.10/site-packages/xarray/core/computation.py", line 1204, in apply_ufunc
return apply_dataarray_vfunc(
File "/glade/u/home/tomasc/miniconda3/envs/py310/lib/python3.10/site-packages/xarray/core/computation.py", line 315, in apply_dataarray_vfunc
result_var = func(*data_vars)
File "/glade/u/home/tomasc/miniconda3/envs/py310/lib/python3.10/site-packages/xarray/core/computation.py", line 771, in apply_variable_ufunc
result_data = func(*input_data)
File "/glade/u/home/tomasc/miniconda3/envs/py310/lib/python3.10/site-packages/xarray/core/computation.py", line 747, in func
res = da.apply_gufunc(
File "/glade/u/home/tomasc/miniconda3/envs/py310/lib/python3.10/site-packages/dask/array/gufunc.py", line 489, in apply_gufunc
core_output_shape = tuple(core_shapes[d] for d in ocd)
File "/glade/u/home/tomasc/miniconda3/envs/py310/lib/python3.10/site-packages/dask/array/gufunc.py", line 489, in <genexpr>
core_output_shape = tuple(core_shapes[d] for d in ocd)
KeyError: 'dim0' When using data_inv = dask.array.map_blocks(pinv, da.data).compute() # works! So I guess I must be missing something simple here? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You were almost there, you just needed to add the sizes of new the output dimensions by including the kwarg dask_gufunc_kwargs={'output_sizes': {'i': 2, 'j': 3}} It does sort of say this in the docstring for That's a very unhelpful error, but it's ultimately being thrown because the keys |
Beta Was this translation helpful? Give feedback.
You were almost there, you just needed to add the sizes of new the output dimensions by including the kwarg
It does sort of say this in the docstring for
apply_ufunc
but it could definitely be clearer!That's a very unhelpful error, but it's ultimately being thrown because the keys
'i'
and'j'
don't exist in the dict of expected sizes of the output (because you didn't provide them). We should improve that error!