human
Complete the below
def closest_matches_monocular(ref_pts, pupil_pts, max_dispersion=(1 / 15.0)):

    ''''get pupil positions closest in time to ref points.
    return list of dict with matching ref and pupil datum.
    if your data is binocular use:
    pupil0 = [p for p in pupil_pts if p[\'id\']==0]
    pupil1 = [p for p in pupil_pts if p[\'id\']==1]
    to get the desired eye and pass it as pupil_pts'
    '''

=====
Can you provide a the Python program you requested?
assistant
#Here is the Python program you requested.
    ref = ref_pts
    pupil0 = pupil_pts
    pupil0_ts = np.array([p['timestamp'] for p in pupil0])
    def find_nearest_idx(array, value):
        idx = np.searchsorted(array, value, side='left')
        try:
            if (abs((value - array[(idx - 1)])) < abs((value - array[idx]))):
                return (idx - 1)
            else:
                return idx
        except IndexError:
            return (idx - 1)
    matched = []
    if pupil0:
        for r in ref_pts:
            closest_p0_idx = find_nearest_idx(pupil0_ts, r['timestamp'])
            closest_p0 = pupil0[closest_p0_idx]
            dispersion = (max(closest_p0['timestamp'], r['timestamp']) - min(closest_p0['timestamp'], r['timestamp']))
            if (dispersion < max_dispersion):
                matched.append({'ref': r, 'pupil': closest_p0})
            else:
                pass
    return matched