Exemplo n.º 1
0
def run_body_speed_for_all_data():
    details = load_session_details()
    details['OF_body_speed_max'] = np.nan
    details['OF_body_speed_mean'] = np.nan
    details['OF_body_distance'] = np.nan
                 
    # OF
    base_folder = r'Y:\Data 2018-2019\Complement 4 - schizophrenia Project\2019 Adult Behavior C4_for revisions\OF'
    litters = ['ALC_050319_1_control','ALC_050319_2_mC4','ALC_051719_1_control','ALC_051719_2_mC4','ALC_060519_1_control','ALC_060519_2_mC4','ALC_070519_1_control'] # 'ALC_060519_1_control','ALC_060519_2_mC4',
    
    for litter in litters:
         # get the subject folders
         sessions = os.listdir(os.path.join(base_folder,litter))
         for session in sessions:
             sub = get_subject_from_session(session)
             sess_folder = os.path.join(base_folder,litter,session)
             vids = get_all_videos_in_session(sess_folder)
             df = get_analysis_dfs(sess_folder,vids)
             bs = get_animal_speed(df,time_filter=300)
             
             details.loc[sub,'OF_body_speed_max'] = np.nanmax(bs)/1000.
             details.loc[sub,'OF_body_speed_mean'] = np.nanmean(bs)/1000.
             details.loc[sub,'OF_body_distance'] = np.nansum(bs/20.)/1000.

             print(litter,':',session)
    details.to_csv(r'C:\Users\User\Desktop\Code\dlc_utils\OF_speed_distance.csv')       
Exemplo n.º 2
0
def run_open_probability_for_all_data():
    details = load_session_details()
    details['EZM_open_probability'] = np.nan

    # OF
    base_folder = r'Y:\Data 2018-2019\Complement 4 - schizophrenia Project\2019 Adult Behavior C4_for revisions\EZM'
    litters = [
        'ALC_050319_1_control', 'ALC_050319_2_mC4', 'ALC_051719_1_control',
        'ALC_051719_2_mC4', 'ALC_060519_1_control', 'ALC_060519_2_mC4',
        'ALC_070519_1_control'
    ]  # 'ALC_070519_1_control'

    for litter in litters:
        # get the subject folders
        sessions = os.listdir(os.path.join(base_folder, litter))
        for session in sessions:
            sub = get_subject_from_session(session)
            sess_folder = os.path.join(base_folder, litter, session)
            vids = get_all_videos_in_session(sess_folder)
            df = get_analysis_dfs(sess_folder, vids)
            q_loc, body_loc, zm_center, open_coord, closed_coord = get_quadrant(
                df, time_filter=300
            )  #### change time_filter=None if you dont want a time filter

            details.loc[sub, 'EZM_open_probability'] = np.nansum(
                q_loc) / np.count_nonzero(~np.isnan(q_loc))

            print(litter, ':', session)
    details.to_csv(
        r'C:\Users\User\Desktop\Code\dlc_utils\EZM_open_probability.csv')
