U
    ÃmœdíI  ã                   @   sr  d dl mZ d dlmZ d dlmZ d dlZd dlm	Z	m
Z
 d dlZd dlZd dlmZ zd dlmZ W n ek
r|   Y nX d dlmZmZmZmZmZmZ ejjdd	„ ƒZejjd
d„ ƒZejjdd„ ƒZejjdd„ ƒZ ejjdd„ ƒZ!ejj"ejjdd„ ƒƒ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+d$d%„ Z,ejjd&d'„ ƒZ-ejjd(d)„ ƒZ.dS )*é    )Úlrange)ÚBytesIO)ÚproductN)Úassert_Úassert_raises)Údatasets)Ú_hierarchical_splitÚ_key_splittingÚ_normalize_splitÚ_reduce_dictÚ_split_rectÚmosaicc                 C   sD  dd l }t dd¡\}}ddddœ}t||d dd	d
� | |¡}t||d dd	d
� dddg}t||d dd	d
� t |¡}t||d dd	d
� t d¡ dddddœ}t||d dd	d
� t||d dddgd	d� | |¡}t||d dd	d
� t||d dddgd� ddgddgg}t||d dd	d
� t||d d ddgd� t ddgddgg¡}t||d! d"d	d
� t||d# d$ddgd	d� t d¡ d%d%d%d&d&d&g}d'd(d(d'd(d'g}| 	||d)œ¡}t|d*g|d+ d,d	d
� t|d-g|d. d/d	d
� t|d*d-g|d0 d1d	d
� t|d-d*g|d2 d3d	d
� t d¡ t 
d4¡ d S )5Nr   é   é   é   é   )ÚaxÚbxÚcx©r   r   z
basic dictF©r   ÚtitleÚ
axes_label©r   r   zbasic series)r   r   z
basic list)r   r   zbasic arrayÚall))r   r   )r   r   )r   Údx)r   r   ©r   r   zcompound dict)r   r   zinverted keys dict)r   r   Úindexr   ©r   r   zcompound series)r   r   zinverted keys series)r   r   r   )r   r   zcompound list)r   r   zinverted keys list)r   r   zcompound array)r   r   zinverted keys arrayÚmaleÚfemaleÚcatÚdog)ÚgenderÚpetr#   )r   r   zdataframe by key 1r$   )r   r   zdataframe by key 2)r   r   z	both keys)r   r   zkeys invertedz%testing data conversion (plot 1 of 4))ÚpandasÚpltÚsubplotsr   ZSeriesÚnpZasarrayÚcloseÚarrayÚ	DataFrameÚsuptitle)Úclose_figuresr%   Ú_r   Údatar#   r$   © r0   úc/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/graphics/tests/test_mosaicplot.pyÚtest_data_conversion    sB    






r2   c                 C   s¸   ddgdddgddgdd	gf}t t|Ž ƒ}tt|td
d
t|ƒ ƒƒƒ}i }ddi|d< ddi|d< |D ]2}d	|krfd|krŠdddœ||< qfdddœ||< qft|d|dd� t d¡ d S )Nr   r    ÚoldÚadultÚyoungÚworkerÚ
unemployedÚhealtyÚillr   ÚcolorÚb)r   Úr©r    Z
BlueVioletú+)r:   ZhatchZCrimsonçš™™™™™©?F)ÚgapÚ
propertiesr   z)syntetic data, 4 categories (plot 2 of 4))	Úlistr   ÚdictÚzipÚrangeÚlenr   r&   r,   )r-   Úkey_setÚkeysr/   ÚpropsÚkeyr0   r0   r1   Útest_mosaic_simpleK   s      ÿrK   c                 C   s.  t j ¡ }|j}|jdk|d< | ddg¡}ddddd	d
œ}|d  |¡|d< dddddœ}|d  |¡|d< dddœ}|d  |¡|d< t dd¡\}}t	|ddg|d dd� t	|ddg|d dd� t	|dddg|d ddd„ d� |d  
d¡ |d  d¡ t	|ddg|d dd d!� t d"¡ d S )#Nr   ZcheatedZrate_marriageZ	religiousZawfulÚbadZintermediateZgoodZ	wonderful)r   r   r   r   é   znon religiouszpoorly religiouszvery religious)r   r   r   r   Zfaithful)FTr   r   zby marriage happiness)r   r   r   zby religiosityr   zby bothc                 S   s   dS )NÚ r0   ©Úkr0   r0   r1   Ú<lambda>…   ó    ztest_mosaic.<locals>.<lambda>)r   r   Ú	labelizerzmarriage ratingzreligion statusr   zinter-dependenceFr   z"extramarital affairs (plot 3 of 4))r   ZfairZload_pandasZexogZendogÚsort_valuesÚmapr&   r'   r   Z
set_xlabelZ
set_ylabelr,   )r-   ZaffairsZdatasZnum_to_descZnum_to_faithZnum_to_cheatr.   r   r0   r0   r1   Útest_mosaich   sB    
 ÿÿ
