ó
 ‰\c           @   sô  d  Z  d d l Z d d l m Z m Z d d l m Z yA d d l	 m
 Z
 e
 j j Z d „  Z e e
 j _ e
 j ƒ  Z Wn e Z n Xy d d l m Z e Z Wn e k
 r¼ e Z n Xd d	 l m Z d d
 l m Z d d l m Z d d l Z d d l m Z d d l Z e e j  ƒ e d ƒ k rHe j! e d d ƒZ n  d „  Z" d d e d „ Z# d „  Z$ d „  Z% e d „ Z& d „  Z' d „  Z( e d e d „ Z) d „  Z* d d d e e e e d „ Z+ e d „ Z, e d  „ Z- e d! „ Z. d S("   s  
Random walker segmentation algorithm

from *Random walks for image segmentation*, Leo Grady, IEEE Trans
Pattern Anal Mach Intell. 2006 Nov;28(11):1768-83.

Installing pyamg and using the 'cg_mg' mode of random_walker improves
significantly the performance.
iÿÿÿÿN(   t   sparset   ndimagei   (   t   warn(   t   umfpackc         C   s&   y t  |  ƒ Wn t k
 r! n Xd  S(   N(   t   old_delt   AttributeError(   t   self(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   new_del   s    (   t   ruge_stuben_solver(   t   img_as_float(   t
   rank_order(   t   cg(   t   LooseVersions   1.1t   atoli    c         C   s  t  j |  | | ƒ j |  | | f ƒ } t  j | d d … d d … d d … f j ƒ  | d d … d d … d d … f j ƒ  f ƒ } t  j | d d … d d … f j ƒ  | d d … d d … f j ƒ  f ƒ } t  j | d  j ƒ  | d j ƒ  f ƒ } t  j | | | f ƒ } | S(   s!  Returns a list of edges for a 3D image.

    Parameters
    ----------
    n_x: integer
        The size of the grid in the x direction.
    n_y: integer
        The size of the grid in the y direction
    n_z: integer
        The size of the grid in the z direction

    Returns
    -------
    edges : (2, N) ndarray
        with the total number of edges::

            N = n_x * n_y * (nz - 1) +
                n_x * (n_y - 1) * nz +
                (n_x - 1) * n_y * nz

        Graph edges with each column describing a node-id pair.
    Niÿÿÿÿi   (   t   npt   aranget   reshapet   vstackt   ravelt   hstack(   t   n_xt   n_yt   n_zt   verticest
   edges_deept   edges_rightt
   edges_downt   edges(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _make_graph_edges_3d8   s    ).1%()i‚   gíµ ÷Æ°>c         C   s§   d } x? t  d |  j d ƒ D]' } | t |  d | f | ƒ d 7} q W| d |  j ƒ  :} | r | t j |  j d ƒ :} n  | | 9} t j | ƒ } | | 7} | S(   Ni    iÿÿÿÿ.i   i
   (   t   ranget   shapet   _compute_gradients_3dt   stdR   t   sqrtt   exp(   t   datat   spacingt   betat   epst   multichannelt	   gradientst   channelt   weights(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _compute_weights_3dY   s    

c         C   sé   t  j |  d  d  … d  d  … d  d … f |  d  d  … d  d  … d d  … f ƒ j ƒ  | d } t  j |  d  d  … d  d … f |  d  d  … d d  … f ƒ j ƒ  | d } t  j |  d  |  d ƒ j ƒ  | d } t  j | | | f S(   Niÿÿÿÿi   i   i    (   R   t   absR   t   r_(   R#   R$   t   gr_deept   gr_rightt   gr_down(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyR   m   s    _M)c   	      C   s  |  j  ƒ  d } t j | ƒ } t j |  d |  d f ƒ } t j |  d |  d f ƒ } t j | | f ƒ } t j | | | f f d | | f ƒ} t j | j d d ƒ ƒ } t j t j | | f ƒ t j | | f ƒ t j | | f ƒ f f d | | f ƒ} | j ƒ  S(   s   
    Sparse implementation
    i   i    R   t   axis(	   t   maxR   R   R   R    t
   coo_matrixR   t   sumt   tocsr(	   R   R*   t   pixel_nbt   diagt	   i_indicest	   j_indicesR#   t   lapt   connect(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _make_laplacian_sparset   s    $c         C   sM   |  j  | j ƒ }  | r* t j | ƒ } n  t j | ƒ } |  | | d k <| S(   Ni    (   t   astypet   dtypeR   t   copyR   (   t   Xt   labelsR?   (    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _clean_labels_ar‡   s    c         C   sì   | | d k } t  j | j ƒ } | | d k } | | d k } |  | d d … | f } |  | d d … | f }  | j ƒ  } g  } xW t d | d ƒ D]B } | | | k }	 t j |	 ƒ }
 |
 j ƒ  }
 | j | |
 ƒ qœ W|  | f S(   s„   
    Build the matrix A and rhs B of the linear system to solve.
    A and B are two block of the laplacian of the image graph.
    i    Ni   (	   R   R   t   sizeR2   R   R    t
   csr_matrixt	   transposet   append(   t
   lap_sparseRA   t   indicest   unlabeled_indicest   seeds_indicest   Bt   nlabelst   rhst   labt   maskt   fs(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _buildAB   s    c         C   sS  t  j | d d … d d … d d … f j ƒ  | d d … d d … f j ƒ  | d  j ƒ  f ƒ } t  j | d d … d d … d d … f j ƒ  | d d … d d … f j ƒ  | d j ƒ  f ƒ } t  j | | ƒ } |  d d … | f | | }  } |  j ƒ  } t  j t  j |  j ƒ  ƒ t  j | d ƒ ƒ } | |  j t  j	 ƒ }  |  | f S(   sq   
    Remove edges of the graph connected to masked nodes, as well as
    corresponding weights of the edges.
    Niÿÿÿÿi   (
   R   R   R   t   logical_andR2   t   searchsortedt   uniqueR   R=   t   int64(   R   R*   RO   t   mask0t   mask1t   ind_maskt   max_node_indext   order(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _mask_edges_weights¦   s    MM!i2   c      	      sž   t  ‡  f d †  t d ƒ Dƒ ƒ \ } } } t | | | ƒ } t ˆ  | d | d d d | ƒ}	 | d  k	 r… t | |	 | ƒ \ } }	 n  t | |	 ƒ }
 ~ ~	 |
 S(   Nc         3   s   |  ] } ˆ  j  | Vq d  S(   N(   R   (   t   .0t   i(   R#   (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pys	   <genexpr>»   s    i   R%   R&   g»½×Ùß|Û=R'   (   t   tupleR   R   R+   t   NoneR[   R<   (   R#   R$   RO   R%   R'   t   l_xt   l_yt   l_zR   R*   R:   (    (   R#   sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _build_laplacian¹   s    +	c         C   so   t  j |  d k d |  d k ƒ} t j |  d k t j | ƒ ƒ } t j | ƒ } |  | } d |  | <| | f S(   sÆ   
    Prune isolated seed pixels to prevent labeling errors, and
    return coordinates and label values of isolated seeds, so
    that it is possible to put labels back in random walker output.
    i    RO   iÿÿÿÿ(   t   ndit   binary_propagationR   RR   t   logical_nott   nonzero(   RA   t   fillt   isolatedt   indst   values(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   _check_isolated_seedsÆ   s    !!

t   bfgü©ñÒMbP?c	      	   C   s§  | d k r9 t r d } q` t d k	 r0 d } q` d } n' | d k r` t d j d | ƒ ƒ ‚ n  t d k r… | d k r… t d ƒ n  | d k j ƒ  r.t d ƒ | r$t j | ƒ }	 |	 |	 d k }	 t j	 | j
 t |	 ƒ f d	 t j ƒ}
 x9 t |	 ƒ D]" \ } } | | k |
 d
 | f <qû Wn | }
 |
 S| s|  j d k  sR|  j d k rat d ƒ ‚ n  |  j
 } t j t |  ƒ ƒ d
 t j f }  nt |  j d k  r­t d ƒ ‚ n  |  d j
 } t |  ƒ }  |  j d k r|  d d … d d … t j d d … f }  n  | d k r!t j d ƒ } n^ t | ƒ t | ƒ k rst | ƒ d k rat j | d f } qt j | ƒ } n t d ƒ ‚ | r—t j | ƒ } n  t j | ƒ } t | ƒ \ } } t j t j | ƒ d k ƒ r	| d k } t | | ƒ d j | j ƒ | | <n  | j t j ƒ } t j | d k  ƒ r|t j | d k d | d k ƒ} d | t j t j | ƒ | d k ƒ <~ n  t j | ƒ } t j | d k  ƒ rÊt  |  | d | d k d | d | ƒ} n t  |  | d | d | ƒ} t! | | ƒ \ } } | d k r$t" | | d | d | ƒ} n  | d k r|t s^t d ƒ t" | | d | d | ƒ} q|t# | | d | d | ƒ} n  | d k r t$ | | d | ƒ} n  | r}| j t j% ƒ } t | ƒ d k r×| | | <n  t j& g  | D]$ } t' | | d t( ƒj) | ƒ ^ qäƒ } x t* d t+ | j, ƒ  ƒ d ƒ D]E } t j- | | k ƒ } d | d d … | f <d | | d | f <q1Wn& t' | d | ƒ j) | ƒ } | | | <| S(   s;  Random walker algorithm for segmentation from markers.

    Random walker algorithm is implemented for gray-level or multichannel
    images.

    Parameters
    ----------
    data : array_like
        Image to be segmented in phases. Gray-level `data` can be two- or
        three-dimensional; multichannel data can be three- or four-
        dimensional (multichannel=True) with the highest dimension denoting
        channels. Data spacing is assumed isotropic unless the `spacing`
        keyword argument is used.
    labels : array of ints, of same shape as `data` without channels dimension
        Array of seed markers labeled with different positive integers
        for different phases. Zero-labeled pixels are unlabeled pixels.
        Negative labels correspond to inactive pixels that are not taken
        into account (they are removed from the graph). If labels are not
        consecutive integers, the labels array will be transformed so that
        labels are consecutive. In the multichannel case, `labels` should have
        the same shape as a single channel of `data`, i.e. without the final
        dimension denoting channels.
    beta : float
        Penalization coefficient for the random walker motion
        (the greater `beta`, the more difficult the diffusion).
    mode : string, available options {'cg_mg', 'cg', 'bf'}
        Mode for solving the linear system in the random walker algorithm.
        If no preference given, automatically attempt to use the fastest
        option available ('cg_mg' from pyamg >> 'cg' with UMFPACK > 'bf').

        - 'bf' (brute force): an LU factorization of the Laplacian is
          computed. This is fast for small images (<1024x1024), but very slow
          and memory-intensive for large images (e.g., 3-D volumes).
        - 'cg' (conjugate gradient): the linear system is solved iteratively
          using the Conjugate Gradient method from scipy.sparse.linalg. This is
          less memory-consuming than the brute force method for large images,
          but it is quite slow.
        - 'cg_mg' (conjugate gradient with multigrid preconditioner): a
          preconditioner is computed using a multigrid solver, then the
          solution is computed with the Conjugate Gradient method.  This mode
          requires that the pyamg module (http://pyamg.org/) is
          installed. For images of size > 512x512, this is the recommended
          (fastest) mode.

    tol : float
        tolerance to achieve when solving the linear system, in
        cg' and 'cg_mg' modes.
    copy : bool
        If copy is False, the `labels` array will be overwritten with
        the result of the segmentation. Use copy=False if you want to
        save on memory.
    multichannel : bool, default False
        If True, input data is parsed as multichannel data (see 'data' above
        for proper input format in this case)
    return_full_prob : bool, default False
        If True, the probability that a pixel belongs to each of the labels
        will be returned, instead of only the most likely label.
    spacing : iterable of floats
        Spacing between voxels in each spatial dimension. If `None`, then
        the spacing between pixels/voxels in each dimension is assumed 1.

    Returns
    -------
    output : ndarray
        * If `return_full_prob` is False, array of ints of same shape as
          `data`, in which each pixel has been labeled according to the marker
          that reached the pixel first by anisotropic diffusion.
        * If `return_full_prob` is True, array of floats of shape
          `(nlabels, data.shape)`. `output[label_nb, i, j]` is the probability
          that label `label_nb` reaches the pixel `(i, j)` first.

    See also
    --------
    skimage.morphology.watershed: watershed segmentation
        A segmentation algorithm based on mathematical morphology
        and "flooding" of regions from markers.

    Notes
    -----
    Multichannel inputs are scaled with all channel data combined. Ensure all
    channels are separately normalized prior to running this algorithm.

    The `spacing` argument is specifically for anisotropic datasets, where
    data points are spaced differently in one or more spatial dimensions.
    Anisotropic data is commonly encountered in medical imaging.

    The algorithm was first proposed in *Random walks for image
    segmentation*, Leo Grady, IEEE Trans Pattern Anal Mach Intell.
    2006 Nov;28(11):1768-83.

    The algorithm solves the diffusion equation at infinite times for
    sources placed on markers of each phase in turn. A pixel is labeled with
    the phase that has the greatest probability to diffuse first to the pixel.

    The diffusion equation is solved by minimizing x.T L x for each phase,
    where L is the Laplacian of the weighted graph of the image, and x is
    the probability that a marker of the given phase arrives first at a pixel
    by diffusion (x=1 on markers of the phase, x=0 on the other markers, and
    the other coefficients are looked for). Each pixel is attributed the label
    for which it has a maximal value of x. The Laplacian L of the image
    is defined as:

       - L_ii = d_i, the number of neighbors of pixel i (the degree of i)
       - L_ij = -w_ij if i and j are adjacent pixels

    The weight w_ij is a decreasing function of the norm of the local gradient.
    This ensures that diffusion is easier between pixels of similar values.

    When the Laplacian is decomposed into blocks of marked and unmarked
    pixels::

        L = M B.T
            B A

    with first indices corresponding to marked pixels, and then to unmarked
    pixels, minimizing x.T L x for one phase amount to solving::

        A x = - B x_m

    where x_m = 1 on markers of the given phase, and 0 on other markers.
    This linear system is solved in the algorithm using a direct method for
    small images, and an iterative method for larger images.

    Examples
    --------
    >>> np.random.seed(0)
    >>> a = np.zeros((10, 10)) + 0.2 * np.random.rand(10, 10)
    >>> a[5:8, 5:8] += 1
    >>> b = np.zeros_like(a)
    >>> b[3, 3] = 1  # Marker for first phase
    >>> b[6, 6] = 2  # Marker for second phase
    >>> random_walker(a, b)
    array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
           [1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
           [1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32)

    t   cg_mgR   Rm   sB   {mode} is not a valid mode. Valid modes are 'cg_mg', 'cg' and 'bf't   modes)  "cg" mode will be used, but it may be slower than "bf" because SciPy was built without UMFPACK. Consider rebuilding SciPy with UMFPACK; this will greatly accelerate the conjugate gradient ("cg") solver. You may also install pyamg and run the random_walker function in "cg_mg" mode (see docstring).i    s…   Random walker only segments unlabeled areas, where labels == 0. No zero valued areas in labels were found. Returning provided labels.R>   .i   i   s=   For non-multichannel input, data must be of dimension 2 or 3.s9   For multichannel input, data must have 3 or 4 dimensions.Ng      ð?s`   Input argument `spacing` incorrect, should be an iterable with one number per spatial dimension.i   RO   iÿÿÿÿR%   R'   t   tolt   return_full_probs   pyamg (http://pyamg.org/)) is needed to use
                this mode, but is not installed. The 'cg' mode will be used
                instead.R?   (   Rn   R   Rm   (   .i    (   g      ð?(   g      ð?g      ð?g      ð?(.   R_   t
   amg_loadedt   UmfpackContextt
   ValueErrort   formatR   t   allR   RT   t   emptyR   t   lent   boolt	   enumeratet   ndimt
   atleast_3dR	   t   newaxist   asarrayR-   R?   Rl   t   anyt   diffR
   R=   R>   t   int32Rd   Re   RR   Rf   Rc   RQ   t	   _solve_cgt   _solve_cg_mgt	   _solve_bft   floatt   arrayRB   t   TrueR   R   t   intR2   t   squeeze(   R#   RA   R%   Ro   Rp   R?   R'   Rq   R$   t   unique_labelst
   out_labelst   nR]   t   dimst   label_valuest   inds_isolated_seedst   isolated_valuesRO   t   filledRG   RK   R@   t   Xlinet   mask_i(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   random_walker×   s¬    “			
	%.'!%		1&
c         C   sš   |  j  ƒ  }  t j j |  j t j ƒ ƒ } t j g  t t	 | ƒ ƒ D], } | t j | | j
 ƒ  ƒ j ƒ  ƒ ^ qC ƒ } | s– t j | d d ƒ} n  | S(   s¾   
    solves lap_sparse X_i = B_i for each phase i. An LU decomposition
    of lap_sparse is computed first. For each pixel, the label i
    corresponding to the maximal X_i is returned.
    R1   i    (   t   tocscR    t   linalgt
   factorizedR=   R   t   doubleR†   R   Rx   t   toarrayR   t   argmax(   RG   RK   Rq   t   solverR]   R@   (    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyR„   ø  s    	Ec         C   s‘   |  j  ƒ  }  g  } xK t t | ƒ ƒ D]7 } t |  | | j ƒ  d | ƒd } | j | ƒ q% W| s t j | ƒ } t j | d d ƒ} n  | S(   s®   
    solves lap_sparse X_i = B_i for each phase i, using the conjugate
    gradient method. For each pixel, the label i corresponding to the
    maximal X_i is returned.
    Rp   i    R1   (	   R•   R   Rx   R   R™   RF   R   R†   Rš   (   RG   RK   Rp   Rq   R@   R]   t   x0(    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyR‚     s    $c   	   
   C   s¯   g  } t  |  ƒ } | j d d ƒ } xW t t | ƒ ƒ D]C } t |  | | j ƒ  d | d | d d ƒd } | j | ƒ q7 W| s« t j | ƒ } t j	 | d d ƒ} n  | S(	   së   
    solves lap_sparse X_i = B_i for each phase i, using the conjugate
    gradient method with a multigrid preconditioner (ruge-stuben from
    pyamg). For each pixel, the label i corresponding to the maximal
    X_i is returned.
    t   cyclet   VRp   t   Mt   maxiteri   i    R1   (
   R   t   aspreconditionerR   Rx   R   R™   RF   R   R†   Rš   (	   RG   RK   Rp   Rq   R@   t   mlRŸ   R]   Rœ   (    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyRƒ     s    0(/   t   __doc__t   numpyR   t   scipyR    R   Rd   t   _shared.utilsR   t   scipy.sparse.linalg.dsolveR   Rs   t   __del__R   R   R_   t   pyamgR   R‡   Rr   t   ImportErrort   Falset   utilR	   t   filtersR
   t   scipy.sparse.linalgR   t   distutils.versionR   t   Versiont	   functoolst   __version__t   partialR   R+   R   R<   RB   RQ   R[   Rc   Rl   R”   R„   R‚   Rƒ   (    (    (    sN   lib/python2.7/site-packages/skimage/segmentation/random_walker_segmentation.pyt   <module>	   sR   	


	!						ÿ !