-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
3,072 additions
and
35 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+61.5 KB
docs/auto_examples/1eof/images/thumb/sphx_glr_plot_bootstrap_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n# Significance teting of EOF analysis via bootstrap\n\nTesting the significance of individual modes and obtain confidence intervals\nfor both EOFs and PCs.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Load packages and data:\nimport numpy as np\nimport xarray as xr\nimport matplotlib.pyplot as plt\nfrom matplotlib.gridspec import GridSpec\nfrom cartopy.crs import Orthographic, PlateCarree\n\nfrom xeofs.xarray import EOF, Bootstrapper" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"t2m = xr.tutorial.load_dataset('air_temperature')['air']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Perform EOF analysis\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model = EOF(t2m, n_modes=25, norm=False, dim='time')\nmodel.solve()\nexpvar = model.explained_variance_ratio()\neofs = model.eofs()\npcs = model.pcs()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Perform bootstrapping of the model to identy the number of significant modes.\nWe choose a significance level of alpha=0.05 and perform 25 bootstraps.\nNote - if computationallly feasible - you typically want to choose higher\nnumbers of bootstraps e.g. 100 or 1000.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"alpha = .05\nn_boot = 25\n\nbs = Bootstrapper(n_boot=n_boot, alpha=alpha)\nbs.bootstrap(model)\nn_significant_modes = bs.n_significant_modes()\nprint('{:} modes are significant at alpha={:.2}'.format(n_significant_modes, alpha))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The bootstrapping procedure identifies 5 significant modes. We can also\ncompute the 95 % confidence intervals of the EOFs/PCs and mask out\ninsignificant elements of the obtained EOFs.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"eofs_ci, eofs_mask = bs.eofs()\npcs_ci, pcs_mask = bs.pcs()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Summarize the results in a figure.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"lons, lats = np.meshgrid(eofs_mask.lon.values, eofs_mask.lat.values)\nproj = Orthographic(central_latitude=30, central_longitude=-80)\nkwargs = {\n 'cmap' : 'RdBu', 'vmin' : -.05, 'vmax': .05, 'transform': PlateCarree()\n}\n\nfig = plt.figure(figsize=(10, 16))\ngs = GridSpec(5, 2)\nax1 = [fig.add_subplot(gs[i, 0], projection=proj) for i in range(5)]\nax2 = [fig.add_subplot(gs[i, 1]) for i in range(5)]\n\nfor i, (a1, a2) in enumerate(zip(ax1, ax2)):\n a1.coastlines(color='.5')\n eofs.isel(mode=i).plot(ax=a1, **kwargs)\n a1.scatter(\n lons, lats, eofs_mask.isel(mode=i).values * .5,\n color='k', alpha=.5, transform=PlateCarree()\n )\n pcs_ci.isel(mode=i, quantile=0).plot(ax=a2, color='.3', lw='.5', label='2.5%')\n pcs_ci.isel(mode=i, quantile=1).plot(ax=a2, color='.3', lw='.5', label='97.5%')\n pcs.isel(mode=i).plot(ax=a2, lw='.5', alpha=.5, label='PC')\n a2.legend(loc=2)\n\nplt.tight_layout()\nplt.savefig('bootstrap.jpg')" | ||
] | ||
} | ||
], | ||
"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.8.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
4 changes: 2 additions & 2 deletions
4
examples/3significance/plot_bootstrap.py → docs/auto_examples/1eof/plot_bootstrap.py
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
b574b07fccf1b8340da617a56baf5ea3 |
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
Binary file not shown.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.