ÿÿ ÿ ÿrV   c              
      sÒ  ddddg}ddgddgd	d
gddgf}t t|Ž ƒ}tt|tddt|ƒ ƒƒƒ}i }ddi|d< ddi|d< t|ƒ}t ||¡\}}t|ƒD �]4}	t|ƒD �]$}
tt|ƒƒ 	t|	|
fƒ¡‰|	|
k�r.||	|	f j
dd||	 ddd� ||	|	f  g ¡ ||	|	f  g ¡ ||	|	f  g ¡ ||	|	f  g ¡ qšt|	|
ƒ‰t|	|
ƒ‰ t‡ ‡‡fdd„| ¡ D ƒƒ}t | ¡ ƒ}|D ].}t||d d… ƒ}|||d d… < ||= �qpt|||	|
f d|d|	|
kd� qšqŒt d¡ d S )Nr#   ZageZhealthZworkr   r    r3   r5   r8   r9   r7   r   r:   r<   )r   r3   Zpinkr=   ç      à?Úcenter)ZhaÚvac                    s:   g | ]2\‰ }ˆ ˆ ˆ ˆ ft ‡ fd d„ˆD ƒƒ |f‘qS )c                 3   s   | ]}ˆ | V  qd S ©Nr0   )Ú.0r<   rO   r0   r1   Ú	<genexpr>©   s     z6test_mosaic_very_complex.<locals>.<listcomp>.<genexpr>)Útuple)r[   Úv©ZijZjiÚmrO   r1   Ú
<listcomp>©   s   ÿz,test_mosaic_very_complex.<locals>.<listcomp>r   Fr?   )r   r   rA   r@   Ú
horizontalz0old males should look bright red,  (plot 4 of 4))rB   r   rC   rD   rE   rF   r&   r'   ÚsetÚ
differenceÚtextZ
set_xticksZset_xticklabelsZ
set_yticksZset_yticklabelsÚmaxÚminÚitemsrH   r   r   r,   )r-   Zkey_nameZkey_baserH   r/   rI   ÚLr.   ZaxesÚiÚjZ	temp_datarP   Úvaluer0   r_   r1   Útest_mosaic_very_complex�   sP     ÿ
 ÿ

ÿ
  ÿ
rm   c           	   	   C   s¦   ddl m} ddgdddgdd	gd
dgf}tt|Ž ƒ}tt||t|ƒƒƒƒ}dd„ }tjdddd�\}\}}t	|||ddd� t	|||dddddgd� | 
d¡ d S )Nr   )Úrandr   r    r3   r4   r5   r6   r7   ÚyesÚnoc                 S   s   d  dd„ | D ƒ¡S )NrN   c                 s   s   | ]}|d  V  qdS )r   Nr0   )r[   Úsr0   r0   r1   r\   ¿   s     z7test_axes_labeling.<locals>.<lambda>.<locals>.<genexpr>)ÚjoinrO   r0   r0   r1   rQ   ¿   rR   z$test_axes_labeling.<locals>.<lambda>r   r   )é   é   )ZfigsizeTé-   )r   rS   rb   Zlabel_rotationFéZ   z$correct alignment of the axes labels)Znumpy.randomrn   rB   r   rC   rD   rF   r&   r'   r   r,   )	r-   rn   rG   rH   r/   ZlabÚfigZax1Zax2r0   r0   r1   Útest_axes_labeling¶   s     ÿ

ÿrx   c                 C   s€   dd l }| ddddddddddddœdd	d
d
dddd
d
d
d
dœdœ¡}| |j|j¡}t|j ¡ ƒ\}}t|ddgƒ\}}d S )Nr   ZAngelicaZDXW_UIDZcasuid01ZEC93_uidZAtmosFox)é@   éA   éB   éC   éD   éE   éF   é<   é=   é>   é?   ZTGPZRetention01ÚdefaultZMusa_EC_9_3)Úid2Úid1r†   r…   )r%   r+   Zcrosstabr†   r…   r   ÚTZunstack)r-   ÚpdZmydataÚctr.   Úvalsr0   r0   r1   Útest_mosaic_empty_cellsÈ   s:         û     ûúr‹   c                 C   s   t t | |¡ƒS rZ   )r   r(   Zallclose)ÚxÚyr0   r0   r1   rQ   ß   rR   rQ   c                  C   sÎ   t tdƒƒ} tt| dgt| ƒ ƒƒ}t|dd�}tt | ¡ ƒ| kƒ d|d< d|d< t tdd	ƒƒ} tt| dgt| ƒ ƒƒ}t|dd�}tt | ¡ ƒ| kƒ d
|d< d|d< d|d< d|d< d|d< d|d< d S )NÚmfr   r   )r@   ©ç        r�   rW   ç      ð?©r`   ©rW   r�   rW   r‘   )ÚfZyao)r�   r�   rW   çUUUUUUÕ?)r`   r�   )r�   r•   rW   r•   )r`   Úa)r�   çUUUUUUå?rW   r•   ©r`   Úo)rW   r�   rW   r•   )r”   r�   )rW   r•   rW   r•   )r”   r–   )rW   r—   rW   r•   )r”   r™   )rB   r   rC   rD   rF   r   r   rH   )rH   r/   Úresr0   r0   r1   Útest_recursive_splitâ   s     r›   c                  C   s¢   t tttdddƒƒdgd ƒƒ} tt| dƒdƒ tt| dƒd	ƒ tt| d
ƒdƒ t tttdddƒƒtdƒƒƒ} tt| dƒdƒ tt| dƒdƒ tt| d
ƒdƒ d S )NrŽ   ZoyZwnr   rt   r’   r   r˜   r   )r`   r™   Úwé   r   )rC   rD   rB   r   Úeqr   r   )r/   r0   r0   r1   Útest__reduce_dictõ   s     rŸ   c                  C   s  t ƒ di} t| ddgddgt ƒ ddƒ}tt| ¡ ƒddgkƒ t|d d	ƒ t|d d
ƒ t|ddgddgdddƒ}tt| ¡ ƒdddgkƒ t|d dƒ t|d dƒ t|d d
ƒ ddi} t| ddgddgdddƒ}tt| ¡ ƒdd„ dD ƒkƒ t|d dƒ t|d dƒ d S )N)r   r   r   r   r–   r;   r   Tr   )r–   )r;   )r   r   rW   r   )rW   r   rW   r   ÚcÚdF)r–   r    )r–   r¡   )r�   r�   rW   rW   )r�   rW   rW   rW   ©Útotalr   c                 S   s   g | ]}d |f ‘qS )r¢   r0   )r[   Úer0   r0   r1   ra     s     z'test__key_splitting.<locals>.<listcomp>)r–   r;   )r£   r–   )r   r   r•   r   )r£   r;   )r•   r   r—   r   )r]   r	   r   rB   rH   rž   )Z	base_rectrš   Zres_bisr0   r0   r1   Útest__key_splitting   s    
r¥   c                  C   s^  t tdƒdddgƒ t tdƒdddgƒ t tdƒdddgƒ tttdƒ tttddgƒ tttdddgƒ tttdgƒ tttddgƒ t tdgƒddgƒ t tdgƒddgƒ t tdgƒddgƒ dD ]} t t| ƒd| dgƒ qÈdD ](\} }t t| |gƒd| | |  dgƒ qædD ]D\} }}t t| ||gƒd| | | |  | | | | |  dgƒ �qd S )	Nr�   r‘   g       @éÿÿÿÿrW   )g333333Ó?rW   gÍÌÌÌÌÌì?))ç      Ð?rW   )çš™™™™™¹?gš™™™™™é?)ç      $@ç      >@))r‘   r‘   r‘   )r¨   rW   gffffffæ?)r©   rª   é(   )rž   r
   r   Ú
ValueError)rŒ   r�   Úzr0   r0   r1   Útest_proportion_normalization  s*    "ÿ&ÿr®   c                  C   st  ddddg} t dgddd�}t dgddd�}tt| |Ž| ƒ tt| |Ž| ƒ t dgddd�}t dgddd�}tt| |Ž| ƒ tt| |Ž| ƒ ddddg}t dgddd�}tt||Ž|ƒ t dgddd�}tt||Ž|ƒ ddddg}t dgddd�}tttf|ž|Ž t ddgddd�}tttf|ž|Ž t dgddd�}tttf|ž|Ž t ddgddd�}tttf|ž|Ž d S )	Nr�   r‘   r   T©Z
proportionr@   rb   FrW   ç      ð¿)rC   rž   r   r   r¬   )Úpure_squareÚconf_hÚconf_vÚnull_squareÚconfZ
neg_squarer0   r0   r1   Útest_false_split1  s.    r¶   c                  C   sÈ  ddddg} ddg}t ddgddd�}tt| |Ž|ƒ dd	g}t ddgdd
d�}tt| |Ž|ƒ ddg}t ddgddd�}tt| |Ž|ƒ ddg}t ddgdd
d�}tt| |Ž|ƒ dddg}t dddgddd�}tt| |Ž|ƒ dddg}t dddgdd
d�}tt| |Ž|ƒ dddg}t dddgddd�}tt| |Ž|ƒ dddg}t dddgdd
d�}tt| |Ž|ƒ ddddg}t ddgddd�}tt||Ž||gƒ t ddgddd�}tt||Ž||gƒ d S )Nr�   r‘   r�   r“   r   Tr¯   )r�   r�   r‘   rW   )r�   rW   r‘   rW   F)r�   r�   r•   r‘   )r•   r�   r—   r‘   r   )r�   r�   r‘   r•   )r�   r•   r‘   r—   )r•   r�   r•   r‘   ©r—   r�   r•   r‘   )r�   r•   r‘   r•   )r�   r—   r‘   r•   ©r�   r�   r§   r‘   )r§   r�   rW   r‘   ©ç      è?r�   r§   r‘   )r�   r�   r‘   r§   )r�   r§   r‘   rW   )r�   rº   r‘   r§   ©rC   rž   r   )r±   Úh_2splitr²   Úv_2splitr³   r´   rµ   r0   r0   r1   Útest_rect_pure_splitQ  s<    



r¾   c                  C   s¸   ddddg} ddg}t ddgddd	�}tt| |Ž|ƒ d
dg}t ddgddd	�}tt| |Ž|ƒ ddg}t ddgddd	�}tt| |Ž|ƒ ddg}t ddgddd	�}tt| |Ž|ƒ d S )Nr‘   r°   rW   )r‘   r°   rW   rW   )g      ø?r°   rW   rW   r   r�   Tr¯   )r‘   r°   r‘   r§   )r‘   g      è¿r‘   r§   F)r‘   r°   r•   rW   )gUUUUUUõ?r°   r—   rW   r   )r‘   r°   r‘   çUUUUUUÅ?)r‘   g«ªªªªªê¿r‘   r•   r»   )Znon_pure_squarer¼   r²   r½   r³   r0   r0   r1   Útest_rect_deformed_splitƒ  s    rÀ   c                  C   s„   ddddg} t dgddd�}tt| |Ž| ƒ ddg}t ddgddd�}tt| |Ž|ƒ dd	g}t dd
gddd�}tt| |Ž|ƒ d S )Nr�   r‘   r   Tr¯   r¸   r¹   )r�   r�   r¿   r‘   r·   r   r»   )r±   r²   r¼   r0   r0   r1   Útest_gap_split˜  s    rÁ   c              	   C   s<   t  ddddddgddddddgdœ¡}ttt|dd� d S )NÚsmallÚlargeÚlongÚshort)ÚsizeÚlengthZfoobar)r/   r   )rˆ   r+   r   r¬   r   )r-   Zdfr0   r0   r1   Útest_default_arg_indexª  s    ÿ
ÿþrÈ   c              
   C   s�   ddddddddddg
}ddddddddddg
}t  ||dœ¡}t j|d dddgd�|d< | d¡}t|d	dgƒ\}}tƒ }|j|d
d� d S )Nr"   r!   ZmediumrÃ   rÂ   )ÚanimalrÆ   rÆ   )Ú
categoriesrÉ   Zpng)Úformat)rˆ   r+   ZCategoricalrT   r   r   Zsavefig)r-   rÉ   rÆ   Ztestdatarw   r.   Zbior0   r0   r1   Útest_missing_category´  s&      ÿ   ÿ
ÿ

rÌ   )/Zstatsmodels.compat.pythonr   Úior   Ú	itertoolsr   Únumpyr(   Znumpy.testingr   r   r%   rˆ   ZpytestZstatsmodels.apir   Zmatplotlib.pyplotZpyplotr&   ÚImportErrorZstatsmodels.graphics.mosaicplotr   r	   r
   r   r   r   ÚmarkZ
matplotlibr2   rK   rV   rm   rx   Zsmoker‹   rž   r›   rŸ   r¥   r®   r¶   r¾   rÀ   rÁ   rÈ   rÌ   r0   r0   r0   r1   Ú<module>   sL    

*

$
(
 2
	