o
    ^i                     @   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   sx   |  |D ]4}| | | }zd|d d< t| || W n	 ty$   Y nw |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node	heap_listnbrdatawt	heap_item r   U/var/www/html/RAG/RAG_venv/lib/python3.10/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)_add_node_silentnodesupdater   add_edgeremove_node)graphnode_id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   sx  |r|  }g }|jddD ]\}}	}
|
d }|||	dg}t|| ||
d< qt|dkr|d d |k rt|\}}}	}|r||D ]}t||| qF||	D ]}t||	| qT|so| }t	||	| ||}}n||	}}|||| |
|||}t||| t|dkr|d d |k s6t|  d }t|jddD ]\}\}}|d D ]}|||< qq||  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   merge_nodesr   nparangemax	enumerater   )r    r
   threshrag_copyin_place_merge
merge_funcweight_func	edge_heapr   r   r   r   r   _validr   r%   srcdstnew_id	label_mapixndlabelr   r   r   merge_hierarchical;   s<   %


r;   )numpyr'   r   r   r   r   r;   r   r   r   r   <module>   s    $