o
    piV3                     @   s   d dl mZmZmZ d dlZd dlmZmZm	Z	 d dl
mZ ddlmZmZ ddlmZmZ ddlmZ d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZ G dd deZ!G dd de!Z"G dd de!Z#G dd deeZ$G dd de$Z%dS )     )TupleUnionOptionalN)SegmentTimeline
Annotation)pairwise   )
BaseMetric	f_measure)MetricComponentsDetails)UEMSupportMixinzsegmentation purityzsegmentation coveragezsegmentation F[purity|coverage]ztotal durationzintersection durationzpty total durationzpty intersection durationzcvg total durationzcvg intersection durationzsegmentation precisionzsegmentation recallznumber of boundariesznumber of matchesc                       s   e Zd ZdZddef fddZdededefd	d
Zdede	eef de
eef fddZdededefddZedd ZedefddZdede	eef fddZdedefddZ  ZS )SegmentationCoveragezSegmentation coverage

    Parameters
    ----------
    tolerance : float, optional
        When provided, preprocess reference by filling intra-label gaps shorter
        than `tolerance` (in seconds).

          ?	tolerancec                       t  jdi | || _d S N super__init__r   selfr   kwargs	__class__r   Q/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/metrics/segmentation.pyr   G   s   
zSegmentationCoverage.__init__timelinecoveragereturnc                 C   sj   t g }|D ]}||j ||j qt }tt|D ]\}}t||}d||< q|j|dd	 S )N_intersection)mode)
setaddstartendr   r   sortedr   croprelabel_tracks)r   r   r   
boundariessegment	partitionr&   r'   r   r   r   
_partitionK   s   

zSegmentationCoverage._partition	reference
hypothesisc                 C   s   t |ts	tdt |tr| }t }| D ]%}||}| D ]}|j| j	k r1|
| q$| D ]}|
| q6q| }| ||}	| ||}
|	|
fS )Nz-reference must be an instance of `Annotation`)
isinstancer   	TypeErrorget_timeliner   labelslabel_timelinegapsdurationr   r%   supportr.   )r   r/   r0   filledlabelr5   gapr,   r   reference_partitionhypothesis_partitionr   r   r   _preprocess]   s$   



z SegmentationCoverage._preprocessc                 C   sB   |   }|| }t| |t< ttj|dd |t< |S )Nr	   axis)init_componentsnpsumitemPTY_CVG_TOTALmaxPTY_CVG_INTERr   r/   r0   detailKr   r   r   _processz   s
   zSegmentationCoverage._processc                 C      t S N)COVERAGE_NAMEclsr   r   r   metric_name      z SegmentationCoverage.metric_namec                 C      t tgS rM   )rE   rG   rO   r   r   r   metric_components      z&SegmentationCoverage.metric_componentsc                 K   s   |  ||\}}| ||S rM   r>   rK   r   r/   r0   r   r   r   r   compute_components   s   z'SegmentationCoverage.compute_componentsrI   c                 C   s   |t  |t  S rM   )rG   rE   )r   rI   r   r   r   compute_metric   s   z#SegmentationCoverage.compute_metric)r   )__name__
__module____qualname____doc__floatr   r   r   r.   r   r   r>   r   rK   classmethodrQ   r   rT   rX   rY   __classcell__r   r   r   r   r   <   s0    







r   c                   @   s@   e Zd ZdZedefddZdedeee	f de
fddZd	S )
SegmentationPurityzSegmentation purity

    Parameters
    ----------
    tolerance : float, optional
        When provided, preprocess reference by filling intra-label gaps shorter
        than `tolerance` (in seconds).

    r    c                 C   rL   rM   )PURITY_NAMErO   r   r   r   rQ      rR   zSegmentationPurity.metric_namer/   r0   c                 K   s   |  ||\}}| ||S rM   rV   rW   r   r   r   rX      s   z%SegmentationPurity.compute_componentsN)rZ   r[   r\   r]   r_   strrQ   r   r   r   r   rX   r   r   r   r   ra      s    

ra   c                       s   e Zd ZdZd fdd	Zdedeeef defd	d
Z	dedeeef defddZ
dedefddZddee deeeef fddZedefddZedefddZ  ZS )"SegmentationPurityCoverageFMeasureal  
    Compute segmentation purity and coverage, and return their F-score.


    Parameters
    ----------
    tolerance : float, optional
        When provided, preprocess reference by filling intra-label gaps shorter
        than `tolerance` (in seconds).

    beta : float, optional
            When beta > 1, greater importance is given to coverage.
            When beta < 1, greater importance is given to purity.
            Defaults to 1.

    See also
    --------
    pyannote.metrics.segmentation.SegmentationPurity
    pyannote.metrics.segmentation.SegmentationCoverage
    pyannote.metrics.base.f_measure
    r   r	   c                    s$   t t| jdd|i| || _d S )Nr   r   )r   rd   r   beta)r   r   re   r   r   r   r   r      s   
