<jupyter_start><jupyter_text>Behavioral PreprocessingFunctions included: Distance between defeated and bullying mouse; Defeated mouse head direction; Behavioral annotation.<jupyter_code>import numpy as np 
import os
import glob
import pandas as pd
import h5py
from math import degrees, atan2
os.chdir("/home/donghan/DeepLabCut/data/rotated") #Change to your own directory<jupyter_output><empty_output><jupyter_text>Data read from h5 file<jupyter_code>filenames = glob.glob('*.h5') 
#Return the file name with extention of .h5, which contain the data of coordination axis
f = []
for filename in filenames:
    f = h5py.File(filename, 'r')
    start = filename.find('10') 
    #Find the string that start with "10"
    end = filename.find(' rotated', start) 
    #Return the string with end of " rotated", aims to name the file
    csvfile = []
    with pd.HDFStore(filename, 'r') as d:
        df = d.get(list(f.keys())[0])
        df.to_csv(filename[start:end] + '.csv') 
        #Automaticaly change to unique file name with specific mouse number
        csvfile.append(filename[start:end] + '.csv')
df<jupyter_output><empty_output><jupyter_text>Distance bewteen defeated mouse head and encloser The Pythagorean theorem<jupyter_code>#Distance bewteen defeated mouse head and encloser
#Encloser coordination axis need modification #IMPORTANT

def cal_dist(data): 
    fixedX = 50
    fixedY = 200
    x = data["x"]
    y = data["y"]
    dist = np.sqrt((x - fixedX)**2 + (y - fixedY)**2)
    if __name__ == "__main__": 
        return dist<jupyter_output><empty_output><jupyter_text>Head Direction<jupyter_code>#Head Direction

def head_dir(data):
    p1 = pd.concat([data["x"], data["y"]],axis=1)
    p2 = pd.concat([data["x.1"], data["y.1"]], axis = 1, keys=['x', 'y']) 
    #Reassign column names
    xDiff = p2.x - p1.x
    yDiff = p2.y - p1.y
    direction = []
    degreeL = []
    for i in range(0,len(xDiff)):
        degree = degrees(atan2(tuple(yDiff)[i], tuple(xDiff)[i]))
        degreeL.append(degree)
        if (degree >= 90 and degree <= 180) or (degree <= -90 and degree >= -180): 
            #Facing encloser
            direction.append(1)
        else: 
            direction.append(0) 
            #Facing other side
    if __name__ == "__main__": 
        return (direction, degreeL)
# Whether the mouse's direction towards bullying mouse or not, 1: yes; 0: no
# The degree of head direction, clockwise.
head_dir(move_data)<jupyter_output><empty_output><jupyter_text>AnnotationsBased on pre-setting video, the coordination axis could be used in annotating. For example, since the behavioral camera is not perfectly match the size of the cage, we have the interaction zone of four points, (70, 116), (70, 307), (171, 116), (171, 307), so once the mouse's head enter this area, the behavior will be labled as "1"<jupyter_code>#Annotations of bahavior definition

# Approaching the interaction zone (area around the enclosure)
# Avoiding the interaction zone - this could include instances where the animal enters the 1/2 of the chamber containing the enclosure + the head direction is facing the enclosure, but it doesn't enter the interaction zone
# Sniffing/directly interacting with the enclosure
# Sitting in the corners (2 corners closest to enclosure vs 2 far corners)


