{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import modelskill as ms" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dfsu2D\n", "number of elements: 958\n", "number of nodes: 570\n", "projection: LONG/LAT\n", "number of items: 15\n", "time: 23 steps with dt=10800.0s\n", " 2017-10-27 00:00:00 -- 2017-10-29 18:00:00" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fn = '../tests/testdata/SW/HKZN_local_2017_DutchCoast.dfsu'\n", "mr = ms.model_result(fn, name='HKZN_local', item=0)\n", "mr.data" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Configuration of comparison, see [SW_DutchCoast.ipynb](SW_DutchCoast.ipynb) for more details." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Comparer: HKNA\n", "Comparer: EPL\n", "Comparer: c2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "o1 = ms.PointObservation('../tests/testdata/SW/HKNA_Hm0.dfs0', item=0, x=4.2420, y=52.6887, name=\"HKNA\")\n", "o2 = ms.PointObservation(\"../tests/testdata/SW/eur_Hm0.dfs0\", item=0, x=3.2760, y=51.9990, name=\"EPL\")\n", "o3 = ms.TrackObservation(\"../tests/testdata/SW/Alti_c2_Dutch.dfs0\", item=3, name=\"c2\")\n", "cc = ms.match([o1, o2, o3], mr)\n", "cc" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Standard set of metrics" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nbiasrmseurmsemaeccsir2
observation
HKNA386-0.3153800.4473110.3172100.3413440.9683230.1021220.847042
EPL67-0.0775200.2279270.2143390.1926890.9694540.0828660.929960
c2113-0.0047010.3524700.3524390.2947580.9750500.1280100.899121
\n", "
" ], "text/plain": [ " n bias rmse urmse mae cc si \\\n", "observation \n", "HKNA 386 -0.315380 0.447311 0.317210 0.341344 0.968323 0.102122 \n", "EPL 67 -0.077520 0.227927 0.214339 0.192689 0.969454 0.082866 \n", "c2 113 -0.004701 0.352470 0.352439 0.294758 0.975050 0.128010 \n", "\n", " r2 \n", "observation \n", "HKNA 0.847042 \n", "EPL 0.929960 \n", "c2 0.899121 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cc.skill()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Select a specific metric" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nmean_absolute_error
observation
HKNA3860.341344
EPL670.192689
c21130.294758
\n", "
" ], "text/plain": [ " n mean_absolute_error\n", "observation \n", "HKNA 386 0.341344\n", "EPL 67 0.192689\n", "c2 113 0.294758" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cc.skill(metrics=\"mean_absolute_error\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Some metrics has parameters, which require a bit special treatment." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nhit_ratio_05_pcthit_ratio_01_pct
observation
HKNA38680.05181317.098446
EPL6798.50746328.358209
c211385.84070817.699115
\n", "
" ], "text/plain": [ " n hit_ratio_05_pct hit_ratio_01_pct\n", "observation \n", "HKNA 386 80.051813 17.098446\n", "EPL 67 98.507463 28.358209\n", "c2 113 85.840708 17.699115" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import modelskill.metrics as mtr\n", "from modelskill.metrics import hit_ratio\n", "\n", "def hit_ratio_05_pct(obs, model):\n", " return hit_ratio(obs, model, 0.5) * 100\n", "\n", "def hit_ratio_01_pct(obs, model):\n", " return hit_ratio(obs, model, 0.1) * 100\n", "\n", "\n", "cc.skill(metrics=[hit_ratio_05_pct, hit_ratio_01_pct])" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And you are of course always free to specify your own special metric or import metrics from other libraries, e.g. scikit-learn." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nmcae
observation
HKNA3860.328362
EPL670.135104
c21130.149729
\n", "
" ], "text/plain": [ " n mcae\n", "observation \n", "HKNA 386 0.328362\n", "EPL 67 0.135104\n", "c2 113 0.149729" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def my_special_metric_with_long_descriptive_name(obs, model):\n", "\n", " res = obs - model\n", "\n", " res_clipped = np.clip(res,0,np.inf)\n", "\n", " return np.mean(np.abs(res_clipped))\n", "\n", "# short alias to avoid long column names in output\n", "def mcae(obs, model): return my_special_metric_with_long_descriptive_name(obs, model)\n", "\n", "cc.skill(metrics=mcae)" ] } ], "metadata": { "interpreter": { "hash": "04089dccd669f5475382045dd9eff7870585234f8cdbd51ae6db76ccd1cd85e5" }, "kernelspec": { "display_name": "Python 3.8.5 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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }