
    IR-e0                         d Z ddlZddlmZ ddlmZ 	 ddlm	Z	 dZ
n# e$ r dZ
Y nw xY wg d	Zd
dgiZ G d de          ZddZ edd          Z edd          ZdZdez   e_         dez   e_         dS )a  
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')

    N)units   )Model)interpnTF)tabular_model	Tabular1D	Tabular2Dr   scipyc                        e Zd ZdZdZdZdZdZdZdddde	j
        f fd	Zd Zd	 Zed
             Zed             Zed             Zd Zed             Z xZS )_Tabulara  
    Returns an interpolated lookup table value.

    Parameters
    ----------
    points : tuple of ndarray of float, optional
        The points defining the regular grid in n dimensions.
        ndarray must have shapes (m1, ), ..., (mn, ),
    lookup_table : array-like
        The data on a regular grid in n dimensions.
        Must have shapes (m1, ..., mn, ...)
    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 or `~astropy.units.Quantity`, 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". If Quantity is given, it will be converted to the unit of
        ``lookup_table``, if applicable.

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

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

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

    FTr   Nlinearc                 2   |                     dd          }|dk    rt          d           t                      j        di | d| _        |t          d          t          |t          j                  st          j
        |          }| j        j        |j        k    rt          d| j        j         d          |t          d |j        D                       }n|j        dk    rt          |t                    s|f}t          |          }||j        k    rt          d	|j         d