def annotate(interactionXL = 70, interactionXR = 171, interactionYUpper = 307, interactionYLower = 116, enclosureX = 134, enclosureYLower = 146, enclosureYUpper = 256, corX = 315, corUppRY = 302, corLowRY = 124, MidLineX = 190, data = None):
    posX = data['x'].tolist()
    posY = data['y'].tolist()
    head = head_dir(data)[0] 
    #Whether the head is facing enclosure
    label = []
    for i in range(0, len(posX)):
        if posX[i] < interactionXR and posY[i] < interactionYUpper and posY[i] > interactionYLower: 
            #Approaching
            label.append(1)
        elif posX[i] <= MidLineX and posX[i] > interactionXR and (posY[i] > interactionYUpper or posY[i] < interactionYLower): 
            #Avoidance
            label.append(2)
        elif posX[i] < enclosureX and posY[i] > enclosureYLower and posY[i] < enclosureYUpper: 
            #Sniffing
            label.append(3)
        elif (posX[i] > corX and posY[i] < corLowRY) or (posX[i] > corX and posY[i] > corUppRY): 
            #Sitting in the corners
            label.append(4)
        else: #Other cases
            label.append(5)
    return label<jupyter_output><empty_output><jupyter_text>Remove the first ten seconds, which has no mouse in the case (~200 frames)<jupyter_code>for i in csvfile:
    data = pd.read_csv(i, skiprows = 2) 
    #Skip the rows of scorer and bodyparts
    move_data = data.loc[200:] 
    
    #There are no mouse in the first ten seconds
    print(cal_dist(move_data)) 
    #Distance
    print(head_dir(move_data)) 
    #Head direction
    print(annotate(data = move_data))
    #Annotation<jupyter_output><empty_output><jupyter_text>Appendix Video Rotation<jupyter_code>import numpy as np
import cv2
import glob
import os
os.chdir("/home/donghan/DeepLabCut/data") 
#Working directory that stores video data
def rotate(image, angle, center=None, scale=1): 
    #scale = 1: original size
    rows,cols,ch = image.shape
    if center == None:
        center = (cols / 2, rows / 2)
    M = cv2.getRotationMatrix2D(center, angle, scale) 
    #Matrix: Rotate with center by angles
    dst = cv2.warpAffine(image,M,(cols,rows)) 
    #After rotation
    return dst


def videorotate(filename, output_name, display_video = False):
    # capture video
    cap = cv2.VideoCapture(filename)

    #read video frame by frame
    #extract original video frame features
    sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

    fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))

    fps = int(cap.get(cv2.CAP_PROP_FPS))

    #Make a directory to store the rotated videos
    path = "./rotated"
    try:  
        os.mkdir(path)
    except OSError:  
        pass
    else:  
        print ("Successfully created the directory %s " % path)
        
    #Automatically name the rotated videos  
    file = "./rotated/" + output_name
    out = cv2.VideoWriter(file, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, sz) 
    #Integrate all frames to video

    
    #Read videos and rotate by certain degrees
    while(cap.isOpened()):
        #flip for truning(fliping) frames of video
        ret,img = cap.read()
        try:
#             img2 = rotateimg, -3.5) 
            #Flipped Vertically
            out.write(img)
            if display_video == True:
                cv2.imshow('rotated video',img) 

            k=cv2.waitKey(30) & 0xff
            #once you inter Esc capturing will stop
            if k==27:
                break
        except:
            print (filename, 'successfully rotated!!!' )
            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    
    
# #Generate all rotating videos    
# filenames = glob.glob('*.mp4') #Return the file name with .mp4 extention 
# for i in filenames:
#     videorotate(i,os.path.splitext(i)[0] + " rotated.mp4")
    
videorotate("behavCam1.avi", "test.avi", display_video = True)<jupyter_output>behavCam1.avi successfully rotated!!!<jupyter_text>Videos editing<jupyter_code>def videoediting(filename, output_name, corrX, corrY, dist = 320, display_video = False):
    # capture video
    cap = cv2.VideoCapture("/home/donghan/DeepLabCut/data/rotated/" + filename)

    #read video frame by frame
    #extract original video frame features
    sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

    fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))

    fps = int(cap.get(cv2.CAP_PROP_FPS))

    #Make a directory to store the rotated videos
    path = "./edited"
    try:  
        os.mkdir("/home/donghan/DeepLabCut/data/edited")
    except OSError:  
        print("Folder already exists, no need to create a new one.")
    else:  
        print ("Successfully created the directory %s " % path)
        
    #Automatically name the rotated videos  
    file = "./edited/" + output_name
    out = cv2.VideoWriter(file, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (dist, dist)) 
    #Integrate all frames to video
    corrX1 = corrX + dist
    corrY1 = corrY + dist
    
    #Read videos and rotate by certain degrees
    while(cap.isOpened()):
        #flip for truning(fliping) frames of video
        ret,img = cap.read()
        try:
            img2 = img[corrX:corrX1, corrY:corrY1]
            out.write(img2)
            if display_video == True:
                cv2.imshow('edited video',img2) 

            k=cv2.waitKey(30) & 0xff
            #once you inter Esc capturing will stop
            if k==27:
                break
        except:
            print (filename, 'successfully edited!!!' )
            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    
    
