<jupyter_start><jupyter_text>Process raw avi videos via the trained DLC network<jupyter_code># imports
import os
import sys
sys.path.insert(0, os.path.abspath(r'D:\Code Repos\prey_capture'))

import shutil
import deeplabcut as dlc
import paths
import functions_bondjango as bd
import functions_misc as fm
import yaml

# define the config_path
config_path = paths.config_path
try:
    # get the target video path
    video_path = snakemake.input[0]
    out_path = snakemake.output[0]
    video_data = yaml.load(snakemake.params.file_info, Loader=yaml.FullLoader)

except NameError:
    # define the search string
    search_string = 'result:succ, lighting:normal, rig:miniscope, imaging:doric'
    # query the database for data to plot
    data_all = bd.query_database('video_experiment', search_string)
    video_data = data_all[0]
    video_path = video_data['avi_path']
    # assemble the output path
    out_path = video_path.replace('.avi', '_dlc.h5')
# analyze the video
# dlc.analyze_videos?
dlc.analyze_videos(config_path, [video_path], destfolder=paths.temp_path)
# filter the data
dlc.filterpredictions(config_path, [video_path],filtertype='median', 
                      windowlength=11, destfolder=paths.temp_path, save_as_csv=False)
# dlc.filterpredictions?
# move and rename the file

# get a list of the files present in the temp folder
origin_file = [el for el in os.listdir(paths.temp_path) if ('filtered.h5' in el) and (video_data['slug'] in fm.slugify(el))]

assert len(origin_file) > 0, 'The target file was not found'

# rename the file to the final path
shutil.move(os.path.join(paths.temp_path, origin_file[0]), out_path)
# update the bondjango entry (need to sort out some fields)
ori_data = video_data.copy()
ori_data['dlc_path'] = out_path
mouse = ori_data['mouse']
ori_data['mouse'] = '/'.join((paths.bondjango_url, 'mouse', mouse, ''))
ori_data['experiment_type'] = '/'.join((paths.bondjango_url, 'experiment_type', 'Free_behavior', ''))

update_url = '/'.join((paths.bondjango_url, 'video_experiment', ori_data['slug'], ''))
output_entry = bd.update_entry(update_url, ori_data)

print(output_entry.status_code)<jupyter_output>200
{'fraction_non_alphanumeric': 0.11631139944392957, 'fraction_numerical': 0.006024096385542169, 'mean_word_length': 5.387573964497041, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '321432', 'license': 'MIT', 'n_tokens_mistral': 764, 'n_tokens_neox': 737, 'n_words': 182, 'path': 'snakemake_scripts/notebooks/Process_videoexperiment.ipynb', 'repo_name': 'drguggiana/prey_capture', 'size': '2158'}