o
    ִi(                     @   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   T/home/kuhnn/.local/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 ||sJ |
 }t d||}t ||sJ 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 )zTests 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 )zTests 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    

  