def add_position_information_to_rows(rows, arena_center, arena_radius):
    '''
        Values I used: 
            arena_center=[0.15, 0.48], arena_radius=1.0
    '''

    info_dtype = [
        ('distance_from_wall', 'float64'),
        ('distance_from_center', 'float64'),
        ('axis_angle', 'float64'),
        ('approach_angle', 'float64'), # degrees
    ]

    info = np.zeros(dtype=info_dtype, shape=(len(rows),))
    
    # let's keep track of the maximum error
    min_distance_from_wall = arena_radius
    num_outside = 0
    
    for i, row in enumerate(rows):
        x = row['position'][0]
        y = row['position'][1]
        ax = x - arena_center[0]
        ay = y - arena_center[1]
        
        distance_from_center = np.hypot(ax, ay)
        
        distance_from_wall = arena_radius - distance_from_center
        if distance_from_wall <= 0:
            num_outside += 1
            
            if distance_from_wall < min_distance_from_wall:
                msg = ('Fly is outside of arena for %d/%d (of %d) samples. '
                       'New record: %s' % 
                       (num_outside, i + 1, len(rows), distance_from_wall))
                print(msg)
#            
#            Arena: %s radius %s distance %s position %s' % 
#                    (arena_center, arena_radius, distance_from_center,
#                                         row['position']))
        
                min_distance_from_wall = distance_from_wall
            
            distance_from_wall = 0
            # also correct coordinates to put it inside:
            correct = (arena_radius - 0.01) / distance_from_center 
            ax *= correct
            ay *= correct 
        theta = row['reduced_angular_orientation']
        
        approach_angle = compute_approach_angle(ax, ay, theta,
                                                radius=arena_radius)
        axis_angle = compute_axis_angle(ax, ay, theta)

        info[i]['distance_from_center'] = distance_from_center
        info[i]['distance_from_wall'] = distance_from_wall
        info[i]['axis_angle'] = np.degrees(axis_angle)
        info[i]['approach_angle'] = np.degrees(approach_angle)
        
    return merge_fields(rows, info)  
def add_position_information(saccades, arena_center, arena_radius):
    '''
        Values I used: 
            arena_center=[0.15, 0.48], arena_radius=1.0
    '''
    info_dtype = [
        ('distance_from_wall', 'float64'),
        ('distance_from_center', 'float64'),
        ('axis_angle', 'float64'),
        ('approach_angle', 'float64'), # degrees
        ('saccade_angle', 'float64'), # degrees
    ]

    info = np.zeros(dtype=info_dtype, shape=(len(saccades),))
    
    for i, saccade in enumerate(saccades):
        x = saccade['position'][0]
        y = saccade['position'][1]
        ax = x - arena_center[0]
        ay = y - arena_center[1]
        
        distance_from_center = np.hypot(ax, ay)
        
        distance_from_wall = arena_radius - distance_from_center
        assert distance_from_wall > 0
        
        saccade_angle = saccade['sign'] * saccade['amplitude']    
        theta = np.radians(saccade['orientation_start'])
        
        approach_angle = compute_approach_angle(ax, ay, theta, radius=arena_radius)

        axis_angle = compute_axis_angle(ax, ay, theta)
        
        info[i]['distance_from_center'] = distance_from_center
        info[i]['distance_from_wall'] = distance_from_wall
        info[i]['saccade_angle'] = saccade_angle
        info[i]['axis_angle'] = np.degrees(axis_angle)
        info[i]['approach_angle'] = np.degrees(approach_angle)
        
    return merge_fields(saccades, info)