
    9i                     ,    S SK rS SKrS rS rS rS rg)    Nc                     U R                  U5       HC  nX   U   n SUS   S'   [        XU5        US   nXQUS/nXdS'   [        R                  " X&5        ME     g! [         a     N5f = f)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          Z/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/graph/_graph_merge.py_revalidate_node_edgesr      s    & }}T"y~	#(Da S, (^sD)	%[y, #  	 	s   A
A'&A'c                 
   U R                  U5        U R                  U   R                  U R                  U   5        U R                  U5       H!  nX   U   S   nU R	                  X2SU05        M#     U R                  U5        g)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   )   sx     
7#	KKG 45w'^C *sh^4 ( 
g    c                     SX   U   S   S'   g)z*Invalidates the edge (n1, n2) in the heap.Fr   r   N )r   n1n2s      r   r   r   6   s    $)EIbM+q!r   c                 \   U(       a  UR                  5       n/ nUR                  SS9 H*  u  pn
U
S   nXU	S/n[        R                  " X|5        XS'   M,     [	        U5      S:  a  US   S   U:  a  [        R
                  " U5      u  ppU(       a  UR                  U5       H  n[        XU5        M     UR                  U	5       H  n[        XU5        M     U(       d!  UR                  5       n[        XU5        UUnnOXnnU" UUU5        UR                  UUU5      n[        UUU5        [	        U5      S:  a  US   S   U:  a  M  [        R                  " U R                  5       S-   5      n[        UR!                  SS95       H  u  nu  nnUS    H  nUUU'   M
     M     UU    $ )a  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	,(^R&	y, &[ - i.1
1a6!9 ==3r  }}R( #. ) }}R( #. ) "++-Sg.wSSSsC%__S#{;F"3	:- i.1
1a6!90 		&**,*+I		t	 45
FQx[E!Ie ! 6 Vr   )numpyr-   r
   r   r   r   rA   r!   r   r   <module>rC      s!     !-H
*
Or   