{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from fmskill import PointObservation, TrackObservation\n", "from fmskill import ModelResult, Connector" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "output_type": "execute_result", "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" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "fn = '../tests/testdata/SW/HKZN_local_2017_DutchCoast.dfsu'\n", "mr = ModelResult(fn, name='HKZN_local', item=0)\n", "mr.dfs" ] }, { "source": [ "Configuration of comparison, see [SW_DutchCoast.ipynb](SW_DutchCoast.ipynb) for more details." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "o1 = PointObservation('../tests/testdata/SW/HKNA_Hm0.dfs0', item=0, x=4.2420, y=52.6887, name=\"HKNA\")\n", "o2 = PointObservation(\"../tests/testdata/SW/eur_Hm0.dfs0\", item=0, x=3.2760, y=51.9990, name=\"EPL\")\n", "o3 = TrackObservation(\"../tests/testdata/SW/Alti_c2_Dutch.dfs0\", item=3, name=\"c2\")\n", "con = Connector([o1, o2, o3], mr)\n", "cc = con.extract()\n", "cc" ] }, { "source": [ "Standard set of metrics" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "" ], "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 bias rmse urmse mae cc si r2
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.120.90
" }, "metadata": {}, "execution_count": 13 } ], "source": [ "cc.skill().style(precision=2)" ] }, { "source": [ "Select a specific metric" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " n mean_absolute_error\n", "observation \n", "EPL 67 0.188513\n", "HKNA 386 0.251839\n", "c2 113 0.294585" ], "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
" }, "metadata": {}, "execution_count": 38 } ], "source": [ "cc.skill(metrics=\"mean_absolute_error\")" ] }, { "source": [], "cell_type": "markdown", "metadata": {} }, { "source": [ "Some metrics has parameters, which require a bit special treatment." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "" ], "text/html": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
n hit_ratio_05_pct hit_ratio_01_pct
observation
EPL679927
HKNA3868730
c21138617
" }, "metadata": {}, "execution_count": 39 } ], "source": [ "from fmskill.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", "cc.skill(metrics=[hit_ratio_05_pct, hit_ratio_01_pct]).style(precision=0)" ] }, { "source": [ "It is also possible to do it in a single line using a lambda function (anonymous function), with the downside that the anonymous function has no name, and thus no automatic column label :-(" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " n \n", "observation \n", "EPL 67 0.268657\n", "HKNA 386 0.295337\n", "c2 113 0.168142" ], "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<lambda>
observation
EPL670.268657
HKNA3860.295337
c21130.168142
\n
" }, "metadata": {}, "execution_count": 37 } ], "source": [ "cc.skill(metrics=lambda obs, model : hit_ratio(obs, model, 0.1))" ] }, { "source": [ "And you are of course always free to specify your own special metric." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " n my_special_metric\n", "observation \n", "EPL 67 0.127555\n", "HKNA 386 0.223049\n", "c2 113 0.147897" ], "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
" }, "metadata": {}, "execution_count": 33 } ], "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", "cc.skill(metrics=my_special_metric)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3.8.5 64-bit ('base': conda)" }, "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.8.5" }, "interpreter": { "hash": "04089dccd669f5475382045dd9eff7870585234f8cdbd51ae6db76ccd1cd85e5" } }, "nbformat": 4, "nbformat_minor": 4 }