Velocity Calculation - NorrisLab/DLC_Python_Scripts GitHub Wiki

Velocity Calculation

How to generate velocity graphs from a DLC h5 file

Code in reference: link to code

You will need to understand the naming conventions of these generated files. For example:

Vglut-cre C135 M3+_2_DLC_resnet50_EnclosedBehaviorMay27shuffle1_307000_filtered.h5

|------Video Name-----|-------------DLCscorer--------------------------|

Basic Imports:

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
from textwrap import wrap

Things you may want to change:

rolling_avg_duration: Tells you how long the rolling average duration needs to be

sample_int: how often to take a sample of the video. If the sample_int is 50, then the values will be taken every 50 frames and placed in that DataFrame

pixels_per_cm: this value would need to be calculated based on a video. Run the video through bonsai to see how many pixels is in 1cm

fps: Self-explanatory...I hope. If not, this is the frames per second for the video

body_part_to_plot: The body part you would like to plot. For the vel_det function, consider that only one point is being used to calculate the average. For the vel_det_paws function you would probably be using a forepaw or a hindpaw.

Velocity Determination:

def vel_det(file, line_color):

Example of calling the function: vel_det(file='Vglut-cre_C269_Day2_M0_side_viewDLC_resnet50_FST-90May6shuffle1_1030000filtered.h5', line_color="blue")

Parameters
----------
file : STRING
    The h5 file that DLC generates after analysis
line_color : STRING
    what color you want the lines to appear
Returns
-------
None.
    Generates a velocity plot. Using the x and y coordinate from the chosen body part, calculate the displacement
    if there's a zero value then take the value from before. Afterwards, calculate the absolute value and plot
    the results.
    
    A window will be used which is specified above to roll the velocity based on how many seconds you specify.
    If you do NOT want to roll the velocity, just set this value to 1.

def vel_det_(file, line_color):

Parameters
----------
file : STRING
    The h5 file that DLC generates after analysis. You can either put the h5 into a folder containing this file
line_color : STRING
    what color you want the lines to appear.

Returns
-------
None.
    Generates a velocity plot. This is similar to the function above except this calculates an average velocity between the left and right 
    paws. Goes through and calculates the distance travelled and absolute value to calculate velocity.

Graphing things:

Example function call: graph_things("Velocity of VGlut-c276 M0")

def graph_things(title):

Parameters
----------
title : STRING
    Title to be plotted on the graph.

Returns
-------
None.
    Creates a graph and saves the plot if desired.