
    hh                     ,    d dl Zd dlZd Zd Zd Zd Zy)    Nc                     | j                  |      D ]F  }| |   |   }	 d|d   d<   t        | ||       |d   }|||dg}||d<   t        j                  ||       H y# t        $ r Y 4w xY w)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_items          X/var/www/html/dev/engine/venv/lib/python3.12/site-packages/skimage/graph/_graph_merge.py_revalidate_node_edgesr      s    & }}T" -4y~	#(Da S$, (^sD)	%[y),-  	 	s   A	A('A(c                 
   | j                  |       | j                  |   j                  | j                  |          | j                  |      D ]"  }| |   |   d   }| j	                  ||d|i       $ | j                  |       y)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   s        r   _rename_noder   )   s     
7#	KKG 45w' 57^C *sGh^45 
g    c                      d| |   |   d   d<   y)z*Invalidates the edge (n1, n2) in the heap.Fr   r   N )r   n1n2s      r   r   r   6   s    $)E"IbM+q!r   c                 :   |r|j                         }g }|j                  d      D ],  \  }}	}
|
d   }|||	dg}t        j                  ||       ||
d<   . t	        |      dkD  r|d   d   |k  rt        j
                  |      \  }}}	}|r|j                  |      D ]  }t        |||        |j                  |	      D ]  }t        ||	|        |s"|j                         }t        ||	|       ||}}n||	}} ||||       |j                  |||      }t        |||       t	        |      dkD  r|d   d   |k  rt        j                  | j                         dz         }t        |j!                  d            D ]  \  }\  }}|d   D ]  }|||<   	  ||    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labels                            r   merge_hierarchicalrA   ;   s   J hhjI		t	, &B(^R&	y), &[& i.1
1a6!9 ==32r5  }}R( / b#./ }}R( / b#./ "++-S"g.wSrSsC%__S#{;F"3	:- i.1
1a6!90 		&**,*+I		t	 45 "
FQx[ 	"E!Ie	"" Vr   )numpyr-   r
   r   r   r   rA   r!   r   r   <module>rC      s!     !-H
*
Or   