ó
 ‰\c           @   sÃ   y d  d l  Z Wn+ e k
 r= d d l m Z e d ƒ n Xd  d l Z d d l m Z d d l m	 Z	 d  d l
 m Z e d	 „ Z d
 d e d d „ Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿNi   (   t   warns   RAGs require networkxi   (   t   _ncut(   t   _ncut_cy(   t   linalgc         C   só   | s | j  ƒ  } n  g  | j d t ƒ D]+ \ } } } | d | k r( | | f ^ q( } | j | ƒ t j | ƒ } t j |  j ƒ  d d |  j	 ƒ}	 xQ t
 | ƒ D]C \ }
 } x4 | D], } x# | j | d D] } |
 |	 | <qÏ Wq· Wq¤ W|	 |  S(   sú  Combine regions separated by weight less than threshold.

    Given an image's labels and its RAG, output new labels by
    combining regions whose nodes are separated by a weight less
    than the given threshold.

    Parameters
    ----------
    labels : ndarray
        The array of labels.
    rag : RAG
        The region adjacency graph.
    thresh : float
        The threshold. Regions connected by edges with smaller weights are
        combined.
    in_place : bool
        If set, modifies `rag` in place. The function will remove the edges
        with weights less that `thresh`. If set to `False` the function
        makes a copy of `rag` before proceeding.

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

    Examples
    --------
    >>> from skimage import data, segmentation
    >>> from skimage.future import graph
    >>> img = data.astronaut()
    >>> labels = segmentation.slic(img)
    >>> rag = graph.rag_mean_color(img, labels)
    >>> new_labels = graph.cut_threshold(labels, rag, 10)

    References
    ----------
    .. [1] Alain Tremeau and Philippe Colantoni
           "Regions Adjacency Graph Applied To Color Image Segmentation"
           http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.11.5274

    t   datat   weighti   t   dtypet   labels(   t   copyt   edgest   Truet   remove_edges_fromt   nxt   connected_componentst   npt   aranget   maxR   t	   enumeratet   node(   R   t   ragt   thresht   in_placet   xt   yt   dt	   to_removet   compst	   map_arrayt   it   nodesR   t   label(    (    s=   lib/python2.7/site-packages/skimage/future/graph/graph_cut.pyt   cut_threshold   s    *"""gü©ñÒMbP?i
   g      ð?c   
      C   s±   | s | j  ƒ  } n  x* | j ƒ  D] } | j | | d | ƒq" Wt | | | ƒ t j |  j ƒ  d d |  j ƒ} x2 | j d t ƒ D] \ } }	 |	 d | |	 d <q‡ W| |  S(   sQ  Perform Normalized Graph cut on the Region Adjacency Graph.

    Given an image's labels and its similarity RAG, recursively perform
    a 2-way normalized cut on it. All nodes belonging to a subgraph
    that cannot be cut further are assigned a unique label in the
    output.

    Parameters
    ----------
    labels : ndarray
        The array of labels.
    rag : RAG
        The region adjacency graph.
    thresh : float
        The threshold. A subgraph won't be further subdivided if the
        value of the N-cut exceeds `thresh`.
    num_cuts : int
        The number or N-cuts to perform before determining the optimal one.
    in_place : bool
        If set, modifies `rag` in place. For each node `n` the function will
        set a new attribute ``rag.node[n]['ncut label']``.
    max_edge : float, optional
        The maximum possible value of an edge in the RAG. This corresponds to
        an edge between identical regions. This is used to put self
        edges in the RAG.

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

    Examples
    --------
    >>> from skimage import data, segmentation
    >>> from skimage.future import graph
    >>> img = data.astronaut()
    >>> labels = segmentation.slic(img)
    >>> rag = graph.rag_mean_color(img, labels, mode='similarity')
    >>> new_labels = graph.cut_normalized(labels, rag)

    References
    ----------
    .. [1] Shi, J.; Malik, J., "Normalized cuts and image segmentation",
           Pattern Analysis and Machine Intelligence,
           IEEE Transactions on, vol. 22, no. 8, pp. 888-905, August 2000.

    R   i   R   R   s
   ncut labelR   (	   R   R   t   add_edget   _ncut_relabelR   t   zerosR   R   R
   (
   R   R   R   t   num_cutsR   t   max_edgeR   R   t   nR   (    (    s=   lib/python2.7/site-packages/skimage/future/graph/graph_cut.pyt   cut_normalizedL   s    1"c         C   s’   g  t  | j ƒ  ƒ D] \ } } |  | r | ^ q } g  t  | j ƒ  ƒ D] \ } } |  | sH | ^ qH } | j | ƒ } | j | ƒ } | | f S(   sQ  Compute resulting subgraphs from given bi-parition.

    Parameters
    ----------
    cut : array
        A array of booleans. Elements set to `True` belong to one
        set.
    rag : RAG
        The Region Adjacency Graph.

    Returns
    -------
    sub1, sub2 : RAG
        The two resulting subgraphs from the bi-partition.
    (   R   R   t   subgraph(   t   cutR   R   R%   t   nodes1t   nodes2t   sub1t   sub2(    (    s=   lib/python2.7/site-packages/skimage/future/graph/graph_cut.pyt   partition_by_cut   s
    55c         C   sÁ   t  j } |  j ƒ  } |  j ƒ  } t  j |  d t  j ƒ} t  j | | ƒ rU | | f Sx_ t  j | | | d t ƒD]B } |  | k }	 t	 j
 |	 | | ƒ }
 |
 | k  rq |	 } |
 } qq qq W| | f S(   sö  Threshold an eigenvector evenly, to determine minimum ncut.

    Parameters
    ----------
    ev : array
        The eigenvector to threshold.
    d : ndarray
        The diagonal matrix of the graph.
    w : ndarray
        The weight matrix of the graph.
    num_cuts : int
        The number of evenly spaced thresholds to check for.

    Returns
    -------
    mask : array
        The array of booleans which denotes the bi-partition.
    mcut : float
        The value of the minimum ncut.
    R   t   endpoint(   R   t   inft   minR   t
   zeros_liket   boolt   allcloset   linspacet   FalseR   t	   ncut_cost(   t   evR   t   wR#   t   mcutt   mnt   mxt   min_maskt   tt   maskt   cost(    (    s=   lib/python2.7/site-packages/skimage/future/graph/graph_cut.pyt   get_min_ncut°   s    	
"c         C   sX   t  |  j ƒ  ƒ } |  j | d d } x* |  j d t ƒ D] \ } } | | | <q: Wd S(   sq  Assign a unique integer to the given attribute in the RAG.

    This function assumes that all labels in `rag` are unique. It
    picks up a random label from them and assigns it to the `attr_name`
    attribute of all the nodes.

    rag : RAG
        The Region Adjacency Graph.
    attr_name : string
        The attribute to which a unique integer is assigned.
    R   i    R   N(   R0   R   R   R
   (   R   t	   attr_nameR   t	   new_labelR%   R   (    (    s=   lib/python2.7/site-packages/skimage/future/graph/graph_cut.pyt
   _label_allÜ   s    c      	   C   s\  t  j |  ƒ \ } } | j d } | d k rK| j ƒ  } t j t j | j d | j ƒd | j ƒ| _ t j	 | | | | d d d t
 d | d ƒ ƒ\ } } t j | ƒ t j | ƒ } } t j | ƒ }	 | d d … |	 f }
 t |
 | | | ƒ \ } } | | k  rKt | |  ƒ \ } } t | | | ƒ t | | | ƒ d Sn  t |  d	 ƒ d S(
   s  Perform Normalized Graph cut on the Region Adjacency Graph.

    Recursively partition the graph into 2, until further subdivision
    yields a cut greater than `thresh` or such a cut cannot be computed.
    For such a subgraph, indices to labels of all its nodes map to a single
    unique value.

    Parameters
    ----------
    labels : ndarray
        The array of labels.
    rag : RAG
        The region adjacency graph.
    thresh : float
        The threshold. A subgraph won't be further subdivided if the
        value of the N-cut exceeds `thresh`.
    num_cuts : int
        The number or N-cuts to perform before determining the optimal one.
    map_array : array
        The array which maps old labels to new ones. This is modified inside
        the function.
    i    i   t   outt   whicht   SMt   kid   Ns
   ncut label(   R   t   DW_matricest   shapeR   R   t
   reciprocalt   sqrtR   R   t   eigshR0   t   realR   t   argmin2R@   R-   R!   RC   (   R   R   R#   R   R8   t   mt   d2t   valst   vectorst   index2R7   t   cut_maskR9   R+   R,   (    (    s=   lib/python2.7/site-packages/skimage/future/graph/graph_cut.pyR!   î   s"    0(   t   networkxR   t   ImportErrort   _shared.utilsR    t   numpyR   t    R   R   t   scipy.sparseR   R
   R   R&   R-   R@   RC   R!   (    (    (    s=   lib/python2.7/site-packages/skimage/future/graph/graph_cut.pyt   <module>   s   @	@	#	,	