U
    e»{j­$  ã                   @   st  d Z ddlZddlZddl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 dd
lmZ ejejejejgZdd„ Ze	 ¡ e	 ¡  e	 ¡  e	 ¡ e	 ¡  e	 ¡ B e	 ¡ e	 ¡  e	 ¡ B gZ G dd„ deƒZ!dd„ Z"ej#j$e dd�ej# %de¡dd„ ƒƒZ&ej#j$e dd�ej# %de¡dd„ ƒƒZ'ej#j$e dd�ej# %de¡dd„ ƒƒZ(ej#j$e dd�ej# %de¡dd„ ƒƒZ)ej#j$e dd�ej# %de¡dd„ ƒƒZ*ej#j$e dd�ej# +d¡ej# +d¡ej# %d e ¡ej# %de¡d!d"„ ƒƒƒƒƒZ,ej#j-d#d�ej# %de¡d$d%„ ƒƒZ.ej#j$e dd�ej# +d&¡ej# %d e"ƒ ¡ej# %de¡d'd(„ ƒƒƒƒZ/dS ))z>
Tests that relate to fitting models with quantity parameters
é    N)Úunits)ÚfittingÚmodels)ÚFittable1DModel)Ú	Parameter)Úassert_quantity_allclose)Ú
UnitsError)ÚNumpyRNGContext)Ú	HAS_SCIPYc               	   C   sr   t dƒ�H t ddd¡} dt d| d d  d	 ¡ }|tj d
d| j¡7 }W 5 Q R X | tj } |tj	 }| |fS )Né90  g      Àg      @iÐ  é   g      à¿çÍÌÌÌÌÌô?é   g|®Gázä?g        gš™™™™™É?)
r	   ÚnpÚlinspaceÚexpÚrandomÚnormalÚshapeÚuÚmÚJy)ÚxÚy© r   úm/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/modeling/tests/test_quantities_fitting.pyÚ_fake_gaussian_data   s    
 

r   c                       s^   e Zd ZdZdZedd�Zedd�Zeef‡ fdd„	Ze	dd„ ƒZ
edd	„ ƒZd
d„ Z‡  ZS )ÚCustomInputNamesModelé   ç      ð?)Údefaultc                    s    t ƒ j||d� d| _d| _d S )N©ÚaÚb©Úinn©Úout)ÚsuperÚ__init__ÚinputsÚoutputs)Úselfr"   r#   ©Ú	__class__r   r   r)   9   s    zCustomInputNamesModel.__init__c                 C   s   ||  | S )Nr   )r%   r"   r#   r   r   r   Úevaluate>   s    zCustomInputNamesModel.evaluatec                 C   s4   | j jd kr| jjd krd S d| jj| j j iS d S )Nr%   )r"   Úunitr#   )r,   r   r   r   Úinput_unitsB   s    z!CustomInputNamesModel.input_unitsc                 C   s   |d |d  |d dœS )Nr'   r%   r!   r   ©r,   Zinputs_unitZoutputs_unitr   r   r   Ú_parameter_units_for_data_unitsI   s    z5CustomInputNamesModel._parameter_units_for_data_units)Ú__name__Ú
__module__Ú__qualname__Zn_inputsZ	n_outputsr   r"   r#   r)   Ústaticmethodr/   Úpropertyr1   r3   Ú__classcell__r   r   r-   r   r   2   s   



r   c                  C   sN   t  dtj tj dtj ¡} d| _d| _tdtj tj dtj ƒ}| |gS )Nr   r   r$   r&   )r   ÚLinear1Dr   r   Úsr*   r+   r   )ÚlineZcustom_names_modelr   r   r   Úmodels_with_custom_namesM   s
    r=   zrequires scipy)ÚreasonÚfitterc                 C   sj   | ƒ } t ƒ \}}t ¡ }| |||ƒ}t|jdtj dd� t|jdtj dd� t|j	dtj dd� d S )Nr   çš™™™™™©?©Zrtolr   çš™™™™™é?©
r   r   Ú
Gaussian1Dr   Ú	amplituder   r   Úmeanr   Ústddev©r?   r   r   Úg_initÚgr   r   r   Útest_fitting_simpleV   s    
rK   c                 C   s„   | ƒ } t ƒ \}}tjdtj dtj dtj d�}| |||ƒ}t|jdtj	 dd� t|j
dtj dd� t|jdtj dd� d S )	Nr   r   r   ©rE   rF   rG   r@   rA   r   rB   )r   r   rD   r   ÚmJyÚcmÚmmr   rE   r   rF   r   rG   rH   r   r   r   Ú test_fitting_with_initial_valuesh   s    
"rP   c              	   C   sÌ   | ƒ } G dd„ dt jƒ}|dtj dtj dtj d�}d}tjt|d��< | |d	ddgd
ddgtj	tj
tj tj tj   ƒ W 5 Q R X d}tjt|d��$ | |d	ddgtj d
ddgƒ W 5 Q R X dS )zD
    Raise an error if the model has units but the data doesn't
    c                   @   s   e Zd Zdd„ ZdS )z<test_fitting_missing_data_units.<locals>.UnorderedGaussian1Dc                 S   s   |d |d |d dœS )Nr   r   rL   r   r2   r   r   r   r3   …   s    ýz\test_fitting_missing_data_units.<locals>.UnorderedGaussian1D._parameter_units_for_data_unitsN)r4   r5   r6   r3   r   r   r   r   ÚUnorderedGaussian1D‚   s   rQ   r   r   r   rL   z%'cm' .* and '' .* are not convertible©Úmatchr   é   é   é   z&'mJy' .* and '' .* are not convertibleN)r   rD   r   rM   rN   rO   ÚpytestÚraisesr   Zergr;   ÚHzr   )r?   rQ   rI   ÚMESSAGEr   r   r   Útest_fitting_missing_data_unitsz   s    
 @r[   c                 C   sÜ   | ƒ } t ƒ \}}tjdddd�}| |||ƒ}t|jdtj dd� t|jdtj dd� t|j	dtj dd� tjddtj dtj d�}| |||ƒ}t|jdtj dd� t|jdtj dd� t|j	dtj dd� d	S )
