knuckletouch/python/Step_32_ReadData-Evaluation...

367 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## This notebook creates one dataframe from all participants data\n",
"## It also removes 1% of the data as this is corrupted"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def cast_to_int(row):\n",
" try:\n",
" return np.array([a if float(a) >= 0 else 0 for a in row[2:-1]], dtype=np.uint8)\n",
" except Exception as e:\n",
" return None\n",
" \n",
"def load_csv(file):\n",
" temp_df = pd.read_csv(file, delimiter=\";\")\n",
" temp_df.Image = temp_df.Image.str.split(',')\n",
" temp_df.Image = temp_df.Image.apply(cast_to_int)\n",
" return temp_df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['DataStudyEvaluation/2_studyData.csv', 'DataStudyEvaluation/12_studyData.csv', 'DataStudyEvaluation/5_studyData.csv', 'DataStudyEvaluation/1_studyData.csv', 'DataStudyEvaluation/10_studyData.csv', 'DataStudyEvaluation/6_studyData.csv', 'DataStudyEvaluation/3_studyData.csv', 'DataStudyEvaluation/7_studyData.csv', 'DataStudyEvaluation/8_studyData.csv', 'DataStudyEvaluation/9_studyData.csv', 'DataStudyEvaluation/11_studyData.csv', 'DataStudyEvaluation/4_studyData.csv']\n",
"CPU times: user 1.35 s, sys: 786 ms, total: 2.14 s\n",
"Wall time: 1min 43s\n"
]
}
],
"source": [
"%%time\n",
"pool = Pool(cpu_count() - 2)\n",
"data_files = [\"DataStudyEvaluation/%s\" % file for file in os.listdir(\"DataStudyEvaluation\") if file.endswith(\".csv\") and \"studyData\" in file]\n",
"print(data_files)\n",
"df_lst = pool.map(load_csv, data_files)\n",
"dfAll = pd.concat(df_lst)\n",
"pool.close()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"608084"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = dfAll[dfAll.Image.notnull()]\n",
"df = df[df.userID != \"userID\"]\n",
"df.userID = pd.to_numeric(df.userID)\n",
"len(df)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loaded 610816 values\n",
"removed 2732 values (thats 0.447%)\n",
"new df has size 608084\n"
]
}
],
"source": [
"print(\"loaded %s values\" % len(dfAll))\n",
"print(\"removed %s values (thats %s%%)\" % (len(dfAll) - len(df), round((len(dfAll) - len(df)) / len(dfAll) * 100, 3)))\n",
"print(\"new df has size %s\" % len(df))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"df = df.reset_index(drop=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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>0</th>\n",
" <td>2</td>\n",
" <td>1553593631562</td>\n",
" <td>0</td>\n",
" <td>34</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>false</td>\n",
" <td>false</td>\n",
" <td>[3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 2, 1, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1553593631595</td>\n",
" <td>0</td>\n",
" <td>34</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>false</td>\n",
" <td>false</td>\n",
" <td>[3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>1553593631634</td>\n",
" <td>0</td>\n",
" <td>34</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>false</td>\n",
" <td>false</td>\n",
" <td>[3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>1553593631676</td>\n",
" <td>0</td>\n",
" <td>34</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>false</td>\n",
" <td>false</td>\n",
" <td>[3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>1553593631716</td>\n",
" <td>0</td>\n",
" <td>34</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>false</td>\n",
" <td>false</td>\n",
" <td>[3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" userID Timestamp Current_Task Task_amount TaskID VersionID \\\n",
"0 2 1553593631562 0 34 0 0 \n",
"1 2 1553593631595 0 34 0 0 \n",
"2 2 1553593631634 0 34 0 0 \n",
"3 2 1553593631676 0 34 0 0 \n",
"4 2 1553593631716 0 34 0 0 \n",
"\n",
" RepetitionID Actual_Data Is_Pause \\\n",
"0 0 false false \n",
"1 0 false false \n",
"2 0 false false \n",
"3 0 false false \n",
"4 0 false false \n",
"\n",
" Image \n",
"0 [3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 2, 1, 0, 0, ... \n",
"1 [3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0... \n",
"2 [3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0... \n",
"3 [3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0... \n",
"4 [3, 3, 3, 2, 0, 0, 1, 0, 0, 0, 1, 222, 0, 0, 0... "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 12, 5, 1, 10, 6, 3, 7, 8, 9, 11, 4])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.userID.unique()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"df.userID = pd.to_numeric(df.userID)\n",
"df.TaskID = pd.to_numeric(df.TaskID)\n",
"df.VersionID = pd.to_numeric(df.VersionID)\n",
"df.Timestamp = pd.to_numeric(df.Timestamp)\n",
"df.Current_Task = pd.to_numeric(df.Current_Task)\n",
"df.Task_amount = pd.to_numeric(df.Task_amount)\n",
"df.RepetitionID = pd.to_numeric(df.RepetitionID)\n",
"df.loc[df.Actual_Data == \"false\", \"Actual_Data\"] = False\n",
"df.loc[df.Actual_Data == \"true\", \"Actual_Data\"] = True\n",
"df.loc[df.Is_Pause == \"false\", \"Is_Pause\"] = False\n",
"df.loc[df.Is_Pause == \"true\", \"Is_Pause\"] = True"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"df.to_pickle(\"DataStudyEvaluation/AllData.pkl\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}