o
    8wi                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
Zd dlmZ d dlmZ d dlmZ d dlmZ d	d
lmZ d	dlmZ G dd deZG dd deZdS )    N)Optional)fcluster)linkagefcluster_auto)
squareform)pdist)
dist_range)l2_normalize   )Pipeline)Uniformc                
       sb   e Zd ZdZ				ddee dee dee dee f fd	d
Zdej	dej	fddZ
  ZS )#HierarchicalAgglomerativeClusteringa%  Hierarchical agglomerative clustering

    Parameters
    ----------
    method : `str`, optional
        Linkage method. Defaults to 'pool'.
    metric : `str`, optional
        Distance metric. Defaults to 'cosine'
    normalize : `bool`, optional
        L2 normalize vectors before clustering.
    use_threshold : `bool`, optional
        Stop merging clusters when their distance is greater than the value of
        `threshold` hyper-parameters. By default (False), this pipeline relies
        on the within-class sum of square elbow criterion to select the best
        number of clusters.

    Hyper-parameters
    ----------------
    threshold : `float`
        Stop merging clusters when their distance is greater than `threshold`.
        Not used when `use_threshold` is True (default).
    poolcosineFmethodmetricuse_threshold	normalizec                    sv   t    || _|| _|| _|| _| jr9t| j| jd\}}t|s1d}d|dd}t	
| t||| _d S d S )N)r   r   g    .Azbounding distance threshold to gz(: this might lead to suboptimal results.)super__init__r   r   r   r   r   npisfinitewarningswarnr   	threshold)selfr   r   r   r   min_distmax_distmsg	__class__ `/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pyannote/pipeline/blocks/clustering.pyr   F   s    



z,HierarchicalAgglomerativeClustering.__init__Xreturnc                 C   s|   |j \}}|dk rd}t||dkrtjdgtdS | jr"t|}t|| j| j	d}| j
r6t|| jddS t||| j	dS )a.  Apply hierarchical agglomerative clustering

        Parameters
        ----------
        X : `np.ndarray`
            (n_samples, n_dimensions) feature vectors.

        Returns
        -------
        y : `np.ndarray`
            (n_samples, ) cluster assignment (between 1 and n_clusters).
           +There should be at least one sample in `X`.dtype)r   r   distance)	criterionr   )shape
ValueErrorr   arrayintr   r	   r   r   r   r   r   r   r   )r   r$   	n_samples_r   Zr"   r"   r#   __call__d   s   
z,HierarchicalAgglomerativeClustering.__call__)r   r   FF)__name__
__module____qualname____doc__r   strboolr   r   ndarrayr4   __classcell__r"   r"   r    r#   r   .   s     r   c                       sJ   e Zd ZdZddee f fddZdd Zdej	d	ej	fd
dZ
  ZS )AffinityPropagationClusteringa#  Clustering based on affinity propagation

    Parameters
    ----------
    metric : `str`, optional
        Distance metric. Defaults to 'cosine'

    Hyper-parameters
    ----------------
    damping : `float`
    preference : `float`
        See `sklearn.cluster.AffinityPropagation`
    r   r   c                    s,   t    || _tdd| _tdd| _d S )Ng      ?g      ?g      $g        )r   r   r   r   damping
preference)r   r   r    r"   r#   r      s   
z&AffinityPropagationClustering.__init__c                 C   s    t jj| j| jdddd| _dS )z7Initialize internal sklearn.cluster.AffinityPropagationprecomputed   2   )r>   r?   affinitymax_iterconvergence_iterN)sklearnclusterAffinityPropagationr>   r?   affinity_propagation_)r   r"   r"   r#   
initialize   s   z(AffinityPropagationClustering.initializer$   r%   c              
   C   s   |j \}}|dk rd}t||dkrtjdgtdS ztt|| jd }| j	|}W n t
yD } zt|}W Y d}~nd}~ww t|dk rQt|}|d7 }|S )a1  Apply clustering based on affinity propagation

        Parameters
        ----------
        X : `np.ndarray`
            (n_samples, n_dimensions) feature vectors.

        Returns
        -------
        y : `np.ndarray`
            (n_samples, ) cluster assignment (between 1 and n_clusters).
        r&   r'   r(   r,   Nr   )r-   r.   r   r/   r0   r   r   r   rI   fit_predictMemoryErrorarangeany)r   r$   r1   r2   r   rC   clusterser"   r"   r#   r4      s"   

z&AffinityPropagationClustering.__call__)r   )r5   r6   r7   r8   r   r9   r   rJ   r   r;   r4   r<   r"   r"   r    r#   r=      s
    r=   )r   numpyr   typingr   scipy.cluster.hierarchyr   pyannote.core.utils.hierarchyr   r   sklearn.clusterrF   scipy.spatial.distancer   pyannote.core.utils.distancer   r   r	   pipeliner   	parameterr   r   r=   r"   r"   r"   r#   <module>   s   \