o
    si7Y                     @   s   d Z ddlZddlZddlZddlZddlZddlm	Z	 ddlm
Z
 ddlmZ ejdd Zd(ddZd(ddZd(ddZd(ddZd(ddZd(ddZdd Ze	jdddd)ddZd(dd Zd(d!d"Zd(d#d$Zed	d%d
fd&d'ZdS )*zBFunctions for loading annotations from files in different formats.    N   )util)key)tempoc              
   k   s    t | dr| V  dS z t| fi |}|V  W d   W dS 1 s$w   Y  W dS  ty? } ztd|  |d}~ww )zEither open a file handle, or use an existing file-like object.

    If `file_or_path` has the `read` attribute, it will return `file_or_path`.

    Otherwise, it will attempt to open the file at the specified location.
    readNzInvalid file-or-path object: )hasattropen	TypeErrorIOError)file_or_pathkwargs	file_descexc r   ?/home/ubuntu/.local/lib/python3.10/site-packages/mir_eval/io.py_open   s   

&r   \s+#c                 C   s6  t |}tdd t|D }t|}|du rd}ntd| }t| dd`}t|dD ]R\}	}
|dur=||
r=q/||
	 |d }|t |krZt
d|t || |	|
t|||D ] \}}}z||}W n   t
d	||j| |	|
|| q`q/W d   n1 sw   Y  |dkr|d
 S |S )a^  Load data from an annotation file where columns are delimited.
    The number of columns is inferred from the length of the provided converters list.

    Examples
    --------
    >>> # Load in a one-column list of event times (floats)
    >>> load_delimited('events.txt', [float])
    >>> # Load in a list of labeled events, separated by commas
    >>> load_delimited('labeled_events.csv', [float, str], ',')

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    converters : list of functions
        Each entry in column ``n`` of the file will be cast by the function
        ``converters[n]``.

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    columns : tuple of lists
        Each list in this tuple corresponds to values in one of the columns
        in the file.
    c                 s   s    | ]}t  V  qd S )N)list).0_r   r   r   	<genexpr>F   s    z!load_delimited.<locals>.<genexpr>N^rmoder   z+Expected {} columns, got {} at {}:{:d}:
	{}8Couldn't convert value {} using {} found at {}:{:d}:
	{}r   )lentuplerangerecompiler   	enumeratematchsplitstrip
ValueErrorformatzip__name__append)filename
converters	delimitercomment	n_columnscolumnssplitter	commenter
input_filerowlinedatavaluecolumn	converterconverted_valuer   r   r   load_delimited    sB   %
r;   c              
   C   sd   t | tg||d}t|}zt| W |S  ty1 } zt|j	d  W Y d}~|S d}~ww )a  Import time-stamp events from an annotation file.  The file should
    consist of a single column of numeric values corresponding to the event
    times. This is primarily useful for processing events which lack duration,
    such as beats or onsets.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    event_times : np.ndarray
        array of event times (float)

    r-   r.   r   N)
r;   floatnparrayr   validate_eventsr&   warningswarnargs)r+   r-   r.   eventserrorr   r   r   load_events{   s   
rF   c              
   C   sr   t | ttg||d\}}t|}z
t| W ||fS  ty8 } zt	|j
d  W Y d}~||fS d}~ww )a  Import labeled time-stamp events from an annotation file.  The file should
    consist of two columns; the first having numeric values corresponding to
    the event times and the second having string labels for each event.  This
    is primarily useful for processing labeled events which lack duration, such
    as beats with metric beat number or onsets with an instrument label.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    event_times : np.ndarray
        array of event times (float)
    labels : list of str
        list of labels

    r<   r   N)r;   r=   strr>   r?   r   r@   r&   rA   rB   rC   )r+   r-   r.   rD   labelsrE   r   r   r   load_labeled_events   s   

rI   c              
   C   sp   t | ttg||d\}}t||gj}zt| W |S  ty7 } zt	|j
d  W Y d}~|S d}~ww )a;  Import intervals from an annotation file.  The file should consist of two
    columns of numeric values corresponding to start and end time of each
    interval.  This is primarily useful for processing events which span a
    duration, such as segmentation, chords, or instrument activation.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    intervals : np.ndarray, shape=(n_events, 2)
        array of event start and end times

    r<   r   Nr;   r=   r>   r?   Tr   validate_intervalsr&   rA   rB   rC   )r+   r-   r.   startsends	intervalsrE   r   r   r   load_intervals   s   
rP   c              
   C   s|   t | tttg||d\}}}t||gj}z
t| W ||fS  ty= } zt	
|jd  W Y d}~||fS d}~ww )a  Import labeled intervals from an annotation file.  The file should consist
    of three columns: Two consisting of numeric values corresponding to start
    and end time of each interval and a third corresponding to the label of
    each interval.  This is primarily useful for processing events which span a
    duration, such as segmentation, chords, or instrument activation.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    intervals : np.ndarray, shape=(n_events, 2)
        array of event start and end time
    labels : list of str
        list of labels

    r<   r   N)r;   r=   rG   r>   r?   rK   r   rL   r&   rA   rB   rC   )r+   r-   r.   rM   rN   rH   rO   rE   r   r   r   load_labeled_intervals   s   rQ   c                 C   s4   t | ttg||d\}}t|}t|}||fS )a  Import a time series from an annotation file.  The file should consist of
    two columns of numeric values corresponding to the time and value of each
    sample of the time series.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    times : np.ndarray
        array of timestamps (float)
    values : np.ndarray
        array of corresponding numeric values (float)

    r<   )r;   r=   r>   r?   )r+   r-   r.   timesvaluesr   r   r   load_time_series%  s   


rT   c                 C   s  g }g }g }t | ddk}| D ]C}d|v r.|g kr || |g kr)|| g }g }qd|v r>|g kr;|| g }q|d}t|d t|d f}|| q|g kr^|| |g kro|| W d   |S W d   |S 1 szw   Y  |S )	a  Load the patterns contained in the filename and puts them into a list
    of patterns, each pattern being a list of occurrence, and each
    occurrence being a list of (onset, midi) pairs.

    The input file must be formatted as described in MIREX 2013:
    http://www.music-ir.org/mirex/wiki/2013:Discovery_of_Repeated_Themes_%26_Sections

    Parameters
    ----------
    filename : str or `os.Pathlike`
        The input file path containing the patterns of a given piece using the
        MIREX 2013 format.

    Returns
    -------
    pattern_list : list
        The list of patterns, containing all their occurrences,
        using the following format::

            onset_midi = (onset_time, midi_number)
            occurrence = [onset_midi1, ..., onset_midiO]
            pattern = [occurrence1, ..., occurrenceM]
            pattern_list = [pattern1, ..., patternN]

        where ``N`` is the number of patterns, ``M[i]`` is the number of
        occurrences of the ``i`` th pattern, and ``O[j]`` is the number of
        onsets in the ``j``'th occurrence.  E.g.::

            occ1 = [(0.5, 67.0), (1.0, 67.0), (1.5, 67.0), (2.0, 64.0)]
            occ2 = [(4.5, 65.0), (5.0, 65.0), (5.5, 65.0), (6.0, 62.0)]
            pattern1 = [occ1, occ2]

            occ1 = [(10.5, 67.0), (11.0, 67.0), (11.5, 67.0), (12.0, 64.0),
                    (12.5, 69.0), (13.0, 69.0), (13.5, 69.0), (14.0, 67.0),
                    (14.5, 76.0), (15.0, 76.0), (15.5, 76.0), (16.0, 72.0)]
            occ2 = [(18.5, 67.0), (19.0, 67.0), (19.5, 67.0), (20.0, 62.0),
                    (20.5, 69.0), (21.0, 69.0), (21.5, 69.0), (22.0, 67.0),
                    (22.5, 77.0), (23.0, 77.0), (23.5, 77.0), (24.0, 74.0)]
            pattern2 = [occ1, occ2]

            pattern_list = [pattern1, pattern2]
    r   r   pattern
occurrence,r   r   N)r   	readlinesr*   r$   r=   )r+   pattern_listrU   rV   r3   r5   string_values
onset_midir   r   r   load_patternsK  s@   ,






r\   z0.8.1z0.9.0)versionversion_removedTc                 C   s   t jj| \}}|jdkr|td }n |jdkr!|td }n|jdkr-|td }ntd|j|rB|jdkrB|j	dd	}||fS )
a  Load a .wav file as a numpy array using ``scipy.io.wavfile``.

    .. warning:: This function is deprecatred in mir_eval 0.8.1
        and will be removed in 0.9.0.
        We recommend using a dedicated audio IO library such as
        `soundfile` instead.

    Parameters
    ----------
    path : str or `os.Pathlike`
        Path to a .wav file
    mono : bool
        If the provided .wav has more than one channel, it will be
        converted to mono if ``mono=True``. (Default value = True)

    Returns
    -------
    audio_data : np.ndarray
        Array of audio samples, normalized to the range [-1., 1.]
    fs : int
        Sampling rate of the audio data
    int8   int16i   int32i   z Got unexpected .wav data type {}r   )axis)
scipyiowavfiler   dtyper=   r&   r'   ndimmean)pathmonofs
audio_datar   r   r   load_wav  s   


rn   c              
   C   s   t | tttg||d\}}}t||gj}zt| W n ty7 } zt	|j
d  W Y d}~nd}~ww t|}||fS )a  Import valued intervals from an annotation file. The file should
    consist of three columns: Two consisting of numeric values corresponding to
    start and end time of each interval and a third, also of numeric values,
    corresponding to the value of each interval. This is primarily useful for
    processing events which span a duration and have a numeric value, such as
    piano-roll notes which have an onset, offset, and a pitch value.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    intervals : np.ndarray, shape=(n_events, 2)
        Array of event start and end times
    values : np.ndarray, shape=(n_events,)
        Array of values

    r<   r   NrJ   )r+   r-   r.   rM   rN   rS   rO   rE   r   r   r   load_valued_intervals  s    
ro   c              
   C   s   t | ttg||d\}}t|dkrtd|d |d }}| d| }zt| W |S  tyI } zt|jd  W Y d}~|S d}~ww )a  Load key labels from an annotation file. The file should
    consist of two string columns: One denoting the key scale degree
    (semitone), and the other denoting the mode (major or minor).  The file
    should contain only one row.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    key : str
        Key label, in the form ``'(key) (mode)'``

    r<   r   z&Key file should contain only one line.r    N)	r;   rG   r   r&   r   validate_keyrA   rB   rC   )r+   r-   r.   scaler   
key_stringrE   r   r   r   load_key  s   
rt   c              
   C   s   t | tttg||d\}}}|d }t||g}t|dkr#tdzt| W n tyD } zt	|j
d  W Y d}~nd}~ww d|  krOdksWn td| ||fS )a  Load tempo estimates from an annotation file in MIREX format.
    The file should consist of three numeric columns: the first two
    correspond to tempo estimates (in beats-per-minute), and the third
    denotes the relative confidence of the first value compared to the
    second (in the range [0, 1]). The file should contain only one row.

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    tempi : np.ndarray, non-negative
        The two tempo estimates
    weight : float [0, 1]
        The relative importance of ``tempi[0]`` compared to ``tempi[1]``
    r<   r   r   z(Tempo file should contain only one line.NzInvalid weight: )r;   r=   r>   concatenater   r&   r   validate_tempirA   rB   rC   )r+   r-   r.   t1t2weighttempirE   r   r   r   
load_tempo  s    r{   Fc                 C   sh  g }g }t |}|du rd}nt d| }|rd}	nd}	t| dd}
t|
|	D ]r\}}|dur9||r9q+|| }zt|d }W n tt	fye } zt	d
|d tj| |||d}~ww || ztj|dd |d}W n tt	fy } zt	d
|dd |j| |||d}~ww || q+W d   n1 sw   Y  t||fS )	a  Load data from a delimited time series annotation file with
    a variable number of columns.

    This function assumes that column 0 contains time stamps and
    columns 1 through n contain values.
    n may be variable from time stamp to time stamp.

    Examples
    --------
    >>> # Load a ragged list of tab-delimited multi-f0 midi notes
    >>> times, vals = load_ragged_time_series('multif0.txt', dtype=int,
                                              delimiter='\t')
    >>> # Load a raggled list of space delimited multi-f0 values with a header
    >>> times, vals = load_ragged_time_series('labeled_events.csv',
                                              header=True)

    Parameters
    ----------
    filename : str or `os.Pathlike`
        Path to the annotation file

    dtype : function
        Data type to apply to values columns.

    delimiter : str
        Separator regular expression.
        By default, lines will be split by any amount of whitespace.

    header : bool
        Indicates whether a header row is present or not.
        By default, assumes no header is present.

    comment : str or None
        Comment regular expression.
        Any lines beginning with this string or pattern will be ignored.

        Setting to `None` disables comments.

    Returns
    -------
    times : np.ndarray
        array of timestamps (float)
    values : list of np.ndarray
        list of arrays of corresponding values

    Nr   r   r   r   r   r   )rg   )r    r!   r   r"   r#   r$   r%   r=   r	   r&   r'   r)   r*   r>   r?   )r+   rg   r-   headerr.   rR   rS   r1   r2   	start_rowr3   r4   r5   r6   converted_timeexer:   r   r   r   load_ragged_time_seriesR  sZ   2

 r   )r   r   )T)__doc__
contextlibnumpyr>   r    rA   scipy.io.wavfilerd    r   r   r   contextmanagerr   r;   rF   rI   rP   rQ   rT   r\   
deprecatedrn   ro   rt   r{   r=   r   r   r   r   r   <module>   s2    


[
'
,
*
-&M
'
1
-5