Exemplo n.º 3
0
def run_latency_for_all_data():
    details = load_session_details()
    details['NOR_T1_choice'] = np.nan
    details['NOR_T1_object1_latency'] = np.nan
    details['NOR_T1_nonobject_latency'] = np.nan
    details['NOR_T2_choice'] = np.nan
    details['NOR_T2_object1_latency'] = np.nan
    details['NOR_T2_object2_latency'] = np.nan
    details['SI_T1_choice'] = np.nan
    details['SI_T1_subject1_latency'] = np.nan
    details['SI_T1_nonsubject_latency'] = np.nan
    details['SI_T2_choice'] = np.nan
    details['SI_T2_subject1_latency'] = np.nan
    details['SI_T2_subject2_latency'] = np.nan
    
    # NOR
    base_folder = r'Y:\Data 2018-2019\Complement 4 - schizophrenia Project\2019 Adult Behavior C4_for revisions\NOR'
    litters = ['ALC_050319_1_control','ALC_050319_2_mC4','ALC_051719_1_control','ALC_051719_2_mC4','ALC_060519_1_control','ALC_060519_2_mC4','ALC_070519_1_control']
    
    for litter in litters:
         # get the subject folders
         sessions = os.listdir(os.path.join(base_folder,litter))
         for session in sessions:
             sub = get_subject_from_session(session)
             trials = os.listdir(os.path.join(base_folder,litter,session))
             for trial in trials:
                 sess_folder = os.path.join(base_folder,litter,session,trial)
                 vids = get_all_videos_in_session(sess_folder)
                 df = get_analysis_dfs(sess_folder,vids)
                 choice,bl,tr = get_latency(df,plot_on=False,time_filter=300)
                 
                 # now comes the annoying if then statements
                 
                 # trial1 or trial 2?
                 if 'Trial1' in trial or 'TRIAL1' in trial or 'trial1' in trial:
                     if details.loc[sub,'NOR_obj1']=='bl':
                         #This is trial 1 and the object is in bl
                         o_pref = bl
                         o_nonpref = tr
                         if choice=='bl':details.loc[sub,'NOR_T1_choice'] = 'object'
                         else: details.loc[sub,'NOR_T1_choice'] = 'nonobject'
                         details.loc[sub,'NOR_T1_object1_latency'] = bl
                         details.loc[sub,'NOR_T1_nonobject_latency'] = tr
                     else:
                         #This is trial 1 and the object is in tr
                         o_pref = tr
                         o_nonpref = bl
                         if choice=='bl':details.loc[sub,'NOR_T1_choice'] = 'nonobject'
                         else: details.loc[sub,'NOR_T1_choice'] = 'object'
                         details.loc[sub,'NOR_T1_object1_latency'] = tr
                         details.loc[sub,'NOR_T1_nonobject_latency'] = bl
                 elif 'Trial2' in trial or 'TRIAL2' in trial or 'trial2' in trial:
                     if details.loc[sub,'NOR_obj1']=='bl':
                         #This is trial 2 and object1 was in bl in trial1 and 2
                         o_pref = bl
                         o_nonpref = tr
                         if choice=='bl':details.loc[sub,'NOR_T2_choice'] = 'familiar'
                         else: details.loc[sub,'NOR_T2_choice'] = 'novel'
                         details.loc[sub,'NOR_T2_object1_latency'] = bl
                         details.loc[sub,'NOR_T2_object2_latency'] = tr
                     else:
                         #This is trial 2 and object1 was in tr in trial1 and 2
                         o_pref = tr
                         o_nonpref = bl
                         if choice=='bl':details.loc[sub,'NOR_T2_choice'] = 'novel'
                         else: details.loc[sub,'NOR_T2_choice'] = 'familiar'
                         details.loc[sub,'NOR_T2_object1_latency'] = tr
                         details.loc[sub,'NOR_T2_object2_latency'] = bl
                 else:
                     print(sess_folder)
                     ValueError('why am I here?')
                 print(litter,':',session,':',trial,':opref:',o_pref,':ononpref:',o_nonpref)
                 
    # SI
    base_folder = r'Y:\Data 2018-2019\Complement 4 - schizophrenia Project\2019 Adult Behavior C4_for revisions\SocialInteraction'
    litters = ['ALC_050319_1_control','ALC_050319_2_mC4','ALC_051719_1_control','ALC_051719_2_mC4','ALC_060519_1_control','ALC_060519_2_mC4','ALC_070519_1_control']
    
    for litter in litters:
         # get the subject folders
         sessions = os.listdir(os.path.join(base_folder,litter))
         for session in sessions:
             sub = get_subject_from_session(session)
             trials = os.listdir(os.path.join(base_folder,litter,session))
             trials = [x for x in trials if x !='.DS_Store']
             for trial in trials:
                 sess_folder = os.path.join(base_folder,litter,session,trial)
                 vids = get_all_videos_in_session(sess_folder)
                 df = get_analysis_dfs(sess_folder,vids)
                 choice,bl,tr = get_latency(df,plot_on=False,fractional_size=0.36,time_filter=300)
                 
                 # now comes the annoying if then statements
                 
                 # trial1 or trial 2?
                 if 'Trial1' in trial or 'TRIAL1' in trial:
                     if details.loc[sub,'SI_mouse1']=='bl':
                         #This is trial 1 and the subject is in bl
                         o_pref = bl
                         o_nonpref = tr
                         if choice=='bl':details.loc[sub,'SI_T1_choice'] = 'subject'
                         else: details.loc[sub,'SI_T1_choice'] = 'nonsubject'
                         details.loc[sub,'SI_T1_subject1_latency'] = bl
                         details.loc[sub,'SI_T1_nonsubject_latency'] = tr
                     else:
                         #This is trial 1 and the object is in tr
                         o_pref = tr
                         o_nonpref = bl
                         if choice=='bl':details.loc[sub,'SI_T1_choice'] = 'nonsubject'
                         else: details.loc[sub,'SI_T1_choice'] = 'subject'
                         details.loc[sub,'SI_T1_subject1_latency'] = tr
                         details.loc[sub,'SI_T1_nonsubject_latency'] = bl
                 elif 'Trial2' in trial or 'TRIAL2' in trial:
                     if details.loc[sub,'SI_mouse1']=='bl':
                         #This is trial 2 and object1 was in bl in trial1 and 2
                         o_pref = bl
                         o_nonpref = tr
                         if choice=='bl':details.loc[sub,'SI_T2_choice'] = 'familiar'
                         else: details.loc[sub,'SI_T2_choice'] = 'novel'
                         details.loc[sub,'SI_T2_subject1_latency'] = bl
                         details.loc[sub,'SI_T2_subject2_latency'] = tr
                     else:
                         #This is trial 2 and object1 was in tr in trial1 and 2
                         o_pref = tr
                         o_nonpref = bl
                         if choice=='bl':details.loc[sub,'SI_T2_choice'] = 'novel'
                         else: details.loc[sub,'SI_T2_choice'] = 'familiar'
                         details.loc[sub,'SI_T2_subject1_latency'] = tr
                         details.loc[sub,'SI_T2_subject2_latency'] = bl
                 else:
                     print(sess_folder)
                     ValueError('why am I here?')
                 print(litter,':',session,':',trial,':opref:',o_pref,':ononpref:',o_nonpref)
    details.to_csv(r'C:\Users\User\Desktop\Code\C4_behavior_utils\SIandNOR_details_NOR_SI_latency_300s.csv')
