ó
šßÈ[c           @` sŒ  d  Z  d d l m Z m Z m Z m Z d d l m Z m Z d d l	 Z
 d d l m Z d d l m Z m Z d d l m Z m Z d d	 l m Z d d
 l m Z d d l m Z d d d d d d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d  „  Z( d d d! „ Z* d" „  Z+ d d d# „ Z, d S($   u?   
This module provides utility functions for the models package
i    (   t   absolute_importt   unicode_literalst   divisiont   print_function(   t   dequet   MutableMappingNi   (   t   six(   t   ranget   zip(   t
   isiterablet   check_broadcast(   t   NUMPY_LT_1_14(   t	   signature(   t   unitsu   ExpressionTreeu	   AliasDictu   check_broadcastu   poly_map_domainu   combu   ellipse_extentt   ExpressionTreec           B` s‰   e  Z d  d d g Z d d d „ Z d „  Z d „  Z e d „  ƒ Z d „  Z	 d „  Z
 d	 „  Z d d
 d d „ Z d „  Z d d „ Z RS(   u   leftu   rightu   valuec         C` sF   | |  _  | |  _ | d  k	 r9 | | k r9 | j ƒ  } n  | |  _ d  S(   N(   t   valuet   leftt   Nonet   copyt   right(   t   selfR   R   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __init__   s
    		c         ` s   t  ‡  f d †  ˆ  j Dƒ ƒ S(   Nc         3` s$   |  ] } | t  ˆ  | ƒ f Vq d  S(   N(   t   getattr(   t   .0t   slot(   R   (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pys	   <genexpr>,   s    (   t   dictt	   __slots__(   R   (    (   R   s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __getstate__)   s    c         C` s1   x* | j  ƒ  D] \ } } t |  | | ƒ q Wd  S(   N(   t   itemst   setattr(   R   t   stateR   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __setstate__.   s    c         C` s   |  j  d  k o |  j d  k S(   N(   R   R   R   (   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   isleaf2   s    c         c` su   t  |  g ƒ } x_ | rp | j ƒ  } | V| j d  k	 rK | j | j ƒ n  | j d  k	 r | j | j ƒ q q Wd  S(   N(   R   t   popR   R   t   appendR   (   R   t   stackt   node(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   traverse_preorder6   s    	c         c` sk   t  ƒ  } |  } xU | s$ | d  k	 rf | d  k	 rI | j | ƒ | j } q | j ƒ  } | V| j } q Wd  S(   N(   R   R   R"   R   R!   R   (   R   R#   R$   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   traverse_inorderA   s    	c         c` sã   t  |  g ƒ } d  } xÇ | rÞ | d } | d  k sR | | j k sR | | j k r™ | j d  k	 rt | j | j ƒ qÕ | j d  k	 rÕ | j | j ƒ qÕ n< | j | k rÊ | j d  k	 rÊ | j | j ƒ n | j ƒ  V| } q Wd  S(   Niÿÿÿÿ(   R   R   R   R   R"   R!   (   R   R#   t   lastR$   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   traverse_postorderM   s    	
*i    c         C` sÊ  t  ƒ  } | d k r! d „  } n  | d k r6 d } n  d } x}|  j ƒ  D]o} | j r„ | j | | | j ƒ | f ƒ | d 7} qI | | j } t | ƒ d k  r© qI n  | j ƒ  }	 | j ƒ  }
 g  } xz |
 |	 f D]l } | d d k rú | j | ƒ qÔ | \ } } | | k rÔ | d k s*| | k  rÔ | j | | f ƒ qÔ qÔ Wt | ƒ d k r‰| \ }
 }	 | j | |
 d |	 d ƒ d f ƒ qI t | ƒ d k r«| j |
 ƒ qI | j | ƒ qI W| j ƒ  d S(   uF  Evaluate the expression represented by this tree.

        ``Operators`` should be a dictionary mapping operator names ('tensor',
        'product', etc.) to a function that implements that operator for the
        correct number of operands.

        If given, ``getter`` is a function evaluated on each *leaf* node's
        value before applying the operator between them.  This could be used,
        for example, to operate on an attribute of the node values rather than
        directly on the node values.  The ``getter`` is passed both the index
        of the leaf (a count starting at 0 that is incremented after each leaf
        is found) and the leaf node itself.

        The ``start`` and ``stop`` arguments allow evaluating a sub-expression
        within the expression tree.

        TODO: Document this better.
        c         S` s   | S(   N(    (   t   idxR   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   <lambda>t   s    i    i   i   iÿÿÿÿN(	   R   R   R(   R    R"   R   t   lenR!   t   extend(   R   t	   operatorst   gettert   startt   stopR#   t   leaf_idxR$   t   operatorR   R   t   operandst   operandR)   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   evaluate]   s:    			$'
c         C` sy   g  } xL |  j  |  j f D]8 } t | t ƒ rD | j | j ƒ  ƒ q | j | ƒ q W|  j |  j d | d d | d ƒS(   NR   i    R   i   (   R   R   t
   isinstanceR   R"   R   t	   __class__R   (   R   t   childrent   child(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyR   ±   s    c   	      C` sI  d } t  ƒ  } | d  k r' d „  } n  x|  j ƒ  D]} | j ri | j | | | ƒ ƒ | d 7} q4 n  | | j } | j ƒ  } | j ƒ  } | j d  k	 rÒ | j j rÒ | | j j | k  rÒ d j | ƒ } n  | j	 d  k	 r| j	 j r| | j	 j | k  rd j | ƒ } n  | j d j
 | | j | f ƒ ƒ q4 Wd j
 | ƒ S(   Ni    c         S` s   d j  |  ƒ S(   Nu   [{0}](   t   format(   t   it   l(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyR*   Ä   s    i   u   ({0})u    u    (   R   R   R(   R    R"   R   R!   R   R:   R   t   join(	   R   t   operator_precedencet   format_leafR1   R3   R$   t
   oper_orderR   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   format_expression¿   s(    		
&N(   t   __name__t
   __module__R   R   R   R   R   t   propertyR    R%   R&   R(   R5   R   RA   (    (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyR      s   					T	t	   AliasDictc           B` sS   e  Z d  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(   uA  
    Creates a `dict` like object that wraps an existing `dict` or other
    `MutableMapping`, along with a `dict` of *key aliases* that translate
    between specific keys in this dict to different keys in the underlying
    dict.

    In other words, keys that do not have an associated alias are accessed and
    stored like a normal `dict`.  However, a key that has an alias is accessed
    and stored to the "parent" dict via the alias.

    Parameters
    ----------
    parent : dict-like
        The parent `dict` that aliased keys and accessed from and stored to.

    aliases : dict-like
        Maps keys in this dict to their associated keys in the parent dict.

    Examples
    --------

    >>> parent = {'a': 1, 'b': 2, 'c': 3}
    >>> aliases = {'foo': 'a', 'bar': 'c'}
    >>> alias_dict = AliasDict(parent, aliases)
    >>> alias_dict['foo']
    1
    >>> alias_dict['bar']
    3

    Keys in the original parent dict are not visible if they were not
    aliased::

    >>> alias_dict['b']
    Traceback (most recent call last):
    ...
    KeyError: 'b'

    Likewise, updates to aliased keys are reflected back in the parent dict::

    >>> alias_dict['foo'] = 42
    >>> alias_dict['foo']
    42
    >>> parent['a']
    42

    However, updates/insertions to keys that are *not* aliased are not
    reflected in the parent dict::

    >>> alias_dict['qux'] = 99
    >>> alias_dict['qux']
    99
    >>> 'qux' in parent
    False

    In particular, updates on the `AliasDict` to a key that is equal to
    one of the aliased keys in the parent dict does *not* update the parent
    dict.  For example, ``alias_dict`` aliases ``'foo'`` to ``'a'``.  But
    assigning to a key ``'a'`` on the `AliasDict` does not impact the
    parent::

    >>> alias_dict['a'] = 'nope'
    >>> alias_dict['a']
    'nope'
    >>> parent['a']
    42
    c         C` s+   | |  _  |  j ƒ  |  _ t | ƒ |  _ d  S(   N(   t   _parentt   _store_typet   _storeR   t   _aliases(   R   t   parentt   aliases(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyR   '  s    	c         C` sS   | |  j  k rH y |  j |  j  | SWqH t k
 rD t | ƒ ‚ qH Xn  |  j | S(   N(   RI   RF   t   KeyErrorRH   (   R   t   key(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __getitem__,  s    c         C` s7   | |  j  k r& | |  j |  j  | <n | |  j | <d  S(   N(   RI   RF   RH   (   R   RM   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __setitem__5  s    c         C` sU   | |  j  k rG y |  j |  j  | =WqQ t k
 rC t | ƒ ‚ qQ Xn
 |  j | =d  S(   N(   RI   RF   RL   RH   (   R   RM   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __delitem__;  s    c         c` sW   x7 t  j |  j ƒ D]# \ } } | |  j k r | Vq q Wx |  j D] } | VqD Wd S(   u¡   
        First iterates over keys from the parent dict (if the aliased keys are
        present in the parent), followed by any keys in the local store.
        N(   R   t	   iteritemsRI   RF   RH   (   R   RM   t   alias(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __iter__D  s
    c         C` s   t  t t |  ƒ ƒ ƒ S(   N(   R+   t   listt   iter(   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __len__Q  s    c         C` sl   |  j  ƒ  } xC t j |  j ƒ D]/ \ } } | |  j k r |  j | | | <q q W| j |  j ƒ t | ƒ S(   N(   RG   R   RQ   RI   RF   t   updateRH   t   repr(   R   t
   store_copyRM   RR   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __repr__W  s    (   RB   RC   t   __doc__R   RG   R   RN   RO   RP   RS   RV   RZ   (    (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyRE   Ü   s   B								t   _BoundingBoxc           B` s8   e  Z d  Z d Z d d „ Z d „  Z e d „  ƒ Z RS(   u;  
    Base class for models with custom bounding box templates (methods that
    return an actual bounding box tuple given some adjustable parameters--see
    for example `~astropy.modeling.models.Gaussian1D.bounding_box`).

    On these classes the ``bounding_box`` property still returns a `tuple`
    giving the default bounding box for that instance of the model.  But that
    tuple may also be a subclass of this class that is callable, and allows
    a new tuple to be returned using a user-supplied value for any adjustable
    parameters to the bounding box.
    c         C` s7   t  t |  ƒ j |  | ƒ } | d  k	 r3 | | _ n  | S(   N(   t   superR\   t   __new__R   t   _model(   t   clst   input_R_   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyR^   r  s    c         O` s   t  d ƒ ‚ d  S(   NuP   This bounding box is fixed by the model and does not have adjustable parameters.(   t   NotImplementedError(   R   t   argst   kwargs(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   __call__{  s    c         C` sò   | j  } | d k r” t | ƒ s= t j | ƒ d d	 f k rX t d j | j ƒ ƒ ‚ n  t | ƒ d k r |  t | d ƒ f ƒ S|  t | ƒ ƒ SnZ t | ƒ s¼ t j | ƒ | d f k rÚ t d j | j | ƒ ƒ ‚ n  |  d „  | Dƒ ƒ Sd S(
   u%  
        Validate a given bounding box sequence against the given model (which
        may be either a subclass of `~astropy.modeling.Model` or an instance
        thereof, so long as the ``.inputs`` attribute is defined.

        Currently this just checks that the bounding_box is either a 2-tuple
        of lower and upper bounds for 1-D models, or an N-tuple of 2-tuples
        for N-D models.

        This also returns a normalized version of the bounding_box input to
        ensure it is always an N-tuple (even for the 1-D case).
        i   i   u   Bounding box for {0} model must be a sequence of length 2 consisting of a lower and upper bound, or a 1-tuple containing such a sequence as its sole element.i    u·   Bounding box for {0} model must be a sequence of length {1} (the number of model inputs) consisting of pairs of lower and upper bounds for those inputs on which to evaluate the model.c         s` s   |  ] } t  | ƒ Vq d  S(   N(   t   tuple(   R   t   bounds(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pys	   <genexpr>§  s    N(   i   (   i   i   (	   t   n_inputsR	   t   npt   shapet
   ValueErrorR:   t   nameR+   Rf   (   R`   t   modelt   bounding_boxt   nd(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   validate€  s     	N(	   RB   RC   R[   R   R_   R^   Re   t   classmethodRp   (    (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyR\   c  s
   		c         ` s   ‡  ‡ ‡ f d †  S(   u  
    Given a binary operator (as a callable of two arguments) ``oper`` and
    two callables ``f`` and ``g`` which accept the same arguments,
    returns a *new* function that takes the same arguments as ``f`` and ``g``,
    but passes the outputs of ``f`` and ``g`` in the given ``oper``.

    ``f`` and ``g`` are assumed to return tuples (which may be 1-tuples).  The
    given operator is applied element-wise to tuple outputs).

    Example
    -------

    >>> from operator import add
    >>> def prod(x, y):
    ...     return (x * y,)
    ...
    >>> sum_of_prod = make_binary_operator_eval(add, prod, prod)
    >>> sum_of_prod(3, 5)
    (30,)
    c         ` s5   t  ‡ f d †  t ˆ  |  | ƒ ˆ |  | ƒ ƒ Dƒ ƒ S(   Nc         3` s$   |  ] \ } } ˆ  | | ƒ Vq d  S(   N(    (   R   t   xt   y(   t   oper(    s5   lib/python2.7/site-packages/astropy/modeling/utils.pys	   <genexpr>Á  s    (   Rf   R   (   t   inputst   params(   t   ft   gRt   (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyR*   À  s    (    (   Rt   Rw   Rx   (    (   Rw   Rx   Rt   s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   make_binary_operator_evalª  s    c         C` s   t  j | d t  j ƒ} t  j | d t  j ƒ} | d | d | d | d } | d | d | d | d | d | d } | | |  S(   u  
    Map domain into window by shifting and scaling.

    Parameters
    ----------
    oldx : array
          original coordinates
    domain : list or tuple of length 2
          function domain
    window : list or tuple of length 2
          range into which to map the domain
    t   dtypei   i    (   Ri   t   arrayt   float64(   t   oldxt   domaint   windowt   sclt   off(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   poly_map_domainÅ  s
    "2c         C` sl   | |  k s$ |  d k  s$ | d k  r( d Sd } x7 t  t | |  | ƒ ƒ D] } | |  | | d } qH W| S(   uÅ   
    The number of combinations of N things taken k at a time.

    Parameters
    ----------
    N : int, array
        Number of things.
    k : int, array
        Number of elements taken.

    i    i   (   R   t   min(   t   Nt   kt   valt   j(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   combÙ  s    $ c         C` sJ   t  r d n d } t j |  d | d t ƒ} d j d „  | j ƒ  Dƒ ƒ S(   uR   
    Represents a multi-dimensional Numpy array flattened onto a single line.
    u   ,u   , t	   separatort   suppress_smallu    c         s` s   |  ] } | j  ƒ  Vq d  S(   N(   t   strip(   R   R<   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pys	   <genexpr>ó  s    (   R   Ri   t   array2stringt   TrueR=   t
   splitlines(   R{   t   sept   r(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   array_repr_onelineí  s    c         C` sL   t  |  ƒ j | ƒ rD t d „  |  Dƒ ƒ }  t d „  | Dƒ ƒ } n  |  | S(   uR  
    For use with the join operator &: Combine left input/output labels with
    right input/output labels.

    If none of the labels conflict then this just returns a sum of tuples.
    However if *any* of the labels conflict, this appends '0' to the left-hand
    labels and '1' to the right-hand labels so there is no ambiguity).
    c         s` s   |  ] } | d  Vq d S(   u   0N(    (   R   R<   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pys	   <genexpr>  s    c         s` s   |  ] } | d  Vq d S(   u   1N(    (   R   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pys	   <genexpr>  s    (   t   sett   intersectionRf   (   R   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   combine_labelsö  s    
c         C` s  t  j | t  j | ƒ |  ƒ } |  t  j | ƒ t  j | ƒ | t  j | ƒ t  j | ƒ } t  j | |  t  j | ƒ ƒ } | t  j | ƒ t  j | ƒ |  t  j | ƒ t  j | ƒ } t | t j ƒ sß t | t j ƒ rû t  j t j | | g ƒ ƒ St  j | | g ƒ Sd S(   u  
    Calculates the extent of a box encapsulating a rotated 2D ellipse.

    Parameters
    ----------
    a : float or `~astropy.units.Quantity`
        Major axis.
    b : float or `~astropy.units.Quantity`
        Minor axis.
    theta : float or `~astropy.units.Quantity`
        Rotation angle. If given as a floating-point value, it is assumed to be
        in radians.

    Returns
    -------
    offsets : tuple
        The absolute value of the offset distances from the ellipse center that
        define its bounding box region, ``(dx, dy)``.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt
        from astropy.modeling.models import Ellipse2D
        from astropy.modeling.utils import ellipse_extent, render_model

        amplitude = 1
        x0 = 50
        y0 = 50
        a = 30
        b = 10
        theta = np.pi/4

        model = Ellipse2D(amplitude, x0, y0, a, b, theta)

        dx, dy = ellipse_extent(a, b, theta)

        limits = [x0 - dx, x0 + dx, y0 - dy, y0 + dy]

        model.bounding_box = limits

        image = render_model(model)

        plt.imshow(image, cmap='binary', interpolation='nearest', alpha=.5,
                  extent = limits)
        plt.show()
    N(	   Ri   t   arctan2t   tant   cost   sinR6   t   ut   Quantityt   abs(   t   at   bt   thetat   tt   dxt   dy(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   ellipse_extent  s    4 >>$c         C` s•   t  |  ƒ } g  } g  } xp | j j ƒ  D]_ } | j | j | j f k rX t d ƒ ‚ n  | j | j k rz | j	 | ƒ q( | j	 | ƒ q( W| | f S(   uò   
    Given a callable, determine the input variables and the
    parameters.

    Parameters
    ----------
    func : callable

    Returns
    -------
    inputs, params : tuple
        Each entry is a list of inspect.Parameter objects
    u)   Signature must not have *args or **kwargs(
   R   t
   parameterst   valuest   kindt   VAR_POSITIONALt   VAR_KEYWORDRk   t   defaultt   emptyR"   (   t   funct   sigRu   Rv   t   param(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   get_inputs_and_paramsG  s    c         C` s.   |  j  d  k r |  j | S|  j j | ƒ Sd  S(   N(   t   unitR   R   t   quantityt   to(   t	   parameterR®   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   _parameter_with_unitd  s    c         C` s%   | d  k r |  S|  | j | ƒ Sd  S(   N(   R   R°   (   R   t   old_unitt   new_unit(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   _parameter_without_unitk  s    c         C` s…   i  } xx |  D]p } g  | | <| d  k	 rM | | k rM | | j | | ƒ n  | d  k	 r | | k r | | j | | ƒ q q W| S(   N(   R   R,   (   t   keyst   eq1t   eq2t   eqRM   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   _combine_equivalency_dictr  s    
c         C` s3   t  |  t j ƒ r" |  j t j ƒ St j |  ƒ Sd S(   u    Convert ``value`` to radian. N(   R6   R™   Rš   R°   t   radRi   t   deg2rad(   R   (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt
   _to_radian€  s    c         C` s.   | d k	 r |  | j | ƒ St j |  ƒ Sd S(   u3    Convert value with ``raw_unit`` to ``orig_unit``. N(   R   R°   Ri   t   rad2deg(   R   t   raw_unitt	   orig_unit(    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   _to_orig_unitˆ  s    (-   R[   t
   __future__R    R   R   R   t   collectionsR   R   t   numpyRi   t   externR   t   extern.six.movesR   R   t   utilsR	   R
   t   utils.compatR   t   utils.compat.funcsigsR   t    R   R™   t   __all__t   objectR   RE   Rf   R\   Ry   R‚   Rˆ   R‘   R”   R¢   R­   R²   Rµ   R   Rº   R½   RÁ   (    (    (    s5   lib/python2.7/site-packages/astropy/modeling/utils.pyt   <module>   s4   "	Á‡G							@				