ó
 ‰\c           @   sJ   d  d l  Z d d l m Z d „  Z e d ƒ d „  ƒ Z d d „ Z d S(	   iÿÿÿÿNi   (   t
   deprecatedc         C   s   |  j  | j  k r5 t d d |  j  | j  f ƒ ‚ n  t |  ƒ d }  t | ƒ d } | j ƒ  d |  | } t | ƒ d } | S(   sS  Return the join of the two input segmentations.

    The join J of S1 and S2 is defined as the segmentation in which two
    voxels are in the same segment if and only if they are in the same
    segment in *both* S1 and S2.

    Parameters
    ----------
    s1, s2 : numpy arrays
        s1 and s2 are label fields of the same shape.

    Returns
    -------
    j : numpy array
        The join segmentation of s1 and s2.

    Examples
    --------
    >>> from skimage.segmentation import join_segmentations
    >>> s1 = np.array([[0, 0, 1, 1],
    ...                [0, 2, 1, 1],
    ...                [2, 2, 2, 1]])
    >>> s2 = np.array([[0, 1, 1, 0],
    ...                [0, 1, 1, 0],
    ...                [0, 1, 1, 1]])
    >>> join_segmentations(s1, s2)
    array([[0, 1, 3, 2],
           [0, 5, 3, 2],
           [4, 5, 5, 3]])
    s.   Cannot join segmentations of different shape. s   s1.shape: %s, s2.shape: %si    i   (   t   shapet
   ValueErrort   relabel_sequentialt   max(   t   s1t   s2t   j(    (    s9   lib/python2.7/site-packages/skimage/segmentation/_join.pyt   join_segmentations   s    R   c         C   s   t  |  d d ƒS(   s”   Convert labels in an arbitrary label field to {1, ... number_of_labels}.

    This function is deprecated, see ``relabel_sequential`` for more.
    t   offseti   (   R   (   t   label_field(    (    s9   lib/python2.7/site-packages/skimage/segmentation/_join.pyt   relabel_from_one.   s    i   c   	      C   sF  |  j  ƒ  } t j |  j t j ƒ sZ t j t | ƒ ƒ } |  j | ƒ }  | j | ƒ } n  t j |  ƒ } | | d k } | t	 | ƒ k r˜ |  | | f St j
 | d t ƒ } t j | | t	 | ƒ ƒ | | <| d k j ƒ  sû t j d g | f ƒ } n  t j
 | d t	 | ƒ d t j ƒ} | | | d )| |  } | | | f S(   sŸ	  Relabel arbitrary labels to {`offset`, ... `offset` + number_of_labels}.

    This function also returns the forward map (mapping the original labels to
    the reduced labels) and the inverse map (mapping the reduced labels back
    to the original ones).

    Parameters
    ----------
    label_field : numpy array of int, arbitrary shape
        An array of labels.
    offset : int, optional
        The return labels will start at `offset`, which should be
        strictly positive.

    Returns
    -------
    relabeled : numpy array of int, same shape as `label_field`
        The input label field with labels mapped to
        {offset, ..., number_of_labels + offset - 1}.
    forward_map : numpy array of int, shape ``(label_field.max() + 1,)``
        The map from the original label space to the returned label
        space. Can be used to re-apply the same mapping. See examples
        for usage.
    inverse_map : 1D numpy array of int, of length offset + number of labels
        The map from the new label space to the original space. This
        can be used to reconstruct the original label field from the
        relabeled one.

    Notes
    -----
    The label 0 is assumed to denote the background and is never remapped.

    The forward map can be extremely big for some inputs, since its
    length is given by the maximum of the label field. However, in most
    situations, ``label_field.max()`` is much smaller than
    ``label_field.size``, and in these cases the forward map is
    guaranteed to be smaller than either the input or output images.

    Examples
    --------
    >>> from skimage.segmentation import relabel_sequential
    >>> label_field = np.array([1, 1, 5, 5, 8, 99, 42])
    >>> relab, fw, inv = relabel_sequential(label_field)
    >>> relab
    array([1, 1, 2, 2, 3, 5, 4])
    >>> fw
    array([0, 1, 0, 0, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 5])
    >>> inv
    array([ 0,  1,  5,  8, 42, 99])
    >>> (fw[label_field] == relab).all()
    True
    >>> (inv[relab] == label_field).all()
    True
    >>> relab, fw, inv = relabel_sequential(label_field, offset=5)
    >>> relab
    array([5, 5, 6, 6, 7, 9, 8])
    i    i   t   dtype(   R   t   npt
   issubdtypeR   t   signedintegert   min_scalar_typet   intt   astypet   uniquet   lent   zerost   aranget   anyt   concatenatet   intp(	   R
   R	   t   mt   new_typet   labelst   labels0t   forward_mapt   inverse_mapt	   relabeled(    (    s9   lib/python2.7/site-packages/skimage/segmentation/_join.pyR   7   s"    > &
(   t   numpyR   t   _shared.utilsR    R   R   R   (    (    (    s9   lib/python2.7/site-packages/skimage/segmentation/_join.pyt   <module>   s   	)	