ó
šßÈ[c           @` sÿ   d  d l  m Z m Z m Z m Z d  d l Z d d l m Z m	 Z	 d d l
 m Z d g Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d „  Z d „  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z d „  Z d „  Z d S(   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   FittableModelt   custom_model(   t   rangeu   discretize_modelt   DiscretizationErrorc           B` s   e  Z d  Z RS(   u:   
    Called when discretization of models goes wrong.
    (   t   __name__t
   __module__t   __doc__(    (    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR      s   t   KernelSizeErrorc           B` s   e  Z d  Z RS(   u.   
    Called when size of kernels is even.
    (   R   R	   R
   (    (    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR      s   c         C` sØ   |  j  | j  k rh |  j ƒ  } |  j  d } t | | j  d | | j  d d ƒ } | | c | 7<| S| j  |  j  k rÐ | j ƒ  } | j  d } t | |  j  d | |  j  d d ƒ } | | c |  7<| S| |  S(   uw   
    Add two 1D kernel arrays of different size.

    The arrays are added with the centers lying upon each other.
    i   i   (   t   sizet   copyt   slice(   t   array_1t   array_2t	   new_arrayt   centert   slice_(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyt   add_kernel_arrays_1D   s    c         C` sœ  |  j  | j  k rÊ |  j ƒ  } g  |  j D] } | d ^ q( } t | d | j d d | d | j d d d ƒ } t | d | j d d | d | j d d d ƒ } | | | f c | 7<| S| j  |  j  k r”| j ƒ  } g  | j D] } | d ^ qò } t | d |  j d d | d |  j d d d ƒ } t | d |  j d d | d |  j d d d ƒ } | | | f c |  7<| S| |  S(   uw   
    Add two 2D kernel arrays of different size.

    The arrays are added with the centers lying upon each other.
    i   i   i    (   R   R   t   shapeR   (   R   R   R   t	   axes_sizeR   t   slice_xt   slice_y(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyt   add_kernel_arrays_2D0   s&          u   centeri
   c         C` s1  t  |  ƒ s t d ƒ ‚ n  t |  t ƒ s< t |  ƒ ƒ  }  n  |  j } | d k r` t d ƒ ‚ n  t t j	 | ƒ ƒ j
 ƒ  sŠ t d ƒ ‚ n  | r½ t t j	 | ƒ ƒ j
 ƒ  s½ t d ƒ ‚ q½ n  | d k rä | d k rä t d ƒ ‚ n  | d k r| d k	 rt d ƒ ‚ n  | d	 k rO| d k r0t |  | ƒ S| d k r-t |  | | ƒ SnÞ | d
 k r“| d k rtt |  | ƒ S| d k r-t |  | | ƒ Snš | d k rÝ| d k r»t |  | | ƒ S| d k r-t |  | | | ƒ SnP | d k r!| d k rt |  | ƒ S| d k r-t |  | | ƒ Sn t d ƒ ‚ d S(   uÊ
  
    Function to evaluate analytical model functions on a grid.

    So far the function can only deal with pixel coordinates.

    Parameters
    ----------
    model : `~astropy.modeling.FittableModel` or callable.
        Analytic model function to be discretized. Callables, which are not an
        instances of `~astropy.modeling.FittableModel` are passed to
        `~astropy.modeling.custom_model` and then evaluated.
    x_range : tuple
        x range in which the model is evaluated. The difference between the
        upper an lower limit must be a whole number, so that the output array
        size is well defined.
    y_range : tuple, optional
        y range in which the model is evaluated. The difference between the
        upper an lower limit must be a whole number, so that the output array
        size is well defined. Necessary only for 2D models.
    mode : str, optional
        One of the following 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.
                For 2D models interpolation is bilinear.
            * ``'oversample'``
                Discretize model by taking the average
                on an oversampled grid.
            * ``'integrate'``
                Discretize model by integrating the model
                over the bin using `scipy.integrate.quad`.
                Very slow.
    factor : float or int
        Factor of oversampling. Default = 10.

    Returns
    -------
    array : `numpy.array`
        Model value array

    Notes
    -----
    The ``oversample`` mode allows to conserve the integral on a subpixel
    scale. Here is the example of a normalized Gaussian1D:

    .. plot::
        :include-source:

        import matplotlib.pyplot as plt
        import numpy as np
        from astropy.modeling.models import Gaussian1D
        from astropy.convolution.utils import discretize_model
        gauss_1D = Gaussian1D(1 / (0.5 * np.sqrt(2 * np.pi)), 0, 0.5)
        y_center = discretize_model(gauss_1D, (-2, 3), mode='center')
        y_corner = discretize_model(gauss_1D, (-2, 3), mode='linear_interp')
        y_oversample = discretize_model(gauss_1D, (-2, 3), mode='oversample')
        plt.plot(y_center, label='center sum = {0:3f}'.format(y_center.sum()))
        plt.plot(y_corner, label='linear_interp sum = {0:3f}'.format(y_corner.sum()))
        plt.plot(y_oversample, label='oversample sum = {0:3f}'.format(y_oversample.sum()))
        plt.xlabel('pixels')
        plt.ylabel('value')
        plt.legend()
        plt.show()


    u   Model must be callable.i   u2   discretize_model only supports 1-d and 2-d models.uT   The difference between the upper an lower limit of 'x_range' must be a whole number.uT   The difference between the upper an lower limit of 'y_range' must be a whole number.u'   y range not specified, but model is 2-di   u)   y range specified, but model is only 1-d.u   centeru   linear_interpu
   oversampleu	   integrateu   Invalid mode.N(   t   callablet	   TypeErrort
   isinstanceR   R   t   n_inputst
   ValueErrort   floatt   npt   difft
   is_integert   Nonet   discretize_center_1Dt   discretize_center_2Dt   discretize_linear_1Dt   discretize_bilinear_2Dt   discretize_oversample_1Dt   discretize_oversample_2Dt   discretize_integrate_1Dt   discretize_integrate_2DR   (   t   modelt   x_ranget   y_ranget   modet   factort   ndim(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyt   discretize_modelK   sJ    F	c         C` s   t  j | Œ  } |  | ƒ S(   uH   
    Discretize model by taking the value at the center of the bin.
    (   R    t   arange(   R,   R-   t   x(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR$   ¾   s    c         C` sC   t  j | Œ  } t  j | Œ  } t  j | | ƒ \ } } |  | | ƒ S(   uJ   
    Discretize model by taking the value at the center of the pixel.
    (   R    R3   t   meshgrid(   R,   R-   R.   R4   t   y(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR%   Æ   s    c         C` sB   t  j | d d | d d ƒ } |  | ƒ } d | d | d  S(   u@   
    Discretize model by performing a linear interpolation.
    i    g      à?i   iÿÿÿÿ(   R    R3   (   R,   R-   R4   t   values_intermediate_grid(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR&   Ð   s    "c         C` sã   t  j | d d | d d ƒ } t  j | d d | d d ƒ } t  j | | ƒ \ } } |  | | ƒ } d | d d … d d … f | d d … d d … f } d | d d … d d … f | d d … d d … f } | S(   uB   
    Discretize model by performing a bilinear interpolation.
    i    g      à?i   Niÿÿÿÿ(   R    R3   R5   (   R,   R-   R.   R4   R6   R7   t   values(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR'   Ú   s    ""c         C` s€   t  j | d d d d | | d d d d | d | ƒ } |  | ƒ } t  j | | j | | f ƒ } | j d d ƒ d  S(   uH   
    Discretize model by taking the average on an oversampled grid.
    i    g      à?i   g      ð?t   axisiÿÿÿÿ(   R    R3   t   reshapeR   t   mean(   R,   R-   R0   R4   R8   (    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR(   í   s
    $c   
      C` s  t  j | d d d d | | d d d d | d | ƒ } t  j | d d d d | | d d d d | d | ƒ } t  j | | ƒ \ } } |  | | ƒ } | j | | | j | | f }	 t  j | |	 ƒ } | j d d ƒ j d d ƒ d d … d d … f S(	   uH   
    Discretize model by taking the average on an oversampled grid.
    i    g      à?i   g      ð?R9   i   Niÿÿÿÿ(   R    R3   R5   R   R:   R;   (
   R,   R-   R.   R0   R4   R6   t   x_gridt   y_gridR8   R   (    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR)   ü   s    $$ c      	   C` s‘   d d l  m } t j | d d | d d ƒ } t j g  ƒ } xI t | j d ƒ D]4 } t j | | |  | | | | d ƒ d ƒ } qU W| S(   uM   
    Discretize model by integrating numerically the model over the bin.
    i    (   t   quadg      à?i   (   t   scipy.integrateR>   R    R3   t   arrayR   R   t   append(   R,   R-   R>   R4   R8   t   i(    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR*     s    "2c      	   ` s
  d d l  m } t j | d d | d d ƒ } t j | d d | d d ƒ ‰ t j ˆ j d | j d f ƒ } xŒ t | j d ƒ D]w } xn t ˆ j d ƒ D]Y ‰  | ‡ f d †  | | | | d ‡  ‡ f d †  ‡  ‡ f d †  ƒ d | ˆ  | f <q¥ Wq‹ W| S(   uC   
    Discretize model by integrating the model over the pixel.
    i    (   t   dblquadg      à?i   c         ` s   ˆ  | |  ƒ S(   N(    (   R6   R4   (   R,   (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyt   <lambda>+  s    c         ` s   ˆ ˆ  S(   N(    (   R4   (   t   jR6   (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyRD   ,  s    c         ` s   ˆ ˆ  d S(   Ni   (    (   R4   (   RE   R6   (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyRD   ,  s    (   R?   RC   R    R3   t   emptyR   R   (   R,   R-   R.   RC   R4   R8   RB   (    (   RE   R,   R6   s8   lib/python2.7/site-packages/astropy/convolution/utils.pyR+     s    ""#!:(   t
   __future__R    R   R   R   t   numpyR    t   modeling.coreR   R   t   extern.six.movesR   t   __all__t	   ExceptionR   R   R   R   R#   R2   R$   R%   R&   R'   R(   R)   R*   R+   (    (    (    s8   lib/python2.7/site-packages/astropy/convolution/utils.pyt   <module>   s"   "			s		
	
		