<jupyter_start><jupyter_text>ReachMaster Block Analysis Intended to use repeatedly on single trial blocks of video, kinematic, and experiment data. Items required to run: Experimental dataframe, Kinematic dataframe, block video of known date/session/rat The following code : imports and utilize experimental data to coarsely segment trial video blocks into fine trials. Additional blocks save the images and video for each trial into a folder the user generated name information for. The final blocks import the kinematic data for a given rat (the rat you are examining in the video ex RM16) and visualize data across blocks and for a given trial.<jupyter_code>## import seaborn as sns
from ReachViz import ReachViz
    
    
block_video_file = '/Users/bassp/OneDrive/Desktop/Classification Project/2019-09-20-S1-RM14_cam2_DLC.mp4'
#block_video_file = '/Users/bassp/OneDrive/Desktop/bwlabeled.mp4'
kin_file = 'DataFrames/3D_positions_RM14.pkl'
exp_datafile = 'DataFrames/RM14_expdf.pickle'
date = '20'
session = 'S1'
rat = 'RM14'
R = ReachViz(date,session,exp_datafile, block_video_file,kin_file, rat)
#R.reach_splitter_threshold()
R.vid_splitter_and_grapher()

def loop_over_rats_and_extract_reaches(prediction_dataframe,e_dataframe, dummy_video_path, rat):
    save_path = '/Users/bassp/OneDrive/Desktop/Classification Project/reach_thresholds_RM15/'
    # Get rat, date, session for each block we need to process.
    k_dataframe = pd.read_pickle(prediction_dataframe)
    #pdb.set_trace()
    for kk in k_dataframe:
        session = kk.columns[2][1] 
        date = kk.columns[2][0][2:4]
        print(session,date)
        R = ReachViz(datez,session,e_dataframe, dummy_video_path,prediction_dataframe, rat)
        reaching, mask, bout = R.reach_splitter_threshold(save_dir=save_path)   
    return

#loop_over_rats_and_extract_reaches(kin_file,exp_datafile,block_video_file, rat)

# Not all camera angles are created equal : Let's use a weight of camera 1, 2, and 3 to create a metric for or against interpolation 

# Apply mean filter for segmentation (we can't let just any value's be looked at, general domain boundary...)

# Create 
# Post-segmentation apply more coordinated filtering eg: compare trajectories etc for trials, data-driven analysis.

# Detect single-reach, double- reach, or "multi-reach" 
import imageio
import pdb
images = []
writer = imageio.get_writer('bw.mp4', fps=30)
reader = imageio.get_reader(block_video_file)
for img in reader.iter_data():
    
    writer.append_data(img[:,:,1]) # "L" channel
writer.close()

def rz_length(rz_array,time_array):
    ### Takes in matched length Reward Zone and Normalized Time arrays (m_times)
        
    ###
    mask=np.zeros(len(time_array))
    for ix,ct in enumerate(rz_array):
        if ct == 1:
            mask[ix]=1
    # get rough amt of time spent in reward zone w/ # of exposures
    # exposures are variable, but only to 4th float place so we are ok (ms)
    rz_len = np. count_nonzero(mask == 1)
    flip=0
    for ji,jn in enumerate(mask):
        try:
            if jn == 1:
                if mask[ji-1]==0:
                    if mask[ji+1]==1:
                        if mask[ji-20]==0:
                            flip+=1
        except:
            flip=flip
    return rz_len,flip
        

def trial_length(start,stop,sf):
    t_length = []
    reward_region = []
    for i,x  in enumerate(start):
        if i in sf:
            t_length.append(stop[i]-x)
            reward_region.append(stop[i]+200-x+50)
    return t_length,reward_region<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08968609865470852, 'fraction_numerical': 0.016816143497757848, 'mean_word_length': 3.494962216624685, 'pattern_counts': {'":': 0, '<': 5, '<?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': '105847', 'license': 'BSD-3-Clause-LBNL', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1131, 'n_words': 335, 'path': 'software/visualization/Block_Reaching_Visualization.ipynb', 'repo_name': 'luzgool/ReachMaster', 'size': '3568'}