Operator Activity Analysis via Spatial Hand Position Distributions

Sebastian Hell                       contact@sebastianhell.com

Loading Full Experimental Data

In [1]:
import numpy as np
import pandas as pd
import os
import pickle
from scipy import stats
# setup paths
path = ' /dat/'
(_, dirnames, filenames) = os.walk(path).next()
filelabel = ['Label27.txt', 'Label28.txt',
             'Label32.txt', 'Label37.txt', 'Label40.txt']
pathL = '/Label/'
# inti Dataframe
Dataset = pd.DataFrame()


# creating column names
joints = 'SpineBase SpineMid Neck Head ShoulderLeft ElbowLeft WristLeft HandLeft ShoulderRight ElbowRight WristRight HandRight HipLeft KneeLeft AnkleLeft FootLeft HipRight KneeRight AnkleRight FootRight SpineShoulder HandTipLeft ThumbLeft HandTipRight ThumbRight'.split(' ')
columnS = ['MaxP', 'BodyNum', 'Time']
for item in joints:
    list.append(columnS, item + '_x')
    list.append(columnS, item + '_y')
    list.append(columnS, item + '_z')
    list.append(columnS, item + '_state')
list.append(columnS, 'Testperson')
list.append(columnS, 'Label')

# loop over skeleton files
for fi in filenames:
    print(fi)
    data = np.loadtxt(path + fi, delimiter=',').T
    pers = np.ones([1, data.shape[1]]) * int(fi[-6:-4])
    LabDum = np.ones([1, data.shape[1]]) * np.NaN
    data = np.append(data, pers, axis=0)
    data = np.append(data, LabDum, axis=0)
    # compare label files with skelton if there are lables available
    for items in filelabel:
        if fi[-6:-4] == items[-6:-4]:
            Lab = np.array(np.loadtxt(pathL + items, delimiter=','))
            data[-1, :] = Lab
        else:
            pass
    dfTemp = pd.DataFrame(data.T, columns=columnS)
    Dataset = Dataset.append(dfTemp, ignore_index=True)
#"write"
with pd.HDFStore('/FullDataset.h5',  mode='w') as store:
    store.append('RRRU', Dataset, data_columns=Dataset.columns, format='table')
skeleton_23.txt
skeleton_27.txt
skeleton_28.txt
skeleton_29.txt
skeleton_30.txt
skeleton_32.txt
skeleton_33.txt
skeleton_34.txt
skeleton_35.txt
skeleton_36.txt
skeleton_37.txt
skeleton_39.txt
skeleton_40.txt

Ploting

In [1]:
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

Import Skeleton Tracking Data

In [72]:
with pd.HDFStore('/FullDataset.h5',  mode='r') as newstore:
    Dataset = newstore.select('RRRU')
Dataset[0:3]
Out[72]:
MaxP BodyNum Time SpineBase_x SpineBase_y SpineBase_z SpineBase_state SpineMid_x SpineMid_y SpineMid_z ... HandTipRight_x HandTipRight_y HandTipRight_z HandTipRight_state ThumbRight_x ThumbRight_y ThumbRight_z ThumbRight_state Testperson Label
0 6.0 0.0 14670808.0 0.314792 -0.050282 1.56754 2.0 0.323520 0.183996 1.52702 ... 0.524438 -0.052018 1.59915 2.0 0.485295 -0.045905 1.52667 2.0 23.0 NaN
1 6.0 0.0 15670791.0 0.309842 -0.048114 1.56087 2.0 0.319482 0.185231 1.52420 ... 0.512685 -0.051019 1.58592 2.0 0.504713 0.009075 1.55690 2.0 23.0 NaN
2 6.0 0.0 16330042.0 0.295937 -0.050413 1.54759 2.0 0.301963 0.184655 1.51578 ... 0.496801 -0.048134 1.57029 2.0 0.483440 -0.014053 1.56123 2.0 23.0 NaN

3 rows × 105 columns

Labels

Label Action
1 wait
2 work
3 fetch
4 Tab
5 undefined
9 untracked

Creating a Dataframe

  • Selecting the Labels and Joinst for Plotting
  • Determine size of smaller Dataset
  • Pick Random sample of the Datasets with the size of the smaller one
