Video Tutorial and Walkthrough: Link
This Jupyter script is designed for motion analysis of tracked body parts in DeepLabCut CSV files. I created this notebook to help me with behavioral neuroscience experiments. It calculates the average positions and velocities of two body parts (for better accuracy). It creates visualizations such as GIFs and streamline plots to represent the motion and flow of movement. Although there are tools like Simba, DLCAnalyzer, and Bonsai, none are as easy as running a Python script.
To run this script, you will need Python installed on your system along with the following libraries:
pandas
numpy
matplotlib
imageio
scipy
IPython
You can install these libraries using pip: For example pip install pandas numpy matplotlib imageio scipy IPython
To use this script, you will need a CSV file containing the tracked body part data generated from DLC analyses. The script reads this data, performs calculations and generates visualizations.
- Load your CSV data into a DataFrame.
- Create a dictionary mapping body part names to their coordinate columns.
- Define functions to calculate average positions and velocities.
- Generate GIFs to visualize the movement of body parts.
- Create optical flow diagrams and streamline plots to visualize the flow of movement.
calculate_average_position
- Calculates the average position of two body parts.
create_position_gif
- Creates and displays a GIF from the average positions of two body parts.
calculate_average_velocity
- Calculates the average velocity of two body parts.
create_optical_flow_gif
- Creates and displays a GIF from the average positions and velocities of two body parts.
Optical Flow Diagram for Individual Body Part
- This Section creates quivers to generate optical flow diagrams.
Streamline Plot
- This Section of the code creates a streamlined plot where the red color represents the higher average speed.
Other Notes:
- Ensure that the CSV file path is correct and accessible on your system.
- Replace placeholder comments with actual values like frame rates and body part names based on your experimental data.
- The calculate_average_velocity function uses np.diff, which reduces the array size by one. Ensure that the positions array is adjusted accordingly when used with velocities.
- When creating streamline plots, ensure that the grid interpolation is performed correctly.