U
    ?hå  ã                   @   s4   d dl Zd dlZdd„ Zdd„ Zdd„ Zdd	„ ZdS )
é    Nc              	   C   sz   |   |¡D ]j}| | | }zd|d d< t| ||ƒ W n tk
rJ   Y nX |d }|||dg}||d< t ||¡ q
dS )a¼  Handles validation and invalidation of edges incident to a node.

    This function invalidates all existing edges incident on `node` and inserts
    new items in `heap_list` updated with the valid weights.

    rag : RAG
        The Region Adjacency Graph.
    node : int
        The id of the node whose incident edges are to be validated/invalidated
        .
    heap_list : list
        The list containing the existing heap of edges.
    Fú	heap itemé   ÚweightTN)Ú	neighborsÚ_invalidate_edgeÚKeyErrorÚheapqÚheappush)ÚragÚnodeZ	heap_listÚnbrÚdataÚwtÚ	heap_item© r   úL/var/www/html/venv/lib/python3.8/site-packages/skimage/graph/_graph_merge.pyÚ_revalidate_node_edges   s    r   c                 C   s`   |   |¡ | j|  | j| ¡ |  |¡D ]&}| | | d }|  ||d|i¡ q*|  |¡ dS )z+ Rename `node_id` in `graph` to `copy_id`. r   N)Z_add_node_silentÚnodesÚupdater   Zadd_edgeÚremove_node)ÚgraphZnode_idZcopy_idr   r   r   r   r   Ú_rename_node)   s    
r   c                 C   s   d| | | d d< dS )z, Invalidates the edge (n1, n2) in the heap. Fr   r   Nr   )r   Ún1Ún2r   r   r   r   6   s    r   c                 C   sf  |r|  ¡ }g }|jddD ]2\}}	}
|
d }|||	dg}t ||¡ ||
d< qt|ƒdkr|d d |k rt |¡\}}}	}|rP| |¡D ]}t|||ƒ q| |	¡D ]}t||	|ƒ q¬|sâ| ¡ }t	||	|ƒ || }}n
||	 }}||||ƒ | 
|||¡}t|||ƒ qPt |  ¡ d ¡}t|jddƒD ]&\}\}}|d D ]}|||< qJq6||  S )a1  Perform hierarchical merging of a RAG.

    Greedily merges the most similar pair of nodes until no edges lower than
    `thresh` remain.

    Parameters
    ----------
    labels : ndarray
        The array of labels.
    rag : RAG
        The Region Adjacency Graph.
    thresh : float
        Regions connected by an edge with weight smaller than `thresh` are
        merged.
    rag_copy : bool
        If set, the RAG copied before modifying.
    in_place_merge : bool
        If set, the nodes are merged in place. Otherwise, a new node is
        created for each merge..
    merge_func : callable
        This function is called before merging two nodes. For the RAG `graph`
        while merging `src` and `dst`, it is called as follows
        ``merge_func(graph, src, dst)``.
    weight_func : callable
        The function to compute the new weights of the nodes adjacent to the
        merged node. This is directly supplied as the argument `weight_func`
        to `merge_nodes`.

    Returns
    -------
    out : ndarray
        The new labeled array.

    T)r   r   r   r   é   Úlabels)ÚcopyÚedgesr   r	   ÚlenÚheappopr   r   Únext_idr   Zmerge_nodesr   ÚnpZarangeÚmaxÚ	enumerater   )r   r
   ZthreshZrag_copyZin_place_mergeZ
merge_funcZweight_funcZ	edge_heapr   r   r   r   r   Ú_Zvalidr   r    ÚsrcÚdstZnew_idZ	label_mapZixÚnÚdÚlabelr   r   r   Úmerge_hierarchical;   s8    $
 
r*   )Únumpyr!   r   r   r   r   r*   r   r   r   r   Ú<module>   s
   $