ó
šßÈ[c           @` sN  d  Z  d d l m Z 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 m Z y& d d l Z d d	 l m Z e Z Wn e k
 r¯ e Z n Xe oÂ e e d
 ƒ Z d d d g Z i d g d 6Z d e
 f d „  ƒ  YZ d d „ Z e d d d ƒZ e d d d ƒZ d Z d e e _  d e e _  d S(   u¤  
Tabular models.

Tabular models of any dimension can be created using `tabular_model`.
For convenience `Tabular1D` and `Tabular2D` are provided.

Examples
--------
>>> table = np.array([[ 3.,  0.,  0.],
...                  [ 0.,  2.,  0.],
...                  [ 0.,  0.,  0.]])
>>> points = ([1, 2, 3], [1, 2, 3])
>>> t2 = Tabular2D(points, lookup_table=table, bounds_error=False, fill_value=None, method='nearest')

i    (   t   absolute_importt   unicode_literalst   divisiont   print_functionNi   (   t   Modeli   (   t
   minversion(   t   range(   t   interpnu   0.14u   tabular_modelu	   Tabular1Du	   Tabular2Du   scipyt   _Tabularc           B` sƒ   e  Z d  Z e Z e Z e Z d	 Z e j	 ƒ  Z
 e Z d Z d
 d
 d e e j d „ Z d „  Z d „  Z e d „  ƒ Z d „  Z RS(   uÜ  
    Returns an interpolated lookup table value.

    Parameters
    ----------
    points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, ), optional
        The points defining the regular grid in n dimensions.
    lookup_table : array-like, shape (m1, ..., mn, ...)
        The data on a regular grid in n dimensions.
    method : str, optional
        The method of interpolation to perform. Supported are "linear" and
        "nearest", and "splinef2d". "splinef2d" is only supported for
        2-dimensional data. Default is "linear".
    bounds_error : bool, optional
        If True, when interpolated values are requested outside of the
        domain of the input data, a ValueError is raised.
        If False, then ``fill_value`` is used.
    fill_value : float, optional
        If provided, the value to use for points outside of the
        interpolation domain. If None, values outside
        the domain are extrapolated.  Extrapolation is not supported by method
        "splinef2d".

    Returns
    -------
    value : ndarray
        Interpolated values at input coordinates.

    Raises
    ------
    ImportError
        Scipy is not installed.

    Notes
    -----
    Uses `scipy.interpolate.interpn`.

    u   yi    u   linearc         K` si  | j  d d ƒ } | d k r- t d ƒ ‚ n  t t |  ƒ j |   | d  k	 r t j | ƒ } |  j j	 | j	 k r‘ t
 d j |  j j	 ƒ ƒ ‚ n  | |  _ n  | d  k rË t d „  |  j j Dƒ ƒ |  _ n |  j j	 d k rü t | t ƒ rü | f |  _ n	 | |  _ t |  j ƒ |  j j	 k rJt
 d j |  j j	 t |  j ƒ ƒ ƒ ‚ n  | |  _ | |  _ | |  _ d  S(   Nu   n_modelsi   u   Only n_models=1 is supported.u3   lookup_table should be an array with {0} dimensionsc         s` s'   |  ] } t  j | d  t  j ƒVq d S(   t   dtypeN(   t   npt   aranget   float(   t   .0t   x(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pys	   <genexpr>n   s   u/   Expected grid points in {0} directions, got {1}(   t   gett   NotImplementedErrort   superR   t   __init__t   NoneR
   t   asarrayt   lookup_tablet   ndimt
   ValueErrort   formatt   tuplet   shapet   pointst
   isinstancet   lent   bounds_errort   methodt
   fill_value(   t   selfR   R   R   R   R    t   kwargst   n_models(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyR   _   s.    		"					c         C` s%   d j  |  j j |  j |  j ƒ } | S(   Nu#   <{0}(points={1}, lookup_table={2})>(   R   t	   __class__t   __name__R   R   (   R!   t   fmt(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyt   __repr__~   s    c         C` s¼   d |  j  j f d |  j f d |  j f d |  j f d d |  j f d |  j f d	 |  j f d
 |  j f d |  j	 f g
 } g  | D]* \ } } | d  k	 r d j | | ƒ ^ q } d j | ƒ S(   Nu   Modelu   Nameu   Inputsu   Outputsu
   Parametersu    u     pointsu     lookup_tableu     methodu     fill_valueu     bounds_erroru   {0}: {1}u   
(   u
   Parametersu    (   R$   R%   t   namet   inputst   outputsR   R   R   R    R   R   R   t   join(   R!   t   default_keywordst   keywordt   valuet   parts(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyt   __str__ƒ   s    $c         C` sd   g  |  j  D] } t | ƒ t | ƒ f ^ q
 d d d … } t | ƒ d k rZ | d } n  t | ƒ S(   uÖ  
        Tuple defining the default ``bounding_box`` limits,
        ``(points_low, points_high)``.

        Examples
        --------
        >>> from astropy.modeling.models import Tabular1D, Tabular2D
        >>> t1 = Tabular1D(points=[1,2,3], lookup_table=[10, 20, 30])
        >>> t1.bounding_box
        (1, 3)
        >>> t2 = Tabular2D(points=[[1,2,3],[2,3,4]], lookup_table=[[10,20,30],[20,30,40]])
        >>> t2.bounding_box
        ((2, 4), (1, 3))

        Niÿÿÿÿi   i    (   R   t   mint   maxR   R   (   R!   t   pt   bbox(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyt   bounding_box—   s    ;c      
   G` sÔ   | d j  } g  | |  j  D] } | j ƒ  ^ q } t j | ƒ j } t sZ t d ƒ ‚ n  t |  j	 |  j
 | d |  j d |  j d |  j ƒ} |  j d k r® | j | ƒ } n" g  | D] } | j | ƒ ^ qµ } | S(   u  
        Return the interpolated values at the input coordinates.

        Parameters
        ----------
        inputs : list of scalars or ndarrays
            Input coordinates. The number of inputs must be equal
            to the dimensions of the lookup table.
        i    u"   This model requires scipy >= v0.14R   R   R    i   (   R   t   n_inputst   flattenR
   t   arrayt   Tt	   has_scipyt   ImportErrorR   R   R   R   R   R    t	   n_outputst   reshape(   R!   R)   R   t   inpt   resultt   r(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyt   evaluate­   s    &"(   u   yN(   R%   t
   __module__t   __doc__t   Falset   lineart   fittablet   standard_broadcastingR*   t   abct   abstractpropertyR   t   Truet   _is_dynamict   _idR   R
   t   nanR   R'   R0   t   propertyR5   RA   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyR   +   s   &			c         C` s˜   t  j d g |  ƒ } t d „  t | j ƒ Dƒ ƒ } i | d 6| d 6} | d k r t j } t j d 7_ d j | ƒ } n  t	 t
 | ƒ t f | ƒ S(   ué  
    Make a ``Tabular`` model where ``n_inputs`` is
    based on the dimension of the lookup_table.

    This model has to be further initialized and when evaluated
    returns the interpolated values.

    Parameters
    ----------
    dim : int
        Dimensions of the lookup table.
    name : str
        Name for the class.

    Examples
    --------
    >>> table = np.array([[3., 0., 0.],
    ...                   [0., 2., 0.],
    ...                   [0., 0., 0.]])
    >>> tab = tabular_model(2, name='Tabular2D')
    >>> print(tab)
        <class 'abc.Tabular2D'>
        Name: Tabular2D
        Inputs: (u'x0', u'x1')
        Outputs: (u'y',)

    >>> points = ([1, 2, 3], [1, 2, 3])

    Setting fill_value to None, allows extrapolation.
    >>> m = tab(points, lookup_table=table, name='my_table', bounds_error=False, fill_value=None, method='nearest')

    >>> xinterp = [0, 1, 1.5, 2.72, 3.14]
    >>> m(xinterp, xinterp)  # doctest: +FLOAT_CMP
    array([3., 3., 3., 0., 0.])

    i   c         s` s   |  ] } d  j  | ƒ Vq d S(   u   x{0}N(   R   (   R   t   idx(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pys	   <genexpr>ï   s    u   lookup_tableu   inputsi   u
   Tabular{0}N(   R
   t   zerosR   R   R   R   R   RL   R   t   typet   str(   t   dimR(   t   tableR)   t   memberst   model_id(    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyt   tabular_modelÉ   s    %	R(   uŒ  
    method : str, optional
        The method of interpolation to perform. Supported are "linear" and
        "nearest", and "splinef2d". "splinef2d" is only supported for
        2-dimensional data. Default is "linear".
    bounds_error : bool, optional
        If True, when interpolated values are requested outside of the
        domain of the input data, a ValueError is raised.
        If False, then ``fill_value`` is used.
    fill_value : float, optional
        If provided, the value to use for points outside of the
        interpolation domain. If None, values outside
        the domain are extrapolated.  Extrapolation is not supported by method
        "splinef2d".

    Returns
    -------
    value : ndarray
        Interpolated values at input coordinates.

    Raises
    ------
    ImportError
        Scipy is not installed.

    Notes
    -----
    Uses `scipy.interpolate.interpn`.
u!  
    Tabular model in 1D.
    Returns an interpolated lookup table value.

    Parameters
    ----------
    points : array-like of float of ndim=1.
        The points defining the regular grid in n dimensions.
    lookup_table : array-like, of ndim=1.
        The data in one dimensions.
uQ  
    Tabular model in 2D.
    Returns an interpolated lookup table value.

    Parameters
    ----------
    points : tuple of ndarray of float, with shapes (m1, m2), optional
        The points defining the regular grid in n dimensions.
    lookup_table : array-like, shape (m1, m2)
        The data on a regular grid in 2 dimensions.

(   RC   t
   __future__R    R   R   R   RH   t   numpyR
   t   coreR   t   utilsR   t   extern.six.movesR   t   scipyt   scipy.interpolateR   RJ   R:   R;   RD   t   __all__t   __doctest_requires__R   R   RW   t	   Tabular1Dt	   Tabular2Dt	   _tab_docs(    (    (    s7   lib/python2.7/site-packages/astropy/modeling/tabular.pyt   <module>   s,   "

ž0