o
    ñhÄ(  ã                   @   sl   d dl Z e  d¡Ze  d¡Zd dlZd dlmZmZm	Z	 d dl
mZ G dd„ dƒZe j dd	¡d
d„ ƒZdS )é    NÚnumpyÚscipy)Úbarbell_graphÚcycle_graphÚ
path_graph)Úgraphs_equalc                   @   s¬   e Z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)S )*ÚTestConvertScipyc                 C   s@   t ddƒ| _tdtjd| _|  t ¡ ¡| _|  t ¡ ¡| _	d S )Né
   é   ©Úcreate_using)
r   ÚG1r   ÚnxÚDiGraphÚG2Úcreate_weightedÚGraphÚG3ÚG4©Úself© r   úU/var/www/vscode/kcb/lib/python3.10/site-packages/networkx/tests/test_convert_scipy.pyÚsetup_method   s   zTestConvertScipy.setup_methodc                 C   s$   G dd„ dƒ}t  tjtj|¡ d S )Nc                   @   s   e Zd ZdZdS )z+TestConvertScipy.test_exceptions.<locals>.GN)Ú__name__Ú
__module__Ú__qualname__Úformatr   r   r   r   ÚG   s    r   )ÚpytestÚraisesr   ÚNetworkXErrorÚto_networkx_graph)r   r   r   r   r   Útest_exceptions   s   z TestConvertScipy.test_exceptionsc                 C   sX   t dƒ}t| ¡ ƒ}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}t|||ƒ}| |¡ |S )Né   c                 S   s   g | ]\}}|‘qS r   r   ©Ú.0ÚuÚvr   r   r   Ú
<listcomp>   ó    z4TestConvertScipy.create_weighted.<locals>.<listcomp>c                 S   s   g | ]\}}|‘qS r   r   r%   r   r   r   r)      r*   c                 S   s   g | ]}|d  ‘qS )r	   r   )r&   Úsr   r   r   r)      r*   )r   ÚlistÚedgesÚzipÚadd_weighted_edges_from)r   r   ÚgÚeÚsourceÚdestÚweightÚexr   r   r   r      s   
z TestConvertScipy.create_weightedc                 C   s4  t j||d}t  ||¡sJ ‚t j||d}t  ||¡sJ ‚t  d|¡ |¡}t  ||¡s/J ‚| ¡ }t  d|¡ |¡}t  ||¡sDJ ‚| ¡ }t  d|¡ |¡}t  ||¡sYJ ‚| ¡ }	t  d|¡ |	¡}t  ||¡snJ ‚| 	¡ }
t  d|¡ |
¡}t  ||¡sƒJ ‚| 
¡ }t  d|¡ |¡}t  ||¡s˜J ‚d S )Nr   r   )r   Úfrom_scipy_sparse_arrayÚis_isomorphicr"   Úempty_graphÚ	__class__ÚtocsrÚtocooÚtocscÚtodenseÚtoarray)r   r   ÚAr   ÚGGÚGWÚGIÚACSRÚACOOÚACSCÚADÚAAr   r   r   Úidentity_conversion#   s*   z$TestConvertScipy.identity_conversionc                 C   s.   t j g d¢g d¢g¡}t tjtj|¡ dS )z(Conversion from non-square sparse array.)é   é   r
   )r$   é   é   N)ÚspÚsparseÚ	lil_arrayr   r    r   r!   r6   ©r   r?   r   r   r   Ú
test_shapeA   s   zTestConvertScipy.test_shapec                 C   ó$   t  | j¡}|  | j|t  ¡ ¡ dS )z0Conversion from graph to sparse matrix to graph.N)r   Úto_scipy_sparse_arrayr   rH   r   rP   r   r   r   Útest_identity_graph_matrixF   ó   z+TestConvertScipy.test_identity_graph_matrixc                 C   rR   )z4Conversion from digraph to sparse matrix to digraph.N)r   rS   r   rH   r   rP   r   r   r   Útest_identity_digraph_matrixK   rU   z-TestConvertScipy.test_identity_digraph_matrixc                 C   rR   )zBConversion from weighted graph to sparse matrix to weighted graph.N)r   rS   r   rH   r   rP   r   r   r   Ú#test_identity_weighted_graph_matrixP   rU   z4TestConvertScipy.test_identity_weighted_graph_matrixc                 C   rR   )zFConversion from weighted digraph to sparse matrix to weighted digraph.N)r   rS   r   rH   r   rP   r   r   r   Ú%test_identity_weighted_digraph_matrixU   rU   z6TestConvertScipy.test_identity_weighted_digraph_matrixc                 C   sœ   t dƒ}t dƒ}t| ¡ ƒ}tj||d}t |¡}t ||¡s"J ‚tjtj	tj|g d |dg }tjtj	tj||d g d¢}tjtj	tj||d dS )z>Conversion from graph to sparse matrix to graph with nodelist.r$   r
   ©Únodelistr   )éÿÿÿÿr   rI   rJ   N)
r   r,   Únodesr   rS   r   r7   r   r    r!   )r   ÚP4ÚP3rZ   r?   ÚGAÚlong_nlÚnon_nlr   r   r   Útest_nodelistZ   s   

zTestConvertScipy.test_nodelistc                 C   sš   t  ¡ }| dd„ tdƒD ƒ¡ tdƒ}t  |¡}tj | 	¡ t j|d d 	¡ ¡ tj d| 	¡  t  |¡ 	¡ ¡ tj d| 	¡  t j|dd 	¡ ¡ d S )	Nc                 s   ó$    | ]}||d  dddœfV  qdS ©rI   ç      à?ç333333Ó?)r4   ÚotherNr   ©r&   Únr   r   r   Ú	<genexpr>n   ó   €" z7TestConvertScipy.test_weight_keyword.<locals>.<genexpr>r
   r$   ©r4   re   rf   rg   ©
r   r   Úadd_edges_fromÚranger   rS   ÚnpÚtestingÚassert_equalr=   ©r   ÚWP4r]   r?   r   r   r   Útest_weight_keywordl   s   
ÿÿÿz$TestConvertScipy.test_weight_keywordc                 C   sn  t  ¡ }| dd„ tdƒD ƒ¡ tdƒ}t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|d	d}tj | 	¡ t j|d d 	¡ ¡ t j|d
d}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ d S )Nc                 s   rc   rd   r   rh   r   r   r   rj   }   rk   z7TestConvertScipy.test_format_keyword.<locals>.<genexpr>r
   r$   Úcsr©r   rl   ÚcscÚcooÚbsrÚlilÚdiaÚdokrm   rs   r   r   r   Útest_format_keyword{   s>   ÿÿÿÿÿÿÿz$TestConvertScipy.test_format_keywordc                 C   sh   t  tj¡$ t ¡ }| dd„ tdƒD ƒ¡ tdƒ}tj|dd W d   ƒ d S 1 s-w   Y  d S )Nc                 s   rc   rd   r   rh   r   r   r   rj   ¥   s   € 
ÿz=TestConvertScipy.test_format_keyword_raise.<locals>.<genexpr>r
   r$   Ú	any_otherrw   )	r   r    r   r!   r   rn   ro   r   rS   )r   rt   r]   r   r   r   Útest_format_keyword_raise¢   s   
ÿ"úz*TestConvertScipy.test_format_keyword_raisec                 C   s@   t  tj¡ t t ¡ ¡ W d   ƒ d S 1 sw   Y  d S )N)r   r    r   r!   rS   r   r   r   r   r   Útest_null_raise«   s   "ÿz TestConvertScipy.test_null_raisec                 C   s<   t  ¡ }| d¡ t  |¡}tj | ¡ t dgg¡¡ d S )NrI   r   )	r   r   Úadd_noderS   rp   rq   rr   r>   Úarray©r   r   ÚMr   r   r   Ú
test_empty¯   s   

 zTestConvertScipy.test_emptyc              	   C   sl   t  ¡ }| dd¡ | dd¡ | dd¡ t j|g d¢d}tj | ¡ t g d¢g d¢g d¢g¡¡ d S )	NrI   rJ   r
   )r
   rJ   rI   rY   ©r   r   rI   )rI   r   r   ©r   rI   r   )	r   r   Úadd_edgerS   rp   rq   rr   r>   rƒ   r„   r   r   r   Útest_orderingµ   s    ÿzTestConvertScipy.test_orderingc              	   C   s€   t  dg¡}t  |¡}tj | ¡ t dgg¡¡ | ddg¡ t j|g d¢d}tj | ¡ t g d¢g d¢g d¢g¡¡ d S )	N©rI   rI   rI   ©rJ   r
   ©r
   r$   ©rJ   r
   r$   rY   rˆ   )rI   r   rI   )	r   r   rS   rp   rq   rr   r>   rƒ   rn   r„   r   r   r   Útest_selfloop_graph¿   ó   
 ÿz$TestConvertScipy.test_selfloop_graphc              	   C   s€   t  dg¡}t  |¡}tj | ¡ t dgg¡¡ | ddg¡ t j|g d¢d}tj | ¡ t g d¢g d¢g d	¢g¡¡ d S )
Nr‹   rI   rŒ   r   rŽ   rY   rˆ   r‡   )r   r   r   )	r   r   rS   rp   rq   rr   r>   rƒ   rn   r„   r   r   r   Útest_selfloop_digraphÊ   r   z&TestConvertScipy.test_selfloop_digraphc                 C   s"  t j ddgddgg¡}t ¡ }g d¢}| dd„ |D ƒ¡ |jdddd tj|dtjd}t||ƒs6J ‚tj|d	tjd}t||ƒsFJ ‚g d
¢}t 	¡ }| dd„ |D ƒ¡ tj|dtj	d}t||ƒshJ ‚t 	¡ }|j
t|ƒdd d|d d d d< tj|d	tj	d}t||ƒsJ ‚dS )z±Tests that the :func:`networkx.from_scipy_sparse_array` function
        interprets integer weights as the number of parallel edges when
        creating a multigraph.

        rI   rJ   )©r   r   ©r   rI   ©rI   r   c                 S   ó   g | ]	\}}||d f‘qS ©rI   r   r%   r   r   r   r)   à   ó    zPTestConvertScipy.test_from_scipy_sparse_array_parallel_edges.<locals>.<listcomp>rl   T)Úparallel_edgesr   F)r’   r“   r”   r‹   r‹   c                 S   r•   r–   r   r%   r   r   r   r)   ï   r—   r   r4   N)rM   rN   Ú	csr_arrayr   r   r/   r‰   r6   r   ÚMultiDiGraphrn   Úset)r   r?   Úexpectedr-   Úactualr   r   r   Ú+test_from_scipy_sparse_array_parallel_edgesÕ   s6   ÿÿÿÿz<TestConvertScipy.test_from_scipy_sparse_array_parallel_edgesc                 C   sR   t j ddgddgg¡}tj|tjd}t ¡ }|jdddd t||ƒs'J ‚dS )z¢Tests that a symmetric matrix has edges added only once to an
        undirected multigraph when using
        :func:`networkx.from_scipy_sparse_array`.

        r   rI   r   rl   N)rM   rN   r™   r   r6   Ú
MultiGraphr‰   r   )r   r?   r   rœ   r   r   r   Útest_symmetricý   s
   zTestConvertScipy.test_symmetricN)r   r   r   r   r#   r   rH   rQ   rT   rV   rW   rX   rb   ru   r~   r€   r   r†   rŠ   r   r‘   rž   r    r   r   r   r   r      s*    
'	
(r   Úsparse_format)rv   rx   r}   c                 C   s–   t  ¡ }| ddddifddddifddddifddddifddddifddddifg¡ tj g d¢g d¢g d¢g¡ | ¡}t|t  |¡ƒsIJ ‚d	S )
z7Test all formats supported by _generate_weighted_edges.r   rI   r4   r
   rJ   )r   r
   rJ   )r
   r   rI   )rJ   rI   r   N)	r   r   rn   rM   rN   Ú	coo_arrayÚasformatr   r6   )r¡   rœ   r?   r   r   r   Ú$test_from_scipy_sparse_array_formats
  s   úÿ$
r¤   )r   Úimportorskiprp   rM   Únetworkxr   Únetworkx.generators.classicr   r   r   Únetworkx.utilsr   r   ÚmarkÚparametrizer¤   r   r   r   r   Ú<module>   s    

  