σ
ίΘ[c           @` sί   d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l Z	 d d l
 m Z d d l m Z m Z m Z d Z d	 d
 d d g Z d e f d     YZ d e f d     YZ d e f d     YZ d   Z d S(   ul  
This module contains the convolution and filter functionalities of astropy.

A few conceptual notes:
A filter kernel is mainly characterized by its response function. In the 1D
case we speak of "impulse response function", in the 2D case we call it "point
spread function". This response function is given for every kernel by an
astropy `FittableModel`, which is evaluated on a grid to obtain a filter array,
which can then be applied to binned data.

The model is centered on the array and should have an amplitude such that the array
integrates to one per default.

Currently only symmetric 2D kernels are supported.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   AstropyUserWarningi   (   t   discretize_modelt   add_kernel_arrays_1Dt   add_kernel_arrays_2Did   u   Kernelu   Kernel1Du   Kernel2Du   kernel_arithmeticst   Kernelc           B` sζ   e  Z d  Z e Z e Z d Z d   Z	 e
 d    Z e
 d    Z e
 d    Z e
 d    Z e
 d    Z d d  Z e
 d	    Z e
 d
    Z e
 d    Z d   Z d   Z d   Z d   Z d   Z d d  Z RS(   uz   
    Convolution kernel base class.

    Parameters
    ----------
    array : `~numpy.ndarray`
        Kernel array.
    c         C` s   t  j |  |  _ d  S(   N(   t   npt
   asanyarrayt   _array(   t   selft   array(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   __init__.   s    c         C` s   |  j  S(   u:   
        Deviation from the normalization to one.
        (   t   _truncation(   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt
   truncation1   s    c         C` s   |  j  S(   uͺ   
        Indicates if kernel is bool.

        If the kernel is bool the multiplication in the convolution could
        be omitted, to increase the performance.
        (   t   _is_bool(   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   is_bool8   s    c         C` s   |  j  S(   u(   
        Kernel response model.
        (   t   _model(   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   modelB   s    c         C` s
   |  j  j S(   u#   
        Kernel dimension.
        (   R   t   ndim(   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt	   dimensionI   s    c         C` s!   g  |  j  j D] } | d ^ q S(   u-   
        Index of the kernel center.
        i   (   R   t   shape(   R   t	   axes_size(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   centerP   s    u   integralc         C` s   | d k r |  j  j   } n* | d k r< |  j  j   } n t d   | d k rg t j d t  n t j |  j  | |  j   |  j  j   |  _	 d S(   ug  
        Normalize the filter kernel.

        Parameters
        ----------
        mode : {'integral', 'peak'}
            One of the following modes:
                * 'integral' (default)
                    Kernel is normalized such that its integral = 1.
                * 'peak'
                    Kernel is normalized such that its peak = 1.
        u   integralu   peaku*   invalid mode, must be 'integral' or 'peak'i    u8   The kernel cannot be normalized because it sums to zero.N(
   R   t   sumt   maxt
   ValueErrort   warningst   warnR   R	   t   dividet   _kernel_sum(   R   t   modet   normalization(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt	   normalizeW   s    	
c         C` s
   |  j  j S(   u,   
        Shape of the kernel array.
        (   R   R   (   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyR   u   s    c         C` s   |  j  S(   uΊ  
        Indicates if the filter kernel is separable.

        A 2D filter is separable, when its filter array can be written as the
        outer product of two 1D arrays.

        If a filter kernel is separable, higher dimension convolutions will be
        performed by applying the 1D filter array consecutively on every dimension.
        This is significantly faster, than using a filter array with the same
        dimension.
        (   t
   _separable(   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt	   separable|   s    c         C` s   |  j  S(   u&   
        Filter kernel array.
        (   R   (   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyR      s    c         C` s   t  |  | d  S(   u)   
        Add two filter kernels.
        u   add(   t   kernel_arithmetics(   R   t   kernel(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   __add__   s    c         C` s   t  |  | d  S(   u.   
        Subtract two filter kernels.
        u   sub(   R&   (   R   R'   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   __sub__   s    c         C` s   t  |  | d  S(   uF   
        Multiply kernel with number or convolve two kernels.
        u   mul(   R&   (   R   t   value(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   __mul__   s    c         C` s   t  |  | d  S(   uF   
        Multiply kernel with number or convolve two kernels.
        u   mul(   R&   (   R   R*   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   __rmul__€   s    c         C` s   |  j  S(   u5   
        Array representation of the kernel.
        (   R   (   R   (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt	   __array__ͺ   s    c         C` s%   t  | d  t j k r t S| Sd S(   u?   
        Wrapper for multiplication with numpy arrays.
        i    N(   t   typeR	   t   ufunct   NotImplemented(   R   R   t   context(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   __array_wrap__°   s    N(   t   __name__t
   __module__t   __doc__t   FalseR$   t   TrueR   t   NoneR   R   t   propertyR   R   R   R   R   R#   R   R%   R   R(   R)   R+   R,   R-   R2   (    (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyR   !   s(   	
					t   Kernel1Dc           B` s    e  Z d  Z d d d d  Z RS(   uω  
    Base class for 1D filter kernels.

    Parameters
    ----------
    model : `~astropy.modeling.FittableModel`
        Model to be evaluated.
    x_size : odd int, optional
        Size of the kernel array. Default = 8 * width.
    array : `~numpy.ndarray`
        Kernel array.
    width : number
        Width of the filter kernel.
    mode : str, optional
        One of the following discretization modes:
            * 'center' (default)
                Discretize model by taking the value
                at the center of the bin.
            * 'linear_interp'
                Discretize model by linearly interpolating
                between the values at the corners of the bin.
            * 'oversample'
                Discretize model by taking the average
                on an oversampled grid.
            * 'integrate'
                Discretize model by integrating the
                model over the bin.
    factor : number, optional
        Factor of oversampling. Default factor = 10.
    c         K` s  | d  k rδ |  j d  k r* t d   n  | d  k rB |  j } n! | t |  k rc t d   n  | d d k r t |  d d t |  d d f } n- t |  d d t |  d d d f } t |  j | |  } n | d  k	 rό d  |  _ n  t t |   j |  d  S(   Nu#   Must specify either array or model.u   x_size should be an integeri   i    g      ΰ?i   (	   R8   R   t	   TypeErrort   _default_sizet   intR   t   superR:   R   (   R   R   t   x_sizeR   t   kwargst   x_range(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyR   Ϊ   s    ,-N(   R3   R4   R5   R8   R   (    (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyR:   Ί   s   t   Kernel2Dc           B` s#   e  Z d  Z d d d d d  Z RS(   uz  
    Base class for 2D filter kernels.

    Parameters
    ----------
    model : `~astropy.modeling.FittableModel`
        Model to be evaluated.
    x_size : odd int, optional
        Size in x direction of the kernel array. Default = 8 * width.
    y_size : odd int, optional
        Size in y direction of the kernel array. Default = 8 * width.
    array : `~numpy.ndarray`
        Kernel array.
    mode : str, optional
        One of the following discretization modes:
            * 'center' (default)
                Discretize model by taking the value
                at the center of the bin.
            * 'linear_interp'
                Discretize model by performing a bilinear interpolation
                between the values at the corners of the bin.
            * 'oversample'
                Discretize model by taking the average
                on an oversampled grid.
            * 'integrate'
                Discretize model by integrating the
                model over the bin.
    width : number
        Width of the filter kernel.
    factor : number, optional
        Factor of oversampling. Default factor = 10.
    c         K` sΈ  | d  k r|  j d  k r* t d   n  | d  k rB |  j } n! | t |  k rc t d   n  | d  k rx | } n! | t |  k r t d   n  | d d k rΥ t |  d d t |  d d f } n- t |  d d t |  d d d f } | d d k r>t |  d d t |  d d f } n- t |  d d t |  d d d f } t |  j | | |  } n | d  k	 rd  |  _ n  t t |   j |  d  S(   Nu#   Must specify either array or model.u   x_size should be an integeru   y_size should be an integeri   i    g      ΰ?i   (	   R8   R   R;   R<   R=   R   R>   RB   R   (   R   R   R?   t   y_sizeR   R@   RA   t   y_range(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyR     s*    	,-,-N(   R3   R4   R5   R8   R   (    (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyRB   υ   s    c         C` sυ  t  |  t  rΎ t  | t  rΎ | d k rB t |  j | j  } n  | d k rg t |  j | j  } n  | d k r t d   n  t d |  } |  j o  | j | _ |  j p΅ | j | _ n3t  |  t  r|t  | t  r|| d k r t |  j | j  } n  | d k r%t |  j | j  } n  | d k r@t d   n  t d |  } |  j o^| j | _ |  j ps| j | _ nu t  |  t  st  |  t  rεt	 j
 |  rε| d k rΦt j |   } | j | 9_ qρt d   n t d   | S(   uε  
    Add, subtract or multiply two kernels.

    Parameters
    ----------
    kernel : `astropy.convolution.Kernel`
        Kernel instance
    value : kernel, float or int
        Value to operate with
    operation : {'add', 'sub', 'mul'}
        One of the following operations:
            * 'add'
                Add two kernels
            * 'sub'
                Subtract two kernels
            * 'mul'
                Multiply kernel with number or convolve two kernels.
    u   addu   subu   muluY   Kernel operation not supported. Maybe you want to use convolve(kernel1, kernel2) instead.R   u   Kernel operation not supported.(   t
   isinstanceR:   R   R   t	   ExceptionR$   R   RB   R   R	   t   isscalart   copyR   (   R'   R*   t	   operationt	   new_arrayt
   new_kernel(    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyR&   =  s8    (   R5   t
   __future__R    R   R   R   R   RH   t   numpyR	   t   utils.exceptionsR   t   utilsR   R   R   t   MAX_NORMALIZATIONt   __all__t   objectR   R:   RB   R&   (    (    (    s7   lib/python2.7/site-packages/astropy/convolution/core.pyt   <module>   s   ";H