
    Gdo6                        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             Ze j                            d	ed
fedfedff          d             Zd Ze j                            d ej                    e                    d dgdd gge          f ej                    e                    d dgd d gge          fg          d             Zd Ze j                            dedfedfedfej        dfej        dff          d             Ze j                            d ej                    ddgddggf ej                    ddgddggfg          d             Zd Z e j                            dd d!g          d"             Z!e j                            d#ej        ej        f          e j                            d$d%d d&d'difd d&d(d)d*fd d&d+dd,d-fg          d.                         Z"e j                            d/ ej                     ej                    f          d0             Z#e j                            d/ ej                     ej                    f          d1             Z$d2 Z%e j                            d#ej&        ej'        f          d3             Z(dS )4    Nnumpyznumpy.testing)barbell_graphcycle_graph
path_graph)graphs_equalc                   t    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S )TestConvertNumpyArrayc                    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    Alib/python3.11/site-packages/networkx/tests/test_convert_numpy.pysetup_methodz"TestConvertNumpyArray.setup_method   sc    A&&brz:::&&rxzz22&&rz||44    c                     t          d          }|                    |           |                    d |                                D                        |S )N   c              3   ,   K   | ]\  }}||d |z   fV  dS )r   N .0uvs      r   	<genexpr>z8TestConvertNumpyArray.create_weighted.<locals>.<genexpr>   s0      !G!GTQ1aa.!G!G!G!G!G!Gr   )r   add_nodes_fromadd_weighted_edges_fromedges)r   Ggs      r   r   z%TestConvertNumpyArray.create_weighted   sQ    NN		!!!G!GQWWYY!G!G!GGGGr   c                    t          |                                          t          |                                          k    sJ t          |                                          t          |                                          k    sJ d S )N)sortednodesr&   )r   r   r   s      r   assert_equalz"TestConvertNumpyArray.assert_equal   sj    bhhjj!!VBHHJJ%7%77777bhhjj!!VBHHJJ%7%7777777r   c                 f   |                                 dk    sJ t          j        ||          }|                     ||           t          j        ||          }|                     ||           t          j        d|                              |          }|                     ||           d S )Nr   r   )sumr   from_numpy_arrayr,   to_networkx_graphempty_graph	__class__)r   r'   Ar   GGGWGIs          r   identity_conversionz)TestConvertNumpyArray.identity_conversion   s    uuww{{{{ >>>!R   !!,???!R   ^A|,,66q99!R     r   c                     t                               g dg dg          }t          j        t          j        t          j        |           dS )z!Conversion from non-square array.)      r   )r         N)nparraypytestraisesr   NetworkXErrorr/   r   r3   s     r   
test_shapez TestConvertNumpyArray.test_shape%   s@    HHiii+,,b&(;Q?????r   c                     t          j        | j                  }|                     | j        |t          j                               dS )z(Conversion from graph to array to graph.N)r   to_numpy_arrayr   r7   r   rB   s     r   test_identity_graph_arrayz/TestConvertNumpyArray.test_identity_graph_array*   :    dg&&  !RXZZ88888r   c                     t          j        | j                  }|                     | j        |t          j                               dS )z,Conversion from digraph to array to digraph.N)r   rE   r   r7   r   rB   s     r   test_identity_digraph_arrayz1TestConvertNumpyArray.test_identity_digraph_array/   :    dg&&  !RZ\\:::::r   c                     t          j        | j                  }|                     | j        |t          j                               dS )z:Conversion from weighted graph to array to weighted graph.N)r   rE   r   r7   r   rB   s     r   "test_identity_weighted_graph_arrayz8TestConvertNumpyArray.test_identity_weighted_graph_array4   rG   r   c                     t          j        | j                  }|                     | j        |t          j                               dS )z>Conversion from weighted digraph to array to weighted digraph.N)r   rE   r   r7   r   rB   s     r   $test_identity_weighted_digraph_arrayz:TestConvertNumpyArray.test_identity_weighted_digraph_array9   rJ   r   c                    t          d          }t          d          }t          |          }t          j        ||          }t          j        |          }|                     ||           ||d         gz  }t          j        t          j        t          j        ||           g d}t          j        t          j        d||j	        z
   d          5  t          j        ||           d	d	d	           d	S # 1 swxY w Y   d	S )
