
    h                     `    S SK rS SKJr  SSKJr  SSKJr  S/rSSSS.S	 jjr	S
 r
SS jrS rg)    N   )contingency_table   )check_shape_equalityvariation_of_information tableignore_labelsc                    [        XX#S9u  pE[        R                  " UR                  5       UR                  5       /5      $ )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         c/var/www/fran/franai/venv/lib/python3.13/site-packages/skimage/metrics/_variation_of_information.pyr   r   	   s3    B F%UJD88TXXZ,--    c                 &   U R                  5       n[        R                  " U5      (       a  UR                  S;   a  UR                  nO[
        R                  " U5      nUR                  5       nX#==   [
        R                  " X#   5      -  ss'   U$ )zCompute 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&   /   se     	
Aqahh.8FFJJqM	
BERWWQU^EHr   c                 ^   [        X5        Uc  [        XUSS9nOUn[        R                  " UR	                  SS95      n[        R                  " UR	                  SS95      n[
        R                  " [        U5      S4UR                  UR                  4S9n[
        R                  " [        U5      S4UR                  UR                  4S9nU* [        Xt-  5      R	                  SS9-  n	[        XH-  5      R	                  SS9* U-  n
[        [        [        R                  X/5      5      $ )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   $ *}MT

  
#''q'/	"B	#''q'/	"B r2A6rww>PQFr2A6rww>PQF 3%))q)11D3< $$!$,,r1DBJJ-..r   c                 d    U R                  5       n[        R                  " U 5      nSX   -  X'   U$ )zCompute 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   s.     hhjG	CBcg+GK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   