| d          |dk    rLt          |d         t          j                  r,t          d |D                       dk    rt          d          t          |t          j                  rQt          |t          j                  st          d|j         d          |                    |j                  j        }|| _        || _        || _        || _        || _        d S )Nn_modelsr   zOnly n_models=1 is supported.)yzMust provide a lookup table.z%lookup_table should be an array with z dimensions.c              3   L   K   | ]}t          j        |t                     V   dS ))dtypeN)nparangefloat).0xs     8lib/python3.11/site-packages/astropy/modeling/tabular.py	<genexpr>z$_Tabular.__init__.<locals>.<genexpr>v   s1      QQ29Qe444QQQQQQ    zExpected grid points in z directions, got .r   c                 0    h | ]}t          |d d          S )unitN)getattrr   ps     r   	<setcomp>z$_Tabular.__init__.<locals>.<setcomp>   s$    BBBaFD11BBBr   z#points must all have the same unit.zfill value is in z but expected to be unitless. )getNotImplementedErrorsuper__init__outputs
ValueError
isinstanceuQuantityr   asarraylookup_tablendimtupleshapelenr   tovaluepointsbounds_errormethod
fill_value)
selfr4   r-   r6   r5   r7   kwargsr   npts	__class__s
            r   r&   z_Tabular.__init__[   sT    ::j!,,a<<%&EFFF""6""";<<<,
33 	4:l33L!\%6668$)8 8 8  
 >QQl>PQQQQQFF A%%j.G.G% v;;D|((( C#(C C;?C C C  
 qvay!*55 BB6BBBCCaGG !FGGGj!*-- 	@lAJ77  V
VVV   $|'899?J(($r   c                 B    d| j         j         d| j         d| j         dS )N<z(points=z, lookup_table=z)>)r;   __name__r4   r-   r8   s    r   __repr__z_Tabular.__repr__   s@    2' 2 2 2 2 -2 2 2	
r   c                     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
}d |D             }d
                    |          S )Nr   NameN_inputs	N_outputs)
Parameters z  pointsz  lookup_tablez  methodz  fill_valuez  bounds_errorc                 &    g | ]\  }}|| d| S )Nz: r"   )r   keywordr3   s      r   
<listcomp>z$_Tabular.__str__.<locals>.<listcomp>   s:     
 
 
  !!%!!   r   
)r;   r>   namen_inputs	n_outputsr4   r-   r6   r7   r5   join)r8   default_keywordspartss      r   __str__z_Tabular.__str__   s    dn-.TY'$.)%t01%T_-t01

 
"2
 
 
 yyr   c                 |    | j         d         t          t          j                  sd S fd| j        D             S )Nr   c                      i | ]
}|j         S r"   )r   )r   r   ptss     r   
<dictcomp>z(_Tabular.input_units.<locals>.<dictcomp>   s    11138111r   )r4   r)   r*   r+   inputs)r8   rT   s    @r   input_unitsz_Tabular.input_units   sC    k!n#qz** 	41111T[1111r   c                 v    t          | j        t          j                  sd S | j        d         | j        j        iS )Nr   )r)   r-   r*   r+   r'   r   r?   s    r   return_unitsz_Tabular.return_units   s5    $+QZ88 	4Q!2!788r   c                 p    d | j         D             ddd         }t          |          dk    r|d         }|S )ay  
        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
        ModelBoundingBox(
            intervals={
                x: Interval(lower=1, upper=3)
            }
            model=Tabular1D(inputs=('x',))
            order='C'
        )
        >>> t2 = Tabular2D(points=[[1, 2, 3], [2, 3, 4]],
        ...                lookup_table=[[10, 20, 30], [20, 30, 40]])
        >>> t2.bounding_box
        ModelBoundingBox(
            intervals={
                x: Interval(lower=1, upper=3)
                y: Interval(lower=2, upper=4)
            }
            model=Tabular2D(inputs=('x', 'y'))
            order='C'
        )

        c                 J    g | ] }t          |          t          |          f!S r"   )minmaxr   s     r   rI   z)_Tabular.bounding_box.<locals>.<listcomp>   s)    666QQQ 666r   Nr   r   )r4   r1   )r8   bboxs     r   bounding_boxz_Tabular.bounding_box   sA    > 76$+666ttt<t99>>7Dr   c                 .   t          j        | }|d         j        d |d| j                 D             }t          j        |          j        }t          st          d          t          | j	        | j
        || j        | j        | j                  }t          | j
        t          j                  r4t          | j	        d         t          j                  s|| j
        j        z  }| j        dk    r|                              }nfd|D             }|S )a  
        Return the interpolated values at the input coordinates.

        Parameters
        ----------
        inputs : list of scalar or list of ndarray
            Input coordinates. The number of inputs must be equal
            to the dimensions of the lookup table.
        r   c                 6    g | ]}|                                 S r"   )flatten)r   inps     r   rI   z%_Tabular.evaluate.<locals>.<listcomp>   s     CCCC#++--CCCr   NzTabular model requires scipy.)r6   r5   r7   r   c                 :    g | ]}|                               S r"   )reshape)r   rr0   s     r   rI   z%_Tabular.evaluate.<locals>.<listcomp>  s%    7771aii&&777r   )r   broadcast_arraysr0   rL   arrayT	has_scipyImportErrorr   r4   r-   r6   r5   r7   r)   r*   r+   r   rM   rf   )r8   rV   resultr0   s      @r   evaluatez_Tabular.evaluate   s    $f-q	CC6/DM/+BCCC&!!# 	?=>>>K;*
 
 
 d'44 	5ZKNAJ>
 >
 	5 d/44F>Q^^E**FF7777777Fr   c                    | j         dk    rt          j        t          j        | j                  dk              r| j        }| j        d         }n]t          j        t          j        | j                  dk               r'| j        d d d         }| j        d         d d d         }nt          t          ||| j        | j	        | j
                  S t          d          )Nr   r   r^   )r4   r-   r6   r5   r7   zHAn analytical inverse transform has not been implemented for this model.)rL   r   alldiffr-   r4   r$   r   r6   r5   r7   )r8   r4   r-   s      r   inversez_Tabular.inverse  s    =A vbgd/001455 
**#{1~ 122Q677 **44R40#{1~ddd3 *)){!.?    "V
 
 	
r   )r>   
__module____qualname____doc__r   fittablestandard_broadcasting_is_dynamic_idr   nanr&   r@   rQ   propertyrW   rY   r`   rn   rr   __classcell__)r;   s   @r   r   r   '   s       ( (T FH!K
C 67% 7% 7% 7% 7% 7%r
 
 
     , 2 2 X2 9 9 X9
 ! ! X!F$ $ $L 
 
 X
 
 
 
 
r   r   c                 4   | dk     rt          d          t          j        dg| z            }|| dd}| dk    rd|d<   nd|d<   |&t          j        }t          xj        dz  c_        d	| }t          t          |          t          f|          }d
|_        |S )a  
    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 'astropy.modeling.tabular.Tabular2D'>
    Name: Tabular2D
    N_inputs: 2
    N_outputs: 1

    >>> 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.])

    r   z.Lookup table must have at least one dimension.   )r-   rL   rM   T
_separableFNTabularzastropy.modeling.tabular)r(   r   zerosr   ry   typestrrs   )dimrK   tablemembersmodel_idmodel_classs         r   r   r   $  s    N QwwIJJJHaS3YE$#AFFG
axx $ %|<###s4yy8+w77K7Kr   r   )rK   r~   r	   a  
    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`.
a!  
    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.
ai  
    Tabular model in 2D.
    Returns an interpolated lookup table value.

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

)N)ru   numpyr   astropyr   r*   corer   scipy.interpolater   rk   rl   __all____doctest_requires__r   r   r   r	   	_tab_docsr"   r   r   <module>r      sJ   $                ))))))II   III 6
5
5''3 z
 z
 z
 z
 z
u z
 z
 z
z9 9 9 9x M!+...	M!+...		>
  
    
   s    ''