# #Generate all rotating videos    
# filenames = glob.glob('*.mp4') #Return the file name with .mp4 extention 
# for i in filenames:
#     videorotate(i,os.path.splitext(i)[0] + " rotated.mp4")
    
videoediting("behavCam1 rotated.avi", "behavCam1 rotated edited.avi", 35, 65, display_video = False)
from multiprocessing import Process
from os import getpid
from random import randint
from time import time, sleep
import numpy as np
import cv2
import glob
os.chdir("/home/donghan/DeepLabCut/data") 

#Working directory that stores video data
def rotate(image, angle, center=None, scale=1): 
    #scale = 1: original size
    rows,cols,ch = image.shape
    if center == None:
        center = (cols / 2, rows / 2)
    M = cv2.getRotationMatrix2D(center, angle, scale) 
    #Matrix: Rotate with center by angles
    dst = cv2.warpAffine(image,M,(cols,rows)) 
    #After rotation
    return dst

def videoProcessing(task, filename, output_name, corrX = None, corrY = None, dist = 320, display_video = False):
    '''
    Task: rotated, edited
    '''
    
    # capture video
    cap = cv2.VideoCapture(filename)

    #read video frame by frame
    #extract original video frame features
    sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

    fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))

    fps = int(cap.get(cv2.CAP_PROP_FPS))
    #Make a directory to store the rotated videos
    path = "./" + task
    try:  
        os.mkdir(path)
    except OSError:  
        print("Folder already exists, no need to create a new one.")
    else:  
        print ("Successfully created the directory %s " % path)
        
    #Automatically name the rotated videos  
    file = "./" + task + "/" + output_name
    if task == "edited":
        out = cv2.VideoWriter(file, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (dist, dist)) 
        #Integrate all frames to video
        corrX1 = corrX + dist
        corrY1 = corrY + dist
    else:
        out = cv2.VideoWriter(file, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, sz)
    #Read videos and rotate by certain degrees
    while(cap.isOpened()):
        #flip for truning(fliping) frames of video
        ret,img = cap.read()
        try:
            if task == "edited":
                img2 = img[corrX:corrX1, corrY:corrY1]
            else:
                img2 = rotate(img, -3.5) 
            out.write(img2)
            if display_video == True:
                cv2.imshow('edited video',img2) 

            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()          k=cv2.waitKey(30) & 0xff
            #once you inter Esc capturing will stop
            if k==27:
                break
        except:
            print (filename, 'successfully ', task, "!!!")
            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    
    
# #Generate all rotating videos    
# filenames = glob.glob('*.mp4') #Return the file name with .mp4 extention 
# for i in filenames:
#     videorotate(i,os.path.splitext(i)[0] + " rotated.mp4")
    



# videoProcessing("rotated", "behavCam1.avi", "behavCam1 rotated.avi", display_video = False)
    
    

def main():
    start = time()
    p1 = Process(target=videoProcessing, args=("rotated", "behavCam1.avi", "behavCam1 rotated.avi"))
    p1.start()
    p2 = Process(target=videoProcessing, args=("edited", "behavCam1 rotated.avi", "behavCam1 rotated edited.avi", 35, 65))
    p2.start()
    p1.join()
    p2.join()
    end = time()
    print('总共耗费了%.2f秒.' % (end - start))


if __name__ == '__main__':
    main()<jupyter_output>Folder already exists, no need to create a new one.
Folder already exists, no need to create a new one.
behavCam1 rotated.avi successfully  edited !!!
behavCam1.avi successfully  rotated !!!
总共耗费了3.52秒.
{'fraction_non_alphanumeric': 0.11224808623652555, 'fraction_numerical': 0.019450085924074365, 'mean_word_length': 2.8149582836710367, 'pattern_counts': {'":': 4, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '171896', 'license': 'Apache-2.0', 'n_tokens_mistral': 4358, 'n_tokens_neox': 4085, 'n_words': 1184, 'path': 'utils/videoProcessing/Preprocessing_behav.ipynb', 'repo_name': 'AdriandLiu/Neural-Decoding-Project', 'size': '12802'}