Memory efficient reduce functions #8018
-
|
Hi, I'm trying to create memory efficient reduce functions, that can apply a map-reduce without blowing up memory For instance, consider the following example, where I'd like code to function something as follows def _reduce_sum_f(fx, y, f):
"""Reduce the sum of f(x)"""
return fx + f(y)
fg = lambda fx, y: _reduce_sum_f(fx, y, f)
# Create sample data
import xarray as xr
import numpy as np
x_values = np.arange(0, 5)
y_values = np.arange(10, 15)
data_values = np.random.rand(len(x_values), len(y_values))
# Create DataArray objects
x_da = xr.DataArray(x_values, dims='x', name='x')
y_da = xr.DataArray(y_values, dims='y', name='y')
data_da = xr.DataArray(data_values, dims=('x', 'y'), name='data')
# Create the Dataset
dataset = xr.Dataset({'data': data_da}, coords={'x': x_da, 'y': y_da})
# Apply toy reduce function
dataset.reduce(fg, axis=0)Now, this code does not work (in this case, there is an error through for |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
We usually use dask for this kind of thing. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @mortonjt ! If you want to do a common reduction (e.g. If you want to apply a custom function (including a custom reduction) then you need to look at |
Beta Was this translation helpful? Give feedback.
Hi @mortonjt !
If you want to do a common reduction (e.g.
mean) without blowing up memory then xarray will do this automatically if dask is installed, as Deepak says. (You should read our docs page on dask though).If you want to apply a custom function (including a custom reduction) then you need to look at
xarray.apply_ufunc, which can apply custom dask-aware operations.