Created
February 27, 2020 19:11
-
-
Save bradyrx/4b55dc8587333d721e8477ce4afb0a69 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Bootstrap and Compute Metric\n", | |
"\n", | |
"---\n", | |
"\n", | |
"Here we bootstrap with replacement members from a historical ensemble and compare the new ensemble mean to observations (e.g. through a pearson r correlation)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import xarray as xr\n", | |
"import numpy as np\n", | |
"%load_ext line_profiler\n", | |
"%matplotlib inline\n", | |
"\n", | |
"import xskillscore as xs\n", | |
"\n", | |
"from climpred.prediction import compute_hindcast\n", | |
"import warnings\n", | |
"warnings.filterwarnings(\"ignore\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# from dask.distributed import Client\n", | |
"# from ncar_jobqueue import NCARCluster\n", | |
"# cluster = NCARCluster(cores=36, memory=\"100GB\", walltime=\"00:45:00\", processes=36) # Change this to SLURMCluster if on DAV/Casper\n", | |
"# cluster.scale(36*1)\n", | |
"# client = Client(cluster)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Create Dummy Data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nlead = 10\n", | |
"nmember = 10\n", | |
"\n", | |
"init = np.arange(1950, 2000)\n", | |
"lead = np.arange(nlead) + 1\n", | |
"member = np.arange(nmember) + 1\n", | |
"time = np.arange(1900, 2020)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"hindcast = xr.DataArray(np.random.rand(len(init), nlead, nmember),\n", | |
" dims=['init', 'lead', 'member'],\n", | |
" coords=[init, lead, member]).rename('SST').to_dataset()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"historical = xr.DataArray(np.random.rand(len(time), nmember),\n", | |
" dims=['time', 'member'],\n", | |
" coords=[time, member]).rename('SST').to_dataset()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"observations = xr.DataArray(np.random.rand(30), dims=['time'], coords=[np.arange(1980, 2010)]).rename('SST').to_dataset()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"hindcast = hindcast.chunk()\n", | |
"historical = historical.chunk()\n", | |
"observations = observations.chunk()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table>\n", | |
"<tr>\n", | |
"<td>\n", | |
"<table>\n", | |
" <thead>\n", | |
" <tr><td> </td><th> Array </th><th> Chunk </th></tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr><th> Bytes </th><td> 40.00 kB </td> <td> 40.00 kB </td></tr>\n", | |
" <tr><th> Shape </th><td> (50, 10, 10) </td> <td> (50, 10, 10) </td></tr>\n", | |
" <tr><th> Count </th><td> 1 Tasks </td><td> 1 Chunks </td></tr>\n", | |
" <tr><th> Type </th><td> float64 </td><td> numpy.ndarray </td></tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</td>\n", | |
"<td>\n", | |
"<svg width=\"172\" height=\"162\" style=\"stroke:rgb(0,0,0);stroke-width:1\" >\n", | |
"\n", | |
" <!-- Horizontal lines -->\n", | |
" <line x1=\"10\" y1=\"0\" x2=\"80\" y2=\"70\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"10\" y1=\"42\" x2=\"80\" y2=\"112\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Vertical lines -->\n", | |
" <line x1=\"10\" y1=\"0\" x2=\"10\" y2=\"42\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"80\" y1=\"70\" x2=\"80\" y2=\"112\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Colored Rectangle -->\n", | |
" <polygon points=\"10.000000,0.000000 80.588235,70.588235 80.588235,112.598126 10.000000,42.009890\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n", | |
"\n", | |
" <!-- Horizontal lines -->\n", | |
" <line x1=\"10\" y1=\"0\" x2=\"52\" y2=\"0\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"80\" y1=\"70\" x2=\"122\" y2=\"70\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Vertical lines -->\n", | |
" <line x1=\"10\" y1=\"0\" x2=\"80\" y2=\"70\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"52\" y1=\"0\" x2=\"122\" y2=\"70\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Colored Rectangle -->\n", | |
" <polygon points=\"10.000000,0.000000 52.009890,0.000000 122.598126,70.588235 80.588235,70.588235\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n", | |
"\n", | |
" <!-- Horizontal lines -->\n", | |
" <line x1=\"80\" y1=\"70\" x2=\"122\" y2=\"70\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"80\" y1=\"112\" x2=\"122\" y2=\"112\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Vertical lines -->\n", | |
" <line x1=\"80\" y1=\"70\" x2=\"80\" y2=\"112\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"122\" y1=\"70\" x2=\"122\" y2=\"112\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Colored Rectangle -->\n", | |
" <polygon points=\"80.588235,70.588235 122.598126,70.588235 122.598126,112.598126 80.588235,112.598126\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n", | |
"\n", | |
" <!-- Text -->\n", | |
" <text x=\"101.593180\" y=\"132.598126\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" >10</text>\n", | |
" <text x=\"142.598126\" y=\"91.593180\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(0,142.598126,91.593180)\">10</text>\n", | |
" <text x=\"35.294118\" y=\"97.304008\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(45,35.294118,97.304008)\">50</text>\n", | |
"</svg>\n", | |
"</td>\n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"dask.array<xarray-SST, shape=(50, 10, 10), dtype=float64, chunksize=(50, 10, 10), chunktype=numpy.ndarray>" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"hindcast.SST.data" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Old Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def resample(hind, hist):\n", | |
" \"\"\"\n", | |
" Resample with a replacement a historical simulation along the ensemble member axis.\n", | |
" \n", | |
" Args:\n", | |
" hind (xarray object): Initialized hindcast simulation.\n", | |
" hist (xarray object): Historical simulation with multiple ensemble members.\n", | |
" \"\"\"\n", | |
" nlead = hind.lead.size\n", | |
" nmember = hind.member.size\n", | |
"\n", | |
" # Resample members with replacement.\n", | |
" random_members = np.random.choice(hist.member.values, hind.member.size)\n", | |
" hist = hist.sel(member=random_members)\n", | |
" \n", | |
" resampled_hist = xr.concat(\n", | |
" [hist.sel(time=hind['init'] + lead) for lead in hind.lead.values], \n", | |
" 'lead',\n", | |
" coords='minimal', compat='override', data_vars='minimal')\n", | |
" del resampled_hist['time']\n", | |
" resampled_hist['member'] = hind['member']\n", | |
" resampled_hist['lead'] = hind['lead']\n", | |
" return resampled_hist" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def bootstrap(hind, hist, verif, N):\n", | |
" uninit_skill = []\n", | |
" for i in range(N):\n", | |
" uninit_rs = resample(hind, hist)\n", | |
" skill = compute_hindcast(uninit_rs, verif, metric='acc')\n", | |
" uninit_skill.append(skill)\n", | |
" return xr.concat(uninit_skill, 'bootstrap')\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Timer unit: 1e-06 s\n", | |
"\n", | |
"Total time: 0.215581 s\n", | |
"File: <ipython-input-8-3a536d8ea0b7>\n", | |
"Function: resample at line 1\n", | |
"\n", | |
"Line # Hits Time Per Hit % Time Line Contents\n", | |
"==============================================================\n", | |
" 1 def resample(hind, hist):\n", | |
" 2 \"\"\"\n", | |
" 3 Resample with a replacement a historical simulation along the ensemble member axis.\n", | |
" 4 \n", | |
" 5 Args:\n", | |
" 6 hind (xarray object): Initialized hindcast simulation.\n", | |
" 7 hist (xarray object): Historical simulation with multiple ensemble members.\n", | |
" 8 \"\"\"\n", | |
" 9 5 1762.0 352.4 0.8 nlead = hind.lead.size\n", | |
" 10 5 1573.0 314.6 0.7 nmember = hind.member.size\n", | |
" 11 \n", | |
" 12 # Resample members with replacement.\n", | |
" 13 5 22306.0 4461.2 10.3 random_members = np.random.choice(hist.member.values, hind.member.size)\n", | |
" 14 5 13278.0 2655.6 6.2 hist = hist.sel(member=random_members)\n", | |
" 15 \n", | |
" 16 5 17.0 3.4 0.0 resampled_hist = xr.concat(\n", | |
" 17 5 133815.0 26763.0 62.1 [hist.sel(time=hind['init'] + lead) for lead in hind.lead.values], \n", | |
" 18 5 13.0 2.6 0.0 'lead',\n", | |
" 19 5 31639.0 6327.8 14.7 coords='minimal', compat='override', data_vars='minimal')\n", | |
" 20 5 148.0 29.6 0.1 del resampled_hist['time']\n", | |
" 21 5 5065.0 1013.0 2.3 resampled_hist['member'] = hind['member']\n", | |
" 22 5 5957.0 1191.4 2.8 resampled_hist['lead'] = hind['lead']\n", | |
" 23 5 8.0 1.6 0.0 return resampled_hist" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"%lprun -f resample bootstrap(hindcast, historical, observations, 5).compute()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.92 s, sys: 61.9 ms, total: 1.99 s\n", | |
"Wall time: 1.98 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%time graph = bootstrap(hindcast, historical, observations, 10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table>\n", | |
"<tr>\n", | |
"<td>\n", | |
"<table>\n", | |
" <thead>\n", | |
" <tr><td> </td><th> Array </th><th> Chunk </th></tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr><th> Bytes </th><td> 800 B </td> <td> 8 B </td></tr>\n", | |
" <tr><th> Shape </th><td> (10, 10) </td> <td> (1, 1) </td></tr>\n", | |
" <tr><th> Count </th><td> 619 Tasks </td><td> 100 Chunks </td></tr>\n", | |
" <tr><th> Type </th><td> float64 </td><td> numpy.ndarray </td></tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</td>\n", | |
"<td>\n", | |
"<svg width=\"170\" height=\"170\" style=\"stroke:rgb(0,0,0);stroke-width:1\" >\n", | |
"\n", | |
" <!-- Horizontal lines -->\n", | |
" <line x1=\"0\" y1=\"0\" x2=\"120\" y2=\"0\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"0\" y1=\"12\" x2=\"120\" y2=\"12\" />\n", | |
" <line x1=\"0\" y1=\"24\" x2=\"120\" y2=\"24\" />\n", | |
" <line x1=\"0\" y1=\"36\" x2=\"120\" y2=\"36\" />\n", | |
" <line x1=\"0\" y1=\"48\" x2=\"120\" y2=\"48\" />\n", | |
" <line x1=\"0\" y1=\"60\" x2=\"120\" y2=\"60\" />\n", | |
" <line x1=\"0\" y1=\"72\" x2=\"120\" y2=\"72\" />\n", | |
" <line x1=\"0\" y1=\"84\" x2=\"120\" y2=\"84\" />\n", | |
" <line x1=\"0\" y1=\"96\" x2=\"120\" y2=\"96\" />\n", | |
" <line x1=\"0\" y1=\"108\" x2=\"120\" y2=\"108\" />\n", | |
" <line x1=\"0\" y1=\"120\" x2=\"120\" y2=\"120\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Vertical lines -->\n", | |
" <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"120\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"12\" y1=\"0\" x2=\"12\" y2=\"120\" />\n", | |
" <line x1=\"24\" y1=\"0\" x2=\"24\" y2=\"120\" />\n", | |
" <line x1=\"36\" y1=\"0\" x2=\"36\" y2=\"120\" />\n", | |
" <line x1=\"48\" y1=\"0\" x2=\"48\" y2=\"120\" />\n", | |
" <line x1=\"60\" y1=\"0\" x2=\"60\" y2=\"120\" />\n", | |
" <line x1=\"72\" y1=\"0\" x2=\"72\" y2=\"120\" />\n", | |
" <line x1=\"84\" y1=\"0\" x2=\"84\" y2=\"120\" />\n", | |
" <line x1=\"96\" y1=\"0\" x2=\"96\" y2=\"120\" />\n", | |
" <line x1=\"108\" y1=\"0\" x2=\"108\" y2=\"120\" />\n", | |
" <line x1=\"120\" y1=\"0\" x2=\"120\" y2=\"120\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Colored Rectangle -->\n", | |
" <polygon points=\"0.000000,0.000000 120.000000,0.000000 120.000000,120.000000 0.000000,120.000000\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n", | |
"\n", | |
" <!-- Text -->\n", | |
" <text x=\"60.000000\" y=\"140.000000\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" >10</text>\n", | |
" <text x=\"140.000000\" y=\"60.000000\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(0,140.000000,60.000000)\">10</text>\n", | |
"</svg>\n", | |
"</td>\n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"dask.array<concatenate, shape=(10, 10), dtype=float64, chunksize=(1, 1), chunktype=numpy.ndarray>" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"graph.SST.data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 95.1 ms, sys: 11.9 ms, total: 107 ms\n", | |
"Wall time: 97.3 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<pre><xarray.Dataset>\n", | |
"Dimensions: (bootstrap: 10, lead: 10)\n", | |
"Coordinates:\n", | |
" * lead (lead) int64 1 2 3 4 5 6 7 8 9 10\n", | |
"Dimensions without coordinates: bootstrap\n", | |
"Data variables:\n", | |
" SST (bootstrap, lead) float64 0.07966 0.222 0.203 ... -0.1767 0.1989\n", | |
"Attributes:\n", | |
" prediction_skill: calculated by climpred https://climpred.re...\n", | |
" skill_calculated_by_function: compute_hindcast\n", | |
" number_of_initializations: 50\n", | |
" number_of_members: 10\n", | |
" metric: pearson_r\n", | |
" comparison: e2o\n", | |
" dim: time\n", | |
" units: None\n", | |
" created: 2020-02-27 12:10:35</pre>" | |
], | |
"text/plain": [ | |
"<xarray.Dataset>\n", | |
"Dimensions: (bootstrap: 10, lead: 10)\n", | |
"Coordinates:\n", | |
" * lead (lead) int64 1 2 3 4 5 6 7 8 9 10\n", | |
"Dimensions without coordinates: bootstrap\n", | |
"Data variables:\n", | |
" SST (bootstrap, lead) float64 0.07966 0.222 0.203 ... -0.1767 0.1989\n", | |
"Attributes:\n", | |
" prediction_skill: calculated by climpred https://climpred.re...\n", | |
" skill_calculated_by_function: compute_hindcast\n", | |
" number_of_initializations: 50\n", | |
" number_of_members: 10\n", | |
" metric: pearson_r\n", | |
" comparison: e2o\n", | |
" dim: time\n", | |
" units: None\n", | |
" created: 2020-02-27 12:10:35" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%time graph.compute()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# New Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def process(hind, hist, obs, lead):\n", | |
" verif_window = hind['init'] + lead\n", | |
" hist_verif = hist.sel(time=verif_window.values)\n", | |
" obs_verif = obs.sel(time=verif_window.values)\n", | |
" return xs.pearson_r(hist_verif, obs_verif, 'time')\n", | |
"\n", | |
"def resample_metric(hind, hist, obs):\n", | |
" \"\"\"\n", | |
" hind : initialized hindcast\n", | |
" hist : historical run with ensemble members\n", | |
" obs : verification product\n", | |
" \"\"\"\n", | |
" nlags = hind.lead.size\n", | |
" \n", | |
" # Common inits\n", | |
" imin = max(hind.init.min(), obs.time.min())\n", | |
" verif_dates = obs.time - nlags\n", | |
" imax = min(hind.init.max(), verif_dates.max())\n", | |
" \n", | |
" # Slice down hindcast\n", | |
" hind = hind.sel(init=slice(imin, imax))\n", | |
" \n", | |
" # Choose random members\n", | |
" random_members = np.random.choice(hist.member.values, hind.member.size)\n", | |
" hist_rs = hist.sel(member=random_members)\n", | |
" \n", | |
" # Assume we're doing deterministic metric.\n", | |
" hist_rs = hist_rs.mean('member')\n", | |
"\n", | |
" uninit_skill = xr.concat(\n", | |
" [process(hind, hist_rs, obs, lead) for lead in hind.lead.values],\n", | |
" 'lead',\n", | |
" coords='minimal', compat='override'\n", | |
" )\n", | |
" return uninit_skill" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def bootstrap(hind, hist, obs, N):\n", | |
" return xr.concat([resample_metric(hind, hist, obs) for _ in range(N)], 'bootstrap',\n", | |
" coords='minimal', compat='override',)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Timer unit: 1e-06 s\n", | |
"\n", | |
"Total time: 0.448864 s\n", | |
"File: <ipython-input-14-c02423364fb7>\n", | |
"Function: resample_metric at line 7\n", | |
"\n", | |
"Line # Hits Time Per Hit % Time Line Contents\n", | |
"==============================================================\n", | |
" 7 def resample_metric(hind, hist, obs):\n", | |
" 8 \"\"\"\n", | |
" 9 hind : initialized hindcast\n", | |
" 10 hist : historical run with ensemble members\n", | |
" 11 obs : verification product\n", | |
" 12 \"\"\"\n", | |
" 13 5 1990.0 398.0 0.4 nlags = hind.lead.size\n", | |
" 14 \n", | |
" 15 # Common inits\n", | |
" 16 5 5074.0 1014.8 1.1 imin = max(hind.init.min(), obs.time.min())\n", | |
" 17 5 1479.0 295.8 0.3 verif_dates = obs.time - nlags\n", | |
" 18 5 3874.0 774.8 0.9 imax = min(hind.init.max(), verif_dates.max())\n", | |
" 19 \n", | |
" 20 # Slice down hindcast\n", | |
" 21 5 9990.0 1998.0 2.2 hind = hind.sel(init=slice(imin, imax))\n", | |
" 22 \n", | |
" 23 # Choose random members\n", | |
" 24 5 3419.0 683.8 0.8 random_members = np.random.choice(hist.member.values, hind.member.size)\n", | |
" 25 5 10157.0 2031.4 2.3 hist_rs = hist.sel(member=random_members)\n", | |
" 26 \n", | |
" 27 # Assume we're doing deterministic metric.\n", | |
" 28 5 17126.0 3425.2 3.8 hist_rs = hist_rs.mean('member')\n", | |
" 29 \n", | |
" 30 5 18.0 3.6 0.0 uninit_skill = xr.concat(\n", | |
" 31 5 352401.0 70480.2 78.5 [process(hind, hist_rs, obs, lead) for lead in hind.lead.values],\n", | |
" 32 5 24.0 4.8 0.0 'lead',\n", | |
" 33 5 43302.0 8660.4 9.6 coords='minimal', compat='override'\n", | |
" 34 )\n", | |
" 35 5 10.0 2.0 0.0 return uninit_skill" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"%lprun -f resample_metric bootstrap(hindcast, historical, observations, 5).compute()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 473 ms, sys: 11.3 ms, total: 484 ms\n", | |
"Wall time: 478 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%time graph = bootstrap(hindcast, historical, observations, 10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table>\n", | |
"<tr>\n", | |
"<td>\n", | |
"<table>\n", | |
" <thead>\n", | |
" <tr><td> </td><th> Array </th><th> Chunk </th></tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr><th> Bytes </th><td> 800 B </td> <td> 8 B </td></tr>\n", | |
" <tr><th> Shape </th><td> (10, 10) </td> <td> (1, 1) </td></tr>\n", | |
" <tr><th> Count </th><td> 643 Tasks </td><td> 100 Chunks </td></tr>\n", | |
" <tr><th> Type </th><td> float64 </td><td> numpy.ndarray </td></tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</td>\n", | |
"<td>\n", | |
"<svg width=\"170\" height=\"170\" style=\"stroke:rgb(0,0,0);stroke-width:1\" >\n", | |
"\n", | |
" <!-- Horizontal lines -->\n", | |
" <line x1=\"0\" y1=\"0\" x2=\"120\" y2=\"0\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"0\" y1=\"12\" x2=\"120\" y2=\"12\" />\n", | |
" <line x1=\"0\" y1=\"24\" x2=\"120\" y2=\"24\" />\n", | |
" <line x1=\"0\" y1=\"36\" x2=\"120\" y2=\"36\" />\n", | |
" <line x1=\"0\" y1=\"48\" x2=\"120\" y2=\"48\" />\n", | |
" <line x1=\"0\" y1=\"60\" x2=\"120\" y2=\"60\" />\n", | |
" <line x1=\"0\" y1=\"72\" x2=\"120\" y2=\"72\" />\n", | |
" <line x1=\"0\" y1=\"84\" x2=\"120\" y2=\"84\" />\n", | |
" <line x1=\"0\" y1=\"96\" x2=\"120\" y2=\"96\" />\n", | |
" <line x1=\"0\" y1=\"108\" x2=\"120\" y2=\"108\" />\n", | |
" <line x1=\"0\" y1=\"120\" x2=\"120\" y2=\"120\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Vertical lines -->\n", | |
" <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"120\" style=\"stroke-width:2\" />\n", | |
" <line x1=\"12\" y1=\"0\" x2=\"12\" y2=\"120\" />\n", | |
" <line x1=\"24\" y1=\"0\" x2=\"24\" y2=\"120\" />\n", | |
" <line x1=\"36\" y1=\"0\" x2=\"36\" y2=\"120\" />\n", | |
" <line x1=\"48\" y1=\"0\" x2=\"48\" y2=\"120\" />\n", | |
" <line x1=\"60\" y1=\"0\" x2=\"60\" y2=\"120\" />\n", | |
" <line x1=\"72\" y1=\"0\" x2=\"72\" y2=\"120\" />\n", | |
" <line x1=\"84\" y1=\"0\" x2=\"84\" y2=\"120\" />\n", | |
" <line x1=\"96\" y1=\"0\" x2=\"96\" y2=\"120\" />\n", | |
" <line x1=\"108\" y1=\"0\" x2=\"108\" y2=\"120\" />\n", | |
" <line x1=\"120\" y1=\"0\" x2=\"120\" y2=\"120\" style=\"stroke-width:2\" />\n", | |
"\n", | |
" <!-- Colored Rectangle -->\n", | |
" <polygon points=\"0.000000,0.000000 120.000000,0.000000 120.000000,120.000000 0.000000,120.000000\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n", | |
"\n", | |
" <!-- Text -->\n", | |
" <text x=\"60.000000\" y=\"140.000000\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" >10</text>\n", | |
" <text x=\"140.000000\" y=\"60.000000\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(0,140.000000,60.000000)\">10</text>\n", | |
"</svg>\n", | |
"</td>\n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"dask.array<concatenate, shape=(10, 10), dtype=float64, chunksize=(1, 1), chunktype=numpy.ndarray>" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"graph.SST.data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 92.6 ms, sys: 13.8 ms, total: 106 ms\n", | |
"Wall time: 101 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<pre><xarray.Dataset>\n", | |
"Dimensions: (bootstrap: 10, lead: 10)\n", | |
"Dimensions without coordinates: bootstrap, lead\n", | |
"Data variables:\n", | |
" SST (bootstrap, lead) float64 0.1173 0.1912 0.2171 ... 0.3657 0.3371</pre>" | |
], | |
"text/plain": [ | |
"<xarray.Dataset>\n", | |
"Dimensions: (bootstrap: 10, lead: 10)\n", | |
"Dimensions without coordinates: bootstrap, lead\n", | |
"Data variables:\n", | |
" SST (bootstrap, lead) float64 0.1173 0.1912 0.2171 ... 0.3657 0.3371" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%time graph.compute()" | |
] | |
} | |
], | |
"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.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment