o
    hP                     @   s~   d Z ddlZddlZedZed G dd dZdd Zd	d
 Zdd Z	ej
dedddd Zdd Zdd ZdS )z Unit tests for layout functions.    Nnumpyscipyc                   @   s   e Zd Ze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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d)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dES )F
TestLayoutc                 C   s8   t dd| _t  | _t | jd t dd| _d S )N   abcdef   )nxgrid_2d_graphGiGraphGsadd_pathbigG)cls r   V/var/www/vscode/kcb/lib/python3.10/site-packages/networkx/drawing/tests/test_layout.pysetup_class   s   
zTestLayout.setup_classc                 C   s\   t d}tjtt j|dgd ddd}tjtt j|ddg|d t j|dd	g|d d S )
N   r   )fixed   r   r   r   r      r   )r   posr   )r   
path_graphpytestraises
ValueErrorspring_layout)selfGr   r   r   r   test_spring_fixed_without_pos   s
   

z(TestLayout.test_spring_fixed_without_posc                    sf   dd l  t }|g d ddi}dg}tj|||d}t fdd| D }|r1J dd S )Nr   )r   r   r   r   )r   r   )r              r'   r   r   c                 3   s$    | ]}|D ]}  |V  qqd S N)isnan).0coordscmathr   r   	<genexpr>$   s   " z2TestLayout.test_spring_init_pos.<locals>.<genexpr>zvalues should not be nan)r/   r   r   add_edges_fromfruchterman_reingold_layoutanyvalues)r    r!   init_pos	fixed_posr   has_nanr   r.   r   test_spring_init_pos   s   zTestLayout.test_spring_init_posc                 C   sx   g }t | t | t | t | t | t | t | t || t 	| t 
| t | d S r)   )r   random_layoutcircular_layoutplanar_layoutr   r2   spectral_layoutshell_layoutbipartite_layoutspiral_layoutmultipartite_layoutkamada_kawai_layoutr    r!   r   r   r   test_smoke_empty_graph'   s   








z!TestLayout.test_smoke_empty_graphc                 C   s   | j }t| t| t| t| t| t| t| j t	| t	|
  t	| j t	| j
  t| t| t| tj|dd tj|dd t| d S Nr   dimr%   )r
   r   r9   r:   r;   r   forceatlas2_layoutr2   r   r<   to_directedr=   r?   rA   
arf_layoutrB   r   r   r   test_smoke_int5   s$   









zTestLayout.test_smoke_intc                 C   s   | j }t| t| t| t| t| t| t| t	| t
| t| tj|dd tj|dd t| d S rD   )r   r   r9   r:   r;   r   rG   r2   r<   r=   r?   rA   rI   rB   r   r   r   test_smoke_stringI   s   









zTestLayout.test_smoke_stringc                 C   sx   t |}|| }|| }t t| }|d|d }|d| k s*J ||k s2J ||k s:J d S )Nr   r   )nparraylistr4   maxminall)r    r   scalecenterlowhivposlengthr   r   r   check_scale_and_centerY   s   
z!TestLayout.check_scale_and_centerc                 C   s   | j }d}td}|d |tj||dddd |tj|d|dd|d |tj|d|dd|d |tj|d|dd|d |tj|d|dd|d |tj	|d|dd|d |tj
|d|dd|d d}|tj
|d	d|d
d|d d S )N)r   r   	   rS         ?)g      @g      @rR   rS   r   )r   r%   r   r%   )rF   rR   rS   rX   r   complete_graphadd_noder9   r   r<   r:   r=   r?   rA   r    scr-   r!   r   r   r   test_scale_and_center_argc   s   

 z$TestLayout.test_scale_and_center_argc                 C   s    t d}tt jt j| d S )NrY   )r   r^   r   r   NetworkXExceptionr;   rB   r   r   r   #test_planar_layout_non_planar_inputt   s   
z.TestLayout.test_planar_layout_non_planar_inputc                 C   s4   t  }|ddgddgddgd t | d S )Nr   r   r   r   r   r   )r   PlanarEmbeddingset_datar;   )r    	embeddingr   r   r   (test_smoke_planar_layout_embedding_inputx   s   z3TestLayout.test_smoke_planar_layout_embedding_inputc                 C   s   | j }d}td}|d |t|ddd |t|d|d |t|d|d |t|d|d |t|d|d |t	|d|d |t
|d|d d}|tj
|dd	d|d d S )
Nr   rY   r[   r[   r[   r\   r   )r   r   r   r%   rE   r]   r`   r   r   r   test_default_scale_and_center}   s   

z(TestLayout.test_default_scale_and_centerc                 C   sr   t d}tjtt j|dd tjtt j|dd tjtt j|dd tjtt j|dd tjtt j|dd d S )Nr   r   rE   r%   )r   r   r   r   r   r:   r=   r;   rB   r   r   r   (test_circular_planar_and_shell_dim_error   s   
z3TestLayout.test_circular_planar_and_shell_dim_errorc                 C   sh   t | j}t jj|}|jdksJ t jjj|dd}|jdks$J t jj|}|jdks2J d S )N   r   r%   rE   rn   r%   )r   to_numpy_arrayr   drawinglayout_fruchterman_reingoldshape_sparse_fruchterman_reingoldr    Ar   r   r   r   test_adjacency_interface_numpy   s   z)TestLayout.test_adjacency_interface_numpyc                 C   sl   t j| jdd}t jj|}|jdksJ t jj|}|jdks$J t jjj|dd}|jdks4J d S )Nd)dtyperm   r%   rE   ro   )r   to_scipy_sparse_arrayr   rq   rr   ru   rt   _sparse_spectralrv   r   r   r   test_adjacency_interface_scipy   s   z)TestLayout.test_adjacency_interface_scipyc                 C   s   t d}t |}|d  rJ t d}t |dgddgdgg}|d  r,J |d  s4J tj|d dks@J t j|dgddgdggdd}tj|d dks[J d S )Nr   r   r   r   r%   )rotate)r   r   r=   r3   rL   linalgnormr    r!   rV   r   r   r   test_single_nodes   s   


zTestLayout.test_single_nodesc                 C       t | j}t j| j|d}d S N)r   )r   r:   r
   rG   r    r   nposr   r   r   "test_smoke_initial_pos_forceatlas2      z-TestLayout.test_smoke_initial_pos_forceatlas2c                 C   r   r   )r   r:   r
   r2   r   r   r   r   +test_smoke_initial_pos_fruchterman_reingold   r   z6TestLayout.test_smoke_initial_pos_fruchterman_reingoldc                 C   r   r   )r   r:   r
   rI   r   r   r   r   test_smoke_initial_pos_arf   r   z%TestLayout.test_smoke_initial_pos_arfc                 C   s   t | j}t j| j|dgd}t|d t|d ksJ t | j}t j| j|dgd}tdD ]}|d | tj|d | ddksGJ q2d S )Nr   r(   r   Hz>abs)	r   r:   r
   r   tupler   ranger   approx)r    r   r   axisr   r   r   $test_fixed_node_fruchterman_reingold   s   (z/TestLayout.test_fixed_node_fruchterman_reingoldc                 C   s
  t d}t j|dd t j|dd}t|d dksJ t j|dd}t|d dks.J t j|dd}t|d dks?J t j|dd}t|d dksPJ t j|dd}t|d dksaJ t j	|dd}t|d dksrJ t j
|dd}t|d dksJ d S )Nr   r   rZ   r   )r   r   r9   r:   r   r;   r   r2   r<   r=   r?   r   r   r   r   test_center_parameter   s    
z TestLayout.test_center_parameterc                 C   s   t d}tt jtt jksJ tjtt j|dd tjtt j	|dd tjtt j
|dd tjtt j|dd tjtt j|ddd tjtt j|dd tjtt j|ddd tjtt j|dd tjtt j|dd tjtt j|dd d S )Nr   )r   r   r   rZ   r%   r   )rF   rS   )r   r   idr   r2   r   r   r   r9   r:   r;   r<   r=   r?   rA   rB   r   r   r   test_center_wrong_dimensions   s   
z'TestLayout.test_center_wrong_dimensionsc                 C   sT  t  }t j|dd}|i ksJ t j|dd}|i ksJ t j|dd}|i ks+J t ||}|i ks7J t j|dd}|i ksDJ t j|dd}|i ksQJ t j|dd}|i ks^J t j	|dd}|i kskJ t j
|dd}|i ksxJ t j|dd}|i ksJ t j|dd}|i ksJ t |}|i ksJ t |}|i ksJ d S )Nr   rZ   )r   empty_graphr9   r:   r;   r>   r   r2   r<   r=   r?   r@   rA   rG   rI   r   r   r   r   test_empty_graph   s6   

zTestLayout.test_empty_graphc           
      C   sH  t dd}t j|\}}t ||}t|t|ksJ |t|d  d }|t|d  d }|D ]}|| d |ks@J q4|D ]}|| d |ksOJ qCt j||ddddd}t|t|kseJ |t|d  d }|t|d  d }	|D ]}|| d |ksJ q{|D ]}|| d |	ksJ qtjt	t j||d	d
 d S )Nr%   r   r   
horizontalr   r   r   r   )alignrS   rR   aspect_ratiofoor   )
r   complete_bipartite_graph	bipartitesetsr>   lenrN   r   r   r   )
r    r!   topbottomrV   top_xbottom_xnodetop_ybottom_yr   r   r   test_bipartite_layout  s*   z TestLayout.test_bipartite_layoutc                    s   d}t j| }t |tt|ksJ d |D ]} | }t fddt d |D s3J  |7  qt j|dddd	tt|ksKJ d |D ]} | }t fd
dt d |D shJ  |7  qOtjtt j|dd d S )N)r   r      r      r   c                 3   (    | ]}  d  | d  kV  qdS )r   Nr   r+   istartrV   r   r   r0   -     & z6TestLayout.test_multipartite_layout.<locals>.<genexpr>r   r   r   r   )r   rR   rS   c                 3   r   )r   Nr   r   r   r   r   r0   6  r   r   r   )	r   complete_multipartite_graphr@   r   rQ   r   r   r   r   )r    sizesr!   nendr   r   r   test_multipartite_layout#  s"   

&
&
z#TestLayout.test_multipartite_layoutc                 C   s   t jjj}tddg}dtddgddgg }||t|ddd\}}|tjd	d
dks/J |d tjdd
dks<J |d tjdd
dksIJ d S )Ng      @g      @r   皙?g       @333333?r   
meanweightrF   g      ?r   r         r[   )r   rq   rr   _kamada_kawai_costfnrL   rM   r   r   )r    costfnr   invdistcostgradr   r   r   test_kamada_kawai_costfn_1d;  s   
z&TestLayout.test_kamada_kawai_costfn_1dc              	   C   s  t jjj}|| t|||d\}}d| ttj|ddd  }t|jd D ])}	t|	d |jd D ]}
tj	
||	 ||
  }||||	 |
  d d 7 }q5q)|tj|dd	ks^J d
}t|jd D ]Z}t|jd D ]P}||jd  | }| }||  |7  < ||t|||jd dd }||  d| 8  < ||t|||jd dd }|| tj|| d|  dd	ksJ qpqgd S )Nr   r[   r   r   r   r         ?r   r   g-C6?gh㈵>)r   rq   rr   r   ravelrL   sumr   rt   r   r   r   r   flatten)r    r   r   meanwtrF   r   r   r   expected_costr   jdiffdxnddmidxpscpluscminusr   r   r   check_kamada_kawai_costfnG  s*   
 (z$TestLayout.check_kamada_kawai_costfnc                 C   s   dt g dg dg dg }d}t ddgdd	gd
dgg}| |||d t g dg dg dg}| |||d d S )Nr   )r    @333333?)r   g?333333?)r   r   r   r   g?g	g@g333333ӿgffffff@g      @r   )g?g333333!@gffffff!)ir   gffffff)g333333"@g333333 g?r%   )rL   rM   r   )r    r   r   r   r   r   r   test_kamada_kawai_costfnb  s    z#TestLayout.test_kamada_kawai_costfnc                 C   s   | j }tttj|dd }tttj|dd }tjj|d d |dd   dd}tjj|d d |dd   dd}t	|t	|ksMJ tttj|dd }tjj|d d |dd   dd}tj
|dd  |d d	d
s}J d S )Ngffffff?)
resolutiong(\?r   r   Tequidistantg{Gz?atol)r   rL   rM   rN   r   r?   r4   r   r   r   allclose)r    r!   pos_standardpos_tighter	distancesdistances_tighterpos_equidistantdistances_equidistantr   r   r   test_spiral_layoutp  s   $$zTestLayout.test_spiral_layoutc                 C   sl   t d}t j|dd}tt| }tjj|dd  |d d  dd}tj	t
|ddd	s4J d S )
N
   Tr   r   r   r   r   gMbP?r   )r   r   r?   rL   rM   rN   r4   r   r   r   r   )r    r!   r   pdistr   r   r   test_spiral_layout_equidistant  s
   
$z)TestLayout.test_spiral_layout_equidistantc                 C   H   | j }tj|}t|}||= tj||d}t|t|ks"J d S r   )r   r   utilsarbitrary_elementr:   rG   r   r    r!   r   r   r   r   r   *test_forceatlas2_layout_partial_input_test     
z5TestLayout.test_forceatlas2_layout_partial_input_testc                 C   s&  t  }t j|dd}t |i ksJ t d}ddd}t |}tjdd t|  D d	k s6J t d
}dddd}t |}t	dt	dt	dd}|
 D ]\}}|| |k sgJ qYt j|dd}t	dt	dt	dd}|
 D ]\}}|| |k sJ qd S )Nr   rZ   r   r&   )r   r   r#   c                 S   s   g | ]}t |qS r   )r   )r+   xr   r   r   
<listcomp>  s    z7TestLayout.test_rescale_layout_dict.<locals>.<listcomp>gư>r%   r   rj   re   )r   r   )rR   )r   r   )r   r   r9   rescale_layout_dictrL   r   r   zipr4   rM   itemsrQ   )r    r!   rV   s_vposexpectationkvr   r   r   test_rescale_layout_dict  s0   


&

z#TestLayout.test_rescale_layout_dictc                 C   r   r   )r   r   r   r   r:   rI   r   r   r   r   r   "test_arf_layout_partial_input_test  r   z-TestLayout.test_arf_layout_partial_input_testc                 C   s   | j }tjttj|dd dS )zl
        Checks input parameters correctly raises errors. For example,  `a` should be larger than 1
        r   )r!   aN)r   r   r   r   r   rI   rB   r   r   r    test_arf_layout_negative_a_check  s   z+TestLayout.test_arf_layout_negative_a_checkc                 C   sB   | j }tj|dd tj|dd tj|dd tj|dd d S )N*   )seed)r   r   r9   r   rI   rG   rB   r   r   r   test_smoke_seed_input  s
   z TestLayout.test_smoke_seed_inputN)&__name__
__module____qualname__classmethodr   r"   r8   rC   rJ   rK   rX   rb   rd   ri   rk   rl   rx   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      sH    

					r   c                  C   sp   t  } | jddd | jddd | jddd | jddd | g d t | }t|t| ks6J d	S )
zSee gh-5123.r   s0subsetr   r   s1r%   )r   )r   r%   r$   N)r   r   r_   r1   r@   r   )r!   r   r   r   r   4test_multipartite_layout_nonnumeric_partition_labels  s   
r  c                  C   s.  t  } ttdd}| D ]\}}| j||d qt j| dd}t j|}t j| d|d}t| | D ]\\}}\}	}
||	krK||
k	 sMJ q9|d d |d	 d ks\J |d
 d |d d   k r||d d   k r||d d k sJ  J d| j
d d< t | }| | ksJ dS )zgReturn the layers in sorted order if the layers of the multipartite
    graph are sortable. See gh-5691)r   br-   ry   e)r   r%   r   r   r   r  r   r   )r   
subset_keyr   r   ry   r-   r	  r
  layer_0r  N)r   r   dictr   r   r_   r@   r   groupsrQ   nodeskeys)r!   
node_groupr   layerr   layerspos_from_layersn1p1n2p2
pos_nosortr   r   r   $test_multipartite_layout_layer_order  s   "F
r  c                 C   s4   t t|  dddf }t j|dd\}}|S )zJHelper function to extract the number of nodes in each layer of bfs_layoutNr   T)return_counts)rL   rM   rN   r4   unique)r   r   _layer_countr   r   r   _num_nodes_per_bfs_layer  s   r  r   r   r   c                 C   s8   t | }t j|dd}tt|d| d gsJ dS )zrThe complete graph should result in two layers: the starting node and
    a second layer containing all neighbors.r   r   r   N)r   r^   
bfs_layoutrL   array_equalr  )r   r!   r   r   r   r   test_bfs_layout_complete_graph  s   
 r#  c                  C   s   t dd} t j| dd}g d}tt||sJ t j| dd}tt||s,J t j| dd}g d}tt||sAJ d S )	Nr   r%   r   r   )r   r   r   r   r   r   r      rn   )r   r   r   r   )r   barbell_graphr!  rL   r"  r  )r!   r   expected_nodes_per_layerr   r   r   test_bfs_layout_barbell  s   r'  c                  C   s\   t d} | ddg tjt jdd t j| dd W d    d S 1 s'w   Y  d S )Nr   )r      )r(  r$  z#bfs_layout didn't include all nodes)matchr   r   )r   r^   r1   r   r   NetworkXErrorr!  )r!   r   r   r   test_bfs_layout_disconnected  s
   
"r+  )__doc__r   networkxr   importorskiprL   r   r  r  r  markparametrizer   r#  r'  r+  r   r   r   r   <module>   s     

   F
