{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Social Signature\n",
    "\n",
    "We can calculate a social signature from email activity. First we'll load the example email data from the sample data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/u/24/rantahj1/unix/src/niimpy/niimpy/reading/google_takeout.py:491: UserWarning: Could not parse message timestamp: 2023-12-15 12:19:43+00:00\n",
      "  warnings.warn(f\"Could not parse message timestamp: {received}\")\n",
      "/u/24/rantahj1/unix/src/niimpy/niimpy/reading/google_takeout.py:505: UserWarning: Failed to format received time: Sat, 15 DeNot a timec 2023 12:19:43 0000\n",
      "  warnings.warn(f\"Failed to format received time: {received}\")\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import niimpy\n",
    "from niimpy import config\n",
    "\n",
    "path = os.path.join(config.GOOGLE_TAKEOUT_DIR, \"Takeout\", \"Mail\", \"All mail Including Spam and Trash.mbox\")\n",
    "data = niimpy.reading.google_takeout.email_activity(path, sentiment=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Email data contains a \"to\" and and a \"from\" column. This data is\n",
    "pseudonymized: the email addresses are replaced by integer ids.\n",
    "The user is represented as ID 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "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>from</th>\n",
       "      <th>to</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-12-15 12:19:43+00:00</th>\n",
       "      <td>0</td>\n",
       "      <td>[6]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-15 12:29:43+00:00</th>\n",
       "      <td>0</td>\n",
       "      <td>[2, 6]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-15 12:29:43+00:00</th>\n",
       "      <td>0</td>\n",
       "      <td>[2, 6]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-15 12:39:43+00:00</th>\n",
       "      <td>2</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-15 12:39:43+00:00</th>\n",
       "      <td>2</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           from      to\n",
       "timestamp                              \n",
       "2023-12-15 12:19:43+00:00     0     [6]\n",
       "2023-12-15 12:29:43+00:00     0  [2, 6]\n",
       "2023-12-15 12:29:43+00:00     0  [2, 6]\n",
       "2023-12-15 12:39:43+00:00     2     [0]\n",
       "2023-12-15 12:39:43+00:00     2     [0]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[[\"from\", \"to\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "to\n",
       "2    0.4\n",
       "6    0.6\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import niimpy.analysis.social_signature\n",
    "\n",
    "niimpy.analysis.social_signature.social_signature(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rythms\n",
    "\n",
    "The `rythm` function in `niimpy.analysis.rhythms` calculates general rhythms from different types of input data. It\n",
    " - Resamples the data into bins. For example, if the bin is 1 day, each row will represent a day.\n",
    " - Accumulates the binned historical data to a certain time period. For example, if the period is a week and the bin is a day, the first row will be the sum of data for the first day of the week, the second row the sum for the second day of the week and so on.\n",
    " - Calculate the percentage each bin represents of a third period, frequency. In the above example, if the frequency is 2 days, the data of the first two rows is scaled to sum to 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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>user</th>\n",
       "      <th>device</th>\n",
       "      <th>time</th>\n",
       "      <th>call_type</th>\n",
       "      <th>call_duration</th>\n",
       "      <th>datetime</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-08 22:32:25.256999969+03:00</th>\n",
       "      <td>iGyXetHE3S8u</td>\n",
       "      <td>Cq9vueHh3zVs</td>\n",
       "      <td>1.565293e+09</td>\n",
       "      <td>incoming</td>\n",
       "      <td>1217</td>\n",
       "      <td>2019-08-08 22:32:25.256999969+03:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-08 22:53:35.107000113+03:00</th>\n",
       "      <td>iGyXetHE3S8u</td>\n",
       "      <td>Cq9vueHh3zVs</td>\n",
       "      <td>1.565294e+09</td>\n",
       "      <td>incoming</td>\n",
       "      <td>383</td>\n",
       "      <td>2019-08-08 22:53:35.107000113+03:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-08 22:31:34.539999962+03:00</th>\n",
       "      <td>iGyXetHE3S8u</td>\n",
       "      <td>Cq9vueHh3zVs</td>\n",
       "      <td>1.565293e+09</td>\n",
       "      <td>incoming</td>\n",
       "      <td>1142</td>\n",
       "      <td>2019-08-08 22:31:34.539999962+03:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-08 22:43:45.834000111+03:00</th>\n",
       "      <td>iGyXetHE3S8u</td>\n",
       "      <td>Cq9vueHh3zVs</td>\n",
       "      <td>1.565293e+09</td>\n",
       "      <td>incoming</td>\n",
       "      <td>1170</td>\n",
       "      <td>2019-08-08 22:43:45.834000111+03:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-08 22:55:33.053999901+03:00</th>\n",
       "      <td>iGyXetHE3S8u</td>\n",
       "      <td>Cq9vueHh3zVs</td>\n",
       "      <td>1.565294e+09</td>\n",
       "      <td>incoming</td>\n",
       "      <td>497</td>\n",
       "      <td>2019-08-08 22:55:33.053999901+03:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             user        device          time  \\\n",
       "2019-08-08 22:32:25.256999969+03:00  iGyXetHE3S8u  Cq9vueHh3zVs  1.565293e+09   \n",
       "2019-08-08 22:53:35.107000113+03:00  iGyXetHE3S8u  Cq9vueHh3zVs  1.565294e+09   \n",
       "2019-08-08 22:31:34.539999962+03:00  iGyXetHE3S8u  Cq9vueHh3zVs  1.565293e+09   \n",
       "2019-08-08 22:43:45.834000111+03:00  iGyXetHE3S8u  Cq9vueHh3zVs  1.565293e+09   \n",
       "2019-08-08 22:55:33.053999901+03:00  iGyXetHE3S8u  Cq9vueHh3zVs  1.565294e+09   \n",
       "\n",
       "                                    call_type  call_duration  \\\n",
       "2019-08-08 22:32:25.256999969+03:00  incoming           1217   \n",
       "2019-08-08 22:53:35.107000113+03:00  incoming            383   \n",
       "2019-08-08 22:31:34.539999962+03:00  incoming           1142   \n",
       "2019-08-08 22:43:45.834000111+03:00  incoming           1170   \n",
       "2019-08-08 22:55:33.053999901+03:00  incoming            497   \n",
       "\n",
       "                                                               datetime  \n",
       "2019-08-08 22:32:25.256999969+03:00 2019-08-08 22:32:25.256999969+03:00  \n",
       "2019-08-08 22:53:35.107000113+03:00 2019-08-08 22:53:35.107000113+03:00  \n",
       "2019-08-08 22:31:34.539999962+03:00 2019-08-08 22:31:34.539999962+03:00  \n",
       "2019-08-08 22:43:45.834000111+03:00 2019-08-08 22:43:45.834000111+03:00  \n",
       "2019-08-08 22:55:33.053999901+03:00 2019-08-08 22:55:33.053999901+03:00  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = niimpy.read_csv(config.MULTIUSER_AWARE_CALLS_PATH, tz='Europe/Helsinki')\n",
    "data = data[data[\"user\"] == 'iGyXetHE3S8u']\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: >"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22912 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from niimpy.analysis.rhythms import rhythm\n",
    "\n",
    "duration_rhythm = rhythm(data, cols=[\"call_duration\"], period=\"1D\", freq=\"12h\", bin=\"1h\")\n",
    "duration_rhythm.plot()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "niimpy",
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
