ó
 ‰\c           @   s@   d  d l  Z d  d l Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿNc         C   s˜   x‘ |  j  | ƒ D]€ } |  | | } y" t | d d <t |  | | ƒ Wn t k
 rY n X| d } | | | t g } | | d <t j | | ƒ q Wd S(   s¼  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.
    s	   heap itemi   t   weightN(   t	   neighborst   Falset   _invalidate_edget   KeyErrort   Truet   heapqt   heappush(   t   ragt   nodet	   heap_listt   nbrt   datat   wtt	   heap_item(    (    s?   lib/python2.7/site-packages/skimage/future/graph/graph_merge.pyt   _revalidate_node_edges   s    

c         C   s   |  j  | ƒ |  j | j |  j | ƒ xC |  j | ƒ D]2 } |  | | d } |  j | | i | d 6ƒ q8 W|  j | ƒ d S(   s+    Rename `node_id` in `graph` to `copy_id`. R    N(   t   _add_node_silentR	   t   updateR   t   add_edget   remove_node(   t   grapht   node_idt   copy_idR   R   (    (    s?   lib/python2.7/site-packages/skimage/future/graph/graph_merge.pyt   _rename_node)   s    c         C   s   t  |  | | d d <d S(   s,    Invalidates the edge (n1, n2) in the heap. s	   heap itemi   N(   R   (   R   t   n1t   n2(    (    s?   lib/python2.7/site-packages/skimage/future/graph/graph_merge.pyR   6   s    c         C   sÿ  | r | j  ƒ  } n  g  } xY | j d t ƒ D]E \ } }	 }
 |
 d } | | |	 t g } t j | | ƒ | |
 d <q. Wxt | ƒ d k r| d d | k  rt j | ƒ \ } } }	 } | rz x' | j | ƒ D] } t | | | ƒ qÑ Wx' | j |	 ƒ D] } t | |	 | ƒ qû W| sG| j	 ƒ  } t
 | |	 | ƒ | | } } n | |	 } } | | | | ƒ | j | | | ƒ } t | | | ƒ qz qz Wt j |  j ƒ  d ƒ } xK t | j d t ƒ ƒ D]1 \ } \ } } x | d D] } | | | <qßWqÂW| |  S(   s1  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.

    R   R    s	   heap itemi    i   t   labels(   t   copyt   edgesR   R   R   t   lent   heappopR   R   t   next_idR   t   merge_nodesR   t   npt   aranget   maxt	   enumeratet   nodes(   R   R   t   thresht   rag_copyt   in_place_merget
   merge_funct   weight_funct	   edge_heapR   R   R   R   R   t   _t   validR   R   t   srct   dstt   new_idt	   label_mapt   ixt   nt   dt   label(    (    s?   lib/python2.7/site-packages/skimage/future/graph/graph_merge.pyt   merge_hierarchical;   s8    $"
)+(   t   numpyR!   R   R   R   R   R6   (    (    (    s?   lib/python2.7/site-packages/skimage/future/graph/graph_merge.pyt   <module>   s
   	$		