In [18]:
dfa = Dataset.query('Label == 1')[
    ['HandRight_x', 'HandRight_y', 'HandRight_z']]
dfb = Dataset.query(' Label == 2')[
    ['HandRight_x', 'HandRight_y', 'HandRight_z']]
idmin = np.min([dfb.shape[0], dfa.shape[0]])
df = pd.concat([dfa.sample(n=idmin, random_state=1),
                dfb.sample(n=idmin, random_state=1)])

Ploting

In [23]:
 sns.set(style="white")
 g = sns.PairGrid(Dataset[['HandRight_x', 'HandRight_y', 'HandRight_z']].sample(n=50000, random_state=1), diag_sharey=False)
 g.map_lower(sns.kdeplot,rasterized=True, cmap="Blues_d")
 g.map_upper(plt.scatter,s = 10,alpha =0.3)
 g.map_diag(sns.kdeplot, lw=2,)
 g.axes[0,0].yaxis.set_label_text('x (m)')
 g.axes[1,0].yaxis.set_label_text('y (m)')
 g.axes[2,0].yaxis.set_label_text('z (m)')
 g.axes[2,0].xaxis.set_label_text('x (m)')
 g.axes[2,1].xaxis.set_label_text('y (m)')
 g.axes[2,2].xaxis.set_label_text('z (m)')
 #plt.savefig('full.pdf')
In [20]:
sns.set(style="white")
g = sns.PairGrid(df, diag_sharey=False)
g.map_lower(sns.kdeplot,rasterized=True, cmap="Blues_d")
g.map_upper(plt.scatter,s = 10,alpha =0.3)
g.map_diag(sns.kdeplot, lw=2,)
g.axes[0,0].yaxis.set_label_text('x (m)')
g.axes[1,0].yaxis.set_label_text('y (m)')
g.axes[2,0].yaxis.set_label_text('z (m)')
g.axes[2,0].xaxis.set_label_text('x (m)')
g.axes[2,1].xaxis.set_label_text('y (m)')
g.axes[2,2].xaxis.set_label_text('z (m)')
Out[20]:
<matplotlib.text.Text at 0x1db6c5c0>

Clear Tracking errors

In [27]:
df = Dataset.query('(((HandRight_x < 0.24)|(HandRight_x > 0.282)) |((HandRight_z < 0.58)|(HandRight_z > 0.62))|((HandRight_y < -0.061)|(HandRight_y > -0.039)))&(Label != 9) ' )
dfclear = df
df = df[['HandRight_x','HandRight_y','HandRight_z']].sample(n= 50000,random_state = 1)
dfclear.shape
Out[27]:
(198436, 105)
  • Plot of 50000 randome samples
  • Right Hand
  • cleaned Errors
In [28]:
sns.set(style="white")
g = sns.PairGrid(df, diag_sharey=False)
g.map_lower(sns.kdeplot,rasterized=True, cmap="Blues_d")
g.map_upper(plt.scatter,s = 10,alpha =0.3)
g.map_diag(sns.kdeplot, lw=2,)
g.axes[0,0].yaxis.set_label_text('x (m)')
g.axes[1,0].yaxis.set_label_text('y (m)')
g.axes[2,0].yaxis.set_label_text('z (m)')
g.axes[2,0].xaxis.set_label_text('x (m)')
g.axes[2,1].xaxis.set_label_text('y (m)')
g.axes[2,2].xaxis.set_label_text('z (m)')
plt.savefig('redu.pdf')
In [48]:
sns.set(style="white")

dfbla = Dataset.query('Label == 3')[
    ['HandRight_x', 'HandRight_y', 'HandRight_z']]
dfblb = Dataset.query(' Label == 4')[
    ['HandRight_x', 'HandRight_y', 'HandRight_z']]
idmin = np.min([dfbla.shape[0], dfblb.shape[0]])
dfH = pd.concat([dfbla.sample(n=idmin, random_state=1),
                dfblb.sample(n=idmin, random_state=1)])
