o
    s·¯i  ã                   @   sB   d Z ddlZddlmZ ddlZdZdd„ Zdd	d
„Zdd„ ZdS )aç  
The goal of an onset detection algorithm is to automatically determine when
notes are played in a piece of music.  The primary method used to evaluate
onset detectors is to first determine which estimated onsets are "correct",
where correctness is defined as being within a small window of a reference
onset.

Based in part on this script:

    https://github.com/CPJKU/onset_detection/blob/master/onset_evaluation.py

Conventions
-----------

Onsets should be provided in the form of a 1-dimensional array of onset
times in seconds in increasing order.

Metrics
-------

* :func:`mir_eval.onset.f_measure`: Precision, Recall, and F-measure scores
  based on the number of estimated onsets which are sufficiently close to
  reference onsets.
é    Né   )Úutilg     LÝ@c                 C   sF   | j dkr
t d¡ |j dkrt d¡ | |fD ]}t |t¡ qdS )a:  Check that the input annotations to a metric look like valid onset time
    arrays, and throws helpful errors if not.

    Parameters
    ----------
    reference_onsets : np.ndarray
        reference onset locations, in seconds
    estimated_onsets : np.ndarray
        estimated onset locations, in seconds

    r   zReference onsets are empty.zEstimated onsets are empty.N)ÚsizeÚwarningsÚwarnr   Úvalidate_eventsÚMAX_TIME)Úreference_onsetsÚestimated_onsetsÚonsets© r   úB/home/ubuntu/.local/lib/python3.10/site-packages/mir_eval/onset.pyÚvalidate#   s   



ÿr   çš™™™™™©?c                 C   sj   t | |ƒ | jdks|jdkrdS t | ||¡}tt|ƒƒt|ƒ }tt|ƒƒt| ƒ }t ||¡||fS )aµ  Compute the F-measure of correct vs incorrectly predicted onsets.
    "Correctness" is determined over a small window.

    Examples
    --------
    >>> reference_onsets = mir_eval.io.load_events('reference.txt')
    >>> estimated_onsets = mir_eval.io.load_events('estimated.txt')
    >>> F, P, R = mir_eval.onset.f_measure(reference_onsets,
    ...                                    estimated_onsets)

    Parameters
    ----------
    reference_onsets : np.ndarray
        reference onset locations, in seconds
    estimated_onsets : np.ndarray
        estimated onset locations, in seconds
    window : float
        Window size, in seconds
        (Default value = .05)

    Returns
    -------
    f_measure : float
        2*precision*recall/(precision + recall)
    precision : float
        (# true positives)/(# true positives + # false positives)
    recall : float
        (# true positives)/(# true positives + # false negatives)

    r   )ç        r   r   )r   r   r   Úmatch_eventsÚfloatÚlenÚ	f_measure)r	   r
   ÚwindowÚmatchingÚ	precisionÚrecallr   r   r   r   8   s   
r   c                 K   s4   t  ¡ }tjt| |fi |¤Ž\|d< |d< |d< |S )aN  Compute all metrics for the given reference and estimated annotations.

    Examples
    --------
    >>> reference_onsets = mir_eval.io.load_events('reference.txt')
    >>> estimated_onsets = mir_eval.io.load_events('estimated.txt')
    >>> scores = mir_eval.onset.evaluate(reference_onsets,
    ...                                  estimated_onsets)

    Parameters
    ----------
    reference_onsets : np.ndarray
        reference onset locations, in seconds
    estimated_onsets : np.ndarray
        estimated onset locations, in seconds
    **kwargs
        Additional keyword arguments which will be passed to the
        appropriate metric or preprocessing functions.

    Returns
    -------
    scores : dict
        Dictionary of scores, where the key is the metric name (str) and
        the value is the (float) score achieved.

    z	F-measureÚ	PrecisionÚRecall)ÚcollectionsÚOrderedDictr   Úfilter_kwargsr   )r	   r
   ÚkwargsÚscoresr   r   r   Úevaluatee   s   ÿÿr    )r   )	Ú__doc__r   Ú r   r   r   r   r   r    r   r   r   r   Ú<module>   s    
-