
    k h                     Z    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	Z	d
 Z
ddZd Zy)    N   )contingency_table   )check_shape_equalityvariation_of_information tableignore_labelsc                    t        | |||      \  }}t        j                  |j                         |j                         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)image0image1r
   r   h0g1h1g0s         /home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/skimage/metrics/_variation_of_information.pyr   r   	   s9    B FF%}UJD$88TXXZ,--    c                    | j                         }t        j                  |      r|j                  dv r|j                  }nt        j                  |      }|j                         }||xx   t        j                  ||         z  cc<   |S )a  Compute x * log_2(x).

    We define 0 * log_2(0) = 0

    Parameters
    ----------
    x : ndarray or scipy.sparse.csc_array or scipy.sparse.csr_array
        The input array.

    Returns
    -------
    y : same type as x
        Result of x * log_2(x).
    )csccsr)	copysparseissparseformatdatar   asarraynonzerolog2)xyznzs       r   _xlogxr&   /   sf     	
Aqahh.8FFJJqM	
BbERWWQrU^EHr   c                    t        | |       |t        | ||d      }n|}t        j                  |j	                  d            }t        j                  |j	                  d            }t        j                  t        |      df|j                  |j                  f      }t        j                  t        |      df|j                  |j                  f      }| t        ||z        j	                  d      z  }	t        ||z        j	                  d       |z  }
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_array, 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.
    T)r   	normalizer   )axisr   )shape)r   r   r   ravelr   r   	dia_array_invert_nonzerosizer&   listmapr   )im_trueim_testr
   r   pxypxpypx_invpy_invhygxhxgys              r   r   r   H   s   $ '*}WMT

  
#''q'/	"B	#''q'/	"B r2A6rww>PQFr2A6rww>PQF 3%))q)11D3< $$!$,,r1DBJJt-..r   c                 f    | j                         }t        j                  |       }d| |   z  ||<   |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    )arrarr_invr%   s      r   r-   r-   u   s2     hhjG	CBc"g+GBKNr   )NN)Nr   )numpyr   scipy.sparser   _contingency_tabler   _shared.utilsr   __all__r   r&   r   r-   r   r   r   <module>rB      s9      1 0%
&#.TV #.L2*/Zr   