z+SegmentationPurityCoverageFMeasure.__init__r/   r0   r    c                 C   sz   |  ||\}}|  }|| }t| |t< ttj|dd |t< |t |t< ttj|dd |t	< |S )Nr	   r?   r   )
r>   rA   rB   rC   rD   	CVG_TOTALrF   	CVG_INTER	PTY_TOTAL	PTY_INTERrH   r   r   r   rK      s   z+SegmentationPurityCoverageFMeasure._processc                 K   s   |  ||S rM   )rK   rW   r   r   r   rX      s   z5SegmentationPurityCoverageFMeasure.compute_componentsrI   c                 C   s   | j |d\}}}|S )N)rI   )compute_metrics)r   rI   r!   valuer   r   r   rY      s   z1SegmentationPurityCoverageFMeasure.compute_metricNc                 C   sh   |d u r| j n|}|t dkrdn|t |t  }|t dkr!dn|t |t  }||t||| jdfS )N              ?)re   )accumulated_rh   ri   rf   rg   r   re   )r   rI   purityr   r   r   r   rj      s   z2SegmentationPurityCoverageFMeasure.compute_metricsc                 C   rL   rM   )PURITY_COVERAGE_NAMErO   r   r   r   rQ      rR   z.SegmentationPurityCoverageFMeasure.metric_namec                 C   s   t tttgS rM   )rh   ri   rf   rg   rO   r   r   r   rT      s   z4SegmentationPurityCoverageFMeasure.metric_components)r   r	   rM   )rZ   r[   r\   r]   r   r   r   r   r   rK   rX   r^   rY   r   r   rj   r_   rc   rQ   r   rT   r`   r   r   r   r   rd      s*    




rd   c                       sr   e Zd ZdZedd Zedd Zd fdd	Zd	ee	e
f d
ee	e
f defddZdedefddZ  ZS )SegmentationPrecisiona  Segmentation precision

    >>> from pyannote.core import Timeline, Segment
    >>> from pyannote.metrics.segmentation import SegmentationPrecision
    >>> precision = SegmentationPrecision()

    >>> reference = Timeline()
    >>> reference.add(Segment(0, 1))
    >>> reference.add(Segment(1, 2))
    >>> reference.add(Segment(2, 4))

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 1))
    >>> hypothesis.add(Segment(1, 2))
    >>> hypothesis.add(Segment(2, 3))
    >>> hypothesis.add(Segment(3, 4))
    >>> precision(reference, hypothesis)
    0.6666666666666666

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 4))
    >>> precision(reference, hypothesis)
    1.0

    c                 C   rL   rM   )PRECISION_NAMErO   r   r   r   rQ     rR   z!SegmentationPrecision.metric_namec                 C   rS   rM   )
PR_MATCHESPR_BOUNDARIESrO   r   r   r   rT     rU   z'SegmentationPrecision.metric_componentsrl   c                    r   r   r   r   r   r   r   r     s   
zSegmentationPrecision.__init__r/   r0   r    c                 K   sn  t |tr	| }t |tr| }|  }d}t|d }t|d }||t< |dks0|dkr6d|t< |S dd |D d d }dd |D d d }	t||f}
t	|D ]\}}t	|	D ]\}}t
|| |
||f< q_qWtj|
t|
| jk< t|
}|tjk r|d7 }t|
}|| }|| }tj|
|d d f< tj|
d d |f< t|
}|tjk s||t< |S )Nrl   r	   r   c                 S      g | ]}|j qS r   r'   .0r,   r   r   r   
<listcomp>>      z<SegmentationPrecision.compute_components.<locals>.<listcomp>c                 S   ru   r   rv   rw   r   r   r   ry   ?  rz   )r1   r   r3   rA   lenrt   rs   rB   zeros	enumerateabsinfwherer   aminargmin)r   r/   r0   r   rI   	n_matchesNMref_boundarieshyp_boundariesdeltarrefBoundaryhhypBoundarykijr   r   r   rX   !  s@   






z(SegmentationPrecision.compute_componentsrI   c                 C   s4   |t  }|t }|dkr|dkrdS td|| S )Nrl   r   rm    )rs   rt   
ValueError)r   rI   	numeratordenominatorr   r   r   rY   c  s   z$SegmentationPrecision.compute_metric)rl   )rZ   r[   r\   r]   r_   rQ   rT   r   r   r   r   r   rX   r^   rY   r`   r   r   r   r   rq      s    




Brq   c                       sJ   e Zd ZdZedd Zdeeef deeef de	f fddZ
  ZS )	SegmentationRecalla  Segmentation recall

    >>> from pyannote.core import Timeline, Segment
    >>> from pyannote.metrics.segmentation import SegmentationRecall
    >>> recall = SegmentationRecall()

    >>> reference = Timeline()
    >>> reference.add(Segment(0, 1))
    >>> reference.add(Segment(1, 2))
    >>> reference.add(Segment(2, 4))

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 1))
    >>> hypothesis.add(Segment(1, 2))
    >>> hypothesis.add(Segment(2, 3))
    >>> hypothesis.add(Segment(3, 4))
    >>> recall(reference, hypothesis)
    1.0

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 4))
    >>> recall(reference, hypothesis)
    0.0

    c                 C   rL   rM   )RECALL_NAMErO   r   r   r   rQ     rR   zSegmentationRecall.metric_namer/   r0   r    c                    s   t t| ||S rM   )r   r   rX   rW   r   r   r   rX     s   
z%SegmentationRecall.compute_components)rZ   r[   r\   r]   r_   rQ   r   r   r   r   rX   r`   r   r   r   r   r   q  s    

r   )&typingr   r   r   numpyrB   pyannote.corer   r   r   pyannote.core.utils.generatorsr   baser
   r   typesr   r   utilsr   rb   rN   rp   rE   rG   rh   ri   rf   rg   rr   r   rt   rs   r   ra   rd   rq   r   r   r   r   r   <module>   s2   ZLx