z6Conversion from graph to array to graph with nodelist.r   r   nodelistr   )r   r9   zNodes z in nodelist is not in GmatchN)
r   listr   rE   r   r,   r?   r@   rA   r+   )r   P4P3rQ   r3   GAs         r   test_nodelistz#TestConvertNumpyArray.test_nodelist>   sE   ]]]]88b8444Xa[["b!!! 	Xa[M!b&(92QQQQ ::]H8bh.HHH
 
 
 	5 	5 b84444		5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s   C::C>C>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 )r9         ?333333?)weightotherNr   )r    ns     r   r#   z<TestConvertNumpyArray.test_weight_keyword.<locals>.<genexpr>U   s5      WWAq1us&C&CDWWWWWWr   r   r   r^   r\   r]   r_   )	r   r   add_edges_fromranger   rE   r=   testingr,   )r   WP4rV   r3   s       r   test_weight_keywordz)TestConvertNumpyArray.test_weight_keywordS   s    hjjWWeTUhhWWWWWW]]b!!

2#4S#F#F#FGGG

a):3)?)?@@@

a):3w)O)O)OPPPPPr   c                 
   t                               dgg          }t          j        |          }t	          |d         d         d                   t
          k    sJ t                               dgg                              t                    }t          j        |          }t	          |d         d         d                   t          k    sJ t                               dgg                              t                    }t          j        |          }t	          |d         d         d                   t          k    sJ t                               dgg                              t                    }t          j        |          }t	          |d         d         d                   t          k    sJ t                               dgg                              t                    }t          j        |          }t	          |d         d         d                   t          k    sJ t                               dgg                              t                    }t          j        t          t          j        |           t                               g dg dgg dg dgg          }t          j        t          j        d|j                   5  t          j        |          }d d d            d S # 1 swxY w Y   d S )Nr9   r   r^   )r9   r9   r9   zInput array must be 2D, not rS   )r=   r>   r   r/   typeintastypefloatstrboolcomplexobjectr?   r@   	TypeErrorrA   ndim)r   r3   r'   r(   s       r   test_from_numpy_array_typez0TestConvertNumpyArray.test_from_numpy_array_type\   s   HHqcUOO""AaDGH%&&#----HHqcUOO""5))""AaDGH%&&%////HHqcUOO""3''""AaDGH%&&#----HHqcUOO""4((""AaDGH%&&$....HHqcUOO""7++""AaDGH%&&'1111HHqcUOO""6**i!4a888HHyyy))),yyy))).DEFF]$K16$K$K
 
 
 	' 	' #A&&A	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   K88K<?K<c                    dt           fdt          fg}t                              dgg|          }t	          j        |          }t          |d         d         d                   t           k    sJ t          |d         d         d                   t          k    sJ |d         d         d         dk    sJ |d         d         d         dk    sJ d S )Nr^   cost)      ?r:   dtyper   r:   ru   )rk   ri   r=   r>   r   r/   rh   )r   dtr3   r'   s       r   test_from_numpy_array_dtypez1TestConvertNumpyArray.test_from_numpy_array_dtypez   s    &#/HHxj\H,,""AaDGH%&&%////AaDGFO$$++++tAwv!####tAwx C''''''r   c                    t                               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_numpy_array` function
        interprets integer weights as the number of parallel edges when
        creating a multigraph.

        r9   r:   )r   r   r   r9   r9   r   c                     g | ]
\  }}||d fS r9   r   r   s      r   
<listcomp>zNTestConvertNumpyArray.test_from_numpy_array_parallel_edges.<locals>.<listcomp>   "    )H)H)HA1a))H)H)Hr   ra   T)parallel_edgesr   F)r{   r|   r}   r9   r9   r   c                     g | ]
\  }}||d fS r   r   r   s      r   r   zNTestConvertNumpyArray.test_from_numpy_array_parallel_edges.<locals>.<listcomp>   r   r   r   r^   N)r=   r>   r   r   r%   add_edger/   r   MultiDiGraphrb   set)r   r3   expectedr&   actuals        r   $test_from_numpy_array_parallel_edgesz:TestConvertNumpyArray.test_from_numpy_array_parallel_edges   s    HHq!fq!f%&& :<<((((()H)H%)H)H)HIII!Qq)))$Qt"*UUUFH-----$Qu2:VVV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                               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 array has edges added only once to an
        undirected multigraph when using :func:`networkx.from_numpy_array`.

        r   r9   r   ra   N)r=   r>   r   r/   