Exemplo n.º 4
0
                         if choice=='bl':details.loc[sub,'SI_T2_choice'] = 'novel'
                         else: details.loc[sub,'SI_T2_choice'] = 'familiar'
                         details.loc[sub,'SI_T2_subject1_latency'] = tr
                         details.loc[sub,'SI_T2_subject2_latency'] = bl
                 else:
                     print(sess_folder)
                     ValueError('why am I here?')
                 print(litter,':',session,':',trial,':opref:',o_pref,':ononpref:',o_nonpref)
    details.to_csv(r'C:\Users\User\Desktop\Code\C4_behavior_utils\SIandNOR_details_NOR_SI_latency_300s.csv')
    
    
if __name__=='__main__':
    import cv2
    # get the occupancy
    folder = r'C:\Users\bsriram\Desktop\Data\ACM_Data\AllData\SI_sample'
    vids = get_all_videos_in_session(folder)
    df = get_analysis_dfs(folder,vids)
    plot_pos_vs_frame(df)
    bl,tr = get_occupancy(df,plot_on=True,fractional_size=0.36)
    print('bl',bl)
    print('tr',tr)
    
    # get the latency
    # folder = r'Y:\Data 2018-2019\Complement 4 - schizophrenia Project\2019 Adult Behavior C4_for revisions\NOR\ALC_051719_1_control\ALC_051719_1_45C_NOR\ALC_051719_1_45C_NOR_Trial2'
    # vids = get_all_videos_in_session(folder)
    # df = get_analysis_dfs(folder,vids)
    # bl,tr = get_latency(df,plot_on=True,fractional_size=0.33)
    
    # run_occupancy_for_all_data()
    # run_latency_for_all_data()
    
Exemplo n.º 5
0
def show_analysis(
    session=r'Y:\Data 2018-2019\Complement 4 - schizophrenia Project\2019 Adult Behavior C4_for revisions\EZM\ALC_060519_2_mC4\ALC_060519_2_57G_EZM',
    output=r"C:\Users\User\Desktop\Code\annotated_SI_part.avi",
    speedX=10,
):
    import cv2
    import tqdm
    import collections
    import sys

    vids = get_all_videos_in_session(session)
    df = get_analysis_dfs(session, vids)
    location, body_loc, zm_center, open_coord, closed_coord = get_quadrant(df)

    try:
        curr_filename = os.path.join(session, vids[0] + '.avi')
        cap = cv2.VideoCapture(curr_filename)
        fourcc = cv2.VideoWriter_fourcc(*'DIVX')
        writer = cv2.VideoWriter(output, fourcc, 1 / cap.get(2),
                                 (int(cap.get(3)), int(cap.get(4))))
        for idx, row in tqdm.tqdm(df.iterrows(), total=df.shape[0]):
            if not idx % int(speed) == 0: continue
            if row['source'][0] in curr_filename:
                # cv is open to the right place
                pass
            else:
                cap.release()
                curr_filename = os.path.join(session,
                                             row['source'][0] + '.avi')
                cap = cv2.VideoCapture(curr_filename)
            # get the frame
            cap.set(1, row['frame_number'][0])  # 1==set frame number
            succ, img = cap.read()

            # draw the object circles
            img = cv2.circle(img, (int(zm_center[0]), int(zm_center[1])), 5,
                             (0, 0, 255))
            img = cv2.circle(img, (int(open_coord[0]), int(open_coord[1])), 5,
                             (0, 0, 255))
            img = cv2.circle(img, (int(closed_coord[0]), int(closed_coord[1])),
                             5, (0, 0, 255))

            if not np.isnan(location[idx]):
                if location[idx] == 1:
                    img = cv2.circle(
                        img, (int(body_loc[0][idx]), int(body_loc[1][idx])), 5,
                        (255, 0, 0))
                else:
                    img = cv2.circle(
                        img, (int(body_loc[0][idx]), int(body_loc[1][idx])), 5,
                        (0, 255, 0))

            # add to writer
            writer.write(img)
    except Exception as er:

        raise er.with_traceback(sys.exc_info()[2])
    finally:
        # for index, row in df.iterrows():
        cap.release()
        writer.release()
        cv2.destroyAllWindows()