{ "cells": [ { "cell_type": "markdown", "source": [ "# Metrics widget\n", "\n", "Execute this notebook to test out the different metrics. " ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from ipywidgets import interact # pip install ipywidgets\n", "import fmskill.metrics as mtr" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "metrics = [mtr.bias, mtr.max_error, mtr.rmse, mtr.urmse, mtr.mae, mtr.mape, \n", " mtr.mef, mtr.si, mtr.cc, mtr.spearmanr, mtr.r2, mtr.willmott, mtr.lin_slope]" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "n = 50\n", "x = np.linspace(0.0, 6.0, num=n)\n", "y_obs = 2.0+3*np.sin(x/2.4)\n", "noise_vec = np.random.randn(n)\n", "noise_vec = noise_vec - noise_vec.mean()" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "def plot_metrics(bias, noise_level, fixed_y_axis=True):\n", " y_mod = y_obs + bias + noise_level*noise_vec\n", " plt.plot(x, y_obs, 'r.-', label=\"obs\")\n", " plt.plot(x, y_mod, 'o-', label=\"model\")\n", " plt.title(f\"y_model = y_obs + {bias} + {noise_level}*noise\")\n", "\n", " ymax = max(max(y_obs),max(y_mod))\n", " ymin = min(min(y_obs), min(y_mod))\n", " if fixed_y_axis:\n", " ymax = 8\n", " ymin = 1\n", " ystep = 1.2*(ymax - ymin)/len(metrics)\n", " ypos = ymax + 0.5\n", " for m in metrics:\n", " plt.text(6.5, ypos, f\"{m.__name__}:\")\n", " plt.text(8.0, ypos, f\"{m(y_obs,y_mod):.4f}\")\n", " ypos = ypos - ystep\n", " plt.legend(loc=2)\n", " if fixed_y_axis:\n", " plt.ylim(ymin, ymax)\n", "\n", " plt.show()" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "interact(plot_metrics, bias = (-1,3,0.1), noise_level=(0,2,0.05));" ], "outputs": [], "metadata": {} } ], "metadata": { "orig_nbformat": 4, "language_info": { "name": "python", "version": "3.8.10", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" }, "kernelspec": { "name": "python3", "display_name": "Python 3.8.10 64-bit ('base': conda)" }, "interpreter": { "hash": "fa576ebcd40e010bdc0ae86b06ce09151f3424f9e9aed6893ff04f39a9299d89" } }, "nbformat": 4, "nbformat_minor": 2 }