
    çg(                         d dl Z  e j        d          Z e j        d          Zd dlZd dlmZmZm	Z	 d dl
mZ  G d d          Ze j                            dd	          d
             ZdS )    Nnumpyscipy)barbell_graphcycle_graph
path_graph)graphs_equalc                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )TestConvertScipyc                    t          dd          | _        t          dt          j                  | _        |                     t          j                              | _        |                     t          j                              | _	        d S )N
      create_using)
r   G1r   nxDiGraphG2create_weightedGraphG3G4selfs    ]/var/www/html/ai-engine/env/lib/python3.11/site-packages/networkx/tests/test_convert_scipy.pysetup_methodzTestConvertScipy.setup_method   sc    A&&brz:::&&rxzz22&&rz||44    c                 t     G d d          }t          j        t          j        t          j        |           d S )Nc                       e Zd ZdZdS )+TestConvertScipy.test_exceptions.<locals>.GN)__name__
__module____qualname__format r   r   Gr      s        FFFr   r%   )pytestraisesr   NetworkXErrorto_networkx_graph)r   r%   s     r   test_exceptionsz TestConvertScipy.test_exceptions   sL    	 	 	 	 	 	 	 	 	b&(<a@@@@@r   c                     t          d          }t          |                                          }d |D             }d |D             }d |D             }t          |||          }|                    |           |S )N   c                     g | ]\  }}|S r$   r$   .0uvs      r   
<listcomp>z4TestConvertScipy.create_weighted.<locals>.<listcomp>   s    """1!"""r   c                     g | ]\  }}|S r$   r$   r.   s      r   r2   z4TestConvertScipy.create_weighted.<locals>.<listcomp>   s       da   r   c                     g | ]}|d z   S )r   r$   )r/   ss     r   r2   z4TestConvertScipy.create_weighted.<locals>.<listcomp>   s    )))Q!b&)))r   )r   listedgeszipadd_weighted_edges_from)r   r%   gesourcedestweightexs           r   r   z TestConvertScipy.create_weighted   s    NNOO"""""  a   ))&)))v&&	!!"%%%r   c                 v   t          j        ||          }t          j        ||          sJ t          j        ||          }t          j        ||          sJ t          j        d|                              |          }t          j        ||          sJ |                                }t          j        d|                              |          }t          j        ||          sJ |                                }t          j        d|                              |          }t          j        ||          sJ |                                }	t          j        d|                              |	          }t          j        ||          sJ |	                                }
t          j        d|                              |
          }t          j        ||          sJ |
                                }t          j        d|                              |          }t          j        ||          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AAs               r   identity_conversionz$TestConvertScipy.identity_conversion#   s   'EEE2&&&&&!!,???2&&&&&^A|,,66q992&&&&&wwyy^A|,,66t<<2&&&&&wwyy^A|,,66t<<2&&&&&wwyy^A|,,66t<<2&&&&&YY[[^A|,,66r::2&&&&&YY[[^A|,,66r::2&&&&&&&r   c                     t           j                            g dg dg          }t          j        t
          j        t
          j        |           dS )z(Conversion from non-square sparse array.)      r   )r,         N)spsparse	lil_arrayr&   r'   r   r(   rA   r   rJ   s     r   
test_shapezTestConvertScipy.test_shapeA   sD    IIII 677b&(BAFFFFFr   c                     t          j        | j                  }|                     | j        |t          j                               dS )z0Conversion from graph to sparse matrix to graph.N)r   to_scipy_sparse_arrayr   rS   r   r\   s     r   test_identity_graph_matrixz+TestConvertScipy.test_identity_graph_matrixF   :    $TW--  !RXZZ88888r   c                     t          j        | j                  }|                     | j        |t          j                               dS )z4Conversion from digraph to sparse matrix to digraph.N)r   r_   r   rS   r   r\   s     r   test_identity_digraph_matrixz-TestConvertScipy.test_identity_digraph_matrixK   :    $TW--  !RZ\\:::::r   c                     t          j        | j                  }|                     | j        |t          j                               dS )zBConversion from weighted graph to sparse matrix to weighted graph.N)r   r_   r   rS   r   r\   s     r   #test_identity_weighted_graph_matrixz4TestConvertScipy.test_identity_weighted_graph_matrixP   ra   r   c                     t          j        | j                  }|                     | j        |t          j                               dS )zFConversion from weighted digraph to sparse matrix to weighted digraph.N)r   r_   r   rS   r   r\   s     r   %test_identity_weighted_digraph_matrixz6TestConvertScipy.test_identity_weighted_digraph_matrixU   rd   r   c                 "   t          d          }t          d          }t          |                                          }t          j        ||          }t          j        |          }t          j        ||          sJ t          j        t          j	        t          j        |g            |dgz   }t          j        t          j	        t          j        ||           g d}t          j        t          j	        t          j        ||           dS )z>Conversion from graph to sparse matrix to graph with nodelist.r,   r   nodelistr   )r   rU   rV   N)
r   r6   nodesr   r_   r   rB   r&   r'   r(   )r   P4P3rk   rJ   GAlong_nlnon_nls           r   test_nodelistzTestConvertScipy.test_nodelistZ   s    ]]]]

##$R(;;;Xa[[B'''''b&(@"rRRRRaS.b&(@"wWWWW b&(@"vVVVVVVr   c                    t          j                    }|                    d t          d          D                        t	          d          }t          j        |          }t          j                            |	                                t          j        |d           	                                           t          j                            d|	                                z  t          j        |          	                                           t          j                            d|	                                z  t          j        |d          	                                           d S )Nc              3   ,   K   | ]}||d z   dddfV  dS rU         ?333333?)r>   otherNr$   r/   ns     r   	<genexpr>z7TestConvertScipy.test_weight_keyword.<locals>.<genexpr>n   5      WWAq1us&C&CDWWWWWWr   r   r,   r>   rw   rx   ry   
r   r   add_edges_fromranger   r_   nptestingassert_equalrH   r   WP4rn   rJ   s       r   test_weight_keywordz$TestConvertScipy.test_weight_keywordl   s.   hjjWWeTUhhWWWWWW]]$R((

IIKK1#dCCCKKMM	
 	
 	
 	
!))++r7<<DDFF	
 	
 	
 	
!))++r7GLLLTTVV	
 	
 	
 	
 	
r   c                    t          j                    }|                    d t          d          D                        t	          d          }t          j        |d          }t          j                            |	                                t          j        |d           	                                           t          j        |d          }t          j                            |	                                t          j        |d           	                                           t          j        |d          }t          j                            |	                                t          j        |d           	                                           t          j        |d	          }t          j                            |	                                t          j        |d           	                                           t          j        |d
          }t          j                            |	                                t          j        |d           	                                           t          j        |d          }t          j                            |	                                t          j        |d           	                                           t          j        |d          }t          j                            |	                                t          j        |d           	                                           d S )Nc              3   ,   K   | ]}||d z   dddfV  dS rv   r$   rz   s     r   r|   z7TestConvertScipy.test_format_keyword.<locals>.<genexpr>}   r}   r   r   r,   csrr#   r~   csccoobsrlildiadokr   r   s       r   test_format_keywordz$TestConvertScipy.test_format_keyword{   s   hjjWWeTUhhWWWWWW]]$R666

IIKK1#dCCCKKMM	
 	
 	
 $R666

IIKK1#dCCCKKMM	
 	
 	
 $R666

IIKK1#dCCCKKMM	
 	
 	
 $R666

IIKK1#dCCCKKMM	
 	
 	
 $R666

IIKK1#dCCCKKMM	
 	
 	
 $R666

IIKK1#dCCCKKMM	
 	
 	
 $R666

IIKK1#dCCCKKMM	
 	
 	
 	
 	
r   c                 <   t          j        t          j                  5  t          j                    }|                    d t          d          D                        t          d          }t          j        |d           d d d            d S # 1 swxY w Y   d S )Nc              3   ,   K   | ]}||d z   dddfV  dS rv   r$   rz   s     r   r|   z=TestConvertScipy.test_format_keyword_raise.<locals>.<genexpr>   sF        >?AEcC889     r   r   r,   	any_otherr   )	r&   r'   r   r(   r   r   r   r   r_   )r   r   rn   s      r   test_format_keyword_raisez*TestConvertScipy.test_format_keyword_raise   s    ]2+,, 	= 	=(**C  CH88      AB$R<<<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s   A%BBBc                     t          j        t          j                  5  t          j        t          j                               d d d            d S # 1 swxY w Y   d S )N)r&   r'   r   r(   r_   r   r   s    r   test_null_raisez TestConvertScipy.test_null_raise   s    ]2+,, 	1 	1$RXZZ000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   &AAAc                    t          j                    }|                    d           t          j        |          }t          j                            |                                t                              dgg                     d S )NrU   r   )	r   r   add_noder_   r   r   r   rI   arrayr   r%   Ms      r   
test_emptyzTestConvertScipy.test_empty   s`    HJJ	

1$Q''

		RXXse__=====r   c           	         t          j                    }|                    dd           |                    dd           |                    dd           t          j        |g d          }t          j                            |                                t                              g dg dg dg                     d S )	NrU   rV   r   )r   rV   rU   rj   r   r   rU   )rU   r   r   r   rU   r   )	r   r   add_edger_   r   r   r   rI   r   r   s      r   test_orderingzTestConvertScipy.test_ordering   s    JLL	

1a	

1a	

1a$Q;;;

IIKK999iii"CDD	
 	
 	
 	
 	
r   c           	         t          j        dg          }t          j        |          }t          j                            |                                t                              dgg                     |                    ddg           t          j        |g d          }t          j                            |                                t                              g dg dg dg                     d S )	NrU   rU   rU   rV   r   r   r,   rV   r   r,   rj   r   )rU   r   rU   )	r   r   r_   r   r   r   rI   r   r   r   s      r   test_selfloop_graphz$TestConvertScipy.test_selfloop_graph   s    HfX$Q''

		RXXse__===	&&)***$Q;;;

IIKK999iii"CDD	
 	
 	
 	
 	
r   c           	         t          j        dg          }t          j        |          }t          j                            |                                t                              dgg                     |                    ddg           t          j        |g d          }t          j                            |                                t                              g dg dg d	g                     d S )
Nr   rU   r   r   r   rj   r   r   )r   r   r   )	r   r   r_   r   r   r   rI   r   r   r   s      r   test_selfloop_digraphz&TestConvertScipy.test_selfloop_digraph   s    Jx  $Q''

		RXXse__===	&&)***$Q;;;

IIKK999iii"CDD	
 	
 	
 	
 	
r   c                    t           j                            ddgddgg          }t          j                    }g d}|                    d |D                        |                    ddd           t          j        |dt          j                  }t          ||          sJ t          j        |dt          j                  }t          ||          sJ g d	}t          j	                    }|                    d
 |D                        t          j        |dt          j	                  }t          ||          sJ t          j	                    }|
                    t          |          d           d|d         d         d         d<   t          j        |dt          j	                  }t          ||          sJ 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.

        rU   rV   )r   r   r   rU   rU   r   c                     g | ]
\  }}||d fS rU   r$   r.   s      r   r2   zPTestConvertScipy.test_from_scipy_sparse_array_parallel_edges.<locals>.<listcomp>   "    )H)H)HA1a))H)H)Hr   r~   T)parallel_edgesr   F)r   r   r   r   r   c                     g | ]
\  }}||d fS r   r$   r.   s      r   r2   zPTestConvertScipy.test_from_scipy_sparse_array_parallel_edges.<locals>.<listcomp>   r   r   r   r>   N)rY   rZ   	csr_arrayr   r   r9   r   rA   r   MultiDiGraphr   set)r   rJ   expectedr7   actuals        r   +test_from_scipy_sparse_array_parallel_edgesz<TestConvertScipy.test_from_scipy_sparse_array_parallel_edges   s    I!Q!Q 011 :<<((((()H)H%)H)H)HIII!Qq)))+d
 
 
 FH-----+e"*
 
 
 FH----- 988?$$(()H)H%)H)H)HIII+d
 
 
 FH-----?$$E

1555&'Aq(#+e"/
 
 
 FH-------r   c                 
   t           j                            ddgddgg          }t          j        |t          j                  }t          j                    }|                    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   rU   r   r~   N)rY   rZ   r   r   rA   
MultiGraphr   r   )r   rJ   r%   r   s       r   test_symmetriczTestConvertScipy.test_symmetric   s     I!Q!Q 011&qr}EEE=??!Qq)))Ax(((((((r   N)r    r!   r"   r   r*   r   rS   r]   r`   rc   rf   rh   rs   r   r   r   r   r   r   r   r   r   r   r$   r   r   r
   r
      sK       5 5 5A A A  ' ' '<G G G
9 9 9
; ; ;
9 9 9
; ; ;
W W W$
 
 
%
 %
 %
N= = =1 1 1> > >
 
 
	
 	
 	
	
 	
 	
&. &. &.P
) 
) 
) 
) 
)r   r
   sparse_format)r   r   r   c                 \   t          j                    }|                    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          j        |                    sJ d	S )
z7Test all formats supported by _generate_weighted_edges.r   rU   r>   r   rV   )r   r   rV   )r   r   rU   )rV   rU   r   N)	r   r   r   rY   rZ   	coo_arrayasformatr   rA   )r   r   rJ   s      r   $test_from_scipy_sparse_array_formatsr   
  s     xzzHHa=!Ha=!Ha=!Ha=!Ha=!Ha=!	
	 	 	 		YYY			999=>>GGVVA""<Q"?"?@@@@@@@r   )r&   importorskipr   rY   networkxr   networkx.generators.classicr   r   r   networkx.utilsr   r
   markparametrizer   r$   r   r   <module>r      s    V!!V!!     N N N N N N N N N N ' ' ' ' ' '|) |) |) |) |) |) |) |)~ *?@@A A A@A A Ar   