DLCImporter
can be used to import result files from DeepLabCut in H5 format.
It will automatically drop the unnecessary scorer
level.
imp = DLCImporter()
df = imp.import_hdf('example_data/coordinates.h5')
display(df)
For further processing and analyzes, it can be useful to transform the tracked coordinates into relative coordinates (being relative to a specified bodypart). This can be done by specifying a bodypart as the relative origin.
df_relative = transform_to_relative(df, 'body')
test_close(df_relative['head','x'][0], -30.6, 0.1)
test_close(df_relative['head','y'][0], -119.1, 0.1)
test_close(df_relative['tail','x'][0], 70.87, 0.1)
test_close(df_relative['tail','y'][0], 135.08, 0.1)
By using the arctan2
function of Numpy, we are able to calculate the angle of the vector from the origin to a specific point.
import numpy as np
y = np.array([1, 1, 1, -1, 0.75])
x = np.array([0, 1, -1, 1, 1.5])
result = np.degrees(np.arctan2(y, x))
expected = np.array([90, 45, 135, -45, 26.565051])
np.testing.assert_almost_equal(result, expected, decimal=3)
Since we already moved the body into the origin, we can now decide for a good point for determining the angle of the pigeon rotation. The vector between the body and middle point between left_neck and right_neck seems to be suitable. Therefore, we first calculate this point, we can later use to determine the rotation of the pigeon.
df_example = df.copy()
df_example['left_neck', 'x'][0] = 1
df_example['left_neck', 'y'][0] = 1
df_example['right_neck', 'x'][0] = 2
df_example['right_neck', 'y'][0] = 0.5
df_result = add_middle_neck(df_example)
test_close(df_result['middle_neck','x'][0], 1.5, 0.1)
test_close(df_result['middle_neck','y'][0], 0.75, 0.1)
Based on the middle_neck
coordinates, we now add the corresponding rotation to the dataframe.
df_result = add_rotation(df_result)
test_close(df_result['rotation_angle'][0], 26.565051, 0.1)
Finally, we can rotate all relative coordinates by this angle around the origin (which is the body
), so we stabilize (and normalize) the pigeon orientation.
df_example = df_result.copy()
df_rotation_applied = apply_rotation(df_example)
test_close(df_rotation_applied['middle_neck','x'][0], 0, 0.1)
test_close(df_rotation_applied['middle_neck','y'][0], 1.6, 0.1)
test_close(df_rotation_applied['left_neck','x'][0], -0.5, 0.1)
test_close(df_rotation_applied['left_neck','y'][0], 1.34, 0.1)
df_rotation_applied