{ "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", "
 nbiasrmseurmsemaeccsir2
observation        
EPL67-0.070.220.210.190.970.080.93
HKNA386-0.190.350.290.250.970.090.91
c2113-0.000.350.350.290.970.130.90
\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cc.skill().style(precision=2)" ] }, { "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
EPL670.188513
HKNA3860.251839
c21130.294585
\n", "
" ], "text/plain": [ " n mean_absolute_error\n", "observation \n", "EPL 67 0.188513\n", "HKNA 386 0.251839\n", "c2 113 0.294585" ] }, "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", "
 nhit_ratio_05_pcthit_ratio_01_pct
observation   
EPL679927
HKNA3868730
c21138617
\n" ], "text/plain": [ "" ] }, "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", "mtr.add_metric(hit_ratio_05_pct)\n", "mtr.add_metric(hit_ratio_01_pct)\n", "\n", "cc.skill(metrics=[hit_ratio_05_pct, hit_ratio_01_pct]).style(precision=0)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And you are of course always free to specify your own special metric." ] }, { "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", "
nmy_special_metric
observation
EPL670.127555
HKNA3860.223049
c21130.147897
\n", "
" ], "text/plain": [ " n my_special_metric\n", "observation \n", "EPL 67 0.127555\n", "HKNA 386 0.223049\n", "c2 113 0.147897" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def my_special_metric(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", "\n", "mtr.add_metric(my_special_metric)\n", "\n", "cc.skill(metrics=my_special_metric)" ] } ], "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.11.1" } }, "nbformat": 4, "nbformat_minor": 4 }