U
    e»{js(  ã                   @   s  d Z ddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZ ddlmZmZ ddlmZ dd	lmZ G d
d„ de
ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!ej" #dej$dfddej$ ff¡dd „ ƒZ%d!d"„ Z&d#d$„ Z'd%d&„ Z(d'd(„ Z)dS ))zF
Tests that relate to using quantities/units on parameters of models.
é    N)Úcoordinates)Úunits)ÚFittable1DModelÚInputParameterError)ÚConst1DÚ
Gaussian1DÚPix2Sky_TANÚRotateNative2CelestialÚ
Rotation2D)Ú	ParameterÚParameterDefinitionError)Úassert_quantity_allclose)Ú
UnitsErrorc                   @   s   e Zd Zedd„ ƒZdS )ÚBaseTestModelc                 C   s   | S )N© )ÚxÚar   r   úp/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/modeling/tests/test_quantities_parameters.pyÚevaluate   s    zBaseTestModel.evaluateN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r   r   r   r   r      s   r   c                  C   sˆ   t dtj dtj dtj ƒ} | jjdks.t‚| jjtjks@t‚| jjdksPt‚| jjtjksbt‚| j	jdksrt‚| j	jtjks„t‚dS )z
    Basic tests for initializing general models (that do not require units)
    with parameters that have units attached.
    é   çš™™™™™¹?ç      ð?N)
