Last active
October 1, 2019 14:27
-
-
Save tinaok/c2ef193e94508a5ba426979d01e99307 to your computer and use it in GitHub Desktop.
automatic chunk size
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import dask.array as da\n", | |
"import numpy as np\n", | |
"import dask\n", | |
"import xarray as xr\n", | |
"import math\n", | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"freq=\"1D\"\n", | |
"dask.config.set({\"array.chunk-size\": '256MB'})\n", | |
"timesteps=20834; lat=320; lon=384\n", | |
"shape=(timesteps,lon,lat)\n", | |
"chunks=(-1,'auto','auto')\n", | |
"lats = xr.DataArray(np.linspace(start=-90, stop=90, num=lat), dims=[\"lat\"])\n", | |
"lons = xr.DataArray(np.linspace(start=-180, stop=180, num=lon), dims=[\"lon\"])\n", | |
"times = xr.DataArray(pd.date_range(start=\"1980-01-01\", freq=freq, periods=timesteps), dims=[\"time\"])\n", | |
"random_data =da.random.RandomState(0).standard_normal(shape, chunks=chunks )\n", | |
"ds = xr.DataArray( random_data, dims=[\"time\", \"lon\", \"lat\"], coords={\"time\": times, \"lon\": lons, \"lat\": lats}, name=\"sst\").to_dataset()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dask.array<standard_normal, shape=(20834, 384, 320), dtype=float64, chunksize=(20834, 32, 32)>" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"random_data" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"When specifying chunk size as 370MB in dask, dask array creates chunksize 187MB." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/Users/todaka/miniconda3/envs/pangeobench/lib/python3.6/site-packages/xarray/core/indexing.py:1195: PerformanceWarning: Slicing with an out-of-order index is generating 58 times more chunks\n", | |
" return self.array[key]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"Frozen(SortedKeysDict({'time': (60, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 90, 92, 92, 91, 91, 92, 92, 91, 45), 'lon': (32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32), 'lat': (32, 32, 32, 32, 32, 32, 32, 32, 32, 32)}))" | |
] | |
}, | |
"execution_count": 51, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"climate= ds.groupby(\"time.season\").mean(dim=\"time\")\n", | |
"anomaly=ds.groupby(\"time.season\") - climate\n", | |
"anomaly.chunks" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Frozen(SortedKeysDict({'season': (1, 1, 1, 1), 'lon': (32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32), 'lat': (32, 32, 32, 32, 32, 32, 32, 32, 32, 32)}))" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"climate.chunks" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "pangeobench", | |
"language": "python", | |
"name": "pangeobench" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment