{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Metrics widget\n",
                "\n",
                "Execute this notebook to test out the different metrics. "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import numpy as np\n",
                "import matplotlib.pyplot as plt\n",
                "from ipywidgets import interact   # pip install ipywidgets\n",
                "import modelskill.metrics as mtr"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "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.nse, mtr.willmott, mtr.lin_slope, mtr.kge, mtr.ev]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "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()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "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()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "interact(plot_metrics, bias = (-1,3,0.1), noise_level=(0,2,0.05));"
            ]
        }
    ],
    "metadata": {
        "interpreter": {
            "hash": "fa576ebcd40e010bdc0ae86b06ce09151f3424f9e9aed6893ff04f39a9299d89"
        },
        "kernelspec": {
            "display_name": "Python 3.8.10 64-bit ('base': conda)",
            "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.11.4"
        },
        "orig_nbformat": 4
    },
    "nbformat": 4,
    "nbformat_minor": 2
}