z=
    Proceed if the data has units but the model doesn't
    r   r   r   rL   r@   rA   r   rB   NrC   rH   r   r   r   Ú test_fitting_missing_model_units™   s    
r\   c              	   C   sn   | ƒ } t jdtj dtj dtj d�}d}tjt|d��* | |dddgtj	 dd	d
gtj ƒ W 5 Q R X dS )zF
    Raise an error if the data and model have incompatible units
    r   r   r   rL   z&'Hz' .* and 'm' .* are not convertiblerR   r   rT   rU   rV   N)
r   rD   r   r   r   rN   rW   rX   r   rY   )r?   rI   rZ   r   r   r   Útest_fitting_incompatible_units²   s
    "r]   z$ignore:The fit may be unsuccessful.*z#ignore:divide by zero encountered.*Úmodelc              	      sò   |ƒ }t  ddd¡tj }tdƒ� t j d¡}W 5 Q R X || ||tj ƒ‰ ˆ jD ]}t	t
ˆ |ƒƒ qRt‡ fdd„tdƒD ƒƒs„t‚ˆ |ƒ}t|tjƒsœt‚|| t  d¡t d¡ |ƒ‰ t‡ fd	d„tdƒD ƒƒsÖt‚ˆ |ƒ}t|t jƒsît‚d S )
NéûÿÿÿrU   é
   r   c                    s   g | ]}ˆ | j ‘qS r   ©Z
_has_units©Ú.0Úi©Zres_fitr   r   Ú
<listcomp>Ï   s     z/test_compound_without_units.<locals>.<listcomp>r   ÚAngstromc                    s   g | ]}ˆ | j ‘qS r   ra   rb   re   r   r   rf   Ô   s     )r   r   r   rg   r	   r   ÚsamplerY   Úparam_namesÚprintÚgetattrÚallÚrangeÚAssertionErrorÚ
isinstanceZQuantityZarangeZUnitÚndarray)r^   r?   r   r   Ú
param_nameÚzr   re   r   Útest_compound_without_unitsÀ   s    

rs   zFlaky and ill-conditionedc              	      sÆ  | ƒ } t  ddd¡tj }t  ddd¡tj }| ƒ } t dtj dtj dtj dtj dtj ¡}t dtj tj dtj tj dtj ¡}|| }|||ƒ}| ||||ƒ‰ tˆ ||ƒt j	ƒsÆt
‚t‡ fd	d
„tdƒD ƒƒsät
‚t ¡ t ¡  }| ||||ƒ‰ tˆ ||ƒt j	ƒ�st
‚t‡ fdd
„tdƒD ƒƒ�s:t
‚tjdtj d�tjdd� }t  ddd¡tj }tdƒ� t j d¡}W 5 Q R X ||ƒ}| |||d|  ƒ‰ t jjˆ jdddgdd� d S )Nr_   rU   é   r`   r   rT   r   r   c                    s   g | ]}ˆ | j ‘qS r   ra   rb   ©Úresr   r   rf   í   s     z4test_compound_fitting_with_units.<locals>.<listcomp>c                    s   g | ]}ˆ | j ‘qS r   ra   rb   ru   r   r   rf   ò   s     i¸  )Ztemperaturer   )rE   i'  r   gãW›%@gHO‘CÄ-@gš™™™™™Ù?rA   )r   r   r   rg   r   Z
Gaussian2DrY   ZPlanar2Dro   rp   rn   rl   rm   Z	BlackBodyÚKZConst1DZmicronr	   r   r   ZtestingZassert_allcloseÚ
parameters)r?   r   r   r   Úpr^   rr   Únr   ru   r   Ú test_compound_fitting_with_unitsÛ   s@        ÿ,
 
  ÿr{   z%ignore:Model is linear in parameters*c                 C   sX   |ƒ }t  ddd¡tj }| |ƒ}|| ||ƒ}| jD ]}tt||ƒjt| |ƒjƒ q4dS )z>Tests fitting of models with custom inputs and outsputs names.r   r`   éd   N)r   r   r   r;   ri   r   rk   Zquantity)r^   r?   r   r   Z	new_modelrq   r   r   r   Útest_fitting_custom_names  s    

 
ÿr}   )0Ú__doc__Únumpyr   rW   Zastropyr   r   Zastropy.modelingr   r   Zastropy.modeling.corer   Zastropy.modeling.parametersr   Zastropy.tests.helperr   Zastropy.unitsr   Zastropy.utilsr	   Z"astropy.utils.compat.optional_depsr
   ZLevMarLSQFitterZTRFLSQFitterZLMLSQFitterZDogBoxLSQFitterZfittersr   r:   rD   ZScaleZShiftZcompound_models_no_unitsr   r=   ÚmarkZskipifZparametrizerK   rP   r[   r\   r]   Úfilterwarningsrs   Úskipr{   r}   r   r   r   r   Ú<module>   sf   üý	

'
