
[c           @` s  d  Z  d d l m Z m Z m Z m Z y d d l Z Wn e k
 rW d d l Z n Xd d l	 Z	 d d l
 Z d d l m Z 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 l m Z d d l m Z y& d d l Z d d l m Z e  Z! Wn e k
 rAe" Z! n Xe! oTe e d  Z# e	 j$ j% d  d d d   Z& d   Z' d   Z( d   Z) d e* f d     YZ+ d e* f d     YZ, e  d d  Z- e	 j$ j. d/ e/ e j0   d d     d! e, f d"     Y Z1 e	 j$ j. d0 e/ e j0   d d#    d$ e+ f d%     Y Z2 d&   Z3 d'   Z4 d(   Z5 d)   Z6 e	 j$ j% d*  d+    Z7 e	 j$ j% d*  d,    Z8 e	 j$ j% d*  d-    Z9 d.   Z: d S(1   uU   
Tests for model evaluation.
Compare the results of some models with other programs.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   assert_allcloset   assert_equali   (   t	   models_1Dt	   models_2Di   (   t   fittingt   models(   t   FittableModel(   t   PolynomialBasei   (   t
   minversion(   t   zip(   t   NumpyRNGContext(   t   optimizeu   0.14u   not HAS_SCIPYi   c         C` s  d d d  } d d d  } t  j | d | } t j d d d  } |   } | j | d d	  } | j | d d	  } t j j d  | |  t j j t	 |   d
 }	 t
 j   }
 |
 | | |	  } t j t j | j j | j j g  t j |  | g  d k   st  d  S(   Ni   i   c         S` s    | t  j d t  j | |   S(   u    
        Model function
        i   (   t   npt   sint   pi(   t   xt	   amplitudet	   frequency(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt
   sine_model+   s    c         S` sb   t  j d t  j | |   } d t  j |  | t  j d t  j | |   } t  j | | f  S(   uv   
        Jacobian of model function, e.g. derivative of the function with
        respect to the *parameters*
        i   (   R   R   R   t   cost   vstack(   R   R   R   t   dat   df(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt
   sine_deriv1   s    1t	   fit_derivi    i2   g      @g       @g      ?gMbP?(   R	   t   custom_modelR   t   linspacet   evaluateR   t   randomt   seedt   randt   lenR   t   LevMarLSQFittert   allt   arrayR   t   valueR   t   AssertionError(   R   R   R   R   t	   SineModelR   t	   sin_modelt   yt   y_primet   datat   fittert   model(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_custom_model(   s    		&$c          C` s[   t  j d d d   }  |  d d d d  } | j d k sB t  | j d k sW t  d  S(   Ni   i   c         S` s    | t  j d t  j | |   S(   u   Model functioni   (   R   R   R   (   R   R   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR)   K   s    R   g       @R   g      ?(   R	   R   R   R(   R   (   R)   R*   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_custom_model_initJ   s
    c          C` s   t  j d d d   }  |    } |  j j d k s9 t  |  j j d k sQ t  | j d k sf t  | j d k s{ t  d  S(   Ni   i   c         S` s    | t  j d t  j | |   S(   u   Model functioni   (   R   R   R   (   R   R   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR)   W   s    (   R	   R   R   t   defaultR(   R   (   R)   R*   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_custom_model_defaultsV   s    	c          C` s  d d d d d d d d  }  d	 t  j |   f d
     Y} |   } | j } | \ } } } t j |  d \ } } }	 t j t | d | d d  t | d | d d  t | d | d d  f \ }
 } } t j t | d | | d | d  t | d | | d | d  t | d |	 | d |	 d  f \ } } } | | | |  } | | | |
  } t | j   | j    | j   d k  st	  d S(   u+   Test bounding box evaluation for a 3D modeli   i
   i   i   i   i   i   c
         S` sF   |  | | d | | | d | | | d }
 |
 d k  |	 } | S(   Ni   i   (    (   R   R+   t   zt   x0t   y0t   z0t   at   bt   ct   ampt   rsqt   val(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt	   ellipsoidh   s    2t   Ellipsoid3Dc           B` s   e  Z e d     Z RS(   c         S` s[   |  j  |  j |  j  |  j f |  j |  j |  j |  j f |  j |  j |  j |  j f f S(   N(   R7   R:   R6   R9   R5   R8   (   t   self(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   bounding_boxn   s    (   t   __name__t
   __module__t   propertyRA   (    (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR?   m   s   i    gHz>N(
   R	   R   RA   R   t   difft   mgridt   slicet   abst   sumR(   (   R>   R?   R/   t   bboxt   zlimt   ylimt   xlimt   dzt   dyt   dxt   z1t   y1t   x1t   z2t   y2t   x2t   arrt   sub_arr(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_custom_model_bounding_boxe   s    		(& 0t   Fittable2DModelTesterc           B` sh   e  Z d  Z d   Z d   Z d   Z d   Z e j j	 d  d    Z
 e j j	 d  d    Z RS(   u  
    Test class for all two dimensional parametric models.

    Test values have to be defined in example_models.py. It currently test the
    model with different input types, evaluates the model at different
    positions and assures that it gives the correct values. And tests if the
    model works with non-linear fitters.

    This can be used as a base class for user defined model testing.
    c         C` s   d |  _  d |  _ d |  _ d |  _ d |  _ d |  _ t j d d d  |  _ t j d d d  |  _	 t j
 d  d  d  d  f \ |  _ |  _ d  S(	   Nid   g-C6?g?g333333@g@i   i
   i   (   t   Nt   Mt
   eval_errort	   fit_errorR   R+   R   t   arangeRS   RR   RF   RU   RV   (   R@   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   setup_class   s    						c         C` sL   t  | |  } | |  j |  j  | |  j |  j  | |  j |  j  d S(   u&   Test model with different input types.N(   t   create_modelR   R+   RS   RR   RV   RU   (   R@   t   model_classt   test_parametersR/   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_input2D   s    c         C` se   t  | |  } | d } | d } | d } t j t j | | |  |  |  j k   sa t  d S(   u*   Test model values add certain given pointsu   x_valuesu   y_valuesu   z_valuesN(   Ra   R   R%   RH   R]   R(   (   R@   Rb   Rc   R/   R   R+   R4   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_eval2D   s
    


c         C` s  t  | |  } d	 d
 f | _ | j d d f k s9 t  d | _ t j t   | j Wd QXt j t   d | _ Wd QX| ` y | j } Wn t k
 r t j d  n X| \ } } t	 j
 |  d \ } } t	 j t | d | d d  t | d | d d  f \ }	 }
 t	 j t | d | | d | d  t | d | | d | d  f \ } } | | |  } | |
 |	  } t | j   | j    | j   d k  st  d S(   u   Test bounding box evaluationii   Nu&   Bounding_box is not defined for model.i   i    i   gHz>(   ii   (   ii   (   ii   (   ii   (   ii   (   Ra   RA   R(   t   Nonet   pytestt   raisest   NotImplementedErrort
   ValueErrort   skipR   RE   RF   RG   RH   RI   (   R@   Rb   Rc   R/   RJ   RL   RM   RO   RP   RR   RS   RU   RV   RW   RX   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_bounding_box2D   s,    	%&-u   not HAS_SCIPYc         C` sB  | d } | d } | d } t  | |  } t | t  r_ g  | j D] } | | ^ qF } n  d | k r | d r t j | d | d |  j  } t j | d | d |  j  }	 q n@ t j | d | d |  j  } t j | d | d |  j  }	 t j | |	  \ }
 } t j	 j
 d  t j	 j |  j |  j  d } | |
 |  d | d | } t j   } | | |
 | |  } g  | j D] } t | |  ^ q} g  | D] } | j ^ q} t j g  t | |  D] \ } } | s| ^ q } t j g  | D] } | j s| j ^ q } t | | d	 |  j d
 S(   u3   Test if the parametric model works with the fitter.u   x_limu   y_limu
   parametersu   log_fiti    i   g      ?g?t   atolN(   Ra   t
   isinstancet   dictt   param_namesR   t   logspaceR[   R   t   meshgridR    R!   R"   R   R$   t   getattrt   fixedR&   R   R'   R   R^   (   R@   Rb   Rc   t   x_limt   y_limt
   parametersR/   t   nameR   R+   t   xvt   yvt   noiseR-   R.   t	   new_modelt   paramst   paramRt   R=   t   expectedt   fitted(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_fitter2D   s4    


#
 &  %"c         C` sj  | d } | d } | j  d k r3 t j d  n  t | t  rR t j d  n  d | k r | d r t j | d | d |  j  } t j | d | d |  j	  } q n@ t j
 | d | d |  j  } t j
 | d | d |  j	  } t j | |  \ } } yU t | | d t d	 d
 }	 t | | d t d	 d
 }
 t | | d t d	 d
 } WnP t k
 rt | | d t }	 t | | d t }
 t | | d t } n Xt j j d  } | d d } d | | j |  j	 |  j  d } | | |  | } t j   } | |	 | | |  } t j   } | |
 | | | d t } t | j | j d d d S(   up   
        Test the derivative of a model by fitting with an estimated and
        analytical derivative.
        u   x_limu   y_limu-   Derivative function is not defined for model.u'   Skip testing derivative of polynomials.u   log_fiti    i   t   use_constraintst   parameter_keyu   deriv_initialiIu
   parametersg?g      ?t   estimate_jacobiant   rtolN(   R   Rf   Rg   Rk   t
   issubclassR   R   Rq   R[   R\   R   Rr   Ra   t   Falset   KeyErrorR    t   RandomStateR"   R   R$   t   TrueR   Rw   (   R@   Rb   Rc   Ru   Rv   R   R+   Ry   Rz   t   model_with_derivt   model_no_derivR/   t   rsnR   t   nR-   t   fitter_with_derivt   new_model_with_derivt   fitter_no_derivt   new_model_no_deriv(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_deriv_2D   sV    


 &  				$				(   RB   RC   t   __doc__R`   Rd   Re   Rl   Rg   t   markt   skipifR   R   (    (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyRZ      s   
					%&t   Fittable1DModelTesterc           B` sh   e  Z d  Z d   Z d   Z d   Z d   Z e j j	 d  d    Z
 e j j	 d  d    Z RS(   u  
    Test class for all one dimensional parametric models.

    Test values have to be defined in example_models.py. It currently test the
    model with different input types, evaluates the model at different
    positions and assures that it gives the correct values. And tests if the
    model works with non-linear fitters.

    This can be used as a base class for user defined model testing.
    c         C` s   d |  _  d |  _ d |  _ d |  _ d |  _ d |  _ t j d d d  |  _ t j d d d  |  _	 t j
 d  d  d  d  f \ |  _ |  _ d  S(	   Nid   g-C6?g?g333333@g@i   i
   i   (   R[   R\   R]   R^   R   R+   R   R_   RS   RR   RF   RU   RV   (   R@   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR`   A  s    						c         C` s:   t  | |  } | |  j  | |  j  | |  j  d S(   u&   Test model with different input types.N(   Ra   R   RS   RV   (   R@   Rb   Rc   R/   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_input1DL  s    c         C` sC   t  | |  } | d } | d } t | |  | d |  j d S(   u;   
        Test model values at certain given points
        u   x_valuesu   y_valuesRm   N(   Ra   R   R]   (   R@   Rb   Rc   R/   R   R+   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_eval1DT  s    

c         C` sq  t  | |  } d | _ d | _ t j t   | j Wd QX| ` t j t   d | _ Wd QXy | j } Wn t k
 r t j d  n Xt | t	 j
  r d } d } n d } d } t j |  d	 } t j t | d
 | d d |  } t j t | d
 | | d | d |  }	 | |	  }
 | |  } t |
 j   | j    |
 j   | k  smt  d S(   u   Test bounding box evaluationii   Nu&   Bounding_box is not defined for model.g{Gz?g?gHz>i   i   i    (   ii   (   Ra   RA   Rf   Rg   Rh   Ri   Rj   Rk   Rn   R	   t	   Lorentz1DR   RE   RF   RG   RH   RI   R(   (   R@   Rb   Rc   R/   RJ   R   t   ddxRP   RS   RV   RW   RX   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_bounding_box1D]  s.    			%-u   not HAS_SCIPYc         C` s  | d } | d } t  | |  } t | t  rU g  | j D] } | | ^ q< } n  d | k r | d r t j | d | d |  j  } q n  t j | d | d |  j  } t j j	 d  d } d | t j j
 t |   | |  }	 t j   }
 |
 | | |	  } g  | j D] } t | |  ^ q} g  | D] } | j ^ q;} t j g  t | |  D] \ } } | sf| ^ qf } t j g  | D] } | j s| j ^ q } t | | d |  j d S(	   uE   
        Test if the parametric model works with the fitter.
        u   x_limu
   parametersu   log_fiti    i   g{Gz?Rm   N(   Ra   Rn   Ro   Rp   R   Rq   R[   R   R    R!   t   randnR#   R   R$   Rs   Rt   R&   R   R'   R   R^   (   R@   Rb   Rc   Ru   Rw   R/   Rx   R   t   relative_noise_amplitudeR-   R.   R|   R}   R~   Rt   R=   R   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_fitter1D  s,    

#
& %"c         C` s{  | d } | j  d k r) t j d  n  t | t  rH t j d  n  d | k r | d r t j | d | d |  j  } q n  t j	 | d | d |  j  } | d } t
 | | d t } t
 | | d t } t j j d	  } d
 | d | j |  j  d }	 | |  |	 }
 t j   } | | | |
  } t j   } | | | |
 d t } t | j | j d d d S(   uk   
        Test the derivative of a model by comparing results with an estimated
        derivative.
        u   x_limu-   Derivative function is not defined for model.u'   Skip testing derivative of polynomials.u   log_fiti    i   u
   parametersR   iIg?g      ?R   Rm   g333333?N(   R   Rf   Rg   Rk   R   R   R   Rq   R[   R   Ra   R   R    R   R"   R   R$   R   R   Rw   (   R@   Rb   Rc   Ru   R   Rw   R   R   R   R   R-   R   R   R   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_deriv_1D  s0    

& 
		"		(   RB   RC   R   R`   R   R   R   Rg   R   R   R   R   (    (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR   5  s   
					'#u
   parametersc         C` s   i  } t  |  t  r# |  | |   St  |  t  r d | k rU t rU t j d  n  | rw d | k rw | d } qw n  |  | | |   Sd S(   u   Create instance of model class.u   requires_scipyu   SciPy not foundu   constraintsN(   R   R   R
   t	   HAS_SCIPYRg   Rk   (   Rb   Rc   R   R   t   constraints(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyRa     s    u   model_classu   test_parameterst   keyc         C` s   t  |  d  S(   Ni    (   t   str(   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   <lambda>  s    t   TestFittable1DModelsc           B` s   e  Z RS(    (   RB   RC   (    (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR     s   c         C` s   t  |  d  S(   Ni    (   R   (   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR     s    t   TestFittable2DModelsc           B` s   e  Z RS(    (   RB   RC   (    (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyR     s   c          C` s   t  j d  }  |  d  d k s' t  t |  d d g  d d g  t  j d d g d d }  t |  d  d d g  t |  d d g d t d d g d d	 g g  d  S(
   Ni*   i    i   i   i+   i,   t   n_modelst   model_set_axisi-   (   R	   t   ShiftR(   R   R   (   t   m(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_ShiftModel  s    c          C` s   t  j d  }  |  d  d k s' t  t |  d d g  d d g  t  j d d g d d }  t |  d  d d g  t |  d d g d t d d g d d	 g g  d  S(
   Ni*   i    i   i   iT   i+   R   R   iV   (   R	   t   ScaleR(   R   R   (   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_ScaleModel  s    c       	   C` se   t  j d d d d d d d d  }  t j d	 d d
  } |  |  } | d | j   k sa t  d S(   u   
    Currently just tests that the model peaks at its origin.
    Regression test for https://github.com/astropy/astropy/issues/3942
    t   x_0i   t   amplitude_Li
   t   fwhm_Lg      ?t   fwhm_Gg?i    g{Gz?i  N(   R	   t   Voigt1DR   R_   t   maxR(   (   R   R   R+   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_voigt_model  s    $c          C` s1   t  j d d d  }  t |   d k s- t  d  S(   Ng      ?g      @g      @u1   <Gaussian1D(amplitude=1.5, mean=2.5, stddev=3.5)>(   R	   t
   Gaussian1Dt   reprR(   (   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_model_instance_repr  s    u   not HAS_SCIPY_14c          C` s;  t  j d d  }  d d d d d g } t j d  } | d	 |  d
 |  } d d d d d g } t | |  d d d d d g  | d
 |  } t | |  d d d d d g  t j t  ! | d d d d d d g  Wd QX| d
 | d t d d  } t | d d d d d d g  d d d d d d g  d S(   u   
    Test Tabular1D model.
    i    i   g      ?i
   i   i-   ii   t   pointst   lookup_tableg        gffffff?gffffff?g @g333333@g333333@g333333@g333333@g?gffffff@Nt   bounds_errort
   fill_valueg333333(
   R   R_   R	   t   tabular_modelR   Rg   Rh   Rj   R   Rf   (   R   t   valuest   LookupTableR/   t   xnew(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_tabular_interp_1d  s    """	c       
   C` s  t  j d d d d d g d d d d	 d
 g d d d d d g d d d d d g d d d d d g g  }  t  j d d  t  j d d  f } t  j d d d d d  g  } t j d!  } | | |   } | | |  } t  j d d" d# d$ d% g  } t | | d& d1 t  j d)  j d2  } t  j d  d*  d  d+  f \ } }	 t j d, |  }
 |
 | |	  } t | |  t	 j
 t  / | d- d. d/ g d. d0 g d* d+ g f  } Wd  QXd  S(3   Ngr:g%Yɿ}g3{`yga.e?g,ypw?g~O2 g|bLg+冿g?g쀐Ѧ?g8gRrwkӷg&2gI+;?g9?c?g`&rg8Qg-
rg©ja?g#ɻ?g廙g{Bg>jYg?gd?i    i   g        gffffff?gffffff?g @g333333@i   ggngBog|g>%ړ?Rm   i
   ii   i   i   R   R   g333333?gffffff@g@gHz>(   i   i   (   R   R&   R_   R	   R   R   t   reshapeRF   t	   Tabular2DRg   Rh   Rj   (   t   tableR   R   R   R/   t   znewt   resultR8   R+   R   t   t(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_tabular_interp_2d.  s*    $%c          C` s   t  j d  j d  }  t  j d  d  d  d  d  d  f \ } } } t j d  } | d |   } | | | |  } t |  |  d  S(   Ni   i   i   i   R   (   i   i   i   (   R   R_   R   RF   R	   R   R   (   R8   R   R+   R4   t   tabR   R   (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_tabular_ndL  s    1c    	      C` s  t  j d  t  j d  @}  t  j d  |  B} t d   t j j d  | _ Wd QXd d f | _ t j	 d d
  d d
  f \ } } | | |  \ } } | | | d t
 \ } } t j |  j   } t | | | |  t | | | |  | | | d t
 d d \ } } | d k j   } t | | | |  t | | | |  t  j d d d d d }  d |  _ |  d  |  d d t
 k st  d S(   uM   
    Test the option to evaluate a model respecting
    its bunding_box.
    i   i    i   i i   Ni   i	   i   i
   t   with_bounding_boxR   i  t   c0t   c1gffffff@i   (   i    i   i    i   (   i   i	   (   i   i   (   i    i   (   R	   t   Polynomial2Dt   MappingR   R   R    R"   Rw   RA   RF   R   t   isnant   nonzeroR   t   Polynomial1DR(   (	   t   pR   R   R+   R8   R9   t   awt   bwt   ind(    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   test_with_bounding_boxV  s$    %!	(   u   model_classu   test_parameters(   u   model_classu   test_parameters(;   R   t
   __future__R    R   R   R   t   cPicklet   picklet   ImportErrorRg   t   numpyR   t   numpy.testingR   R   t   example_modelsR   R   t    R   R	   t   coreR
   t
   polynomialR   t   utilsR   t   extern.six.movesR   R   t   scipyR   R   R   R   t   HAS_SCIPY_14R   R   R0   R1   R3   RY   t   objectRZ   R   Ra   t   parametrizet   sortedt   itemsR   R   R   R   R   R   R   R   R   R   (    (    (    sA   lib/python2.7/site-packages/astropy/modeling/tests/test_models.pyt   <module>   sZ   "

!			"				
