load_data.py
def construct_df_trajectories_per_trial(hd, ed):
    """
    This function takes in the Head Data (hd) and Event Data (ed) frames, and constructs a DataFrame that is indexed
    on time stamp, but contains just the trajectories between the appearance of the target and the end of each trial.

    :param hd: Pandas DataFrame containing the head data
    :param ed: Pandas DataFrame containing the event data
    :return:
    """

    target_times = ed[(ed['Name'] == 'TargetLeft') | (ed['Name'] == 'TargetRight')].index

    end_trial_indices = [ed.index.get_loc(trial) + 1 for trial in target_times]
    end_trial_times = ed.iloc[end_trial_indices].index  # the corresponding timestamps

    target_sides = ed[ed.Name.str.get(0).isin(['T'])].reset_index()

    trajectories = []
    for i, (start, end) in enumerate(zip(target_times, end_trial_times)):
        trial_trajectory = hd.loc[start:end]
        trial_trajectory = trial_trajectory.resample('0.01S').pad()
        trial_trajectory.loc[:, 'Trial number'] = i
        trial_trajectory.loc[:, 'Target side'] = target_sides.iloc[i]['Name']
        trial_trajectory['Trial time'] = trial_trajectory.index - trial_trajectory.index[0]
        trajectories.append(trial_trajectory)

    trajectories_df = pd.concat(trajectories).sort_index()

    # convert to matrices and then to angles
    list_of_matrices = [series2mat4(trajectories_df.iloc[x]) for x in range(trajectories_df.shape[0])]
    angles = np.array([np.degrees(mat2euler(mat, 'syzx')) for mat in list_of_matrices])  # retrieve euler angles
    angles_df = pd.DataFrame(angles, index=trajectories_df.index, columns=['Y rotation', 'Z rotation', 'X rotation'])
    trajectories_df = trajectories_df.join(angles_df)

    trial_starts = trajectories_df[trajectories_df['Trial time'] == trajectories_df.iloc[1]['Trial time']]
    zero_y = trial_starts['Y rotation'].mean()
    zero_z = trial_starts['Z rotation'].mean()
    trajectories_df['Centred Y angle'] = trajectories_df['Y rotation'] - zero_y
    trajectories_df['Centred Z angle'] = trajectories_df['Z rotation'] - zero_z
    return trajectories_df

    This function takes in the Head Data (hd) and Event Data (ed) frames, and constructs a DataFrame that is indexed
    on time stamp, but contains just the trajectories between the appearance of the target and the end of each trial.

    :param hd: Pandas DataFrame containing the head data
    :param ed: Pandas DataFrame containing the event data
    :return:
    
This function takes in the Head Data (hd) and Event Data (ed) frames, and constructs a DataFrame that is indexed
on time stamp, but contains just the trajectories between the appearance of the target and the end of each trial.
This function takes in the Head Data ( hd ) and Event Data ( ed ) frames and constructs a DataFrame that is indexed on time stamp but contains just the trajectories between the appearance of the target and the end of each trial .
def construct_df_trajectories_per_trial(hd, ed):
    target_times = ed[(ed['Name'] == 'TargetLeft') | (ed['Name'] == 'TargetRight')].index
    end_trial_indices = [ed.index.get_loc(trial) + 1 for trial in target_times]
    end_trial_times = ed.iloc[end_trial_indices].index  
    target_sides = ed[ed.Name.str.get(0).isin(['T'])].reset_index()
    trajectories = []
    for i, (start, end) in enumerate(zip(target_times, end_trial_times)):
        trial_trajectory = hd.loc[start:end]
        trial_trajectory = trial_trajectory.resample('0.01S').pad()
        trial_trajectory.loc[:, 'Trial number'] = i
        trial_trajectory.loc[:, 'Target side'] = target_sides.iloc[i]['Name']
        trial_trajectory['Trial time'] = trial_trajectory.index - trial_trajectory.index[0]
        trajectories.append(trial_trajectory)
    trajectories_df = pd.concat(trajectories).sort_index()
    list_of_matrices = [series2mat4(trajectories_df.iloc[x]) for x in range(trajectories_df.shape[0])]
    angles = np.array([np.degrees(mat2euler(mat, 'syzx')) for mat in list_of_matrices])  
    angles_df = pd.DataFrame(angles, index=trajectories_df.index, columns=['Y rotation', 'Z rotation', 'X rotation'])
    trajectories_df = trajectories_df.join(angles_df)
    trial_starts = trajectories_df[trajectories_df['Trial time'] == trajectories_df.iloc[1]['Trial time']]
    zero_y = trial_starts['Y rotation'].mean()
    zero_z = trial_starts['Z rotation'].mean()
    trajectories_df['Centred Y angle'] = trajectories_df['Y rotation'] - zero_y
    trajectories_df['Centred Z angle'] = trajectories_df['Z rotation'] - zero_z
    return trajectories_df
