MICRO Motion capture data from groups of participants standing still to auditory stimuli (2012) 1.0

File: <base>/code/MICRO-master/HPvSP/Python/Mocap.ipynb (28,744 bytes)
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ufNWSOg1e9sw"
   },
   "source": [
    "# Headphones v Speakers Motion Capture Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installing and importing libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "gI8U9n_1fP8H"
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import math\n",
    "\n",
    "import sys\n",
    "\n",
    "import pylab\n",
    "\n",
    "import numpy.linalg\n",
    "\n",
    "import plotly.plotly as py\n",
    "\n",
    "%load_ext rpy2.ipython\n",
    "\n",
    "#r packages\n",
    "from rpy2.robjects import r\n",
    "from rpy2.robjects.packages import importr\n",
    "from rpy2.robjects import FloatVector\n",
    "stats = importr('stats')\n",
    "base = importr('base')\n",
    "\n",
    "from scipy import stats\n",
    "\n",
    "import seaborn\n",
    "\n",
    "from IPython.display import display, Markdown\n",
    "\n",
    "#pyrqa packages\n",
    "from pyrqa.time_series import TimeSeries\n",
    "from pyrqa.settings import Settings\n",
    "from pyrqa.computing_type import ComputingType\n",
    "from pyrqa.neighbourhood import FixedRadius\n",
    "from pyrqa.metric import EuclideanMetric\n",
    "from pyrqa.computation import RQAComputation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reading MoCap csv files for all participants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "5YQSo0OcfaRk"
   },
   "outputs": [],
   "source": [
    "#Reading csv files\n",
    "\n",
    "\n",
    "\n",
    "hp_raw_mocap = {} #headphones condition\n",
    "\n",
    "path = r'/Users/vesanche/Desktop/hp/' # use your path for the folder containing Headphones mocap files\n",
    "hp_files = sorted(glob.glob(os.path.join(path, \"*.csv\")))\n",
    "\n",
    "i=1\n",
    "\n",
    "for filename in hp_files:\n",
    "    hp_raw_mocap[i] = pd.read_csv(filename, delimiter=',', encoding='utf-8', skiprows=10, low_memory=False, na_values='0')\n",
    "    i = i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "jK3vTMqOszM-"
   },
   "outputs": [],
   "source": [
    "sp_raw_mocap = {} #speakers condition\n",
    "\n",
    "path = r'/Users/vesanche/Desktop/sp/' # use your path for the folder containing Speakers mocap files\n",
    "sp_files = sorted(glob.glob(os.path.join(path, \"*.csv\")))\n",
    "\n",
    "i=1\n",
    "\n",
    "for filename in sp_files:\n",
    "    sp_raw_mocap[i] = pd.read_csv(filename, delimiter=',', encoding='utf-8', skiprows=10, low_memory=False, na_values='0')\n",
    "    i = i+1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "QDC3hpksL-v2"
   },
   "outputs": [],
   "source": [
    "hp_raw_mocap[35]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "2CJwKqVbKiIl"
   },
   "outputs": [],
   "source": [
    "#Inserting column with time for all participants in both conditions\n",
    "\n",
    "for j in range (1,len(hp_raw_mocap)+1):\n",
    "  time = np.linspace(0, len(hp_raw_mocap[j])/200, len(hp_raw_mocap[j]))\n",
    "  hp_raw_mocap[j].insert(0, 'Time (s)', time)\n",
    "  \n",
    "for i in range (1,len(sp_raw_mocap)+1):\n",
    "  time = np.linspace(0, len(sp_raw_mocap[i])/200, len(sp_raw_mocap[i]))\n",
    "  sp_raw_mocap[i].insert(0, 'Time (s)', time)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "49vBwQc0mpp5"
   },
   "source": [
    "# Preprocessing data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "rRsYf6EKm10C"
   },
   "source": [
    "## Calculating velocities (QoM) for all markers, all participants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "VkSKn7DkEzme"
   },
   "outputs": [],
   "source": [
    "#Function that computes vector norm of displacement\n",
    "def displacement(posdata):\n",
    "    \n",
    "    times = np.linspace(0, len(posdata)/200, len(posdata))\n",
    "    posdata.insert(0, 'Time (s)', times)\n",
    "\n",
    "    displacement = (\n",
    "        np.roll(posdata, -1, axis=0)\n",
    "        - posdata)[:-1]\n",
    "    \n",
    "    #displacement = displacement.drop(displacement.index[len(displacement)-1])\n",
    "    \n",
    "\n",
    "    displacement = np.sqrt(\n",
    "         displacement.iloc[:,1] ** 2 +\n",
    "         displacement.iloc[:,2] ** 2 +\n",
    "         displacement.iloc[:,3] ** 2\n",
    "    )\n",
    "\n",
    "    return pd.DataFrame({\n",
    "        'Time (s)': posdata['Time (s)'][:-1],\n",
    "        'Distance': displacement,\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "E0Lb1RcrE2zl"
   },
   "outputs": [],
   "source": [
    "#Function that computes vector norm of velocity\n",
    "\n",
    "def velocity(posdata):\n",
    "  \n",
    "  \n",
    "    times = np.linspace(0, len(posdata)/200, len(posdata))\n",
    "    posdata.insert(0, 'Time (s)', times)\n",
    "    \n",
    "    displacement = (\n",
    "        np.roll(posdata, -1, axis=0)\n",
    "        - posdata)[:-1]\n",
    "    \n",
    "    #displacement = displacement.drop(displacement.index[len(displacement)-1])\n",
    "    \n",
    "\n",
    "    speeds = np.sqrt(\n",
    "         displacement.iloc[:,1] ** 2 +\n",
    "         displacement.iloc[:,2] ** 2 +\n",
    "         displacement.iloc[:,3] ** 2\n",
    "    ) /  displacement['Time (s)']\n",
    "\n",
    "    return pd.DataFrame({\n",
    "        'Time (s)': posdata['Time (s)'][:-1],\n",
    "        'Velocity': speeds,\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "wIjNRPsph6bq"
   },
   "outputs": [],
   "source": [
    "#Running displacement function for all participants - HP condition\n",
    "hp_displacements_mocap = {}    \n",
    "\n",
    "for participant in range (1,len(hp_raw_mocap)+1):\n",
    "    hp_displacements_mocap[participant] = {}\n",
    "    \n",
    "    j = 0\n",
    "    k = 1\n",
    "    \n",
    "    for markers in range (4,len(hp_raw_mocap[participant].columns)+1,3):\n",
    "      j=j+1\n",
    "      hp_displacements_mocap[participant][j] = displacement(hp_raw_mocap[participant].iloc[:,k:markers:1])\n",
    "      k = k+3\n",
    "      hp_displacements_mocap[participant][j].index =  hp_displacements_mocap[participant][j]['Time (s)']\n",
    "      hp_displacements_mocap[participant][j] =  hp_displacements_mocap[participant][j].drop(['Time (s)'], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "RzqzwdpCDcEO"
   },
   "outputs": [],
   "source": [
    "#Running displacement function for all participants - SP condition\n",
    "sp_displacements_mocap = {}    \n",
    "\n",
    "for participant in range (1,len(sp_raw_mocap)+1):\n",
    "    sp_displacements_mocap[participant] = {}\n",
    "    \n",
    "    j = 0\n",
    "    k = 1\n",
    "\n",
    "    for markers in range (4,len(sp_raw_mocap[participant].columns)+1,3): \n",
    "      j=j+1\n",
    "      sp_displacements_mocap[participant][j] = displacement(sp_raw_mocap[participant].iloc[:,k:markers:1])\n",
    "      k = k+3\n",
    "      sp_displacements_mocap[participant][j].index =  sp_displacements_mocap[participant][j]['Time (s)']\n",
    "      sp_displacements_mocap[participant][j] =  sp_displacements_mocap[participant][j].drop(['Time (s)'], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "VmJLi6SnVW1Y"
   },
   "outputs": [],
   "source": [
    "sp_displacements_mocap[1][5] #Displacement from participant 1, marker 21 - SP condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "oUqnGGQuo_NB"
   },
   "outputs": [],
   "source": [
    "#Running velocity (QoM) function for all participants - HP condition\n",
    "hp_velocities_mocap = {}    \n",
    "\n",
    "for participant in range (1,len(hp_raw_mocap)+1):\n",
    "    hp_velocities_mocap[participant] = {}\n",
    "    \n",
    "    j = 0\n",
    "    k = 1\n",
    "    \n",
    "    for markers in range (4,len(hp_raw_mocap[participant].columns)+1,3):\n",
    "      j=j+1\n",
    "      hp_velocities_mocap[participant][j] = velocity(hp_raw_mocap[participant].iloc[:,k:markers:1])\n",
    "      k = k+3\n",
    "      hp_velocities_mocap[participant][j].index =  hp_velocities_mocap[participant][j]['Time (s)']\n",
    "      hp_velocities_mocap[participant][j] =  hp_velocities_mocap[participant][j].drop(['Time (s)'], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "w5LuxZ5XqUDt"
   },
   "outputs": [],
   "source": [
    "#Running velocity (QoM) function for all participants - SP condition\n",
    "sp_velocities_mocap = {}    \n",
    "\n",
    "for participant in range (1,len(sp_raw_mocap)+1):\n",
    "    sp_velocities_mocap[participant] = {}\n",
    "    \n",
    "    j = 0\n",
    "    k = 1\n",
    "    \n",
    "    for markers in range (4,len(sp_raw_mocap[participant].columns)+1,3):\n",
    "      j=j+1\n",
    "      sp_velocities_mocap[participant][j] = velocity(sp_raw_mocap[participant].iloc[:,k:markers:1])\n",
    "      k = k+3\n",
    "      sp_velocities_mocap[participant][j].index =  sp_velocities_mocap[participant][j]['Time (s)']\n",
    "      sp_velocities_mocap[participant][j] =  sp_velocities_mocap[participant][j].drop(['Time (s)'], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "u2AaxgG3sJyu"
   },
   "outputs": [],
   "source": [
    "sp_velocities_mocap[1][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "IUWK7SWgMZf6"
   },
   "outputs": [],
   "source": [
    "#Concatenating velocity from all markers for each participant - HP condition\n",
    "hp_vel_markers_mocap = {}\n",
    "for participant in range (1,len(hp_raw_mocap)+1):\n",
    "    hp_vel_markers_mocap[participant] = pd.concat(hp_velocities_mocap[participant],1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "iNfI-yQgNXzO"
   },
   "outputs": [],
   "source": [
    "#Concatenating velocity from all markers for each participant - SP condition\n",
    "sp_vel_markers_mocap = {}\n",
    "for participant in range (1,len(sp_raw_mocap)+1):\n",
    "    sp_vel_markers_mocap[participant] = pd.concat(sp_velocities_mocap[participant],1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "V1i9PzSQNJDw"
   },
   "outputs": [],
   "source": [
    "#Assigning markers' names according to labelling\n",
    "\n",
    "marker_labels_35hp = [\"HF\", \"HL\", \"BU\", \"COM\", \"CHEST\", \"SHL\", \"SHR\", \"ELL\", \"ELR\", \"HIPL\", \"HIPR\", \"HANDL\", \"HANDR\", \"KNL\", \"KNR\", \"HEELL\", \"HEELR\", \"TOEL\", \"TOER\", \"BOARD\", \"FLOOR\"]\n",
    "marker_labels_3hp = [\"HL\", \"HR\", \"BU\", \"COM\", \"CHEST\", \"SHL\", \"SHR\", \"ELL\", \"ELR\", \"HIPL\", \"HIPR\", \"HANDL\", \"HANDR\", \"KNL\", \"KNR\", \"HEELL\", \"HEELR\", \"TOEL\", \"TOER\", \"BOARD\", \"FLOOR\"]\n",
    "marker_labels =  [\"HF\", \"HL\", \"HR\", \"BU\", \"COM\", \"CHEST\", \"SHL\", \"SHR\", \"ELL\", \"ELR\", \"HIPL\", \"HIPR\", \"HANDL\", \"HANDR\", \"KNL\", \"KNR\", \"HEELL\", \"HEELR\", \"TOEL\", \"TOER\", \"BOARD\", \"FLOOR\"]\n",
    "marker_labels_916sp =  [\"HL\", \"HR\", \"BU\", \"COM\", \"CHEST\", \"SHL\", \"SHR\", \"ELL\", \"ELR\", \"HIPL\", \"HIPR\", \"HANDL\", \"HANDR\", \"KNL\", \"KNR\", \"HEELL\", \"HEELR\", \"TOEL\", \"TOER\", \"BOARD\", \"FLOOR\"]\n",
    "marker_labels_1sp =  [\"HF\", \"HL\", \"HR\", \"BU\", \"COM\", \"CHEST\", \"SHL\", \"SHR\", \"ELL\", \"ELR\", \"HIPL\", \"HIPR\", \"HANDL\", \"HANDR\", \"KNL\", \"KNR\", \"HEELR\", \"TOEL\", \"TOER\", \"BOARD\", \"FLOOR\"]\n",
    "\n",
    "hp_vel_markers_mocap[35].columns = marker_labels_35hp\n",
    "hp_vel_markers_mocap[3].columns = marker_labels_3hp\n",
    "sp_vel_markers_mocap[9].columns = marker_labels_916sp\n",
    "sp_vel_markers_mocap[16].columns = marker_labels_916sp\n",
    "sp_vel_markers_mocap[1].columns = marker_labels_1sp\n",
    "\n",
    "\n",
    "for participant in range (1,3):\n",
    "    hp_vel_markers_mocap[participant].columns = marker_labels\n",
    "for participant in range (4,35):\n",
    "    hp_vel_markers_mocap[participant].columns = marker_labels\n",
    "for participant in range (36,43):\n",
    "    hp_vel_markers_mocap[participant].columns = marker_labels\n",
    "    \n",
    "for participant in range (2,9):\n",
    "    sp_vel_markers_mocap[participant].columns = marker_labels\n",
    "for participant in range (10,16):\n",
    "    sp_vel_markers_mocap[participant].columns = marker_labels\n",
    "for participant in range (17,43):\n",
    "    sp_vel_markers_mocap[participant].columns = marker_labels\n",
    "\n",
    "for participant in range (1,len(hp_raw_mocap)+1):\n",
    "    hp_vel_markers_mocap[participant] =  hp_vel_markers_mocap[participant].drop([\"BOARD\", \"FLOOR\"], axis=1) #removing floor and wiiboard markers data before further analysis\n",
    "    \n",
    "for participant in range (1,len(sp_raw_mocap)+1):\n",
    "    sp_vel_markers_mocap[participant] =  sp_vel_markers_mocap[participant].drop([\"BOARD\", \"FLOOR\"], axis=1)\n",
    "    \n",
    "sp_vel_markers_mocap[1] #velocity, all markers participant 1, speakers condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Y_IryvfB-2qh"
   },
   "outputs": [],
   "source": [
    "hp_vel_markers_mocap[35]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "P0kXt3wfm8NQ"
   },
   "source": [
    "## Calculating mean of velocity for all markers, all participants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "5f91klgnnBOo"
   },
   "outputs": [],
   "source": [
    "for participant in range (1,len(sp_raw_mocap)+1):\n",
    "  sp_vel_markers_mocap[participant]['mean'] = sp_vel_markers_mocap[participant][sp_vel_markers_mocap[participant].columns].mean(axis=1)\n",
    "  sp_vel_markers_mocap[participant].loc['mean'] = sp_vel_markers_mocap[participant].mean()\n",
    "  \n",
    "  \n",
    "for participant in range (1,len(hp_raw_mocap)+1):\n",
    "  hp_vel_markers_mocap[participant]['mean'] = hp_vel_markers_mocap[participant][hp_vel_markers_mocap[participant].columns].mean(axis=1)\n",
    "  hp_vel_markers_mocap[participant].loc['mean'] = hp_vel_markers_mocap[participant].mean()\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "YEcdbCzDkLTJ"
   },
   "outputs": [],
   "source": [
    "hp_vel_markers_mocap[1] #velocity and mean velocity, all markers, participant 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "CJZeAP5rnB72"
   },
   "source": [
    "## Smoothing velocity (TO BE DONE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "8E2Jil1fnLn0"
   },
   "outputs": [],
   "source": [
    "hp_smooth_emg = {}\n",
    "\n",
    "for i in list(hp_raw_emg):\n",
    "        hp_smooth_emg[i] = hp_rectfd_emg[i].rolling(1000,center=True,min_periods=1).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "xfOjHAQ0JU8u"
   },
   "outputs": [],
   "source": [
    "sp_smooth_emg = {}\n",
    "\n",
    "for i in list(sp_raw_emg):\n",
    "        sp_smooth_emg[i] = sp_rectfd_emg[i].rolling(1000,center=True,min_periods=1).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "FvrLr8yhVXbu"
   },
   "source": [
    "# Splitting movement data into conditions/songs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "PgSmSEuTScLl"
   },
   "outputs": [],
   "source": [
    "hp_songorder = pd.read_csv('drive/My Drive/UiO RITMO/HeadphonesVSSpeakers/songorderhp.csv') #Reading csv with stimuli order used for each participant during headphones condition\n",
    "hp_songorder.columns = ['Participant','1', '2', '3','4', '5', '6']\n",
    "hp_songorder.set_index('Participant',inplace=True, drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "vFvwEvVpDO6Q"
   },
   "outputs": [],
   "source": [
    "sp_songorder = pd.read_csv('drive/My Drive/UiO RITMO/HeadphonesVSSpeakers/songordersp.csv') #Reading csv with stimuli order used for each participant during speakers condition\n",
    "sp_songorder.columns = ['Participant','1', '2', '3','4', '5', '6']\n",
    "sp_songorder.set_index('Participant',inplace=True, drop=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "G4EibbPPXSx5"
   },
   "source": [
    "## Segmenting velocity data into music-silence segments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "8tH41oXmFbUJ"
   },
   "outputs": [],
   "source": [
    "def segments(songorder,data):\n",
    "    #work in progress, works for emg data at 200Hz, song duration used in 2018 headphones-speakers experiment\n",
    "    #Silence 1 - 45?\n",
    "    #Silence 2, 3, 4, 5, 6 - 30s\n",
    "    #1 -  - 49s\n",
    "    #2 -  - 48s\n",
    "    #3 -  - 47s\n",
    "    #4 -  - 48s\n",
    "    #5 -  - 48s\n",
    "    #6 -  - 48s\n",
    "    #Silence 7 - 32s\n",
    "    #Total duration of recording 500s\n",
    "   \n",
    "    \n",
    "    start = {}\n",
    "    segments = {}\n",
    "    segments[1] = data[0:6000] #First silence segement: 30 seconds\n",
    "    start[1] = 0\n",
    "    start[2] = 6001\n",
    "    song = 0\n",
    "    \n",
    "    while song <= len(songorder.columns)-1:\n",
    "        for inicio in range (2,13,2):\n",
    "            if songorder.iloc[0][song] == 1:\n",
    "                        segments[inicio]=data[start[inicio]:start[inicio]+9800] #song1 49s\n",
    "                        segments[inicio+1]=data[start[inicio]+9801:start[inicio]+9800+6000] #30s silence\n",
    "                        start[inicio+2] = start[inicio]+9801+6000\n",
    "                        song = song+1  \n",
    "            elif songorder.iloc[0][song] == 2:\n",
    "                        segments[inicio]=data[start[inicio]:start[inicio]+9600] #song2 48s\n",
    "                        segments[inicio+1]=data[start[inicio]+9601:start[inicio]+9600+6000] #30s silence\n",
    "                        start[inicio+2] = start[inicio]+9601+6000\n",
    "                        song = song+1\n",
    "            elif songorder.iloc[0][song] == 3:\n",
    "                        segments[inicio]=data[start[inicio]:start[inicio]+9400] #song3 47s\n",
    "                        segments[inicio+1]=data[start[inicio]+9401:start[inicio]+9400+6000] #30s silence\n",
    "                        start[inicio+2] = start[inicio]+9401+6000\n",
    "                        song = song+1\n",
    "            elif songorder.iloc[0][song] == 4:\n",
    "                        segments[inicio]=data[start[inicio]:start[inicio]+9600] #song4 48s\n",
    "                        segments[inicio+1]=data[start[inicio]+9601:start[inicio]+9600+6000] #30s silence\n",
    "                        start[inicio+2] = start[inicio]+9601+6000\n",
    "                        song = song+1\n",
    "            elif songorder.iloc[0][song] == 5:\n",
    "                        segments[inicio]=data[start[inicio]:start[inicio]+9600] #song5 48s\n",
    "                        segments[inicio+1]=data[start[inicio]+9601:start[inicio]+9600+6000] #30s silence\n",
    "                        start[inicio+2] = start[inicio]+9601+6000\n",
    "                        song = song+1\n",
    "            elif songorder.iloc[0][song] == 6:\n",
    "                        segments[inicio]=data[start[inicio]:start[inicio]+9600] #song6 48s\n",
    "                        segments[inicio+1]=data[start[inicio]+96001:start[inicio]+9600+6000] #silence\n",
    "                        start[inicio+2] = start[inicio]+9601+6000\n",
    "                        song = song+1\n",
    "    return segments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "EeBwFPC8QhdV"
   },
   "outputs": [],
   "source": [
    "hp_vel_mocap_segm = {}  #Using segments function on headphones condition processed data for all participants\n",
    "for i in list(hp_raw_mocap):\n",
    "    hp_vel_mocap_segm[i] = segments(hp_songorder[i-1:i],hp_vel_markers_mocap[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "of7BuZbcUFq5"
   },
   "outputs": [],
   "source": [
    "sp_vel_mocap_segm = {}  #Using segments function on headphones condition processed data for all participants\n",
    "for i in list(sp_raw_mocap):\n",
    "    sp_vel_mocap_segm[i] = segments(sp_songorder[i-1:i],sp_vel_markers_mocap[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "sPpCc9RyXLR9"
   },
   "source": [
    "## Grouping segmented data by music/silence condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "O8ktcJHiXPUk"
   },
   "outputs": [],
   "source": [
    "#Odd segments are silence, even segments are music\n",
    "hp_vel_mocap_segm_mus = {} #creating empty dictionaries for music and silence\n",
    "hp_vel_mocap_segm_sil = {}\n",
    "\n",
    "for i in list(hp_raw_mocap):\n",
    "  hp_vel_mocap_segm_mus[i] = {} #creating empty dictionary for each participant for both music and silence conditions\n",
    "  hp_vel_mocap_segm_sil[i] = {}\n",
    "  k = 0\n",
    "  \n",
    "  for j in range (1,13,2):\n",
    "    hp_vel_mocap_segm_sil[i][k] = hp_vel_mocap_segm[i][j] #filling each participant's dictionary with odd segments (silence)\n",
    "    hp_vel_mocap_segm_mus[i][k] = hp_vel_mocap_segm[i][j+1] #filling each participant's dictionary with even segments (music)\n",
    "    k=k+1\n",
    " \n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "8cQJixOLIoHF"
   },
   "outputs": [],
   "source": [
    "#Same as above, for speakers condition\n",
    "sp_vel_mocap_segm_mus = {} #creating empty dictionaries for music and silence\n",
    "sp_vel_mocap_segm_sil = {}\n",
    "\n",
    "for i in list(sp_raw_mocap):\n",
    "  sp_vel_mocap_segm_mus[i] = {} #creating empty dictionary for each participant for both music and silence conditions\n",
    "  sp_vel_mocap_segm_sil[i] = {}\n",
    "  kx = 0\n",
    "  \n",
    "  for j in range (1,13,2):\n",
    "    sp_vel_mocap_segm_sil[i][kx] = sp_vel_mocap_segm[i][j] #filling each participant's dictionary with odd segments (silence)\n",
    "    sp_vel_mocap_segm_mus[i][kx] = sp_vel_mocap_segm[i][j+1] #filling each participant's dictionary with even segments (music)\n",
    "    kx=kx+1\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "SQXEYLxaQEf-"
   },
   "outputs": [],
   "source": [
    "sp_vel_mocap_segm_mus[3][0] #1st music segments participant 3, speakers condition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "DuK3hzaJXhxR"
   },
   "source": [
    "## Sorting music data by stimuli"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "BjpLdBmrXlsa"
   },
   "outputs": [],
   "source": [
    "## Function to sort segments from each participant with corresponding stimuli\n",
    "def sort_stimuli(songorder,data):\n",
    "   \n",
    "    sorted_stimuli = {}\n",
    "    song = 0\n",
    "    while song <= len(songorder.columns)-1:\n",
    "        \n",
    "            if songorder.iloc[0][song] == 1:\n",
    "                       sorted_stimuli[1] = data[song]\n",
    "                       song = song+1\n",
    "            elif songorder.iloc[0][song] == 2:\n",
    "                       sorted_stimuli[2] = data[song]\n",
    "                       song = song+1\n",
    "            elif songorder.iloc[0][song] == 3:\n",
    "                       sorted_stimuli[3] = data[song]\n",
    "                       song = song+1\n",
    "            elif songorder.iloc[0][song] == 4:\n",
    "                       sorted_stimuli[4] = data[song]\n",
    "                       song = song+1\n",
    "            elif songorder.iloc[0][song] == 5:\n",
    "                       sorted_stimuli[5] = data[song]\n",
    "                       song = song+1\n",
    "            elif songorder.iloc[0][song] == 6:\n",
    "                       sorted_stimuli[6] = data[song]\n",
    "                       song = song+1\n",
    "    return sorted_stimuli"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "PSORZM5vnYw-"
   },
   "outputs": [],
   "source": [
    "sp_songorder #Reminding song order for the headphones condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "giho8P5zPDi1"
   },
   "outputs": [],
   "source": [
    "## Running stimuli-sorting function for all participants\n",
    "hp_vel_mocap_sort_mus = {}\n",
    "for i in list(hp_raw_mocap):  \n",
    "    hp_vel_mocap_sort_mus[i] = sort_stimuli(hp_songorder[i-1:i],hp_vel_mocap_segm_mus[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "7uT-T9BuHmDI"
   },
   "outputs": [],
   "source": [
    "## Same as above, for speakers condition\n",
    "sp_vel_mocap_sort_mus = {}\n",
    "for i in list(sp_raw_mocap):  \n",
    "    sp_vel_mocap_sort_mus[i] = sort_stimuli(sp_songorder[i-1:i],sp_vel_mocap_segm_mus[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "QeGE_CpbMDOR"
   },
   "outputs": [],
   "source": [
    "sp_vel_mocap_sort_mus[4][1] #velocity data, participant 4, song 1=Andre Bratten. Headphones condition"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "HeadphonesSpeakersMocap.ipynb",
   "provenance": [],
   "toc_visible": true
  },
  "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}