U
    ?h                     @   sb   d dl Zd dlmZ ddlmZ ddlmZ dgZddddd	dZ	d
d Z
dddZdd ZdS )    N   )contingency_table   )check_shape_equalityvariation_of_information tableignore_labelsc                C   s*   t | |||d\}}t| | gS )u  Return symmetric conditional entropies associated with the VI. [1]_

    The variation of information is defined as VI(X,Y) = H(X|Y) + H(Y|X).
    If X is the ground-truth segmentation, then H(X|Y) can be interpreted
    as the amount of under-segmentation and H(Y|X) as the amount
    of over-segmentation. In other words, a perfect over-segmentation
    will have H(X|Y)=0 and a perfect under-segmentation will have H(Y|X)=0.

    Parameters
    ----------
    image0, image1 : ndarray of int
        Label images / segmentations, must have same shape.
    table : scipy.sparse array in csr format, optional
        A contingency table built with skimage.evaluate.contingency_table.
        If None, it will be computed with skimage.evaluate.contingency_table.
        If given, the entropies will be computed from this table and any images
        will be ignored.
    ignore_labels : sequence of int, optional
        Labels to ignore. Any part of the true image labeled with any of these
        values will not be counted in the score.

    Returns
    -------
    vi : ndarray of float, shape (2,)
        The conditional entropies of image1|image0 and image0|image1.

    References
    ----------
    .. [1] Marina Meilă (2007), Comparing clusterings—an information based
        distance, Journal of Multivariate Analysis, Volume 98, Issue 5,
        Pages 873-895, ISSN 0047-259X, :DOI:`10.1016/j.jmva.2006.11.013`.
    r   )
_vi_tablesnparraysum)Zimage0Zimage1r	   r
   Zh0g1Zh1g0r   r   [/var/www/html/venv/lib/python3.8/site-packages/skimage/metrics/_variation_of_information.pyr   	   s    "
c                 C   sX   |   }t|tjs t|tjr(|j}n
t|}| }||  t	|| 9  < |S )zCompute x * log_2(x).

    We define 0 * log_2(0) = 0

    Parameters
    ----------
    x : ndarray or scipy.sparse.csc_matrix or csr_matrix
        The input array.

    Returns
    -------
    y : same type as x
        Result of x * log_2(x).
    )
copy
isinstancesparseZ
csc_matrixZ
csr_matrixdatar   asarraynonzerolog2)xyznzr   r   r   _xlogx1   s    
r   c                 C   s   t | | |dkr$t| ||dd}n|}t|jdd}t|jdd}tt|}tt|}| t|| jdd }	t|| jdd | }
t	t
tj|
|	gS )a  Compute probability tables used for calculating VI.

    Parameters
    ----------
    im_true, im_test : ndarray of int
        Input label images, any dimensionality.
    table : csr matrix, optional
        Pre-computed contingency table.
    ignore_labels : sequence of int, optional
        Labels to ignore when computing scores.

    Returns
    -------
    hxgy, hygx : ndarray of float
        Per-segment conditional entropies of ``im_true`` given ``im_test`` and
        vice-versa.
    NT)r
   	normalizer   )Zaxisr   )r   r   r   Zravelr   r   Zdiags_invert_nonzeror   listmapr   )Zim_trueZim_testr	   r
   ZpxyZpxpyZpx_invZpy_invZhygxZhxgyr   r   r   r   J   s     
  r   c                 C   s&   |   }t| }d| |  ||< |S )a  Compute the inverse of the non-zero elements of arr, not changing 0.

    Parameters
    ----------
    arr : ndarray

    Returns
    -------
    arr_inv : ndarray
        Array containing the inverse of the non-zero elements of arr, and
        zero elsewhere.
    r   )r   r   r   )ZarrZarr_invr   r   r   r   r   x   s    
r   )NN)Nr   )numpyr   Zscipy.sparser   Z_contingency_tabler   Z_shared.utilsr   __all__r   r   r   r   r   r   r   r   <module>   s   (
.