def construct_df_trajectories_per_trial ( hd , ed ) : target_times = ed [ ( ed [ 'Name' ] == 'TargetLeft' ) | ( ed [ 'Name' ] == 'TargetRight' ) ] . index end_trial_indices = [ ed . index . get_loc ( trial ) + 1 for trial in target_times ] end_trial_times = ed . iloc [ end_trial_indices ] . index target_sides = ed [ ed . Name . str . get ( 0 ) . isin ( [ 'T' ] ) ] . reset_index ( ) trajectories = [ ] for i , ( start , end ) in enumerate ( zip ( target_times , end_trial_times ) ) : trial_trajectory = hd . loc [ start : end ] trial_trajectory = trial_trajectory . resample ( '0.01S' ) . pad ( ) trial_trajectory . loc [ : , 'Trial number' ] = i trial_trajectory . loc [ : , 'Target side' ] = target_sides . iloc [ i ] [ 'Name' ] trial_trajectory [ 'Trial time' ] = trial_trajectory . index - trial_trajectory . index [ 0 ] trajectories . append ( trial_trajectory ) trajectories_df = pd . concat ( trajectories ) . sort_index ( ) list_of_matrices = [ series2mat4 ( trajectories_df . iloc [ x ] ) for x in range ( trajectories_df . shape [ 0 ] ) ] angles = np . array ( [ np . degrees ( mat2euler ( mat , 'syzx' ) ) for mat in list_of_matrices ] ) angles_df = pd . DataFrame ( angles , index = trajectories_df . index , columns = [ 'Y rotation' , 'Z rotation' , 'X rotation' ] ) trajectories_df = trajectories_df . join ( angles_df ) trial_starts = trajectories_df [ trajectories_df [ 'Trial time' ] == trajectories_df . iloc [ 1 ] [ 'Trial time' ] ] zero_y = trial_starts [ 'Y rotation' ] . mean ( ) zero_z = trial_starts [ 'Z rotation' ] . mean ( ) trajectories_df [ 'Centred Y angle' ] = trajectories_df [ 'Y rotation' ] - zero_y trajectories_df [ 'Centred Z angle' ] = trajectories_df [ 'Z rotation' ] - zero_z return trajectories_df
This function takes in the Head Data (hd) and Event Data (ed) frames, and constructs a DataFrame that is indexed
on time stamp, but contains just the trajectories between the appearance of the target and the end of each trial.
This function takes in the Head Data ( hd ) and Event Data ( ed ) frames and constructs a DataFrame that is indexed on time stamp but contains just the trajectories between the appearance of the target and the end of each trial .
"""
    This function takes in the Head Data (hd) and Event Data (ed) frames, and constructs a DataFrame that is indexed
    on time stamp, but contains just the trajectories between the appearance of the target and the end of each trial.

    :param hd: Pandas DataFrame containing the head data
    :param ed: Pandas DataFrame containing the event data
    :return:
    """ # the corresponding timestamps # convert to matrices and then to angles # retrieve euler angles
None
hd
Pandas DataFrame containing the head data
Pandas DataFrame containing the head data
ed
Pandas DataFrame containing the event data
Pandas DataFrame containing the event data