{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic event statistics\n", "**Scott Wales, CLEX CMS**\n", "\n", "We have a set of timeseries from multiple models, and we'd like to compute some basic statistics on where the values exceed some threshold" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy\n", "import xarray\n", "import pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sample data\n", "\n", "Some random data as a sample, we have dimensions of 'model' and 'time'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray (model: 5, time: 10)>\n",
       "array([[-0.32756177, -0.26286746,  0.40030123,  0.35854755,  0.43113588,\n",
       "         0.19164228, -0.24890014, -0.16822426,  0.0288118 ,  0.30851645],\n",
       "       [ 0.02352896,  0.01700063,  0.40107743,  0.09688021, -0.34731748,\n",
       "         0.33646959, -0.22218415,  0.00075536,  0.41923523,  0.46450296],\n",
       "       [-0.36606797,  0.13691949, -0.24407689,  0.26593951, -0.36044558,\n",
       "        -0.09755949,  0.23047747, -0.30123992,  0.49773007, -0.48146704],\n",
       "       [-0.28529412, -0.4137749 , -0.49678032, -0.20963922, -0.37296851,\n",
       "         0.32198888, -0.48008865, -0.35185213, -0.19133728,  0.48969387],\n",
       "       [ 0.35537076, -0.34188436, -0.46208259,  0.41417063, -0.07029039,\n",
       "        -0.39533084,  0.21379516, -0.18512164, -0.43433092, -0.29407475]])\n",
       "Coordinates:\n",
       "  * time     (time) datetime64[ns] 2001-01-01 2001-02-01 ... 2001-10-01\n",
       "  * model    (model) <U1 'A' 'B' 'C' 'D' 'E'
" ], "text/plain": [ "\n", "array([[-0.32756177, -0.26286746, 0.40030123, 0.35854755, 0.43113588,\n", " 0.19164228, -0.24890014, -0.16822426, 0.0288118 , 0.30851645],\n", " [ 0.02352896, 0.01700063, 0.40107743, 0.09688021, -0.34731748,\n", " 0.33646959, -0.22218415, 0.00075536, 0.41923523, 0.46450296],\n", " [-0.36606797, 0.13691949, -0.24407689, 0.26593951, -0.36044558,\n", " -0.09755949, 0.23047747, -0.30123992, 0.49773007, -0.48146704],\n", " [-0.28529412, -0.4137749 , -0.49678032, -0.20963922, -0.37296851,\n", " 0.32198888, -0.48008865, -0.35185213, -0.19133728, 0.48969387],\n", " [ 0.35537076, -0.34188436, -0.46208259, 0.41417063, -0.07029039,\n", " -0.39533084, 0.21379516, -0.18512164, -0.43433092, -0.29407475]])\n", "Coordinates:\n", " * time (time) datetime64[ns] 2001-01-01 2001-02-01 ... 2001-10-01\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray (model: 5)>\n",
       "array([4, 2, 6, 8, 7])\n",
       "Coordinates:\n",
       "  * model    (model) <U1 'A' 'B' 'C' 'D' 'E'
" ], "text/plain": [ "\n", "array([4, 2, 6, 8, 7])\n", "Coordinates:\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray (model: 5)>\n",
       "array([-0.32756177, -0.34731748, -0.48146704, -0.49678032, -0.46208259])\n",
       "Coordinates:\n",
       "  * model    (model) <U1 'A' 'B' 'C' 'D' 'E'
" ], "text/plain": [ "\n", "array([-0.32756177, -0.34731748, -0.48146704, -0.49678032, -0.46208259])\n", "Coordinates:\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray (model: 5)>\n",
       "array([0, 4, 9, 2, 2])\n",
       "Coordinates:\n",
       "  * model    (model) <U1 'A' 'B' 'C' 'D' 'E'
" ], "text/plain": [ "\n", "array([0, 4, 9, 2, 2])\n", "Coordinates:\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'time' (model: 5)>\n",
       "array(['2001-01-01T00:00:00.000000000', '2001-05-01T00:00:00.000000000',\n",
       "       '2001-10-01T00:00:00.000000000', '2001-03-01T00:00:00.000000000',\n",
       "       '2001-03-01T00:00:00.000000000'], dtype='datetime64[ns]')\n",
       "Coordinates:\n",
       "    time     (model) datetime64[ns] 2001-01-01 2001-05-01 ... 2001-03-01\n",
       "  * model    (model) <U1 'A' 'B' 'C' 'D' 'E'
" ], "text/plain": [ "\n", "array(['2001-01-01T00:00:00.000000000', '2001-05-01T00:00:00.000000000',\n", " '2001-10-01T00:00:00.000000000', '2001-03-01T00:00:00.000000000',\n", " '2001-03-01T00:00:00.000000000'], dtype='datetime64[ns]')\n", "Coordinates:\n", " time (model) datetime64[ns] 2001-01-01 2001-05-01 ... 2001-03-01\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'time' (model: 5)>\n",
       "array(['2001-01-01T00:00:00.000000000', '2001-05-01T00:00:00.000000000',\n",
       "       '2001-10-01T00:00:00.000000000', '2001-03-01T00:00:00.000000000',\n",
       "       '2001-03-01T00:00:00.000000000'], dtype='datetime64[ns]')\n",
       "Coordinates:\n",
       "  * model    (model) <U1 'A' 'B' 'C' 'D' 'E'
" ], "text/plain": [ "\n", "array(['2001-01-01T00:00:00.000000000', '2001-05-01T00:00:00.000000000',\n", " '2001-10-01T00:00:00.000000000', '2001-03-01T00:00:00.000000000',\n", " '2001-03-01T00:00:00.000000000'], dtype='datetime64[ns]')\n", "Coordinates:\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:   (model: 5)\n",
       "Coordinates:\n",
       "  * model     (model) <U1 'A' 'B' 'C' 'D' 'E'\n",
       "Data variables:\n",
       "    count     (model) int64 4 2 6 8 7\n",
       "    min       (model) float64 -0.3276 -0.3473 -0.4815 -0.4968 -0.4621\n",
       "    min_date  (model) datetime64[ns] 2001-01-01 2001-05-01 ... 2001-03-01
" ], "text/plain": [ "\n", "Dimensions: (model: 5)\n", "Coordinates:\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:   ()\n",
       "Coordinates:\n",
       "    model     <U1 'A'\n",
       "Data variables:\n",
       "    count     int64 4\n",
       "    min       float64 -0.3276\n",
       "    min_date  datetime64[ns] 2001-01-01
" ], "text/plain": [ "\n", "Dimensions: ()\n", "Coordinates:\n", " model \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", "
timemodelevent_duration
0002
1142
2035
3422
4442
5602
6633
7743
\n", "" ], "text/plain": [ " time model event_duration\n", "0 0 0 2\n", "1 1 4 2\n", "2 0 3 5\n", "3 4 2 2\n", "4 4 4 2\n", "5 6 0 2\n", "6 6 3 3\n", "7 7 4 3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import climtas\n", "\n", "events = climtas.event.find_events(da < 0, min_duration=2)\n", "events" ] }, { "cell_type": "code", "execution_count": 11, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
modeltimeevent_durationminmin_date
0A2001-01-0131 days-0.327561770463492332001-01-01
1E2001-02-0128 days-0.46208259096971662001-03-01
2D2001-01-01120 days-0.496780320116414262001-03-01
3C2001-05-0131 days-0.360445575636102472001-05-01
4E2001-05-0131 days-0.3953308439381172001-06-01
5A2001-07-0131 days-0.24890013607558882001-07-01
6D2001-07-0162 days-0.480088645598888532001-07-01
7E2001-08-0161 days-0.43433091908673752001-09-01
\n", "
" ], "text/plain": [ " model time event_duration min min_date\n", "0 A 2001-01-01 31 days -0.32756177046349233 2001-01-01\n", "1 E 2001-02-01 28 days -0.4620825909697166 2001-03-01\n", "2 D 2001-01-01 120 days -0.49678032011641426 2001-03-01\n", "3 C 2001-05-01 31 days -0.36044557563610247 2001-05-01\n", "4 E 2001-05-01 31 days -0.395330843938117 2001-06-01\n", "5 A 2001-07-01 31 days -0.2489001360755888 2001-07-01\n", "6 D 2001-07-01 62 days -0.48008864559888853 2001-07-01\n", "7 E 2001-08-01 61 days -0.4343309190867375 2001-09-01" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coords = climtas.event.event_coords(da, events)\n", "\n", "def stat_func(event_data):\n", " return {'min': event_data.min().values,\n", " 'min_date': event_data.time[event_data.argmin()].values}\n", " \n", "minimum = climtas.event.map_events(da, events, stat_func)\n", "\n", "coords.join(minimum)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }