{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nBinary Forecast Verification\n============================\n\nThis example demonstrates how to perform binary forecast verification.\n\nIf you are not familiar with the terms, you may wish to refer to the following\nresources:\n \n* `Verification of categorical predictands by Anna Ghelli of ECMWF `_\n* `Verification Measures by Weather Forecasting ... On-Line `_\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Definitions\n--------------------------------------------------------\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import all required modules and methods:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Python package to manage warning message\nimport warnings\n# Python package for time calculations\nimport pandas as pd\n# Python package for numerical calculations\nimport numpy as np\n# Python package for xarrays to read and handle netcdf data\nimport xarray as xr\n# swirlspy traditional binary verification package\nfrom swirlspy.ver.crosstab import contingency\n# swirlspy verification metric package\nimport swirlspy.ver.metric as mt\n\nwarnings.filterwarnings(\"ignore\")\n\n# Generating sample forecast and observed data\n# Dimensions: x, y, time (3 x 3 x 4)\nforecast_data = [\n [[7, 4, 6, 5], [4, 7, 9, 2], [3, 1, 6, 5]],\n [[4, 6, 8, 1], [11, 0, 3, 6], [0, 3, 6, 8]],\n [[5, 3, 7, 5], [7, 7, 6, 2], [3, 8, 9, 2]]\n]\n\nobserved_data = [\n [[8, 7, 11, 4], [6, 7, 6, 0], [3, 3, 7, 3]],\n [[8, 8, 9, 5], [12, 1, 2, 8], [1, 4, 3, 12]],\n [[5, 6, 6, 2], [3, 6, 4, 7], [5, 7, 8, 2]]\n]\n\n# Creating xarrays\nx = np.arange(3)\ny = np.arange(3)\ntime = pd.date_range('1/1/2011', periods=4, freq='D')\nforecast = xr.DataArray(\n forecast_data,\n coords=[('x', x), ('y', y), ('time', time)])\nobserved = xr.DataArray(\n observed_data,\n coords=[('x', x), ('y', y), ('time', time)])\n\n# Applying the contingency function\ncont = contingency(5, forecast, observed)\n\n# Generating skill metrics\npod = mt.pod(cont)\nfar = mt.far(cont)\ncsi = mt.csi(cont)\nfreq_bias = mt.freq_bias(cont)\naccuracy = mt.accuracy(cont)\npofd = mt.pofd(cont)\nhss = mt.hss(cont)\nets = mt.ets(cont)\nf1 = mt.f1Score(forecast, observed, average='weighted')\n\n# Displaying results as pandas series\nd = {\n 'Hits': cont[0],\n 'Misses': cont[1],\n 'False Alarm': cont[2],\n 'Correct Negative': cont[3],\n 'Probability of Detection': pod,\n 'False Alarm Ratio': far,\n 'Critical Success Index': csi,\n 'Frequency Bias': freq_bias,\n 'Accuracy': accuracy,\n 'Probability of False Detection': pofd,\n 'Heidke Skill Score': hss,\n 'Equitable Threat Score': ets,\n 'f1 Score': f1\n}\n\npd_series_d = pd.Series(d)\nprint(pd_series_d)" ] } ], "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.15" } }, "nbformat": 4, "nbformat_minor": 0 }