Skip to content
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

Xarray and Dask Example #51

Merged
merged 12 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions binder/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ channels:
dependencies:
- python=3
- bokeh=0.13
- dask=0.19.4
- dask=0.20
- dask-ml=0.10.0
- distributed=1.23.3
- distributed=1.24
- jupyterlab=0.35.1
- nodejs=8.9
- numpy
Expand All @@ -16,9 +16,11 @@ dependencies:
- nbserverproxy
- nomkl
- h5py
- xarray
jhamman marked this conversation as resolved.
Show resolved Hide resolved
- bottleneck
- py-xgboost
- pip:
- graphviz
- dask_xgboost
- seaborn
- mimesis
- mimesis
Binary file added images/dataset-diagram-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ You can run these examples in a live session here: |Binder|
delayed
futures
machine-learning
xarray

.. toctree::
:maxdepth: 1
Expand Down
353 changes: 353 additions & 0 deletions xarray.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Xarray with Dask Arrays\n",
"\n",
"<img src=\"images/dataset-diagram-logo.png\" \n",
" align=\"right\"\n",
" width=\"66%\"\n",
" alt=\"Xarray Dataset\">\n",
" \n",
"**[Xarray](http://xarray.pydata.org/en/stable/)** is an open source project and Python package that extends the labeled data functionality of [Pandas](https://pandas.pydata.org/) to N-dimensional array-like datasets. It shares a similar API to [NumPy](http://www.numpy.org/) and [Pandas](https://pandas.pydata.org/) and supports both [Dask](https://dask.org/) and [NumPy](http://www.numpy.org/) arrays under the hood."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"from dask.distributed import Client\n",
"import xarray as xr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start Dask Client for Dashboard\n",
"\n",
"Starting the Dask Client is optional. It will provide a dashboard which \n",
"is useful to gain insight on the computation. \n",
"\n",
"The link to the dashboard will become visible when you create the client below. We recommend having it open on one side of your screen while using your notebook on the other side. This can take some effort to arrange your windows, but seeing them both at the same is very useful when learning."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = Client(n_workers=2, threads_per_worker=2, memory_limit='1GB')\n",
"client"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Open a sample dataset\n",
"\n",
"We will use some of xarray's tutorial data for this example. By specifying the chunk shape, xarray will automatically create Dask arrays for each data variable in the `Dataset`. In xarray, `Datasets` are dict-like container of labeled arrays, analogous to the `pandas.DataFrame`. Note that we're taking advantage of xarray's dimension labels when specifying chunk shapes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = xr.tutorial.open_dataset('air_temperature',\n",
" chunks={'lat': 25, 'lon': 25, 'time': 1000})\n",
jhamman marked this conversation as resolved.
Show resolved Hide resolved
"ds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Quickly inspecting the `Dataset` above, we'll note that this `Dataset` has three _dimensions_ akin to axes in NumPy (`lat`, `lon`, and `time`), three _coordinate variables_ akin to `pandas.Index` objects (also named `lat`, `lon`, and `time`), and one data variable (`air`). Xarray also holds Dataset specific metadata in as _attributes_."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da = ds['air']\n",
"da"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each data variable in xarray is called a `DataArray`. These are the fundemental labeled array object in xarray. Much like the `Dataset`, `DataArrays` also have _dimensions_ and _coordinates_ that support many of its label-based opperations."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da.data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Accessing the underlying array of data is done via the `data` property. Here we can see that we have a Dask array. If this array were to be backed by a NumPy array, this property would point to the actual values in the array."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use Standard Xarray Operations\n",
"\n",
"In almost all cases, operations using xarray objects are identical, regardless if the underlying data is stored as a Dask array or a NumPy array."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da2 = da.groupby('time.month').mean('time')\n",
"da3 = da - da2\n",
"da3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call `.compute()` or `.load()` when you want your result as a `xarray.DataArray` with data stored as NumPy arrays.\n",
"\n",
"If you started `Client()` above then you may want to watch the status page during computation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"computed_da = da3.load()\n",
"type(computed_da.data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"computed_da"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Persist data in memory\n",
"\n",
"If you have the available RAM for your dataset then you can persist data in memory. \n",
"\n",
"This allows future computations to be much faster."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da = da.persist()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Time Series Operations\n",
"\n",
"Because we have a datetime index time-series operations work efficiently. Here we demo the use of xarray's resample method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da.resample(time='1w').mean('time').std('time')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da.resample(time='1w').mean('time').std('time').load().plot(figsize=(12, 8))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and rolling window operations:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da_smooth = da.rolling(time=30).mean().persist()\n",
"da_smooth"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since xarray stores each of its coordinate variables in memory, slicing by label is trivial and entirely lazy."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%time da.sel(time='2013-01-01T18:00:00')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%time da.sel(time='2013-01-01T18:00:00').load()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom workflows and automatic parallelization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Almost all of xarray’s built-in operations work on Dask arrays. If you want to use a function that isn’t wrapped by xarray, one option is to extract Dask arrays from xarray objects (.data) and use Dask directly.\n",
"\n",
"Another option is to use xarray’s `apply_ufunc()` function, which can automate embarrassingly parallel “map” type operations where a functions written for processing NumPy arrays should be repeatedly applied to xarray objects containing Dask arrays. It works similarly to `dask.array.map_blocks()` and `dask.array.atop()`, but without requiring an intermediate layer of abstraction.\n",
"\n",
"Here we show an example using NumPy operations and a fast function from `bottleneck`, which we use to calculate Spearman’s rank-correlation coefficient:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import xarray as xr\n",
"import bottleneck\n",
"\n",
"def covariance_gufunc(x, y):\n",
" return ((x - x.mean(axis=-1, keepdims=True))\n",
" * (y - y.mean(axis=-1, keepdims=True))).mean(axis=-1)\n",
"\n",
"def pearson_correlation_gufunc(x, y):\n",
" return covariance_gufunc(x, y) / (x.std(axis=-1) * y.std(axis=-1))\n",
"\n",
"def spearman_correlation_gufunc(x, y):\n",
" x_ranks = bottleneck.rankdata(x, axis=-1)\n",
" y_ranks = bottleneck.rankdata(y, axis=-1)\n",
" return pearson_correlation_gufunc(x_ranks, y_ranks)\n",
"\n",
"def spearman_correlation(x, y, dim):\n",
" return xr.apply_ufunc(\n",
" spearman_correlation_gufunc, x, y,\n",
" input_core_dims=[[dim], [dim]],\n",
" dask='parallelized',\n",
" output_dtypes=[float])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the examples above, we were working with an some air temperature data. For this example, we'll calculate the spearman correlation using the raw air temperature data with the smoothed version that we also created (`da_smooth`). For this, we'll also have to rechunk the data ahead of time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"corr = spearman_correlation(da.chunk({'time': -1}),\n",
" da_smooth.chunk({'time': -1}),\n",
" 'time')\n",
"corr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"corr.plot(figsize=(12, 8))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}