{ "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": 1, "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": 2, "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": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['DataStudyCollection/17_studyData.csv', 'DataStudyCollection/2_studyData.csv', 'DataStudyCollection/12_studyData.csv', 'DataStudyCollection/15_studyData.csv', 'DataStudyCollection/5_studyData.csv', 'DataStudyCollection/1_studyData.csv', 'DataStudyCollection/14_studyData.csv', 'DataStudyCollection/10_studyData.csv', 'DataStudyCollection/13_studyData.csv', 'DataStudyCollection/18_studyData.csv', 'DataStudyCollection/6_studyData.csv', 'DataStudyCollection/16_studyData.csv', 'DataStudyCollection/3_studyData.csv', 'DataStudyCollection/7_studyData.csv', 'DataStudyCollection/8_studyData.csv', 'DataStudyCollection/9_studyData.csv', 'DataStudyCollection/11_studyData.csv', 'DataStudyCollection/4_studyData.csv']\n", "CPU times: user 1.86 s, sys: 1.03 s, total: 2.89 s\n", "Wall time: 17.3 s\n" ] } ], "source": [ "%%time\n", "pool = Pool(cpu_count() - 2)\n", "data_files = [\"DataStudyCollection/%s\" % file for file in os.listdir(\"DataStudyCollection\") 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": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1010014" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = dfAll[dfAll.Image.notnull()]\n", "len(df)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loaded 1013841 values\n", "removed 3827 values (thats 0.377%)\n", "new df has size 1010014\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": 6, "metadata": {}, "outputs": [], "source": [ "df = df.reset_index(drop=True)\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
userIDTimestampCurrent_TaskTask_amountTaskIDVersionIDRepetitionIDActual_DataIs_PauseImage
0171547138602677034000FalseFalse[1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ...
1171547138602697034000FalseFalse[1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ...
2171547138602796034000FalseFalse[1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ...
3171547138602817034000FalseFalse[1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ...
4171547138602863034000FalseFalse[1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ...
\n", "
" ], "text/plain": [ " userID Timestamp Current_Task Task_amount TaskID VersionID \\\n", "0 17 1547138602677 0 34 0 0 \n", "1 17 1547138602697 0 34 0 0 \n", "2 17 1547138602796 0 34 0 0 \n", "3 17 1547138602817 0 34 0 0 \n", "4 17 1547138602863 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 [1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ... \n", "1 [1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ... \n", "2 [1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ... \n", "3 [1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ... \n", "4 [1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, ... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "df.to_pickle(\"DataStudyCollection/AllData.pkl\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(df.userID.unique())" ] } ], "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 }