Skip to content

Instantly share code, notes, and snippets.

@kuchaale
Created February 23, 2017 16:52
Show Gist options
  • Save kuchaale/9a55b868c68ffdb93d569cb5fd0c9a54 to your computer and use it in GitHub Desktop.
Save kuchaale/9a55b868c68ffdb93d569cb5fd0c9a54 to your computer and use it in GitHub Desktop.
Testing the possibility of xarray dot product of two DataArrays along their shared dims.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-02-23T17:51:37.373000",
"end_time": "2017-02-23T17:51:37.388000"
},
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "import xarray as xr\nimport numpy as np\nimport xarray.ufuncs as xrf",
"execution_count": 172,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-02-23T17:51:39.039000",
"end_time": "2017-02-23T17:51:39.046000"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "da_vals = np.arange(6 * 5 * 4 * 3).reshape((6, 5, 4, 3))\nda = xr.DataArray(da_vals, dims=['x', 'y', 'z', 't'])\ndm_vals = np.arange(3)\ndm = xr.DataArray(dm_vals, dims=['t'])",
"execution_count": 173,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-02-23T17:13:35.176000",
"end_time": "2017-02-23T17:13:35.187000"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.cov(da_vals[1,0,:] , dm_vals)",
"execution_count": 139,
"outputs": [
{
"execution_count": 139,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1.66666667, 1.66666667],\n [ 1.66666667, 1.66666667]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-02-23T17:10:35.706000",
"end_time": "2017-02-23T17:10:35.718000"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.corrcoef(da_vals[0,0,:] , dm_vals)",
"execution_count": 123,
"outputs": [
{
"execution_count": 123,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1., 1.],\n [ 1., 1.]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-02-23T17:22:09.766000",
"end_time": "2017-02-23T17:22:09.773000"
},
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "def normalize(da, dim):\n da_norm = da- da.mean(dim)\n da_norm = da_norm/da.std(dim)\n return da_norm",
"execution_count": 151,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-02-23T17:51:54.791000",
"end_time": "2017-02-23T17:51:54.824000"
},
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "dim = 't'\ncor = normalize(da, dim).dot(normalize(dm, dim))/(da[dim].shape[0])#/(da.std(dim)*dm.std(dim))\ncor",
"execution_count": 174,
"outputs": [
{
"execution_count": 174,
"output_type": "execute_result",
"data": {
"text/plain": "<xarray.DataArray (x: 6, y: 5, z: 4)>\narray([[[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]]])\nDimensions without coordinates: x, y, z"
},
"metadata": {}
}
]
}
],
"metadata": {
"kernelspec": {
"name": "conda-root-py",
"display_name": "Python [conda root]",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12",
"file_extension": ".py",
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
},
"anaconda-cloud": {},
"gist": {
"id": "",
"data": {
"description": "Testing the possibility of xarray dot product of two DataArrays along their shared dims.",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment