Created
July 4, 2018 22:04
-
-
Save bradyrx/49fe11507dd7de055fa2604707b569f0 to your computer and use it in GitHub Desktop.
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
""" | |
Test script for parallelization with groupby() objects in xarray. | |
""" | |
import numpy as np | |
import xarray as xr | |
from scipy import stats | |
def linear_regression(x): | |
t = range(len(x)) | |
m, *_ = stats.linregress(t, x) | |
return xr.DataArray(m) | |
def regression_ufunc(x): | |
return xr.core.computation.apply_ufunc(linear_regression, x, | |
dask='parallelize', | |
input_core_dims=[['time']], | |
output_dtypes=[float]) | |
def main(): | |
# Create climate-like data | |
data = np.random.randn(100, 180,3 60) | |
lat = np.arange(-89.5, 90, 1) | |
lon = np.arange(0.5, 360, 1) | |
time = np.arange(0,100,1) | |
ds = xr.DataArray(data, coords=[time, lat, lon], | |
dims=['time', 'lat', 'lon']) | |
# Apply without parallelization | |
grouped = ds.stack(points=['lat', 'lon']).groupby('points') | |
m1 = grouped.apply(linear_regression).unstack('points') | |
# Attempt at parallelization | |
grouped = ds.stack(points=['lat','lon']).groupby('points') | |
m2 = regression_ufunc(grouped).unstack('points') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tested your example and made some modifications on the parallelized calculation. Let me know if you have any question.