{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "c8ee3886-80ba-40be-b83a-4567c06e61fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_101408/3055773264.py:25: UserWarning:\n",
      "\n",
      "pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import psycopg2 as pg\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "import xgboost as xgb\n",
    "from sklearn.metrics import confusion_matrix,matthews_corrcoef,accuracy_score\n",
    "import optuna\n",
    "import pickle\n",
    "from sklearn.feature_selection import SequentialFeatureSelector\n",
    "reload_data = True\n",
    "solo_trentino = True\n",
    "all_seasons = False\n",
    "import plotly.io as pio\n",
    "pio.renderers.default = 'iframe'\n",
    "from datetime import datetime\n",
    "def norm(x):\n",
    "    if len(x)==1 and x[0]=='':\n",
    "        return []\n",
    "    else:\n",
    "        return x\n",
    "if solo_trentino:\n",
    "        engine = pg.connect(\"dbname='safeidx' user='fbk_mpba' host='172.104.247.67' port='5432' password='fbk2024$'\")\n",
    "        if all_seasons is False:\n",
    "            df = pd.read_sql('select * from fbk_export_08052024', con=engine) \n",
    "        else:\n",
    "            df = pd.read_sql('select * from fbk_export_09052024', con=engine) \n",
    "\n",
    "else:\n",
    "    if reload_data:\n",
    "        #fbk_export_08052024\n",
    "        engine = pg.connect(\"dbname='safeidx' user='fbk_mpba' host='172.104.247.67' port='5432' password='fbk2024$'\")\n",
    "        df = pd.read_sql('select * from fbk_export_20240212', con=engine)\n",
    "        with open('../src/data.pkl','wb') as f:\n",
    "            pickle.dump(df,f)\n",
    "    else:\n",
    "        with open('../src/data.pkl','rb') as f:\n",
    "            df = pickle.load(f)\n",
    "\n",
    "\n",
    "    df = df[df.year>2015]\n",
    "df['iii'] = list(range(df.shape[0]))\n",
    "df['hour'] = df.dateandtime.apply(lambda x: x.hour)\n",
    "df['dow'] = df.dateandtime.apply(lambda x: x.weekday())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "ed3c0dad-8da1-4fd3-99a4-f1d781ca643e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>dateandtime</th>\n",
       "      <th>skiarea_id</th>\n",
       "      <th>skiarea_name</th>\n",
       "      <th>day_of_year</th>\n",
       "      <th>minute_of_day</th>\n",
       "      <th>year</th>\n",
       "      <th>season</th>\n",
       "      <th>difficulty</th>\n",
       "      <th>cause</th>\n",
       "      <th>...</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>india</th>\n",
       "      <th>age</th>\n",
       "      <th>country</th>\n",
       "      <th>injury_side</th>\n",
       "      <th>injury_general_location</th>\n",
       "      <th>evacuation_vehicles</th>\n",
       "      <th>iii</th>\n",
       "      <th>hour</th>\n",
       "      <th>dow</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>627685195</td>\n",
       "      <td>2024-04-27 08:00:00+00:00</td>\n",
       "      <td>8</td>\n",
       "      <td>Passo Tonale - Presena</td>\n",
       "      <td>118</td>\n",
       "      <td>480</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>advanced</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>...</td>\n",
       "      <td>fracture</td>\n",
       "      <td>None</td>\n",
       "      <td>52.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>R</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[ambulance, akja]</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>627685151</td>\n",
       "      <td>2024-04-20 10:40:00+00:00</td>\n",
       "      <td>8</td>\n",
       "      <td>Passo Tonale - Presena</td>\n",
       "      <td>111</td>\n",
       "      <td>640</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>illness</td>\n",
       "      <td>...</td>\n",
       "      <td>malaise</td>\n",
       "      <td>None</td>\n",
       "      <td>51.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>[ski_lift, indipendently]</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>627685150</td>\n",
       "      <td>2024-04-20 09:00:00+00:00</td>\n",
       "      <td>8</td>\n",
       "      <td>Passo Tonale - Presena</td>\n",
       "      <td>111</td>\n",
       "      <td>540</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>advanced</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>...</td>\n",
       "      <td>distortion</td>\n",
       "      <td>None</td>\n",
       "      <td>48.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>R</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[akja, car]</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>627685033</td>\n",
       "      <td>2024-04-14 12:30:00+00:00</td>\n",
       "      <td>6</td>\n",
       "      <td>Pampeago</td>\n",
       "      <td>105</td>\n",
       "      <td>750</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>illness</td>\n",
       "      <td>...</td>\n",
       "      <td>other</td>\n",
       "      <td>None</td>\n",
       "      <td>26.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>[snowmobile]</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>627685024</td>\n",
       "      <td>2024-04-14 09:15:00+00:00</td>\n",
       "      <td>8</td>\n",
       "      <td>Passo Tonale - Presena</td>\n",
       "      <td>105</td>\n",
       "      <td>555</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>easy</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>...</td>\n",
       "      <td>bruise</td>\n",
       "      <td>None</td>\n",
       "      <td>9.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>L</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[snowmobile]</td>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          id               dateandtime  skiarea_id            skiarea_name  \\\n",
       "0  627685195 2024-04-27 08:00:00+00:00           8  Passo Tonale - Presena   \n",
       "1  627685151 2024-04-20 10:40:00+00:00           8  Passo Tonale - Presena   \n",
       "2  627685150 2024-04-20 09:00:00+00:00           8  Passo Tonale - Presena   \n",
       "3  627685033 2024-04-14 12:30:00+00:00           6                Pampeago   \n",
       "4  627685024 2024-04-14 09:15:00+00:00           8  Passo Tonale - Presena   \n",
       "\n",
       "   day_of_year  minute_of_day  year  season    difficulty       cause  ...  \\\n",
       "0          118            480  2024    2024      advanced  fall_alone  ...   \n",
       "1          111            640  2024    2024  intermediate     illness  ...   \n",
       "2          111            540  2024    2024      advanced  fall_alone  ...   \n",
       "3          105            750  2024    2024  intermediate     illness  ...   \n",
       "4          105            555  2024    2024          easy  fall_alone  ...   \n",
       "\n",
       "    diagnosis india   age country injury_side injury_general_location  \\\n",
       "0    fracture  None  52.0  Italia           R             lower_limbs   \n",
       "1     malaise  None  51.0  Italia        None                    None   \n",
       "2  distortion  None  48.0  Italia           R             lower_limbs   \n",
       "3       other  None  26.0  Italia        None                    None   \n",
       "4      bruise  None   9.0  Italia           L             lower_limbs   \n",
       "\n",
       "         evacuation_vehicles iii  hour dow  \n",
       "0          [ambulance, akja]   0     8   5  \n",
       "1  [ski_lift, indipendently]   1    10   5  \n",
       "2                [akja, car]   2     9   5  \n",
       "3               [snowmobile]   3    12   6  \n",
       "4               [snowmobile]   4     9   6  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()   ## provare ad aggiungere ora\n",
    "            ## aggiungere regione? e lavorare solo trentino ##chiesto\n",
    "            ## chiedere meteo ##chiesto\n",
    "            ## numero incidenti giornalieri e gravita\n",
    "            ## uso improprio delle ambulanze \n",
    "            ## max un mese (meta' maggio) --> 6 giugno campiglio 17.50 \n",
    "            ## ingressi meteo e pista (MARCO)\n",
    "\n",
    "            ## "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "258eaf97-d3fd-4737-ad3b-bc0f8fc04ce8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(22324, 26)"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "22a0b715-d8e2-4de5-bf50-7c7171ce242a",
   "metadata": {},
   "outputs": [],
   "source": [
    "aa = df.groupby('diagnosis').age.count().reset_index()\n",
    "aa.sort_values(by='age',ascending=True,inplace=True)\n",
    "\n",
    "import plotly.express as px\n",
    "fig = px.bar(aa.rename(columns={'age':'count'}), y='diagnosis', x='count',width=800,height=1200)\n",
    "fig.update_layout(\n",
    "    xaxis_title=\"Counts\",\n",
    "    yaxis_title=\"Diagnosis\",\n",
    "   title = {\n",
    "         'text': \"Distribution of rescues by diagnosis\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig1.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig1.png\") \n",
    "fig = px.bar(aa.rename(columns={'age':'count'}), y='diagnosis', x='count',width=800,height=1200,log_x=True)\n",
    "fig.update_layout(\n",
    "    xaxis_title=\"Counts (log scale)\",\n",
    "    yaxis_title=\"Diagnosis\",\n",
    "   title = {\n",
    "         'text': \"Distribution of rescues by diagnosis\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig1_log.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig1_log.png\") \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "a78b9ccd-e8bb-44f9-a096-a917fa0caed8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Diagnosi')"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 74708 characters]",
      "text/plain": [
       "<Figure size 500x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "aa = df.groupby('diagnosis').age.count().reset_index()\n",
    "aa.sort_values(by='age',ascending=True,inplace=True)\n",
    "plt.figure(figsize = (5,7))\n",
    "plt.barh(aa.diagnosis, aa.age)\n",
    "plt.xlabel('Numero di soccorsi')\n",
    "plt.ylabel('Diagnosi')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "0d54fbe1-9a36-4162-836f-5f45061070bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = [\n",
    "    \"rgb(0, 48, 143)\",\n",
    "    \"rgb(65, 175, 26)\",\n",
    "    #\"rgb(168, 227, 0)\",\n",
    "    \"rgb(255, 201, 77)\",\n",
    "    \"rgb(255, 107, 0)\",\n",
    "    \"rgb(214, 11, 67)\",\n",
    "    ]\n",
    "\n",
    "\n",
    "color_list=[[0, colors[0]],\n",
    "    [1/10, colors[1]],  \n",
    "    [5/10, colors[2]], \n",
    "    #[5/10, colors[3]], \n",
    "    [10/10, colors[4]]]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "ca2eaf74-60e5-4798-9f51-e08da69eb6d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_rr(df,c1,c2):\n",
    "    diagnosis = df.groupby([c1,c2]).iii.count().reset_index()\n",
    "    diagnosis = diagnosis.pivot(columns=c2,values='iii',index=c1).reset_index().fillna(0)\n",
    "    diagnosis.columns.name = None\n",
    "    diagnosis.index.name = None\n",
    "    if c1=='skiarea_id':\n",
    "        diagnosis.loc[:,c1]=diagnosis[c1].apply(lambda x:str(int(float(x))))\n",
    "    diagnosis = diagnosis.set_index(c1)\n",
    "    diagnosis['tot'] = diagnosis.sum(axis=1)\n",
    "    diagnosis = diagnosis[diagnosis.tot>100]\n",
    "    sus=[]\n",
    "    tots = pd.DataFrame(diagnosis.sum(axis=0),columns=['n'])\n",
    "    from scipy.stats.contingency import relative_risk\n",
    "    for i in range(diagnosis.shape[0]):\n",
    "        for j in range(diagnosis.shape[1]-1):\n",
    "            result = relative_risk(int(diagnosis.values[i,j]), int(diagnosis.values[i,-1]), int(tots.values[j][0]), int(tots.values[-1][0]))\n",
    "            ci = result.confidence_interval(confidence_level=0.95)\n",
    "            if ((ci[0]>1) & (ci[1]>1)) | ((ci[0]<1) & (ci[1]<1)):\n",
    "                sus.append({c1:diagnosis.index[i],c2:diagnosis.columns[j],'rr':np.round(result.relative_risk,2)})\n",
    "            else:\n",
    "                sus.append({c1:diagnosis.index[i],c2:diagnosis.columns[j],'rr':np.nan})\n",
    "    sus = pd.DataFrame(sus)   \n",
    "    import matplotlib.pyplot as plt\n",
    "    import plotly.express as px\n",
    "    sus.loc[sus.rr>10,'rr'] = 10\n",
    "    ss = sus.pivot(columns=c2,values='rr',index=c1).reset_index().fillna(1)\n",
    "    #plt.imshow(ss.values[:,2:].astype(float),aspect='auto')\n",
    "    ss.index = ss[c1]\n",
    "    ss = ss.drop(columns=c1)\n",
    "    ss[ss==1] = np.nan\n",
    "\n",
    "    #plt.colorbar()\n",
    "    #x = ss.columns[2:]\n",
    "    #y = ss.skiarea.values\n",
    "    #plt.xticks(range(len(x)), x, fontsize=12);\n",
    "    #plt.yticks(range(len(y)), y, fontsize=12);\n",
    "    \n",
    "    #fig.show()\n",
    "    return diagnosis, ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "d5c1aa3f-15f4-460a-a115-ff54e8036356",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_101408/941372287.py:7: FutureWarning:\n",
      "\n",
      "Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '['1' '3' '4' '6' '7' '8' '9' '10' '11' '12' '17' '20' '24' '28' '31' '32'\n",
      " '36' '37' '51' '55' '58' '59' '61' '64' '65' '78' '81' '170']' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1220px\"\n",
       "    height=\"1220\"\n",
       "    src=\"iframe_figures/figure_115.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "diagnosis,ss = plot_rr(df,'skiarea_id','diagnosis')\n",
    "fig = px.imshow(ss.T,width=1200, height=1200, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "fig.update_coloraxes(showscale=True)\n",
    "\n",
    "fig.update_layout(\n",
    "    yaxis_title=\"Diagnosis\",\n",
    "    xaxis_title=\"Skiarea ID\",\n",
    "   title = {\n",
    "         'text': \"Relative risk Diagnosis-Skiarea\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "fig.show()\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig2.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig2.png\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "1810c816-6e31-44c2-aec3-585556ecb6a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_101408/941372287.py:7: FutureWarning:\n",
      "\n",
      "Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '['1' '3' '4' '6' '7' '8' '9' '10' '11' '12' '17' '20' '24' '28' '31' '32'\n",
      " '36' '37' '51' '55' '58' '59' '61' '64' '65' '78' '81' '170']' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1220px\"\n",
       "    height=\"1220\"\n",
       "    src=\"iframe_figures/figure_116.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "diagnosis,ss = plot_rr(df,'skiarea_id','dow')\n",
    "fig = px.imshow(ss,width=1200, height=1200, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "fig.update_coloraxes(showscale=True)\n",
    "\n",
    "fig.update_layout(\n",
    "    xaxis_title=\"Day of the week\",\n",
    "    yaxis_title=\"Skiarea ID\",\n",
    "   title = {\n",
    "         'text': \"Relative risk Diagnosis-DOW\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "fig.show()\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig3.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig3.png\") \n",
    "aa = df.groupby('dow').age.count().reset_index()\n",
    "days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']\n",
    "\n",
    "aa.dow = aa.dow.apply(lambda x: days[x])\n",
    "import plotly.express as px\n",
    "fig = px.bar(aa.rename(columns={'age':'count'}), y='count', x='dow',width=800,height=800)\n",
    "fig.update_layout(\n",
    "    yaxis_title=\"Counts\",\n",
    "    xaxis_title=\"Day of the week\",\n",
    "   title = {\n",
    "         'text': \"Distribution of rescues by day of the week\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig4.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig4.png\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "ef4c4a52-1be0-4261-9eda-552977c6a1d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_101408/941372287.py:7: FutureWarning:\n",
      "\n",
      "Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '['1' '3' '4' '6' '7' '8' '9' '10' '11' '12' '17' '20' '24' '28' '31' '32'\n",
      " '36' '37' '51' '55' '58' '59' '61' '64' '65' '78' '81' '170']' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1220px\"\n",
       "    height=\"1220\"\n",
       "    src=\"iframe_figures/figure_117.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "diagnosis,ss = plot_rr(df,'skiarea_id','hour')\n",
    "fig = px.imshow(ss,width=1200, height=1200, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "fig.update_coloraxes(showscale=True)\n",
    "fig.update_layout(\n",
    "    xaxis_title=\"Hour\",\n",
    "    yaxis_title=\"Skiarea ID\",\n",
    "   title = {\n",
    "         'text': \"Relative risk Diagnosis-Hour\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "fig.show()\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig5.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig5.png\") \n",
    "\n",
    "aa = df.groupby('hour').age.count().reset_index()\n",
    "\n",
    "import plotly.express as px\n",
    "fig = px.bar(aa.rename(columns={'age':'count'}), y='count', x='hour',width=1000,height=600)\n",
    "fig.update_layout(\n",
    "    yaxis_title=\"Counts\",\n",
    "    xaxis_title=\"Hour\",\n",
    "   title = {\n",
    "         'text': \"Distribution of rescues by hour\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig6.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig6.png\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "aeb4bee2-213f-430a-a66d-35bcee5f4ca1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"620px\"\n",
       "    height=\"620\"\n",
       "    src=\"iframe_figures/figure_118.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "diagnosis,ss = plot_rr(df,'hour','dow')\n",
    "fig = px.imshow(ss,width=600, height=600, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "fig.update_coloraxes(showscale=True)\n",
    "fig.update_layout(\n",
    "    yaxis_title=\"Hour\",\n",
    "    xaxis_title=\"Dow\",\n",
    "   title = {\n",
    "         'text': \"Relative risk Dow-Hour\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "fig.show()\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig7.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig7.png\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "f2b7a2cc-4a7d-44e2-9f4e-23e0005e399c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"920px\"\n",
       "    height=\"920\"\n",
       "    src=\"iframe_figures/figure_119.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "diagnosis,ss = plot_rr(df,'diagnosis','dow')\n",
    "fig = px.imshow(ss,width=900, height=900, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "fig.update_coloraxes(showscale=True)\n",
    "fig.update_layout(\n",
    "    xaxis_title=\"Hour\",\n",
    "    yaxis_title=\"Diagnosis\",\n",
    "   title = {\n",
    "         'text': \"Relative risk Diagnosis-Hour\",\n",
    "         #'y':0.9, # new\n",
    "         'x':0.5,\n",
    "         'xanchor': 'center',\n",
    "         'yanchor': 'top' # new\n",
    "        },\n",
    "        font=dict(\n",
    "        #family=\"Courier New, monospace\",\n",
    "        size=18,\n",
    "        #color=\"RebeccaPurple\"\n",
    "    )\n",
    ")\n",
    "fig.show()\n",
    "if all_seasons:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig8.png\") \n",
    "else:\n",
    "    fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig8.png\") \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "7f2d54a0-f869-4504-9b78-0b964b9e23c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "if all_seasons:\n",
    "    # 'weather', 'snow_condition',\n",
    "    diagnosis,ss = plot_rr(df,'skiarea_id','weather')\n",
    "    fig = px.imshow(ss,width=1200, height=1200, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "    fig.update_coloraxes(showscale=True)\n",
    "    \n",
    "    fig.update_layout(\n",
    "        xaxis_title=\"Weather condition\",\n",
    "        yaxis_title=\"Skiarea ID\",\n",
    "       title = {\n",
    "             'text': \"Relative risk Diagnosis-DOW\",\n",
    "             #'y':0.9, # new\n",
    "             'x':0.5,\n",
    "             'xanchor': 'center',\n",
    "             'yanchor': 'top' # new\n",
    "            },\n",
    "            font=dict(\n",
    "            #family=\"Courier New, monospace\",\n",
    "            size=18,\n",
    "            #color=\"RebeccaPurple\"\n",
    "        )\n",
    "    )\n",
    "    fig.show()\n",
    "    if all_seasons:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig9.png\") \n",
    "    else:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig9.png\") \n",
    "    aa = df.groupby('weather').age.count().reset_index()\n",
    "\n",
    "    import plotly.express as px\n",
    "    fig = px.bar(aa.rename(columns={'age':'count'}), y='count', x='weather',width=800,height=800,log_y=True)\n",
    "    fig.update_layout(\n",
    "        yaxis_title=\"Counts (log scale)\",\n",
    "        xaxis_title=\"Weather condition\",\n",
    "       title = {\n",
    "             'text': \"Distribution of rescues by weather condition\",\n",
    "             #'y':0.9, # new\n",
    "             'x':0.5,\n",
    "             'xanchor': 'center',\n",
    "             'yanchor': 'top' # new\n",
    "            },\n",
    "    \n",
    "            font=dict(\n",
    "            #family=\"Courier New, monospace\",\n",
    "            size=18,\n",
    "            #color=\"RebeccaPurple\"\n",
    "        )\n",
    "    )\n",
    "    if all_seasons:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig10.png\") \n",
    "    else:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig10.png\") \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "a5ac856e-db76-42bd-a088-f9cd4211ad16",
   "metadata": {},
   "outputs": [],
   "source": [
    "if all_seasons:\n",
    "    # 'weather', 'snow_condition',\n",
    "    diagnosis,ss = plot_rr(df,'skiarea_id','snow_condition')\n",
    "    fig = px.imshow(ss,width=1200, height=1200, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "    fig.update_coloraxes(showscale=True)\n",
    "    \n",
    "    fig.update_layout(\n",
    "        xaxis_title=\"Snow condition\",\n",
    "        yaxis_title=\"Skiarea ID\",\n",
    "       title = {\n",
    "             'text': \"Relative risk Diagnosis-DOW\",\n",
    "             #'y':0.9, # new\n",
    "             'x':0.5,\n",
    "             'xanchor': 'center',\n",
    "             'yanchor': 'top' # new\n",
    "            },\n",
    "            font=dict(\n",
    "            #family=\"Courier New, monospace\",\n",
    "            size=18,\n",
    "            #color=\"RebeccaPurple\"\n",
    "        )\n",
    "    )\n",
    "    fig.show()\n",
    "    if all_seasons:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig11.png\") \n",
    "    else:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig11.png\") \n",
    "    aa = df.groupby('snow_condition').age.count().reset_index()\n",
    "\n",
    "    import plotly.express as px\n",
    "    fig = px.bar(aa.rename(columns={'age':'count'}), y='count', x='snow_condition',width=800,height=800,log_y=True)\n",
    "    fig.update_layout(\n",
    "        yaxis_title=\"Counts (log scale)\",\n",
    "        xaxis_title=\"Snow condition\",\n",
    "       title = {\n",
    "             'text': \"Distribution of rescues by weather condition\",\n",
    "             #'y':0.9, # new\n",
    "             'x':0.5,\n",
    "             'xanchor': 'center',\n",
    "             'yanchor': 'top' # new\n",
    "            },\n",
    "    \n",
    "            font=dict(\n",
    "            #family=\"Courier New, monospace\",\n",
    "            size=18,\n",
    "            #color=\"RebeccaPurple\"\n",
    "        )\n",
    "    )\n",
    "    if all_seasons:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres_all_seasons/fig12.png\") \n",
    "    else:\n",
    "        fig.write_image(\"/home/agobbi/Projects/PID/datanalytics/PID/materiale_pres/fig12.png\") \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "82cba189-591d-4498-a606-32dd9c2d6e8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['id', 'dateandtime', 'skiarea_id', 'skiarea_name', 'day_of_year',\n",
       "       'minute_of_day', 'year', 'season', 'difficulty', 'cause', 'town',\n",
       "       'province', 'gender', 'equipment', 'helmet', 'destination', 'diagnosis',\n",
       "       'india', 'age', 'country', 'injury_side', 'injury_general_location',\n",
       "       'evacuation_vehicles', 'weather', 'snow_condition', 'iii', 'hour',\n",
       "       'dow'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "555d73b3-8b99-4cd3-92e3-216be4b4d459",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "14363528-3d2b-4c4f-ae4b-a3abb5943b57",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_orig = df.copy()\n",
    "with open('../src/test/metadata.pkl','rb') as f:\n",
    "    to_remove,use_small,evacuations,encoders = pickle.load(f)\n",
    "\n",
    "for c in evacuations:\n",
    "    df[c] = False\n",
    "df['other']  = False\n",
    "for i,row in df.iterrows():\n",
    "    evacuation = row.evacuation_vehicles\n",
    "    for c in evacuation:\n",
    "        df.loc[i,c] = True\n",
    "        \n",
    "    for c in evacuation:\n",
    "        if c not in evacuations:\n",
    "            df.loc[i,'other'] = True\n",
    "            brea\n",
    "\n",
    "df.drop(columns='evacuation_vehicles', inplace=True)\n",
    "\n",
    "\n",
    "df['age'] =  df['age'].astype(np.float32)\n",
    "\n",
    "\n",
    "\n",
    "for c in df.columns:\n",
    "    if c not in ['india','age','season','skiarea_name','destination']:\n",
    "        df[c] = df[c].astype('str')  \n",
    "if use_small:\n",
    "    for c in to_remove.keys():\n",
    "        for k in to_remove[c]:\n",
    "            df.loc[df[c]==k,c] = 'other'\n",
    "if use_small:\n",
    "    for c in encoders['small']:\n",
    "        df.loc[~df[c].isin( encoders['small'][c].classes_),c] = 'other'\n",
    "else:\n",
    "    for c in encoders['normal']:\n",
    "        df.loc[~df[c].isin( encoders['normal'][c].classes_),c] = 'other'\n",
    "\n",
    "bst_FS = xgb.Booster()\n",
    "bst_FS.load_model(\"../src/test/model.json\")\n",
    "for c in df.columns:\n",
    "    if c not in ['age','season','skiarea_name','india']:\n",
    "        df[c] =  df[c].fillna('None')\n",
    "        if use_small:\n",
    "            if c in bst_FS.feature_names:\n",
    "                df[c] = pd.Categorical( encoders['small'][c].transform(df[c]), categories=list(range(len(encoders['small'][c].classes_))), ordered=False)\n",
    "        else:\n",
    "            if c in bst_FS.feature_names:\n",
    "                df[c] = pd.Categorical( encoders['normal'][c].transform(df[c]), categories=list(range(len(encoders['normal'][c].classes_))), ordered=False)\n",
    "\n",
    "\n",
    "\n",
    "dtest_FS = xgb.DMatrix(df[bst_FS.feature_names],enable_categorical=True)\n",
    "preds = bst_FS.predict(dtest_FS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "656a1519-7f4d-4e22-a8c0-408cff1d8e90",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['computed_SI'] = preds.argmax(1)\n",
    "df_orig['computed_SI'] = preds.argmax(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c736c86-78c9-42bf-a7f7-47e18b5f3210",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "839561a1-5482-4675-bb53-4e2ce55409f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_101408/3067476809.py:2: SettingWithCopyWarning:\n",
      "\n",
      "\n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.8162839248434238"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp = df[~pd.isnull(df.india)]\n",
    "tmp.india = tmp.india.apply(lambda x:int(x[1]))\n",
    "accuracy_score(tmp.india, tmp.computed_SI)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89dad89d-b425-4969-b647-6a4f8ced343f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9fef4151-e178-439b-b2c4-b4ffb93c1309",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_71942/1056303979.py:25: FutureWarning:\n",
      "\n",
      "ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "\n",
      "/tmp/ipykernel_71942/1056303979.py:25: SettingWithCopyWarning:\n",
      "\n",
      "\n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1020px\"\n",
       "    height=\"1020\"\n",
       "    src=\"iframe_figures/figure_35.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "diagnosis = plot_rr(df_orig,'skiarea_id','computed_SI')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "170e8f70-ec31-4989-a534-5411c15465ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_71942/1056303979.py:25: FutureWarning:\n",
      "\n",
      "ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "\n",
      "/tmp/ipykernel_71942/1056303979.py:25: SettingWithCopyWarning:\n",
      "\n",
      "\n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1020px\"\n",
       "    height=\"1020\"\n",
       "    src=\"iframe_figures/figure_36.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "diagnosis = plot_rr(df_orig,'dow','computed_SI')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2f1d3beb-241b-4eb0-8fec-379603db2bcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_71942/1056303979.py:2: FutureWarning:\n",
      "\n",
      "The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "\n",
      "/tmp/ipykernel_71942/1056303979.py:25: FutureWarning:\n",
      "\n",
      "ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "\n",
      "/tmp/ipykernel_71942/1056303979.py:25: SettingWithCopyWarning:\n",
      "\n",
      "\n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1020px\"\n",
       "    height=\"1020\"\n",
       "    src=\"iframe_figures/figure_37.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>tot</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>helicopter</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>16</td>\n",
       "      <td>184</td>\n",
       "      <td>52</td>\n",
       "      <td>0</td>\n",
       "      <td>252</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0    1   2  3  tot\n",
       "helicopter                     \n",
       "0           16  184  52  0  252"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diagnosis = plot_rr(tmp,'helicopter','computed_SI')\n",
    "diagnosis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a067c790-be3f-4abd-b67b-3a8957fae201",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_71942/1056303979.py:2: FutureWarning:\n",
      "\n",
      "The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "\n",
      "/tmp/ipykernel_71942/1056303979.py:25: FutureWarning:\n",
      "\n",
      "ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "\n",
      "/tmp/ipykernel_71942/1056303979.py:25: SettingWithCopyWarning:\n",
      "\n",
      "\n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1020px\"\n",
       "    height=\"1020\"\n",
       "    src=\"iframe_figures/figure_38.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>tot</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>helicopter</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3747</td>\n",
       "      <td>13926</td>\n",
       "      <td>4049</td>\n",
       "      <td>13</td>\n",
       "      <td>19</td>\n",
       "      <td>21754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>428</td>\n",
       "      <td>138</td>\n",
       "      <td>0</td>\n",
       "      <td>570</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0      1     2    3   4    tot\n",
       "helicopter                                   \n",
       "0           3747  13926  4049   13  19  21754\n",
       "1              3      1   428  138   0    570"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diagnosis = plot_rr(df,'helicopter','computed_SI')\n",
    "diagnosis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df8745b6-2aad-477a-bbe7-d16b77fa512a",
   "metadata": {},
   "outputs": [],
   "source": [
    "0\t1243\t11696\t4646\t57\t5\t17647\n",
    "1\t5\t0\t621\t144\t2\t772"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "d90d65eb-12a6-40a8-a68b-131a8a4070c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>computed_SI</th>\n",
       "      <th>skiarea_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>10385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>40284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>13569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   computed_SI  skiarea_id\n",
       "0            0       10385\n",
       "1            1       40284\n",
       "2            2       13569\n",
       "3            3         497\n",
       "4            4          62"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('computed_SI').skiarea_id.count().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "4dc947a9-5b16-41e5-adc4-88da08501314",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>india</th>\n",
       "      <th>skiarea_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>i0</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>i1</td>\n",
       "      <td>355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>i2</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>i3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  india  skiarea_id\n",
       "0    i0          15\n",
       "1    i1         355\n",
       "2    i2         105\n",
       "3    i3           4"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('india').skiarea_id.count().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ed8a05a-7128-496e-943f-38630fc783ca",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ec40f03-bf09-44a1-8322-1e778ab30f23",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cafaf56-8819-478f-bd7e-0a1abd07e5f0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "a7994f45-b384-4551-9d29-4f221d26592e",
   "metadata": {},
   "outputs": [],
   "source": [
    "## posso anche vedere se ci sono delle aree sciistiche con pattern simili!\n",
    "diagnosis = df[df.year>2015].groupby(['skiarea_id','diagnosis']).age.count().reset_index()\n",
    "diagnosis = diagnosis.pivot(columns='diagnosis',values='age',index='skiarea_id').reset_index().fillna(0)\n",
    "diagnosis.columns.name = None\n",
    "diagnosis.index.name = None\n",
    "diagnosis.skiarea_id=diagnosis.skiarea_id.apply(lambda x:str(int(float(x))))\n",
    "diagnosis = diagnosis.set_index('skiarea_id')\n",
    "diagnosis['tot'] = diagnosis.sum(axis=1)\n",
    "diagnosis = diagnosis[diagnosis.tot>20]\n",
    "diagnosis = diagnosis.apply(lambda x:x/x.tot,axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "9b28f525-d393-4d54-aaef-434a045252fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>abdominal_pain</th>\n",
       "      <th>anterior_cruciate_ligament</th>\n",
       "      <th>bruise</th>\n",
       "      <th>bruised_wound</th>\n",
       "      <th>burn</th>\n",
       "      <th>cardiovascular_problem</th>\n",
       "      <th>chest_pain</th>\n",
       "      <th>compound_fracture</th>\n",
       "      <th>concussion</th>\n",
       "      <th>crush</th>\n",
       "      <th>...</th>\n",
       "      <th>other</th>\n",
       "      <th>paralysis</th>\n",
       "      <th>penetrating_wound</th>\n",
       "      <th>pulse_alteration</th>\n",
       "      <th>respiratory_problems</th>\n",
       "      <th>trauma</th>\n",
       "      <th>trauma_crane</th>\n",
       "      <th>unharmed</th>\n",
       "      <th>wound</th>\n",
       "      <th>tot</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>skiarea_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000743</td>\n",
       "      <td>0.215825</td>\n",
       "      <td>0.001114</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.001857</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.151560</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000743</td>\n",
       "      <td>0.000371</td>\n",
       "      <td>0.000371</td>\n",
       "      <td>0.007058</td>\n",
       "      <td>0.008172</td>\n",
       "      <td>0.073923</td>\n",
       "      <td>0.052006</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.001193</td>\n",
       "      <td>0.008948</td>\n",
       "      <td>0.241996</td>\n",
       "      <td>0.003778</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000994</td>\n",
       "      <td>0.001193</td>\n",
       "      <td>0.000199</td>\n",
       "      <td>0.000994</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.117916</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.001193</td>\n",
       "      <td>0.000398</td>\n",
       "      <td>0.000398</td>\n",
       "      <td>0.005170</td>\n",
       "      <td>0.035594</td>\n",
       "      <td>0.053887</td>\n",
       "      <td>0.053887</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000576</td>\n",
       "      <td>0.000863</td>\n",
       "      <td>0.241151</td>\n",
       "      <td>0.002158</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000144</td>\n",
       "      <td>0.000432</td>\n",
       "      <td>0.003022</td>\n",
       "      <td>0.000719</td>\n",
       "      <td>0.000144</td>\n",
       "      <td>...</td>\n",
       "      <td>0.054532</td>\n",
       "      <td>0.000144</td>\n",
       "      <td>0.000576</td>\n",
       "      <td>0.000288</td>\n",
       "      <td>0.000144</td>\n",
       "      <td>0.003309</td>\n",
       "      <td>0.012950</td>\n",
       "      <td>0.021439</td>\n",
       "      <td>0.032086</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.004695</td>\n",
       "      <td>0.247261</td>\n",
       "      <td>0.000782</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.002347</td>\n",
       "      <td>0.003912</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.091549</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.001565</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.007825</td>\n",
       "      <td>0.007825</td>\n",
       "      <td>0.045383</td>\n",
       "      <td>0.034429</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000242</td>\n",
       "      <td>0.246496</td>\n",
       "      <td>0.001450</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000242</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000967</td>\n",
       "      <td>0.000483</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.041808</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000242</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.007975</td>\n",
       "      <td>0.024891</td>\n",
       "      <td>0.044949</td>\n",
       "      <td>0.048574</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>159</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.123288</td>\n",
       "      <td>0.027397</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.006849</td>\n",
       "      <td>0.006849</td>\n",
       "      <td>0.020548</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.150685</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.034247</td>\n",
       "      <td>0.239726</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>162</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.295455</td>\n",
       "      <td>0.007576</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.007576</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.015152</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.007576</td>\n",
       "      <td>0.037879</td>\n",
       "      <td>0.022727</td>\n",
       "      <td>0.068182</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>166</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.181818</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.045455</td>\n",
       "      <td>0.090909</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.045455</td>\n",
       "      <td>0.045455</td>\n",
       "      <td>0.045455</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.027149</td>\n",
       "      <td>0.176471</td>\n",
       "      <td>0.018100</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.004525</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.018100</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.013575</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.027149</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.067873</td>\n",
       "      <td>0.004525</td>\n",
       "      <td>0.018100</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>170</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.235772</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.008130</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.048780</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.016260</td>\n",
       "      <td>0.130081</td>\n",
       "      <td>0.016260</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>71 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            abdominal_pain  anterior_cruciate_ligament    bruise  \\\n",
       "skiarea_id                                                         \n",
       "1                 0.000000                    0.000743  0.215825   \n",
       "2                 0.001193                    0.008948  0.241996   \n",
       "3                 0.000576                    0.000863  0.241151   \n",
       "4                 0.000000                    0.004695  0.247261   \n",
       "5                 0.000000                    0.000242  0.246496   \n",
       "...                    ...                         ...       ...   \n",
       "159               0.000000                    0.000000  0.123288   \n",
       "162               0.000000                    0.000000  0.295455   \n",
       "166               0.000000                    0.000000  0.181818   \n",
       "167               0.000000                    0.027149  0.176471   \n",
       "170               0.000000                    0.000000  0.235772   \n",
       "\n",
       "            bruised_wound  burn  cardiovascular_problem  chest_pain  \\\n",
       "skiarea_id                                                            \n",
       "1                0.001114   0.0                0.000000    0.000000   \n",
       "2                0.003778   0.0                0.000994    0.001193   \n",
       "3                0.002158   0.0                0.000144    0.000432   \n",
       "4                0.000782   0.0                0.000000    0.002347   \n",
       "5                0.001450   0.0                0.000242    0.000000   \n",
       "...                   ...   ...                     ...         ...   \n",
       "159              0.027397   0.0                0.006849    0.006849   \n",
       "162              0.007576   0.0                0.000000    0.000000   \n",
       "166              0.000000   0.0                0.000000    0.045455   \n",
       "167              0.018100   0.0                0.000000    0.004525   \n",
       "170              0.000000   0.0                0.000000    0.008130   \n",
       "\n",
       "            compound_fracture  concussion     crush  ...     other  paralysis  \\\n",
       "skiarea_id                                           ...                        \n",
       "1                    0.001857    0.000000  0.000000  ...  0.151560   0.000000   \n",
       "2                    0.000199    0.000994  0.000000  ...  0.117916   0.000000   \n",
       "3                    0.003022    0.000719  0.000144  ...  0.054532   0.000144   \n",
       "4                    0.003912    0.000000  0.000000  ...  0.091549   0.000000   \n",
       "5                    0.000967    0.000483  0.000000  ...  0.041808   0.000000   \n",
       "...                       ...         ...       ...  ...       ...        ...   \n",
       "159                  0.020548    0.000000  0.000000  ...  0.150685   0.000000   \n",
       "162                  0.000000    0.007576  0.000000  ...  0.015152   0.000000   \n",
       "166                  0.090909    0.000000  0.000000  ...  0.000000   0.000000   \n",
       "167                  0.000000    0.018100  0.000000  ...  0.013575   0.000000   \n",
       "170                  0.000000    0.000000  0.000000  ...  0.048780   0.000000   \n",
       "\n",
       "            penetrating_wound  pulse_alteration  respiratory_problems  \\\n",
       "skiarea_id                                                              \n",
       "1                    0.000743          0.000371              0.000371   \n",
       "2                    0.001193          0.000398              0.000398   \n",
       "3                    0.000576          0.000288              0.000144   \n",
       "4                    0.000000          0.001565              0.000000   \n",
       "5                    0.000242          0.000000              0.000000   \n",
       "...                       ...               ...                   ...   \n",
       "159                  0.000000          0.000000              0.000000   \n",
       "162                  0.000000          0.000000              0.000000   \n",
       "166                  0.000000          0.000000              0.000000   \n",
       "167                  0.027149          0.000000              0.000000   \n",
       "170                  0.000000          0.000000              0.000000   \n",
       "\n",
       "              trauma  trauma_crane  unharmed     wound  tot  \n",
       "skiarea_id                                                   \n",
       "1           0.007058      0.008172  0.073923  0.052006  1.0  \n",
       "2           0.005170      0.035594  0.053887  0.053887  1.0  \n",
       "3           0.003309      0.012950  0.021439  0.032086  1.0  \n",
       "4           0.007825      0.007825  0.045383  0.034429  1.0  \n",
       "5           0.007975      0.024891  0.044949  0.048574  1.0  \n",
       "...              ...           ...       ...       ...  ...  \n",
       "159         0.000000      0.034247  0.239726  0.000000  1.0  \n",
       "162         0.007576      0.037879  0.022727  0.068182  1.0  \n",
       "166         0.000000      0.045455  0.045455  0.045455  1.0  \n",
       "167         0.000000      0.067873  0.004525  0.018100  1.0  \n",
       "170         0.000000      0.016260  0.130081  0.016260  1.0  \n",
       "\n",
       "[71 rows x 33 columns]"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diagnosis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "9e81dcdb-146f-4f12-a78a-1bdc40d05750",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.matrix.ClusterGrid at 0x7f7f4502e8a0>"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 114152 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "sns.clustermap(diagnosis)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "5ce83040-3807-4f1e-af7e-0b8414fa525d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_218931/2034767285.py:21: FutureWarning:\n",
      "\n",
      "ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "diagnosis = df.groupby(['skiarea_id','diagnosis']).age.count().reset_index()\n",
    "diagnosis = diagnosis.pivot(columns='diagnosis',values='age',index='skiarea_id').reset_index().fillna(0)\n",
    "diagnosis.columns.name = None\n",
    "diagnosis.index.name = None\n",
    "diagnosis.skiarea_id=diagnosis.skiarea_id.apply(lambda x:str(int(float(x))))\n",
    "diagnosis = diagnosis.set_index('skiarea_id')\n",
    "diagnosis['tot'] = diagnosis.sum(axis=1)\n",
    "diagnosis = diagnosis[diagnosis.tot>100]\n",
    "sus=[]\n",
    "tots = pd.DataFrame(diagnosis.sum(axis=0),columns=['n'])\n",
    "from scipy.stats.contingency import relative_risk\n",
    "for i in range(diagnosis.shape[0]):\n",
    "    for j in range(diagnosis.shape[1]-1):\n",
    "        result = relative_risk(int(diagnosis.values[i,j]), int(diagnosis.values[i,-1]), int(tots.values[j][0]), int(tots.values[-1][0]))\n",
    "        ci = result.confidence_interval(confidence_level=0.95)\n",
    "        if ((ci[0]>1) & (ci[1]>1)) | ((ci[0]<1) & (ci[1]<1)):\n",
    "            sus.append({'skiarea':diagnosis.index[i],'diagnosis':diagnosis.columns[j],'rr':np.round(result.relative_risk,2)})\n",
    "sus = pd.DataFrame(sus)   \n",
    "import matplotlib.pyplot as plt\n",
    "import plotly.express as px\n",
    "sus.rr[sus.rr>10] = 10\n",
    "ss = sus.pivot(columns='diagnosis',values='rr',index='skiarea').reset_index().fillna(1)\n",
    "#plt.imshow(ss.values[:,2:].astype(float),aspect='auto')\n",
    "ss.index = ss.skiarea\n",
    "ss = ss.drop(columns='skiarea')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "b8afc3be-c951-4982-b1bc-af8b006f3ba8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.matrix.ClusterGrid at 0x7f7f56cbaa80>"
      ]
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 111904 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.clustermap(ss)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "64415581-e2c8-49a2-ae9b-0a60b2a10eef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1220px\"\n",
       "    height=\"1220\"\n",
       "    src=\"iframe_figures/figure_206.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import dash_bio\n",
    "\n",
    "dash_bio.Clustergram(\n",
    "    data=ss,\n",
    "    center_values=False,\n",
    "\n",
    "    column_labels=list(ss.columns.values),\n",
    "    row_labels=list(ss.index),\n",
    "    height=1200,\n",
    "    width=1200,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "46682368-1321-424b-a979-276805c8f27b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_218931/2465247991.py:22: FutureWarning:\n",
      "\n",
      "ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1220px\"\n",
       "    height=\"1220\"\n",
       "    src=\"iframe_figures/figure_196.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "diagnosis = df_orig.groupby(['skiarea_id','computed_SI']).age.count().reset_index()\n",
    "diagnosis = diagnosis.pivot(columns='computed_SI',values='age',index='skiarea_id').reset_index().fillna(0)\n",
    "diagnosis.columns.name = None\n",
    "diagnosis.index.name = None\n",
    "diagnosis.skiarea_id=diagnosis.skiarea_id.apply(lambda x:str(int(float(x))))\n",
    "\n",
    "diagnosis = diagnosis.set_index('skiarea_id')\n",
    "diagnosis['tot'] = diagnosis.sum(axis=1)\n",
    "diagnosis = diagnosis[diagnosis.tot>100]\n",
    "sus = []\n",
    "tots = pd.DataFrame(diagnosis.sum(axis=0),columns=['n'])\n",
    "from scipy.stats.contingency import relative_risk\n",
    "for i in range(diagnosis.shape[0]):\n",
    "    for j in range(diagnosis.shape[1]-1):\n",
    "        result = relative_risk(int(diagnosis.values[i,j]), int(diagnosis.values[i,-1]), int(tots.values[j][0]), int(tots.values[-1][0]))\n",
    "        ci = result.confidence_interval(confidence_level=0.95)\n",
    "        if ((ci[0]>1) & (ci[1]>1)) | ((ci[0]<1) & (ci[1]<1)):\n",
    "            sus.append({'skiarea':diagnosis.index[i],'computed_SI':diagnosis.columns[j],'rr':result.relative_risk})\n",
    "sus = pd.DataFrame(sus)   \n",
    "import matplotlib.pyplot as plt\n",
    "import plotly.express as px\n",
    "sus.rr[sus.rr>10] = 10\n",
    "ss = sus.pivot(columns='computed_SI',values='rr',index='skiarea').reset_index().fillna(1)\n",
    "#plt.imshow(ss.values[:,2:].astype(float),aspect='auto')\n",
    "ss.index = ss.skiarea\n",
    "ss = ss.drop(columns='skiarea')\n",
    "fig = px.imshow(ss,width=1200, height=1200, aspect=\"auto\",  color_continuous_scale=color_list)\n",
    "fig.update_coloraxes(showscale=True)\n",
    "#plt.colorbar()\n",
    "#x = ss.columns[2:]\n",
    "#y = ss.skiarea.values\n",
    "#plt.xticks(range(len(x)), x, fontsize=12);\n",
    "#plt.yticks(range(len(y)), y, fontsize=12);\n",
    "import plotly.io as pio\n",
    "pio.renderers.default = 'iframe'\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "id": "f14d0b7e-140e-4726-bdf9-9ee1db706728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2031521389902816"
      ]
     },
     "execution_count": 203,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ss.values.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "id": "8ccde0a2-bca0-4d10-90da-933c4c211a28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"1220px\"\n",
       "    height=\"1220\"\n",
       "    src=\"iframe_figures/figure_204.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import dash_bio\n",
    "\n",
    "dash_bio.Clustergram(\n",
    "    data=ss,\n",
    "    center_values=False,\n",
    "    column_labels=list(ss.columns.values),\n",
    "    row_labels=list(ss.index),\n",
    "    height=1200,\n",
    "    width=1200,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25f5206d-f605-43a3-b247-5ac51f37dc08",
   "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