g = sns.PairGrid(dfH, diag_sharey=False)
g.map_lower(sns.kdeplot,rasterized=True, cmap="Blues_d")
g.map_upper(plt.scatter,s = 10,alpha =0.3)
g.map_diag(sns.kdeplot, lw=2,)
g.axes[0,0].yaxis.set_label_text('x (m)')
g.axes[1,0].yaxis.set_label_text('y (m)')
g.axes[2,0].yaxis.set_label_text('z (m)')
g.axes[2,0].xaxis.set_label_text('x (m)')
g.axes[2,1].xaxis.set_label_text('y (m)')
g.axes[2,2].xaxis.set_label_text('z (m)')
plt.savefig('Tippen_Holen.pdf')
dfH.shape
Out[48]:
(4864, 3)

Compare if Datsetsize Changes KDE alot

  • Plot of 100 randome samples
  • Right Hand X-Data
  • cleaned Errors
In [66]:
df = dfclear[['HandRight_x','HandRight_y','HandRight_z']]
sns.distplot(df.sample(n = 100,random_state= 1).HandRight_x, hist=False, rug=True, color="r")
Out[66]:
<matplotlib.axes._subplots.AxesSubplot at 0x26f0492e8>
  • Plot of 1000 randome samples
  • Right Hand Z-Data
  • cleaned Errors
In [68]:
sns.distplot(df.sample(n = 1000,random_state= 1).HandRight_z, hist=False, rug=True, color="r")
Out[68]:
<matplotlib.axes._subplots.AxesSubplot at 0x2718d6390>
  • Plot of 1000 randome samples
  • Right Hand Y-Data
  • cleaned Errors
In [46]:
sns.distplot(df.sample(n = 1000,random_state= 1).HandRight_y, hist=False, rug=True, color="r")
Out[46]:
<matplotlib.axes._subplots.AxesSubplot at 0x267439dd8>

Looking at Lefthand Data

  • Plot of 1000 randome samples
  • LeftHand y-Data
  • cleaned Errors
In [72]:
sns.distplot(dfclear.sample(n = 1000,random_state= 1).HandLeft_y, hist=False, rug=True, color="r")
Out[72]:
<matplotlib.axes._subplots.AxesSubplot at 0x273af0eb8>

Looking at Lefthand Data

  • Plot of 1000 randome samples
  • LeftHand y-Data
  • cleaned Errors
  • y >0
In [62]:
df = Dataset.query('((HandLeft_y > 0)) ' )
sns.distplot(df.sample(n = 1000,random_state= 1).HandLeft_y, hist=False, rug=True, color="r")
Out[62]:
<matplotlib.axes._subplots.AxesSubplot at 0x26b8c1e48>

Only Labeld Data

In [29]:
dfLabel= Dataset[pd.notnull(Dataset['Label'])]
In [70]:
df = dfLabel.query('((Label != 9)&(Label != 5))')
dfa = Dataset.query('Label == 1' )
dfb = Dataset.query(' Label == 2' )
dfc = Dataset.query(' Label == 3' )
dfd = Dataset.query(' Label == 4' )
idmin = np.min([dfb.shape[0],dfa.shape[0],dfc.shape[0],dfd.shape[0]])
df= pd.concat([dfa.sample(n= idmin,random_state = 1),dfb.sample(n=idmin,random_state = 1),dfc.sample(n=idmin,random_state = 1),dfd.sample(n=idmin,random_state = 1)])
print(idmin)
print([dfa.shape[0],dfb.shape[0],dfc.shape[0],dfd.shape[0]])
2432
[12575, 24077, 2432, 2704]
Variabl Label
dfa wait
dfb work
dfc fetch
dfd Tab
In [33]:
sns.set(style="white")
g = sns.PairGrid(df[['HandRight_x','HandRight_y','HandRight_z']], diag_sharey=False)
g.map_lower(sns.kdeplot,rasterized=True, cmap="Blues_d")
g.map_upper(plt.scatter,s = 10,alpha =0.3)
g.map_diag(sns.kdeplot, lw=2,)
g.axes[0,0].yaxis.set_label_text('x (m)')
g.axes[1,0].yaxis.set_label_text('y (m)')
g.axes[2,0].yaxis.set_label_text('z (m)')
g.axes[2,0].xaxis.set_label_text('x (m)')
g.axes[2,1].xaxis.set_label_text('y (m)')
g.axes[2,2].xaxis.set_label_text('z (m)')
plt.savefig('evenSamples.pdf')