MultiGraphr   r   )r   r3   r'   r   s       r   test_symmetricz$TestConvertNumpyArray.test_symmetric   s{    
 HHq!fq!f%&&>>>=??!Qq)))Ax(((((((r   c                     t          j        d          }t          j        |t                    }|j        t          k    sJ dS zTest that setting dtype int actually gives an integer array.

        For more information, see GitHub pull request #1363.

        r   rv   N)r   complete_graphrE   ri   rw   r   r'   r3   s      r   test_dtype_int_graphz*TestConvertNumpyArray.test_dtype_int_graph   s<     a  as+++w#~~~~~~r   c                     t          j        t          j        d                    }t          j        |t                    }|j        t          k    sJ dS r   )r   r   r   rE   ri   rw   r   s      r   test_dtype_int_multigraphz/TestConvertNumpyArray.test_dtype_int_multigraph   sF     M"+A..//as+++w#~~~~~~r   N)__name__
__module____qualname__r   r   r,   r7   rC   rF   rI   rL   rN   rY   rf   rr   ry   r   r   r   r   r   r   r   r	   r	      s       5 5 5  8 8 8! ! !@ @ @
9 9 9
; ; ;
9 9 9
; ; ;
5 5 5*Q Q Q' ' '<( ( (". ". ".H	) 	) 	)      r   r	   c                      t          j                    } |                     ddd           |                     ddd           | S )Nr9   r:      ra   F   )r   r   r   )r'   s    r   multigraph_test_graphr      sB    
AJJq!AJJJq!BJHr   )operatorr   M   r   r   c                 N    t          j        | |          }|d         |k    sJ d S )N)multigraph_weightr}   )r   rE   )r   r   r   r3   s       r   test_numpy_multigraphr      s3    
/8LLLAT7hr   c                     | }|                     ddd           t          j        |ddg          }|j        dk    sJ |d         d	k    sJ d S )
Nr   r9   r   ra   r:   rP   )r:   r:   r}   r   )r   r   rE   shape)r   r'   r3   s      r   'test_to_numpy_array_multigraph_nodelistr      s`    AJJq!AJ
!q!f---A7fT7b======r   zG, expected      ?       @rv   c                     |                      ddd           t          j        | t                    }t                              ||           d S )Nr   r9   r   ra   rv   )r   r   rE   rn   nptassert_array_equal)r'   r   r3   s      r   #test_to_numpy_array_complex_weightsr      sM     JJq!FJ###
!7+++A1h'''''r   c                     t          j                    } d}|                     ddd           t          j        | t                    }t
                              d|gddggt                    }t                              ||           t          j        | 	                                t                    }t
                              d|g|dggt                    }t                              ||           d S )Nl   F}r   r9   ra   rv   )
r   r   r   rE   ro   r=   r>   r   r   to_undirected)r'   wr3   r   s       r   %test_to_numpy_array_arbitrary_weightsr      s    

AAJJq!1J222
!6***Axx!Q!Q(x77H1h''' 	!//++6:::Axx!Q!Q(x77H1h'''''r   zfunc, expectedrR   r      gUUUUUU@r:   c                    t          j                    }g d}|D ]}|                    dd|           t          j        || t                    }t
                              |d|gddgg          sJ t          j        |                                | t                    }t
                              |d|g|dgg          sJ dS )z6Test various functions for reducing multiedge weights.)rR   r:   g      $@r   r9   ra   )r   rw   N)r   r   r   rE   rk   r=   allcloser   )funcr   r'   weightsr   r3   s         r   )test_to_numpy_array_multiweight_reductionr      s     	AmmG # #	

1a
""""
!t5AAAA;;qAx=1a&122222 	!//++t5QQQA;;qAx=8Q-89999999r   r{   )r   r;   c                 (   |                      dddd           t                              dt          fdt          fg          }t	          j        | |d	          }t                              ||
          }t                              ||           dS )zWhen `dtype` is structured (i.e. has names) and `weight` is None, use
    the named fields of the dtype to look up edge attributes.r   r9   r   g      @)r^   rt   r^   rt   Nrw   r^   rv   )	r   r=   rw   ri   r   rE   asarrayr   r   )r'   r   rw   r3   s       r   6test_to_numpy_array_structured_dtype_attrs_from_fieldsr   
  s     JJq!BSJ)))HHxo}566E
!5666Azz(%z00H1h'''''r   c                  6   t          j        d          } t                              dt          fg          }t          j        | |d           }t                              g dg dg dgt                    }t                              |d         |           d S )Nr   r^   r   )r   r9   r   )r9   r   r9   rv   )	r   r   r=   rw   rk   rE   r>   r   r   )r'   rw   r3   r   s       r   8test_to_numpy_array_structured_dtype_single_attr_defaultr     s    
aAHHx'())E
!5666AxxIIIyyy9xGGH1X;11111r   )
field_nameexpected_attr_val)r^   r9   )rt   r   c                 ^   t          j                    }|                    ddd           t                              | t
          fg          }t          j        ||d           }t                              d|g|dggt
                    }t          	                    ||          |           d S )Nr   r9   r   )rt   r   rv   )
r   r   r   r=   rw   rk   rE   r>   r   r   )r   r   r'   rw   r3   r   s         r   0test_to_numpy_array_structured_dtype_single_attrr   #  s     	

AJJq!!JHHz5)*++E
!5666Axx!./2CQ1GHPUxVVH1Z=(33333r   
graph_typeedger|   r9   r^   r;   )r^   flowg       @i)r^   rt   r   c                 >    | |g          }t                               dt          fdt          fdt          fg          }t          j        ||d           }|j        D ]?}t          j        |t          |          }t                              ||         |           @d S )Nr^   rt   r   r   )r=   rw   rk   r   rE   namesr   r   )r   r   r'   rw   r3   attrr   s          r   4test_to_numpy_array_structured_dtype_multiple_fieldsr   3  s     	
D6AHHx'&%65/JKKE
!5666A 2 2$QeDAAAqw11112 2r   r'   c                 r   |                      ddd           t                              dt          fdt          fg          }t	          j        | |d t          j                  }|j        D ]J}t	          j        | t          |t          j                  }t          	                    ||         |           Kd S )Nr   r9   r   ra   r^   rt   rw   r^   nonedge)
r   r=   rw   rk   r   rE   nanr   r   r   )r'   rw   r3   r   r   s        r   3test_to_numpy_array_structured_dtype_scalar_nonedger   F  s    JJq!BJHHx'&%9::E
!5rvFFFA 2 2$QeD"&QQQqw11112 2r   c                    |                      ddd           t                              dt          fdt          fg          }t                              dt          j        fg|          }t          j        | |d|	          }|j        D ]H}||         }t          j        | t          ||	          }t          
                    ||         |           IdS )
z[Similar to the scalar case, except has a different non-edge value for
    each named field.r   r9   r   ra   r^   rt   rv   Nr   )r   r=   rw   rk   r>   infr   rE   r   r   r   )r'   rw   nonedgesr3   r   r   r   s          r   0test_to_numpy_array_structured_dtype_nonedge_aryr   P  s     JJq!BJHHx'&%9::Exx!RVUx33H
!5xHHHA 2 24.$QeD'RRRqw11112 2r   c                     t          j        d          } t                              dt          fdt          fg          }d}t          j        t          |          5  t          j        | |           ddd           n# 1 swxY w Y   t          j        t          |          5  t          j        | |d           ddd           dS # 1 swxY w Y   dS )	zgUsing both a structured dtype (with named fields) and specifying a `weight`
    parameter is ambiguous.r   r^   rt   z7Specifying `weight` not supported for structured dtypesrS   rv   Nr   )	r   r   r=   rw   ri   r?   r@   
ValueErrorrE   )r'   rw   exception_msgs      r   7test_to_numpy_array_structured_dtype_with_weight_raisesr   ^  sG    	aAHHxo}566EMM	z	7	7	7 * *
!5))))* * * * * * * * * * * * * * *	z	7	7	7 9 9
!588889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9s$   A??BB%C

CCc                 &   t          j        d|           }t                              dt          fdt          fg          }t          j        t           j        d          5  t          j        ||d            d d d            d S # 1 swxY w Y   d S )Nr   r   r^   rt   z#Structured arrays are not supportedrS   r   )	r   r   r=   rw   ri   r?   r@   rA   rE   )r   r'   rw   s      r   0test_to_numpy_array_structured_multigraph_raisesr   j  s    
aj111AHHxo}566E	r'/T	U	U	U 7 7
!566667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s   !BB
B
))r?   importorskipr=   r   networkxr   networkx.generators.classicr   r   r   networkx.utilsr   r	   fixturer   markparametrizer.   minmaxr   r   r   r>   rn   r   r   r   meanmedianr   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s8   V!!f/**     N N N N N N N N N N ' ' ' ' ' 'y y y y y y y yx    1S"IQx#r3STT  UT
   	RXX6{VQK8XHHI	rxx!Vq!f 5WxEEF ( ( (( ( ( 	2Yb	C9rw&7")QH : :	 : 	vw''6):;<	)FF+;<= ( ( (2 2 2 ' 4 4 4 "*'=>>
	
A"~	
A!R(()	
A#r3778	 2 2  ?>2 xrxzz:2:<<8992 2 :92 xrxzz:2:<<899
2 
2 :9
2	9 	9 	9 r'GHH7 7 IH7 7 7r   