#NEWLINE# @ cache #NEWLINE# def match_events( events_from , events_to ) : #NEWLINE# #INDENT# 'Match one set of events to another.\n\n    This is useful for tasks such as matching beats to the nearest\n    detected onsets, or frame-aligned events to the nearest zero-crossing.\n\n    .. note:: A target event may be matched to multiple source events.\n\n    Examples\n    --------\n    >>> # Sources are multiples of 7\n    >>> s_from = np.arange(0, 100, 7)\n    >>> s_from\n    array([ 0,  7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91,\n           98])\n    >>> # Targets are multiples of 10\n    >>> s_to = np.arange(0, 100, 10)\n    >>> s_to\n    array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])\n    >>> # Find the matching\n    >>> idx = librosa.util.match_events(s_from, s_to)\n    >>> idx\n    array([0, 1, 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 8, 9, 9])\n    >>> # Print each source value to its matching target\n    >>> zip(s_from, s_to[idx])\n    [(0, 0), (7, 10), (14, 10), (21, 20), (28, 30), (35, 30),\n     (42, 40), (49, 50), (56, 60), (63, 60), (70, 70), (77, 80),\n     (84, 80), (91, 90), (98, 90)]\n\n    Parameters\n    ----------\n    events_from : ndarray [shape=(n,)]\n      Array of events (eg, times, sample or frame indices) to match from.\n\n    events_to : ndarray [shape=(m,)]\n      Array of events (eg, times, sample or frame indices) to\n      match against.\n\n    Returns\n    -------\n    event_mapping : np.ndarray [shape=(n,)]\n        For each event in `events_from`, the corresponding event\n        index in `events_to`.\n\n        `event_mapping[i] == arg min |events_from[i] - events_to[:]|`\n\n    See Also\n    --------\n    match_intervals\n\n    Raises\n    ------\n    ParameterError\n        If either array of input events is not the correct shape\n    ' #NEWLINE# if ( ( len ( events_from ) == 0 ) or ( len ( events_to ) == 0 ) ) : #NEWLINE# #INDENT# raise ParameterError ( 'Attempting to match empty event list' ) #NEWLINE# #UNINDENT# output = np . empty_like ( events_from , dtype = np . int ) #NEWLINE# n_rows = int ( ( MAX_MEM_BLOCK / ( ( np . prod ( output . shape [ 1 : ] ) * len ( events_to ) ) * events_from . itemsize ) ) ) #NEWLINE# n_rows = max ( 1 , n_rows ) #NEWLINE# for bl_s in range ( 0 , len ( events_from ) , n_rows ) : #NEWLINE# #INDENT# bl_t = min ( ( bl_s + n_rows ) , len ( events_from ) ) #NEWLINE# event_block = events_from [ event_block : bl_t ] #NEWLINE# output [ bl_s : bl_t ] = np . argmin ( np . abs ( np . subtract . outer ( event_block , events_to ) ) , axis = ( - 1 ) ) #NEWLINE# #UNINDENT# return output