ó
Ê½÷Xc        	   @` si  d  d l  m Z m Z m Z d  d l Z d  d l Z d d l m Z d d l m Z d  d l	 Z	 d d d d	 d
 d d d g Z
 d „  Z d d e j d „ Z d e j d „ Z d „  Z d d d d d e d i  d „ Z d d d d e d „ Z d d d d d d e d „ Z d d d d e d „ Z d d d d e d „ Z d „  Z d e d d d d e d „ Z d S(   i    (   t   divisiont   print_functiont   absolute_importNi   (   t   _ni_support(   t	   _nd_imaget   spline_filter1dt   spline_filtert   geometric_transformt   map_coordinatest   affine_transformt   shiftt   zoomt   rotatec         C` s   t  j |  ƒ }  |  S(   N(   R   t   _extend_mode_to_code(   t   mode(    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR   ,   s    i   iÿÿÿÿc         C` s½   | d k  s | d k r' t  d ƒ ‚ n  t j |  ƒ }  t j |  ƒ rT t d ƒ ‚ n  t j | |  ƒ \ } } | d k rŽ t j |  ƒ | d <n+ t j | |  j	 ƒ } t
 j |  | | | ƒ | S(   s  
    Calculates a one-dimensional spline filter along the given axis.

    The lines of the array along the given axis are filtered by a
    spline filter. The order of the spline must be >= 2 and <= 5.

    Parameters
    ----------
    input : array_like
        The input array.
    order : int, optional
        The order of the spline, default is 3.
    axis : int, optional
        The axis along which the spline filter is applied. Default is the last
        axis.
    output : ndarray or dtype, optional
        The array in which to place the output, or the dtype of the returned
        array. Default is `numpy.float64`.

    Returns
    -------
    spline_filter1d : ndarray or None
        The filtered input. If `output` is given as a parameter, None is
        returned.

    i    i   s   spline order not supporteds   Complex type not supportedi   .(   i    i   (   t   RuntimeErrort   numpyt   asarrayt   iscomplexobjt	   TypeErrorR   t   _get_outputt   arrayt   _check_axist   ndimR   R   (   t   inputt   ordert   axist   outputt   return_value(    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR   1   s    c      	   C` sÒ   | d k  s | d k r' t  d ƒ ‚ n  t j |  ƒ }  t j |  ƒ rT t d ƒ ‚ n  t j | |  ƒ \ } } | d	 k rÀ |  j d k rÀ xD t |  j ƒ D]" } t	 |  | | d | ƒ| }  q— Wn |  d | d <| S(
   sñ  
    Multi-dimensional spline filter.

    For more details, see `spline_filter1d`.

    See Also
    --------
    spline_filter1d

    Notes
    -----
    The multi-dimensional filter is implemented as a sequence of
    one-dimensional spline filters. The intermediate arrays are stored
    in the same data type as the output. Therefore, for output types
    with a limited precision, the results may be imprecise because
    intermediate results may be stored with insufficient precision.

    i   i   s   spline order not supporteds   Complex type not supportedi    i   R   .(   i    i   (
   R   R   R   R   R   R   R   R   t   rangeR   (   R   R   R   R   R   (    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR   Z   s    c         C` sX   t  j |  | | | | | | | | |	 |
 ƒ | d k	 rT | j j rT | j t ƒ n  | S(   s[   
    Wrapper around _nd_image.geometric_transform to work around
    endianness issues
    N(   R   R   t   Nonet   dtypet   isnativet   byteswapt   True(   R   t   mappingt   coordinatest   matrixt   offsetR   R   R   t   cvalt   extra_argumentst   extra_keywords(    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyt   _geometric_transform|   s    t   constantg        c
         C` s%  | d k  s | d k r' t  d ƒ ‚ n  t j |  ƒ }  t j |  ƒ rT t d ƒ ‚ n  | d	 k rl |  j } n  |  j d k  s t | ƒ d k  rœ t  d ƒ ‚ n  t	 | ƒ } | rÕ | d k rÕ t
 |  | d t j ƒ}
 n |  }
 t j | |  d | ƒ\ } } t |
 | d	 d	 d	 | | | | | |	 ƒ | S(
   sM  
    Apply an arbitrary geometric transform.

    The given mapping function is used to find, for each point in the
    output, the corresponding coordinates in the input. The value of the
    input at those coordinates is determined by spline interpolation of
    the requested order.

    Parameters
    ----------
    input : array_like
        The input array.
    mapping : {callable, scipy.LowLevelCallable}
        A callable object that accepts a tuple of length equal to the output
        array rank, and returns the corresponding input coordinates as a tuple
        of length equal to the input array rank.
    output_shape : tuple of ints, optional
        Shape tuple.
    output : ndarray or dtype, optional
        The array in which to place the output, or the dtype of the returned
        array.
    order : int, optional
        The order of the spline interpolation, default is 3.
        The order has to be in the range 0-5.
    mode : str, optional
        Points outside the boundaries of the input are filled according
        to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
        Default is 'constant'.
    cval : scalar, optional
        Value used for points outside the boundaries of the input if
        ``mode='constant'``. Default is 0.0
    prefilter : bool, optional
        The parameter prefilter determines if the input is pre-filtered with
        `spline_filter` before interpolation (necessary for spline
        interpolation of order > 1).  If False, it is assumed that the input is
        already filtered. Default is True.
    extra_arguments : tuple, optional
        Extra arguments passed to `mapping`.
    extra_keywords : dict, optional
        Extra keywords passed to `mapping`.

    Returns
    -------
    return_value : ndarray or None
        The filtered input. If `output` is given as a parameter, None is
        returned.

    See Also
    --------
    map_coordinates, affine_transform, spline_filter1d


    Notes
    -----
    This function also accepts low-level callback functions with one
    the following signatures and wrapped in `scipy.LowLevelCallable`:

    .. code:: c

       int mapping(npy_intp *output_coordinates, double *input_coordinates, 
                   int output_rank, int input_rank, void *user_data)
       int mapping(intptr_t *output_coordinates, double *input_coordinates, 
                   int output_rank, int input_rank, void *user_data)

    The calling function iterates over the elements of the output array,
    calling the callback function at each element. The coordinates of the
    current output element are passed through ``output_coordinates``. The
    callback function must return the coordinates at which the input must
    be interpolated in ``input_coordinates``. The rank of the input and
    output arrays are given by ``input_rank`` and ``output_rank``
    respectively.  ``user_data`` is the data pointer provided 
    to `scipy.LowLevelCallable` as-is. 

    The callback function must return an integer error status that is zero 
    if something went wrong and one otherwise. If an error occurs, you should
    normally set the python error status with an informative message
    before returning, otherwise a default error message is set by the
    calling function.

    In addition, some other low-level function pointer specifications 
    are accepted, but these are for backward compatibility only and should
    not be used in new code.

    Examples
    --------
    >>> from scipy import ndimage
    >>> a = np.arange(12.).reshape((4, 3))
    >>> def shift_func(output_coords):
    ...     return (output_coords[0] - 0.5, output_coords[1] - 0.5)
    ...
    >>> ndimage.geometric_transform(a, shift_func)
    array([[ 0.   ,  0.   ,  0.   ],
           [ 0.   ,  1.362,  2.738],
           [ 0.   ,  4.812,  6.187],
           [ 0.   ,  8.263,  9.637]])

    i    i   s   spline order not supporteds   Complex type not supportedi   s!   input and output rank must be > 0R   t   shapeN(   R   R   R   R   R   R   R,   R   t   lenR   R   t   float64R   R   R*   (   R   R#   t   output_shapeR   R   R   R'   t	   prefilterR(   R)   t   filteredR   (    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR   Œ   s$    e!c   
      C` sl  | d k  s | d k r' t  d ƒ ‚ n  t j |  ƒ }  t j |  ƒ rT t d ƒ ‚ n  t j | ƒ } t j | ƒ r t d ƒ ‚ n  | j d } |  j d k  s¯ t | ƒ d k  r¾ t  d ƒ ‚ n  | j d |  j k rã t  d ƒ ‚ n  t | ƒ } | r| d k rt	 |  | d t j
 ƒ} n |  } t j | |  d	 | ƒ\ } }	 t | d
 | d
 d
 | | | | d
 d
 ƒ |	 S(   s`
  
    Map the input array to new coordinates by interpolation.

    The array of coordinates is used to find, for each point in the output,
    the corresponding coordinates in the input. The value of the input at
    those coordinates is determined by spline interpolation of the
    requested order.

    The shape of the output is derived from that of the coordinate
    array by dropping the first axis. The values of the array along
    the first axis are the coordinates in the input array at which the
    output value is found.

    Parameters
    ----------
    input : ndarray
        The input array.
    coordinates : array_like
        The coordinates at which `input` is evaluated.
    output : ndarray or dtype, optional
        The array in which to place the output, or the dtype of the returned
        array.
    order : int, optional
        The order of the spline interpolation, default is 3.
        The order has to be in the range 0-5.
    mode : str, optional
        Points outside the boundaries of the input are filled according
        to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
        Default is 'constant'.
    cval : scalar, optional
        Value used for points outside the boundaries of the input if
        ``mode='constant'``. Default is 0.0
    prefilter : bool, optional
        The parameter prefilter determines if the input is pre-filtered with
        `spline_filter` before interpolation (necessary for spline
        interpolation of order > 1).  If False, it is assumed that the input is
        already filtered. Default is True.

    Returns
    -------
    map_coordinates : ndarray
        The result of transforming the input. The shape of the output is
        derived from that of `coordinates` by dropping the first axis.

    See Also
    --------
    spline_filter, geometric_transform, scipy.interpolate

    Examples
    --------
    >>> from scipy import ndimage
    >>> a = np.arange(12.).reshape((4, 3))
    >>> a
    array([[  0.,   1.,   2.],
           [  3.,   4.,   5.],
           [  6.,   7.,   8.],
           [  9.,  10.,  11.]])
    >>> ndimage.map_coordinates(a, [[0.5, 2], [0.5, 1]], order=1)
    array([ 2.,  7.])

    Above, the interpolated value of a[0.5, 0.5] gives output[0], while
    a[2, 1] is output[1].

    >>> inds = np.array([[0.5, 2], [0.5, 4]])
    >>> ndimage.map_coordinates(a, inds, order=1, cval=-33.3)
    array([  2. , -33.3])
    >>> ndimage.map_coordinates(a, inds, order=1, mode='nearest')
    array([ 2.,  8.])
    >>> ndimage.map_coordinates(a, inds, order=1, cval=0, output=bool)
    array([ True, False], dtype=bool)

    i    i   s   spline order not supporteds   Complex type not supportedi   s!   input and output rank must be > 0s"   invalid shape for coordinate arrayR   R,   N(   R   R   R   R   R   R,   R   R-   R   R   R.   R   R   R*   R   (
   R   R$   R   R   R   R'   R0   R/   R1   R   (    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR     s,    J!c	         C` s  | d k  s | d k r' t  d ƒ ‚ n  t j |  ƒ }  t j |  ƒ rT t d ƒ ‚ n  | d k rl |  j } n  |  j d k  s t | ƒ d k  rœ t  d ƒ ‚ n  t	 | ƒ } | rÕ | d k rÕ t
 |  | d t j ƒ}	 n |  }	 t j | |  d | ƒ\ } }
 t j | d	 t j ƒ} | j d k s3| j d d k  rBt  d ƒ ‚ n  | j d |  j k rgt  d ƒ ‚ n  | j d
 k r›| j d | j k r›t  d ƒ ‚ n  | j j s¶| j ƒ  } n  t j | |  j ƒ } t j | d	 t j ƒ} | j d k s| j d d k  rt  d ƒ ‚ n  | j j s/| j ƒ  } n  | j d k rqt j d ƒ t j |	 | | | | | | | ƒ n( t |	 d d | | | | | | d d ƒ |
 S(   s>  
    Apply an affine transformation.

    The given matrix and offset are used to find for each point in the
    output the corresponding coordinates in the input by an affine
    transformation. The value of the input at those coordinates is
    determined by spline interpolation of the requested order. Points
    outside the boundaries of the input are filled according to the given
    mode.

    Given an output image pixel index vector ``o``, the pixel value
    is determined from the input image at position ``np.dot(matrix,o) + offset``.

    A diagonal matrix can be specified by supplying a one-dimensional
    array-like to the matrix parameter, in which case a more efficient
    algorithm is applied.

    .. versionchanged:: 0.18.0
        Previously, the exact interpretation of the affine transformation
        depended on whether the matrix was supplied as a one-dimensional or
        two-dimensional array. If a one-dimensional array was supplied
        to the matrix parameter, the output pixel value at index ``o``
        was determined from the input image at position ``matrix * (o + offset)``.

    Parameters
    ----------
    input : ndarray
        The input array.
    matrix : ndarray
        The matrix must be two-dimensional or can also be given as a
        one-dimensional sequence or array. In the latter case, it is assumed
        that the matrix is diagonal. A more efficient algorithms is then
        applied that exploits the separability of the problem.
    offset : float or sequence, optional
        The offset into the array where the transform is applied. If a float,
        `offset` is the same for each axis. If a sequence, `offset` should
        contain one value for each axis.
    output_shape : tuple of ints, optional
        Shape tuple.
    output : ndarray or dtype, optional
        The array in which to place the output, or the dtype of the returned
        array.
    order : int, optional
        The order of the spline interpolation, default is 3.
        The order has to be in the range 0-5.
    mode : str, optional
        Points outside the boundaries of the input are filled according
        to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
        Default is 'constant'.
    cval : scalar, optional
        Value used for points outside the boundaries of the input if
        ``mode='constant'``. Default is 0.0
    prefilter : bool, optional
        The parameter prefilter determines if the input is pre-filtered with
        `spline_filter` before interpolation (necessary for spline
        interpolation of order > 1).  If False, it is assumed that the input is
        already filtered. Default is True.

    Returns
    -------
    affine_transform : ndarray or None
        The transformed input. If `output` is given as a parameter, None is
        returned.

    i    i   s   spline order not supporteds   Complex type not supportedi   s!   input and output rank must be > 0R   R,   R   i   s    no proper affine matrix provideds&   affine matrix has wrong number of rowss)   affine matrix has wrong number of columnss   no proper offset provideds}   The behaviour of affine_transform with a one-dimensional array supplied for the matrix parameter has changed in scipy 0.18.0.N(   i   i   (   R   R   R   R   R   R   R,   R   R-   R   R   R.   R   R   t   flagst
   contiguoust   copyt   _normalize_sequencet   warningst   warnR   t
   zoom_shiftR*   (   R   R%   R&   R/   R   R   R   R'   R0   R1   R   (    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR	   i  sL    D!"%"c   
   
   C` sN  | d k  s | d k r' t  d ƒ ‚ n  t j |  ƒ }  t j |  ƒ rT t d ƒ ‚ n  |  j d k  rr t  d ƒ ‚ n  t | ƒ } | r« | d k r« t |  | d t j ƒ} n |  } t	 j
 | |  ƒ \ } } t	 j | |  j ƒ } g  | D] }	 |	 ^ qå } t j | d t j ƒ} | j j s+| j ƒ  } n  t j | d	 | | | | | ƒ | S(
   sï  
    Shift an array.

    The array is shifted using spline interpolation of the requested order.
    Points outside the boundaries of the input are filled according to the
    given mode.

    Parameters
    ----------
    input : ndarray
        The input array.
    shift : float or sequence, optional
        The shift along the axes. If a float, `shift` is the same for each
        axis. If a sequence, `shift` should contain one value for each axis.
    output : ndarray or dtype, optional
        The array in which to place the output, or the dtype of the returned
        array.
    order : int, optional
        The order of the spline interpolation, default is 3.
        The order has to be in the range 0-5.
    mode : str, optional
        Points outside the boundaries of the input are filled according
        to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
        Default is 'constant'.
    cval : scalar, optional
        Value used for points outside the boundaries of the input if
        ``mode='constant'``. Default is 0.0
    prefilter : bool, optional
        The parameter prefilter determines if the input is pre-filtered with
        `spline_filter` before interpolation (necessary for spline
        interpolation of order > 1).  If False, it is assumed that the input is
        already filtered. Default is True.

    Returns
    -------
    shift : ndarray or None
        The shifted input. If `output` is given as a parameter, None is
        returned.

    i    i   s   spline order not supporteds   Complex type not supportedi   s!   input and output rank must be > 0R   R   N(   R   R   R   R   R   R   R   R   R.   R   R   R5   R2   R3   R4   R   R8   R   (
   R   R
   R   R   R   R'   R0   R1   R   t   ii(    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR
   Ú  s&    *c      
   C` s  | d k  s | d k r' t  d ƒ ‚ n  t j |  ƒ }  t j |  ƒ rT t d ƒ ‚ n  |  j d k  rr t  d ƒ ‚ n  t | ƒ } | r« | d k r« t |  | d t j ƒ} n |  } t	 j
 | |  j ƒ } t g  t |  j | ƒ D]" \ } }	 t t | |	 ƒ ƒ ^ qÜ ƒ }
 t g  t |  j | ƒ D] \ } }	 t | |	 ƒ ^ qƒ } |
 | k rat j d t ƒ n  t j |
 t ƒ d } t j t j |  j ƒ d | d	 t j |  j d
 t j ƒd | d k ƒ} t	 j | |  d |
 ƒ\ } } t j | ƒ } t j | | d | | | | ƒ | S(   s“  
    Zoom an array.

    The array is zoomed using spline interpolation of the requested order.

    Parameters
    ----------
    input : ndarray
        The input array.
    zoom : float or sequence, optional
        The zoom factor along the axes. If a float, `zoom` is the same for each
        axis. If a sequence, `zoom` should contain one value for each axis.
    output : ndarray or dtype, optional
        The array in which to place the output, or the dtype of the returned
        array.
    order : int, optional
        The order of the spline interpolation, default is 3.
        The order has to be in the range 0-5.
    mode : str, optional
        Points outside the boundaries of the input are filled according
        to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
        Default is 'constant'.
    cval : scalar, optional
        Value used for points outside the boundaries of the input if
        ``mode='constant'``. Default is 0.0
    prefilter : bool, optional
        The parameter prefilter determines if the input is pre-filtered with
        `spline_filter` before interpolation (necessary for spline
        interpolation of order > 1).  If False, it is assumed that the input is
        already filtered. Default is True.

    Returns
    -------
    zoom : ndarray or None
        The zoomed input. If `output` is given as a parameter, None is
        returned.

    i    i   s   spline order not supporteds   Complex type not supportedi   s!   input and output rank must be > 0R   s˜   From scipy 0.13.0, the output shape of zoom() is calculated with round() instead of int() - for these inputs the size of the returned array has changed.t   outR   t   whereR,   N(   R   R   R   R   R   R   R   R   R.   R   R5   t   tuplet   zipR,   t   intt   roundR6   R7   t   UserWarningR   t   floatt   dividet	   ones_likeR   t   ascontiguousarrayR   R8   R   (   R   R   R   R   R   R'   R0   R1   R9   t   jjR/   t   output_shape_oldt   zoom_divR   (    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR     s:    (>8
c         C` sž   |  d | d k  r% |  d | d <n  |  d | d k rJ |  d | d <n  |  d | d k  ro |  d | d <n  |  d | d k r” |  d | d <n  | | f S(   Ni    i   (    (   t   coort   minct   maxc(    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyt   _minmaxh  s    c	   #      C` s…  t  j |  ƒ }  t | ƒ } |  j }	 | d d k  rG | d c |	 7<n  | d d k  rj | d c |	 7<n  | d d k  sª | d d k  sª | d |	 k sª | d |	 k r¹ t d ƒ ‚ n  | d | d k rä | d | d f } n  t  j d | } t j | ƒ }
 t j | ƒ } t j | ƒ } t j | ƒ } t  j	 |
 | g | | g g d t  j
 ƒ} |  j | d } |  j | d } | r“t  j	 |
 | g | | g g d t  j
 ƒ} d d g } d d g } t  j | d | g ƒ } t | | | ƒ \ } } t  j | | d g ƒ } t | | | ƒ \ } } t  j | | | g ƒ } t | | | ƒ \ } } t | d | d d ƒ } t | d | d d ƒ } n" |  j | d } |  j | d } t  j d d t  j
 ƒ} t | ƒ d d | d <t | ƒ d d | d <t  j | | ƒ } t  j d d t  j
 ƒ} t | ƒ d d | d <t | ƒ d d | d <| | } t |  j ƒ } | | | d <| | | d <t | ƒ } t j | |  d	 | ƒ\ } } |  j d k rêt |  | | | | | | | | ƒ	 n—g  } t  j |  j d
 d ƒ} | |  j | d } | |  j | d } xL t |  j ƒ D]; } | | k rd| j d ƒ qB| j t d d d ƒ ƒ qBWt t |  j ƒ ƒ } | j ƒ  | j | d ƒ | j | d ƒ | | d | | d f } x  t | ƒ D]’ } |  t | ƒ }  | t | ƒ }! t |  | | | |! | | | | ƒ	 xG | D]? }" | |" |  j |" d k  ro| |" c d 7<Pq:d | |" <q:WqëW| S(   s‰  
    Rotate an array.

    The array is rotated in the plane defined by the two axes given by the
    `axes` parameter using spline interpolation of the requested order.

    Parameters
    ----------
    input : ndarray
        The input array.
    angle : float
        The rotation angle in degrees.
    axes : tuple of 2 ints, optional
        The two axes that define the plane of rotation. Default is the first
        two axes.
    reshape : bool, optional
        If `reshape` is true, the output shape is adapted so that the input
        array is contained completely in the output. Default is True.
    output : ndarray or dtype, optional
        The array in which to place the output, or the dtype of the returned
        array.
    order : int, optional
        The order of the spline interpolation, default is 3.
        The order has to be in the range 0-5.
    mode : str, optional
        Points outside the boundaries of the input are filled according
        to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
        Default is 'constant'.
    cval : scalar, optional
        Value used for points outside the boundaries of the input if
        ``mode='constant'``. Default is 0.0
    prefilter : bool, optional
        The parameter prefilter determines if the input is pre-filtered with
        `spline_filter` before interpolation (necessary for spline
        interpolation of order > 1).  If False, it is assumed that the input is
        already filtered. Default is True.

    Returns
    -------
    rotate : ndarray or None
        The rotated input. If `output` is given as a parameter, None is
        returned.

    i    i   s    invalid rotation plane specifiedi´   R   g      à?i   g       @R,   R   (   i   (   i   N(   R   R   t   listR   R   t   pit   matht   cost   sinR   R.   R,   t   dotRK   R>   t   zerosRA   R<   R   R   R	   t   productR   t   appendt   sliceR   t   reverset   remove(#   R   t   anglet   axest   reshapeR   R   R   R'   R0   t   rankt   m11t   m12t   m21t   m22R%   t   iyt   ixt   mtrxRI   RJ   RH   t   oyt   oxR&   t   tmpR/   R   R$   t   sizeR9   t	   iter_axest   ost   iat   oaRE   (    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyR   t  s˜    /	@


(    (   i   i    (   t
   __future__R    R   R   RN   R   t    R   R   R6   t   __all__R   R.   R   R   R*   R   R"   R   R   R	   R
   R   RK   R   (    (    (    s:   /tmp/pip-build-7oUkmx/scipy/scipy/ndimage/interpolation.pyt   <module>   s8   		)"		wbo?M	