Kinematic analysis#

What libraries should I import?#

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

Recap#

Dummy data for the following exercises is provided here.

file = '/Users/guillermo/Downloads/pose-3d.csv'
data = pd.read_csv(file, header=0)
coords = data.loc[:, ~data.columns.str.contains(
    'score|error|ncams|fnum|center|M_')]

Position in space#

position_x = coords['nose1_x']
position_y = coords['nose1_y']
position_z = coords['nose1_z']

pos_x, = plt.plot(position_x, label='x')
pos_y, = plt.plot(position_y, label='y')
pos_z, = plt.plot(position_z, label='z')
plt.xlabel('Time [frames]')
plt.ylabel('Position [mm]')
plt.legend()
_images/dab6991ca77eb6752e61bd236031646dc552a0ab3108e94df220274f7519461d.png
position_x.hist()
_images/514d639d1ca7b01ad99d93ac2872890dbd938c6fb437ab677a550d42825be49e.png
x = coords['nose1_x']
y = coords['nose1_y']
z = coords['nose1_z']

# creating 3d figures
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(projection='3d')

# creating the path map
img = ax.scatter(x, y, z, marker='o', s=60, color='gray')

# adding title and labels
ax.set_title("3D Path")
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

# displaying plot
plt.show()
_images/c007705ba604bfa0933ebd57ca2f2e5fc105a8cc390cb32f3af99710acaf0efb.png

Velocity as difference between positions#

velocity_x = np.append([0], np.diff(position_x, n=1))
velocity_y = np.append([0], np.diff(position_y, n=1))
velocity_z = np.append([0], np.diff(position_z, n=1))

vel_x, = plt.plot(velocity_x, label='x')
vel_y, = plt.plot(velocity_y, label='y')
vel_z, = plt.plot(velocity_z, label='z')
plt.xlabel('Time [frames]')
plt.ylabel('Velocity [mm/s]')
plt.legend()
_images/72e6a13c95415f081c17ee65a78cf5b2cf6e18e99eafad85155aaff0101c1e1e.png
_ = plt.hist(velocity_x, bins='auto')
_images/1d86fad533bb020802aa98e08d8dff7c6ff48fa7f8e32e251b38b36de82b1b60.png

Acceleration as difference in velocity#

# Acceleration of head movement as frame-to-frame difference in velocity
acceleration_x = np.append([0], np.diff(velocity_x, n=1))
acceleration_y = np.append([0], np.diff(velocity_y, n=1))
acceleration_z = np.append([0], np.diff(velocity_z, n=1))

acc_x, = plt.plot(acceleration_x, label='x')
acc_y, = plt.plot(acceleration_y, label='y')
acc_z, = plt.plot(acceleration_z, label='z')
plt.xlabel('Time [frames]')
plt.ylabel('Acceleration [mm/s^2]')
plt.legend()
_images/b653130bd965b1b12ba3f9ca7bf895772c92fe501b80e7cfdf9e31a28b124366.png
_ = plt.hist(acceleration_x, bins='auto')
_images/543f4a75ed5ea4ad5a5d98bd84fa849eb25a7016556c90616dd4b6b96ed3ef0f.png
plt.figure(figsize=(6, 6))
plt.hist(acceleration_x, bins=100, alpha=0.5, label="acceleration_x")
plt.hist(acceleration_y, bins=100, alpha=0.5, label="acceleration_y")
plt.hist(acceleration_z, bins=100, alpha=0.5, label="acceleration_z")
plt.xlabel("Acceleration", size=14)
plt.ylabel("Count", size=14)
plt.title("Multiple Histograms with Matplotlib")
plt.legend(loc='upper right')
_images/3b8c943931e968e5232b49adb6636dcd5d258fe575171b325862b6d267da2ed0.png
# Grouped plots
_ = plt.figure(figsize=(6, 6))
plt.violinplot(acceleration_x)
plt.violinplot(acceleration_y)
plt.violinplot(acceleration_z)
plt.xlabel("Acceleration", size=14)
plt.ylabel("Count", size=14)
plt.title("Multiple Violin Plots with Matplotlib")
_images/5b5c4160f2bec6088fa7b79850961a12e1e443dab5f30eec6e1df0ad271048d6.png
# Combine data
combined_acc = list([acceleration_x, acceleration_y, acceleration_z])

fig, ax = plt.subplots()

xticklabels = ['x', 'y', 'z']
ax.set_xticks([1, 2, 3])
ax.set_xticklabels(xticklabels)
ax.violinplot(combined_acc)

plt.xlabel("Coordinate", size=14)
plt.ylabel("Acceleration", size=14)
plt.title("Multiple Violin Plots with Matplotlib")
_images/7aa43a97e541173ee2d3a39d4fd432d0a69f5c8af87931f4823f693f8ee68a79.png

In the following section we will learn to cluster our data using some very basic machine learning techniques.