{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from scipy.odr import *\n",
    "from scipy.stats import *\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import time\n",
    "import matplotlib.pyplot as plt\n",
    "import ast\n",
    "from multiprocessing import Pool, cpu_count\n",
    "\n",
    "import scipy\n",
    "\n",
    "from IPython import display\n",
    "from matplotlib.patches import Rectangle\n",
    "\n",
    "from sklearn.metrics import mean_squared_error\n",
    "import json\n",
    "\n",
    "import scipy.stats as st\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "\n",
    "from matplotlib import cm\n",
    "from mpl_toolkits.mplot3d import axes3d\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import copy\n",
    "\n",
    "from sklearn.model_selection import LeaveOneOut, LeavePOut\n",
    "\n",
    "from multiprocessing import Pool\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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>userID</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>Current_Task</th>\n",
       "      <th>Task_amount</th>\n",
       "      <th>TaskID</th>\n",
       "      <th>VersionID</th>\n",
       "      <th>RepetitionID</th>\n",
       "      <th>Actual_Data</th>\n",
       "      <th>Is_Pause</th>\n",
       "      <th>Image</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7919</th>\n",
       "      <td>17</td>\n",
       "      <td>1547138928692</td>\n",
       "      <td>1</td>\n",
       "      <td>680</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7920</th>\n",
       "      <td>17</td>\n",
       "      <td>1547138928735</td>\n",
       "      <td>1</td>\n",
       "      <td>680</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7921</th>\n",
       "      <td>17</td>\n",
       "      <td>1547138928773</td>\n",
       "      <td>1</td>\n",
       "      <td>680</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7922</th>\n",
       "      <td>17</td>\n",
       "      <td>1547138928813</td>\n",
       "      <td>1</td>\n",
       "      <td>680</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7923</th>\n",
       "      <td>17</td>\n",
       "      <td>1547138928861</td>\n",
       "      <td>1</td>\n",
       "      <td>680</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      userID      Timestamp  Current_Task  Task_amount  TaskID  VersionID  \\\n",
       "7919      17  1547138928692             1          680       6          2   \n",
       "7920      17  1547138928735             1          680       6          2   \n",
       "7921      17  1547138928773             1          680       6          2   \n",
       "7922      17  1547138928813             1          680       6          2   \n",
       "7923      17  1547138928861             1          680       6          2   \n",
       "\n",
       "      RepetitionID  Actual_Data  Is_Pause  \\\n",
       "7919             0         True     False   \n",
       "7920             0         True     False   \n",
       "7921             0         True     False   \n",
       "7922             0         True     False   \n",
       "7923             0         True     False   \n",
       "\n",
       "                                                  Image  \n",
       "7919  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
       "7920  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
       "7921  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
       "7922  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  \n",
       "7923  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfAll = pd.read_pickle(\"DataStudyCollection/AllData.pkl\")\n",
    "df = dfAll[(dfAll.Actual_Data == True) & (dfAll.Is_Pause == False)]\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 39 s, sys: 5.78 s, total: 44.8 s\n",
      "Wall time: 43.3 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "def is_max(df):\n",
    "    df_temp = df.copy(deep=True)\n",
    "    max_version = df_temp.RepetitionID.max()\n",
    "    df_temp[\"IsMax\"] = np.where(df_temp.RepetitionID == max_version, True, False)\n",
    "    df_temp[\"MaxRepetition\"] = [max_version] * len(df_temp)\n",
    "    return df_temp\n",
    "\n",
    "df_grp = df.groupby([df.userID, df.TaskID, df.VersionID])\n",
    "pool = Pool(cpu_count() - 1)\n",
    "result_lst = pool.map(is_max, [grp for name, grp in df_grp])\n",
    "df = pd.concat(result_lst)\n",
    "pool.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.Image = df.Image.apply(lambda x: x.reshape(27, 15))\n",
    "df.Image = df.Image.apply(lambda x: x.clip(min=0, max=255))\n",
    "df.Image = df.Image.apply(lambda x: x.astype(np.uint8))\n",
    "df[\"ImageSum\"] = df.Image.apply(lambda x: np.sum(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_pickle(\"DataStudyCollection/dfFiltered.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "recorded actual: 1010014, used data: 851455\n"
     ]
    }
   ],
   "source": [
    "print(\"recorded actual: %s, used data: %s\" % (len(dfAll), len(df)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_pickle(\"DataStudyCollection/dfFiltered.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "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>userID</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>Current_Task</th>\n",
       "      <th>Task_amount</th>\n",
       "      <th>TaskID</th>\n",
       "      <th>VersionID</th>\n",
       "      <th>RepetitionID</th>\n",
       "      <th>Actual_Data</th>\n",
       "      <th>Is_Pause</th>\n",
       "      <th>Image</th>\n",
       "      <th>IsMax</th>\n",
       "      <th>MaxRepetition</th>\n",
       "      <th>ImageSum</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>291980</th>\n",
       "      <td>1</td>\n",
       "      <td>1,54515E+12</td>\n",
       "      <td>33</td>\n",
       "      <td>680</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>291981</th>\n",
       "      <td>1</td>\n",
       "      <td>1,54515E+12</td>\n",
       "      <td>33</td>\n",
       "      <td>680</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>291982</th>\n",
       "      <td>1</td>\n",
       "      <td>1,54515E+12</td>\n",
       "      <td>33</td>\n",
       "      <td>680</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>291983</th>\n",
       "      <td>1</td>\n",
       "      <td>1,54515E+12</td>\n",
       "      <td>33</td>\n",
       "      <td>680</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>291984</th>\n",
       "      <td>1</td>\n",
       "      <td>1,54515E+12</td>\n",
       "      <td>33</td>\n",
       "      <td>680</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>373</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        userID    Timestamp  Current_Task  Task_amount  TaskID  VersionID  \\\n",
       "291980       1  1,54515E+12            33          680       0          2   \n",
       "291981       1  1,54515E+12            33          680       0          2   \n",
       "291982       1  1,54515E+12            33          680       0          2   \n",
       "291983       1  1,54515E+12            33          680       0          2   \n",
       "291984       1  1,54515E+12            33          680       0          2   \n",
       "\n",
       "        RepetitionID  Actual_Data  Is_Pause  \\\n",
       "291980             0         True     False   \n",
       "291981             0         True     False   \n",
       "291982             0         True     False   \n",
       "291983             0         True     False   \n",
       "291984             0         True     False   \n",
       "\n",
       "                                                    Image  IsMax  \\\n",
       "291980  [[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...   True   \n",
       "291981  [[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...   True   \n",
       "291982  [[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...   True   \n",
       "291983  [[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...   True   \n",
       "291984  [[0, 2, 0, 0, 0, 0, 1, 2, 2, 3, 2, 1, 1, 1, 0]...   True   \n",
       "\n",
       "        MaxRepetition  ImageSum  \n",
       "291980              0       307  \n",
       "291981              0       222  \n",
       "291982              0       521  \n",
       "291983              0       318  \n",
       "291984              0       373  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Label if knuckle or finger\n",
    "def f(row):\n",
    "    if row['TaskID'] < 17:\n",
    "        #val = \"Knuckle\"\n",
    "        val = 0\n",
    "    elif row['TaskID'] >= 17:\n",
    "        #val = \"Finger\"\n",
    "        val = 1\n",
    "    return val\n",
    "df['InputMethod'] = df.apply(f, axis=1)\n",
    "\n",
    "def f(row):\n",
    "    if row['TaskID'] < 17:\n",
    "        val = \"Knuckle\"\n",
    "    elif row['TaskID'] >= 17:\n",
    "        val = \"Finger\"\n",
    "    return val\n",
    "df['Input'] = df.apply(f, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Svens new Blob detection\n",
    "def detect_blobs(image, task):\n",
    "    #image = e.Image\n",
    "    large = np.ones((29,17), dtype=np.uint8)\n",
    "    large[1:28,1:16] = np.copy(image)\n",
    "    temp, thresh = cv2.threshold(cv2.bitwise_not(large), 200, 255, cv2.THRESH_BINARY)\n",
    "    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
    "    contours = [a for a in contours if cv2.contourArea(a) > 8 and cv2.contourArea(a) < 255]\n",
    "    lstBlob  = []\n",
    "    lstMin = []\n",
    "    lstMax = []\n",
    "    count = 0\n",
    "    contours.sort(key=lambda a: cv2.contourArea(a))\n",
    "    if len(contours) > 0:\n",
    "        # if two finger or knuckle\n",
    "        cont_count = 2 if task in [1, 6, 7, 18, 23, 24] and len(contours) > 1 else 1\n",
    "        for i in range(1, cont_count + 1):\n",
    "            max_contour = contours[-1 * i]\n",
    "            xmax, ymax = np.max(max_contour.reshape(len(max_contour),2), axis=0)\n",
    "            xmin, ymin = np.min(max_contour.reshape(len(max_contour),2), axis=0)\n",
    "            #croped_im = np.zeros((27,15))\n",
    "            blob = large[max(ymin - 1, 0):min(ymax + 1, large.shape[0]),max(xmin - 1, 0):min(xmax + 1, large.shape[1])]\n",
    "            #croped_im[0:blob.shape[0],0:blob.shape[1]] = blob\n",
    "            #return (1, [croped_im])\n",
    "            lstBlob.append(blob)\n",
    "            lstMin.append(xmax-xmin)\n",
    "            lstMax.append(ymax-ymin)\n",
    "            count = count + 1\n",
    "        return (count, lstBlob, lstMin, lstMax)\n",
    "    else:\n",
    "        return (0, [np.zeros((29, 19))], 0, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 11.9 s, sys: 7.51 s, total: 19.4 s\n",
      "Wall time: 18.6 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "pool = Pool(os.cpu_count()-2)\n",
    "temp_blobs = pool.starmap(detect_blobs, zip(df.Image, df.TaskID))\n",
    "pool.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"BlobCount\"] = [a[0] for a in temp_blobs]\n",
    "df[\"BlobImages\"] = [a[1] for a in temp_blobs]\n",
    "df[\"BlobW\"] = [a[2] for a in temp_blobs]\n",
    "df[\"BlobH\"] = [a[3] for a in temp_blobs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    710145\n",
       "1    128117\n",
       "2     13193\n",
       "Name: BlobCount, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.BlobCount.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfX = df[(df.BlobCount == 1)].copy(deep=True)\n",
    "dfX.BlobImages = dfX.BlobImages.apply(lambda x : x[0])\n",
    "dfX.BlobW = dfX.BlobW.apply(lambda x : x[0])\n",
    "dfX.BlobH = dfX.BlobH.apply(lambda x : x[0])\n",
    "\n",
    "dfY = df[(df.BlobCount == 2)].copy(deep=True)\n",
    "dfY.BlobImages = dfY.BlobImages.apply(lambda x : x[0])\n",
    "dfY.BlobW = dfY.BlobW.apply(lambda x : x[0])\n",
    "dfY.BlobH = dfY.BlobH.apply(lambda x : x[0])\n",
    "\n",
    "dfZ = df[(df.BlobCount == 2)].copy(deep=True)\n",
    "dfZ.BlobImages = dfZ.BlobImages.apply(lambda x : x[1])\n",
    "dfZ.BlobW = dfZ.BlobW.apply(lambda x : x[1])\n",
    "dfZ.BlobH = dfZ.BlobH.apply(lambda x : x[1])\n",
    "\n",
    "df = dfX.append([dfY, dfZ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sample Size not Argumented: 154503\n"
     ]
    }
   ],
   "source": [
    "print(\"Sample Size not Argumented:\", len(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"BlobArea\"] = df[\"BlobW\"] * df[\"BlobH\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    154503.0\n",
       "mean         15.8\n",
       "std           5.1\n",
       "min          12.0\n",
       "25%          12.0\n",
       "50%          16.0\n",
       "75%          16.0\n",
       "max         110.0\n",
       "Name: BlobArea, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.BlobArea.describe().round(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "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>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Input</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>Finger</th>\n",
       "      <td>110839.0</td>\n",
       "      <td>16.6</td>\n",
       "      <td>5.3</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>110.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Knuckle</th>\n",
       "      <td>43664.0</td>\n",
       "      <td>13.7</td>\n",
       "      <td>3.7</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>72.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            count  mean  std   min   25%   50%   75%    max\n",
       "Input                                                      \n",
       "Finger   110839.0  16.6  5.3  12.0  12.0  16.0  16.0  110.0\n",
       "Knuckle   43664.0  13.7  3.7  12.0  12.0  12.0  16.0   72.0"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"Input\").BlobArea.describe().round(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"BlobSum\"] = df.BlobImages.apply(lambda x: np.sum(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.BlobSum.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fef38ed5908>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9572 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df.BlobSum.hist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Small / Blobs where the pixels are only a \"little\" hit\n",
    "dfX = df[df.BlobSum <= 255]\n",
    "len(dfX)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Augmenting by flipping in both axis (datax4)\n",
    "df[\"Version\"] = \"Normal\"\n",
    "dfFlipped = df.copy(deep=True)\n",
    "dfFlipped.BlobImages = dfFlipped.BlobImages.apply(lambda x: np.flipud(x))\n",
    "dfFlipped[\"Version\"] = \"FlippedUD\"\n",
    "df = df.append(dfFlipped)\n",
    "dfFlipped = df.copy(deep=True)\n",
    "dfFlipped.BlobImages = dfFlipped.BlobImages.apply(lambda x: np.fliplr(x))\n",
    "dfFlipped[\"Version\"] = \"FlippedLR\"\n",
    "df = df.append(dfFlipped)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sample Size argumented: 618012\n"
     ]
    }
   ],
   "source": [
    "print(\"Sample Size argumented:\", len(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pasteToEmpty (blob):\n",
    "    croped_im = np.zeros((27,15))\n",
    "    croped_im[0:blob.shape[0],0:blob.shape[1]] = blob\n",
    "    return croped_im"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"Blobs\"] = df.BlobImages.apply(lambda x: pasteToEmpty(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_pickle(\"DataStudyCollection/df_statistics.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[[\"userID\", \"TaskID\", \"Version\", \"Blobs\", \"InputMethod\"]].to_pickle(\"DataStudyCollection/df_blobs_area.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# display blobs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "userID                                                           1\n",
      "Timestamp                                              1,54515E+12\n",
      "Current_Task                                                   121\n",
      "Task_amount                                                    680\n",
      "TaskID                                                           0\n",
      "VersionID                                                        7\n",
      "RepetitionID                                                     0\n",
      "Actual_Data                                                   True\n",
      "Is_Pause                                                     False\n",
      "Image            [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]...\n",
      "IsMax                                                         True\n",
      "MaxRepetition                                                    0\n",
      "ImageSum                                                      1495\n",
      "BlobCount                                                        1\n",
      "BlobImages       [[2, 2, 11, 11, 2], [2, 9, 40, 42, 9], [4, 13,...\n",
      "BlobW                                                            3\n",
      "BlobH                                                            4\n",
      "BlobArea                                                        12\n",
      "BlobSum                                                       1071\n",
      "Version                                                     Normal\n",
      "Blobs            [[2.0, 2.0, 11.0, 11.0, 2.0, 0.0, 0.0, 0.0, 0....\n",
      "InputMethod                                                      0\n",
      "Input                                                      Knuckle\n",
      "Name: 299548, dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 432x288 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 5780 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.clf()\n",
    "plt.figure(figsize=(6, 6))\n",
    "ax = plt.gca()\n",
    "data_point = 100\n",
    "data = df.Blobs.iloc[data_point]\n",
    "print(df.iloc[data_point])\n",
    "plt.imshow(data, cmap='gray', vmin=0, vmax=255)\n",
    "# Loop over data dimensions and create text annotations.\n",
    "for i in range(0, data.shape[0]):\n",
    "    for j in range(0, data.shape[1]):\n",
    "        text = ax.text(j, i, int(data[i, j]),\n",
    "                       ha=\"center\", va=\"center\", color=\"cyan\", fontsize=1)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