r   ÚuÚJÚmÚ	amplitudeÚvalueÚAssertionErrorÚunitÚmeanÚstddev©Úgr   r   r   Útest_parameter_quantity   s    r'   c                  C   s¸   t dtj dtj dtj ƒ} dtj | _t| jdtj ƒ dtj | _t| jdtj ƒ dtj	 | _
t| j
dtj	 ƒ dtj | _t| jdtj ƒ dtj | _t| jdtj ƒ dS )az  
    Make sure that parameters that start off as quantities can be set to any
    other quantity, regardless of whether the units of the new quantity are
    compatible with the original ones.

    We basically leave it up to the evaluate method to raise errors if there
    are issues with incompatible units, and we don't check for consistency
    at the parameter level.
    r   r   é   é   é   N)r   r   r   r   ZkJr   r   Úkmr#   Úmmr$   ÚsÚJyr%   r   r   r   Útest_parameter_set_quantity,   s    r/   c               	   C   s:   t dtj ddƒ} d}tjt|d�� d| _W 5 Q R X dS )ab  
    Check that parameters that have been set to a quantity that are then set to
    a value with no units raise an exception. We do this because setting a
    parameter to a value with no units is ambiguous if units were set before:
    if a parameter is 1 * u.Jy and the parameter is then set to 4, does this mean
    2 without units, or 2 * u.Jy?
    r   r)   r   zVThe .* parameter should be given as a .* because it was originally initialized as a .*©Úmatchr*   N)r   r   r.   ÚpytestÚraisesr   r   ©r&   ÚMESSAGEr   r   r   Útest_parameter_lose_unitsM   s
    	ÿr6   c                  C   s.   t dddƒ} dtj | _t| jdtj ƒ dS )z†
    On the other hand, if starting from a parameter with no units, we should be
    able to add units since this is unambiguous.
    r   r)   r   r*   N)r   r   r.   r   r   r%   r   r   r   Útest_parameter_add_units`   s    r7   c               	   C   sl   t ddtj dtj ƒ} d}tjt|d�� tj| j_W 5 Q R X d}tjt|d�� tj	| j
_W 5 Q R X dS )zå
    Test that changing the unit on a parameter does not work. This is an
    ambiguous operation because it's not clear if it means that the value should
    be converted or if the unit should be changed without conversion.
    r   r   zNCannot attach units to parameters that were not initially specified with unitsr0   zYCannot change the unit attribute directly, instead change the parameter to a new quantityN)r   r   r   r2   r3   Ú
ValueErrorr.   r   r"   Úcmr#   r4   r   r   r   Útest_parameter_change_unitl   s    ÿÿr:   c               	   C   s–   t dtj dtj dtj ƒ} dtj | _d| j_t| jdtj ƒ | jjdksTt‚| jjtjksft‚d}t	j
t|d�� dtj | j_W 5 Q R X dS )	zH
    Test that changing the value on a parameter works as expected.
    r   r   r*   r(   zZThe .value property on parameters should be set to unitless values, not Quantity objects.*r0   r)   N)r   r   r.   r   r   r    r   r!   r"   r2   r3   Ú	TypeErrorr4   r   r   r   Útest_parameter_set_value†   s    ÿr<   c               	   C   s¼   t dtj dtj dtj ƒ} t| jjdtj ƒ dtj | j_| jjdksPt	‚| jj
tjksbt	‚dtj | j_| jjdks€t	‚| jj
tjks’t	‚d}tjt|d�� d| j_W 5 Q R X dS )	zK
    Test that the quantity property of Parameters behaves as expected
    r   r   é   r(   z:The .quantity attribute should be set to a Quantity objectr0   r)   N)r   r   r.   r   r   r   ZquantityZmJyr    r!   r"   r-   r2   r3   r;   r4   r   r   r   Ú test_parameter_quantity_property    s    	r>   c               	   C   s2   d} t jt| d�� G dd„ dtƒ}W 5 Q R X d S )NzNparameter default 1.0 m does not have units equivalent to the required unit Jyr0   c                   @   s    e Zd Zedej ejd�ZdS )z1test_parameter_default_units_match.<locals>.TestCr   ©Údefaultr"   N)r   r   r   r   r   r   r.   r   r   r   r   r   ÚTestCÅ   s   rA   )r2   r3   r   r   )r5   rA   r   r   r   Ú"test_parameter_default_units_match½   s    ÿrB   )r"   r@   r   r   c              	      sb  G ‡ ‡fdd„dt ƒ}|jjtjks(t‚|jjdks8t‚|ƒ }|jjtjksPt‚|jj|jj  krldksrn t‚|dtj ƒ}|jjtjks’t‚|jjdks¢t‚|jjdks²t‚|dtj ƒ}|jjtjksÒt‚|jjdksât‚|jjdksòt‚|dtj	 ƒ}|jjtj	k�st‚|jjdk�s&t‚|jjdk�s8t‚d}t
jt|d�� |dƒ W 5 Q R X dS )zG
    Test that default quantities are correctly taken into account
    c                       s   e Zd Ze” ”d�ZdS )z*test_parameter_defaults.<locals>.TestModelr?   N)r   r   r   r   r   r   r?   r   r   Ú	TestModelÏ   s   rC   r   g       @z'.* requires a Quantity for parameter .*r0   N)r   r   r"   r   r   r!   r@   r    Zpcr.   r2   r3   r   )r"   r@   rC   r   r5   r   r?   r   Útest_parameter_defaultsÉ   s*    "rD   c               	   C   sJ  t dtj dtj dtj ƒ} | jdtj  dtj ks<t‚dtj | j dtj ksZt‚| jd dtj ksrt‚d| j dtj ksŠt‚| jdtj  dtjd  ks¬t‚dtj | j dtjd  ksÎt‚| j dtj ksät‚t| j ƒ| jksút‚d}tjt	|d�� | jd  W 5 Q R X tjt	|d�� d| j  W 5 Q R X dS )zp
    Test that arithmetic operations with properties that have units return the
    appropriate Quantities.
    r   r   r*   éÿÿÿÿzPCan only apply 'add' function to dimensionless quantities when other argument .*r0   N)
r   r   r   r   r#   r!   Úabsr2   r3   r   r4   r   r   r   Ú"test_parameter_quantity_arithmeticû   s    ""ÿrG   c               	   C   sÈ  t dtj dtj dtj ƒ} | jdtj ks2t‚dtj | jksFt‚| jdksTt‚d| jksbt‚| jdtj k svt‚dtj | jksŠt‚d}tjt|d�� | jdk  W 5 Q R X tjt|d�� d| jk W 5 Q R X t ddgtj ddgtj ddgtj ƒ} t	 
| jddgtj k¡�s t‚t	 
ddgtj | jk¡�s@t‚t	 
| jddgk¡�sZt‚t	 
ddg| jk¡�stt‚tjt|d�� | jddgk  W 5 Q R X tjt|d�� ddg| jk W 5 Q R X d	S )
zG
    Basic test of comparison operations on properties with units.
    r   r   r*   zQCan only apply 'less' function to dimensionless quantities when other argument .*r0   gš™™™™™É?r)   r(   N)r   r   r   r   r#   r!   r2   r3   r   ÚnpÚallr4   r   r   r   Ú"test_parameter_quantity_comparison  s,    ÿ*  rJ   c                  C   sF   t ƒ  tjddtjd�} dtj }t| j| j|ƒ}tdƒ}||B  d S )Ngffffff@i¸ÿÿÿ)ÚraÚdecr"   é´   é   )	r   ÚcoordZSkyCoordr   Údegr	   rK   rL   r
   )Z
sky_coordsZlon_poleZn2cZrotr   r   r   Útest_parameters_compound_modelsG  s    
rQ   c                  C   s.   t j} d|  }t|ƒ}|d|  ƒ|ks*t‚dS )z2Regression test for bug reproducer in issue #13133g      4Àg      7ÀN)r   ZABmagr   r!   )r"   ÚcÚmodelr   r   r   Útest_magunit_parameterP  s    rT   )*Ú__doc__ÚnumpyrH   r2   Zastropyr   rO   r   r   Zastropy.modeling.corer   r   Zastropy.modeling.modelsr   r   r   r	   r
   Zastropy.modeling.parametersr   r   Zastropy.tests.helperr   Zastropy.unitsr   r   r'   r/   r6   r7   r:   r<   r>   rB   ÚmarkZparametrizer   rD   rG   rJ   rQ   rT   r   r   r   r   Ú<module>   s0   ! 
1#)	