{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from multiprocessing import Pool, cpu_count\n",
    "\n",
    "import copy\n",
    "\n",
    "import cv2"
   ]
  },
  {
   "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>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>56454</th>\n",
       "      <td>12</td>\n",
       "      <td>1553865148939</td>\n",
       "      <td>1</td>\n",
       "      <td>510</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56455</th>\n",
       "      <td>12</td>\n",
       "      <td>1553865148981</td>\n",
       "      <td>1</td>\n",
       "      <td>510</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56456</th>\n",
       "      <td>12</td>\n",
       "      <td>1553865149021</td>\n",
       "      <td>1</td>\n",
       "      <td>510</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56457</th>\n",
       "      <td>12</td>\n",
       "      <td>1553865149060</td>\n",
       "      <td>1</td>\n",
       "      <td>510</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56458</th>\n",
       "      <td>12</td>\n",
       "      <td>1553865149099</td>\n",
       "      <td>1</td>\n",
       "      <td>510</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       userID      Timestamp Current_Task Task_amount TaskID VersionID  \\\n",
       "56454      12  1553865148939            1         510     17         2   \n",
       "56455      12  1553865148981            1         510     17         2   \n",
       "56456      12  1553865149021            1         510     17         2   \n",
       "56457      12  1553865149060            1         510     17         2   \n",
       "56458      12  1553865149099            1         510     17         2   \n",
       "\n",
       "      RepetitionID Actual_Data Is_Pause  \\\n",
       "56454            0        True    False   \n",
       "56455            0        True    False   \n",
       "56456            0        True    False   \n",
       "56457            0        True    False   \n",
       "56458            0        True    False   \n",
       "\n",
       "                                                   Image  \n",
       "56454  [0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...  \n",
       "56455  [0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...  \n",
       "56456  [0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...  \n",
       "56457  [0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...  \n",
       "56458  [0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, ...  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfAll = pd.read_pickle(\"DataStudyEvaluation/AllData.pkl\")\n",
    "df = dfAll[(dfAll.Actual_Data == True) & (dfAll.Is_Pause == False)]\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12,  5,  1, 10,  6,  3,  7,  8,  9, 11])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.userID.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 12.2 s, sys: 2.34 s, total: 14.6 s\n",
      "Wall time: 14.2 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": 5,
   "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": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_pickle(\"DataStudyEvaluation/dfFiltered.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "recorded actual: 608084, used data: 413500\n"
     ]
    }
   ],
   "source": [
    "print(\"recorded actual: %s, used data: %s\" % (len(dfAll), len(df)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_pickle(\"DataStudyEvaluation/dfFiltered.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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>178160</th>\n",
       "      <td>1</td>\n",
       "      <td>1553521741802</td>\n",
       "      <td>16</td>\n",
       "      <td>510</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...</td>\n",
       "      <td>False</td>\n",
       "      <td>1</td>\n",
       "      <td>286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>178161</th>\n",
       "      <td>1</td>\n",
       "      <td>1553521741842</td>\n",
       "      <td>16</td>\n",
       "      <td>510</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...</td>\n",
       "      <td>False</td>\n",
       "      <td>1</td>\n",
       "      <td>319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>178162</th>\n",
       "      <td>1</td>\n",
       "      <td>1553521741882</td>\n",
       "      <td>16</td>\n",
       "      <td>510</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...</td>\n",
       "      <td>False</td>\n",
       "      <td>1</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>178163</th>\n",
       "      <td>1</td>\n",
       "      <td>1553521741922</td>\n",
       "      <td>16</td>\n",
       "      <td>510</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...</td>\n",
       "      <td>False</td>\n",
       "      <td>1</td>\n",
       "      <td>288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>178164</th>\n",
       "      <td>1</td>\n",
       "      <td>1553521741990</td>\n",
       "      <td>16</td>\n",
       "      <td>510</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...</td>\n",
       "      <td>False</td>\n",
       "      <td>1</td>\n",
       "      <td>308</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        userID      Timestamp Current_Task Task_amount TaskID VersionID  \\\n",
       "178160       1  1553521741802           16         510      0         2   \n",
       "178161       1  1553521741842           16         510      0         2   \n",
       "178162       1  1553521741882           16         510      0         2   \n",
       "178163       1  1553521741922           16         510      0         2   \n",
       "178164       1  1553521741990           16         510      0         2   \n",
       "\n",
       "       RepetitionID Actual_Data Is_Pause  \\\n",
       "178160            0        True    False   \n",
       "178161            0        True    False   \n",
       "178162            0        True    False   \n",
       "178163            0        True    False   \n",
       "178164            0        True    False   \n",
       "\n",
       "                                                    Image  IsMax  \\\n",
       "178160  [[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...  False   \n",
       "178161  [[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...  False   \n",
       "178162  [[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...  False   \n",
       "178163  [[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...  False   \n",
       "178164  [[0, 1, 1, 1, 0, 1, 0, 4, 1, 0, 2, 1, 1, 0, 1]...  False   \n",
       "\n",
       "        MaxRepetition  ImageSum  \n",
       "178160              1       286  \n",
       "178161              1       319  \n",
       "178162              1        72  \n",
       "178163              1       288  \n",
       "178164              1       308  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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": 11,
   "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": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 5.65 s, sys: 4.52 s, total: 10.2 s\n",
      "Wall time: 9.76 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "pool = Pool(os.cpu_count()-1)\n",
    "temp_blobs = pool.starmap(detect_blobs, zip(df.Image, df.TaskID))\n",
    "pool.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    334475\n",
       "1     73449\n",
       "2      5576\n",
       "Name: BlobCount, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.BlobCount.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "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": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sample Size not Argumented: 84601\n"
     ]
    }
   ],
   "source": [
    "print(\"Sample Size not Argumented:\", len(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"BlobArea\"] = df[\"BlobW\"] * df[\"BlobH\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    84601.0\n",
       "mean        16.5\n",
       "std          5.9\n",
       "min         12.0\n",
       "25%         12.0\n",
       "50%         16.0\n",
       "75%         16.0\n",
       "max         72.0\n",
       "Name: BlobArea, dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.BlobArea.describe().round(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "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>59879.0</td>\n",
       "      <td>17.3</td>\n",
       "      <td>5.8</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>56.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Knuckle</th>\n",
       "      <td>24722.0</td>\n",
       "      <td>14.8</td>\n",
       "      <td>5.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   59879.0  17.3  5.8  12.0  12.0  16.0  20.0  56.0\n",
       "Knuckle  24722.0  14.8  5.7  12.0  12.0  12.0  16.0  72.0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"Input\").BlobArea.describe().round(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"BlobSum\"] = df.BlobImages.apply(lambda x: np.sum(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    84601.000000\n",
       "mean      1238.209170\n",
       "std        485.150602\n",
       "min        467.000000\n",
       "25%        930.000000\n",
       "50%       1094.000000\n",
       "75%       1383.000000\n",
       "max       4275.000000\n",
       "Name: BlobSum, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.BlobSum.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7ff3f0375b38>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6732 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": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 23,
     "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": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sample Size argumented: 84601\n"
     ]
    }
   ],
   "source": [
    "print(\"Sample Size argumented:\", len(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "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": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"Blobs\"] = df.BlobImages.apply(lambda x: pasteToEmpty(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_pickle(\"DataStudyEvaluation/df_statistics.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[[\"userID\", \"TaskID\", \"Blobs\", \"InputMethod\"]].to_pickle(\"DataStudyEvaluation/df_blobs_area.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# display blobs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "userID                                                           1\n",
      "Timestamp                                            1553522289862\n",
      "Current_Task                                                   155\n",
      "Task_amount                                                    510\n",
      "TaskID                                                           0\n",
      "VersionID                                                       11\n",
      "RepetitionID                                                     0\n",
      "Actual_Data                                                   True\n",
      "Is_Pause                                                     False\n",
      "Image            [[1, 1, 1, 0, 0, 0, 0, 2, 1, 2, 2, 0, 3, 0, 0]...\n",
      "IsMax                                                         True\n",
      "MaxRepetition                                                    0\n",
      "ImageSum                                                       933\n",
      "InputMethod                                                      0\n",
      "Input                                                      Knuckle\n",
      "BlobCount                                                        1\n",
      "BlobImages       [[2, 2, 4, 5, 2], [1, 5, 11, 13, 5], [4, 9, 71...\n",
      "BlobW                                                            3\n",
      "BlobH                                                            4\n",
      "BlobArea                                                        12\n",
      "BlobSum                                                        710\n",
      "Blobs            [[2.0, 2.0, 4.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0,...\n",
      "Name: 191534, dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 432x288 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 5728 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
}
