
    ܛ7i                          d dl Z d dlZd dlmZmZmZ d dlmZm	Z	m
Z
mZ e j                  j                  dd      d        Ze j                  j                  d      d        Zd	 Zd
 Zd Z G d d      Zy)    N)graph_edit_distanceoptimal_edit_pathsoptimize_graph_edit_distance)circular_ladder_graphcycle_graph
path_graphwheel_graphsource)
   fooc                    t        j                  d       t        j                  d      }t        j                  |d|       }t        j
                  t        j                  d      5  t        |       d d d        y # 1 sw Y   y xY w)Nnumpy      )sample_sizer
   zInitial node.*not in Gmatch)pytestimportorskipnxcomplete_graphgenerate_random_pathsraisesNodeNotFoundnext)r
   Gpath_gens      f/home/rose/Desktop/poly/venv/lib/python3.12/site-packages/networkx/algorithms/tests/test_similarity.py*test_generate_random_paths_source_not_in_Gr      s[    
 
!A''qHH	r.F	GX 
H	G	Gs   (A==Bzignore::RuntimeWarningc            	         t        j                  d       t        j                         } | j	                  g d       | j                  dd       t        t        j                  | dddd            }t        |      dk(  sJ t        d |D              sJ t        d	 |D              sJ t        j                  | dddd      }t        j                  t        d
      5  t        |       d d d        t        j                  | ddd      }t        j                  t        d
      5  t        |       d d d        y # 1 sw Y   RxY w# 1 sw Y   y xY w)Nr   )r         r   r!   r"   *   )path_lengthr
   seedc              3   8   K   | ]  }t        |      d k(    yw)r   N)len.0paths     r   	<genexpr>zAtest_generate_random_paths_with_isolated_nodes.<locals>.<genexpr>%   s     0%$s4yA~%s   c              3   ,   K   | ]  }|d    d k(    ywr   N r(   s     r   r+   zAtest_generate_random_paths_with_isolated_nodes.<locals>.<genexpr>&   s     .tAw!|s   zprobabilities contain NaNr   r$   r%   )r   r   r   Graphadd_nodes_fromadd_edgelistr   r'   allr   
ValueError)r   pathsr   s      r   .test_generate_random_paths_with_isolated_nodesr7      s   
 

AYJJq! ))!QAabQREu:??0%0000..... ''1!ABOH	z)D	EX 
F ''1!"EH	z)D	EX 
F	E 
F	E
 
F	Es   D<'E<EEc                     | |k(  S Nr.   n1n2s     r   nmatchr=   3       8O    c                     | |k(  S r9   r.   e1e2s     r   ematchrD   7   r>   r?   c                  >   t        j                         } | j                  dd       | j                  dd       | j                  dd       | j                  dd       | j                  ddd       | j                  ddd       | j                  ddd       | S )	NAlabelBCDa-bb-cb-d)r   r0   add_noder2   )r   s    r   getCanonicalrP   ;   s    

AJJs#JJJs#JJJs#JJJs#JJJsCuJ%JJsCuJ%JJsCuJ%Hr?   c                      e Zd Ze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 Zd Zd Zd Zd Zd Zej@                  ejB                  jD                  jF                  gZ$e%jL                  jO                  de$      d        Z(e%jL                  jO                  de$      d        Z)e%jL                  jO                  de$      d        Z*e%jL                  jO                  de$      d         Z+e%jL                  jO                  d!e$      d"        Z,d# Z-d$ Z.d% Z/d& Z0d' Z1d( Z2d) Z3d* Z4d+ Z5d, Z6e%jL                  jO                  d-d.      e%jL                  jO                  d/d0      d1               Z7d2 Z8d3 Z9d4 Z:d5 Z;d6 Z<d7 Z=d8 Z>d9 Z?d: Z@d; ZAd< ZBd= ZCd> ZDd? ZEd@ ZFdA ZGdB ZHdC ZIyD)ETestSimilarityc                 X    t        j                  d      at        j                  d       y )Nr   scipy)r   r   np)clss    r   setup_classzTestSimilarity.setup_classH   s"       )G$r?   c                    t        j                  d      }|j                         }t        j                  t
        t        ||dg       t        j                  t
        t        ||g d       t        j                  t         j                  t        ||d       t        j                  t         j                  t        ||d       t        j                  t         j                  t        ||d       t        ||d      d	k(  sJ t        ||d
      dk(  sJ t        ||dd      d	k(  sJ t        ||d
d      dk(  sJ t        ||d
d      J t        j                  t         j                  t        ||d	       y )Nr   r"   )roots)r"   r      )	   r   )r   r[   )r[   r[   r!   r"   r   r   r!      )rY   timeout-C6?)r_   )	r   
star_graphcopyr   r   r5   r   r   NetworkXError)selfG0G1s      r   *test_graph_edit_distance_roots_and_timeoutz9TestSimilarity.test_graph_edit_distance_roots_and_timeoutN   s$   ]]1WWYj"5r2aSIj"5r2YOboo':B&Qboo':B&Qboo':B&Q"2r8A==="2r8A==="2rCqHHH"2rCqHHH"2rHPPPb&&(;RQOr?   c                    t        j                         }t        d      }t        d      }t	        d      }t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ y )	N      r            r!   r^   )r   r0   r   r   r	   r   rd   re   rf   G2G3s        r   test_graph_edit_distancez'TestSimilarity.test_graph_edit_distance^   sx   XXZ]^^"2r*a///"2r*b000"2r*b000"2r*b000"2r*b000"2r*b000"2r*b000"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///r?   c                 >   t        d      }t        d      }|j                  j                         D ]  \  }}|dz  dk(  rdnd|d<    |j                  j                         D ]  \  }}|dz  dk(  rdnd|d<    t        ||      dk(  sJ t        ||d 	      dk(  sJ y )
Nr   r"   r   redbluecolorr!   c                     | d   |d   k(  S Nru   r.   r:   s     r   <lambda>zDTestSimilarity.test_graph_edit_distance_node_match.<locals>.<lambda>       "W+G2Lr?   )
node_match)r   nodesitemsr   )rd   rf   ro   nattrs        r   #test_graph_edit_distance_node_matchz2TestSimilarity.test_graph_edit_distance_node_matchx   s    ^^xx~~'GAt%&UaZEVDM (xx~~'GAt%&UaZEVDM ("2r*a///B#L 	
r?   c                 b   t        d      }t        d      }|j                  j                         D ]  \  }}t        |      dz  dk(  rdnd|d<    |j                  j                         D ]  \  }}t        |      dz  dk(  rdnd|d<    t	        ||      dk(  sJ t	        ||d 	      dk(  sJ y )
Nri   r"   r   rs   rt   ru   r   c                     | d   |d   k(  S rw   r.   rA   s     r   rx   zDTestSimilarity.test_graph_edit_distance_edge_match.<locals>.<lambda>   ry   r?   )
edge_matchr   edgesr|   minr   )rd   rf   ro   er~   s        r   #test_graph_edit_distance_edge_matchz2TestSimilarity.test_graph_edit_distance_edge_match   s    ]]xx~~'GAt%(VaZ1_E&DM (xx~~'GAt%(Vq[A%5E6DM ("2r*a///B#L 	
r?   c                 0   t        d      }t        d      }|j                  j                         D ]  \  }}|dz  dk(  rdnd|d<    |j                  j                         D ]  \  }}|dz  dk(  rdnd|d<    d }d	 }d
 }t        |||||      dk(  sJ y )Nri   r"   r   rs   rt   ru   r!   c                     | d   |d   k(  ryy)Nru   r!   r   r.   )uattrvattrs     r   node_subst_costzJTestSimilarity.test_graph_edit_distance_node_cost.<locals>.node_subst_cost   s    W~w/r?   c                     | d   dk(  ryy)Nru   rt      2   r.   r~   s    r   node_del_costzHTestSimilarity.test_graph_edit_distance_node_cost.<locals>.node_del_cost   s    G}&r?   c                     | d   dk(  ryy)Nru   rt   (   d   r.   r   s    r   node_ins_costzHTestSimilarity.test_graph_edit_distance_node_cost.<locals>.node_ins_cost   s    G}&r?   )r   r   r   )r   r{   r|   r   )rd   rf   ro   r}   r~   r   r   r   s           r   "test_graph_edit_distance_node_costz1TestSimilarity.test_graph_edit_distance_node_cost   s    ]]xx~~'GAt%&UaZEVDM (xx~~'GAt%&UaZEVDM (			   /++ 		
r?   c                 T   t        d      }t        d      }|j                  j                         D ]  \  }}t        |      dz  dk(  rdnd|d<    |j                  j                         D ]  \  }}t        |      dz  dk(  rdnd|d<    d }d	 }d
 }t	        |||||      dk(  sJ y )Nri   r"   r   rs   rt   ru   r   c                     | d   |d   k(  ryy)Nru   {Gz?皙?r.   )gattrhattrs     r   edge_subst_costzJTestSimilarity.test_graph_edit_distance_edge_cost.<locals>.edge_subst_cost   s    W~w/r?   c                     | d   dk(  ryy)Nru   rt   皙?      ?r.   r   s    r   edge_del_costzHTestSimilarity.test_graph_edit_distance_edge_cost.<locals>.edge_del_cost       G}&r?   c                     | d   dk(  ryy)Nru   rt   g?      ?r.   r   s    r   edge_ins_costzHTestSimilarity.test_graph_edit_distance_edge_cost.<locals>.edge_ins_cost   r   r?   )r   r   r   gq=
ףp?r   )rd   rf   ro   r   r~   r   r   r   s           r   "test_graph_edit_distance_edge_costz1TestSimilarity.test_graph_edit_distance_edge_cost   s    ]]xx~~'GAt%(VaZ1_E&DM (xx~~'GAt%(Vq[A%5E6DM (			   /++ 		
r?   c                     t        d      }t        d      }t        ||d      J t        ||d      dk(  sJ t        ||      dk(  sJ y )Nr"   ri   r   )upper_bound      )r   r   rd   rf   ro   s      r   $test_graph_edit_distance_upper_boundz3TestSimilarity.test_graph_edit_distance_upper_bound   sW    "1%"1%"2rq9AAA"2rr:b@@@"2r*b000r?   c                 @   t        d      }t        d      }t        ||      \  }}|dk(  sJ t        |      dk(  sJ d }g dg dfg dg dfg d	g d
fg dg dfg dg dfg dg dfg}|D ch c]  } || 	 c}|D ch c]  } || 	 c}k(  sJ y c c}w c c}w )Nr   r!   ri   c                 X    t        t        |             t        t        |d             fS )Nc                     d | v | fS r9   r.   )xs    r   rx   zKTestSimilarity.test_optimal_edit_paths.<locals>.canonical.<locals>.<lambda>   s    tqy!nr?   )key)tuplesorted)vertex_path	edge_paths     r   	canonicalz9TestSimilarity.test_optimal_edit_paths.<locals>.canonical   s)    f[)*fY,DEF r?   )r   r   r!   r!   r"   r"   )r]   r]   r\   r\   Nr   r"   )r   r\   r"   r!   )r]   r   r   Nr]   )r]   r!   r   r   )r   r\   r   Nr\   )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'   )rd   rf   ro   r6   costr   expected_pathsps           r   test_optimal_edit_pathsz&TestSimilarity.test_optimal_edit_paths   s    ]^(R0tqyy5zQ	 )D
 )D
 )D
 )D
 )D
 )D+
4 (--u!	1u-1XA)Q-1XXXX-1Xs   0BBc                 v    t        d      }t        d      }d}t        ||      D ]  }||k  sJ |} |dk(  sJ y )Nr"   ri   i  r   )r   r   )rd   rf   ro   bestcostr   s        r   !test_optimize_graph_edit_distancez0TestSimilarity.test_optimize_graph_edit_distance  sK    "1%"1%0R8D(?"?H 9 2~~r?   c                 *   t        j                         }t        j                         }|j                  d       t        j                         }|j                  d       t        j                         }|j                  d       t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ y )N)rF   rF   rF   rI   )r   rI   rI   )r   r   r   r   rZ   r   r!   )r   r0   add_edges_fromr   rn   s        r   test_selfloopszTestSimilarity.test_selfloops  s   XXZXXZ
23XXZ
23XXZ
>?"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///r?   c                 *   t        j                         }t        j                         }|j                  d       t        j                         }|j                  d       t        j                         }|j                  d       t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ y )N)r   rI   rJ   rJ   rK   )rK   rF   )r   r   r   rF   rK   )r   rF   rJ   )rI   rK   r   r   r^   r"   rZ   )r   DiGraphr   r   rn   s        r   test_digraphzTestSimilarity.test_digraph;  s   ZZ\ZZ\
JKZZ\
JKZZ\
JK"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///r?   c                 *   t        j                         }t        j                         }|j                  d       t        j                         }|j                  d       t        j                         }|j                  d       t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      d	k(  sJ t        ||      d	k(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ t        ||      dk(  sJ y )
N)r   r   r   )r   r   r   r   )r   r   r   r   r   r   ri   rj   r^   r!   r"   )r   
MultiGraphr   r   rn   s        r   test_multigraphzTestSimilarity.test_multigraphX  s   ]]_]]_
>?]]_
JK]]_
VW"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///"2r*a///r?   c                     t        j                         }|j                  d       t        j                         }|j                  d       t        ||      dk(  sJ t        ||      dk(  sJ y )N))hardwarekernel)r   r   )r   	userspace)r   r   ))winterspring)r   summer)r   autumn)r   r   r   )r   MultiDiGraphr   r   r   s      r   test_multidigraphz TestSimilarity.test_multidigraphu  sn    __
	
 __
	
 #2r*a///"2r*a///r?   c                     t        j                         }|j                  dd       |j                  dd       |j                  ddd       t	        ||j                         t        t              dk(  sJ y NrF   rG   rI   rL   rz   r   r   )r   r0   rO   r2   r   rb   r=   rD   rd   r   s     r   testCopyzTestSimilarity.testCopy  sa    HHJ	

3c
"	

3c
"	

35
)16686RVWW	
Wr?   c                 t   t        j                         }|j                  dd       |j                  dd       |j                  ddd       t        j                         }|j                  dd       |j                  dd       |j                  ddd       t	        ||t
        t              dk(  sJ y r   r   r0   rO   r2   r   r=   rD   r   s      r   testSamezTestSimilarity.testSame      XXZ
Cs#
Cs#
CE*XXZ
Cs#
Cs#
CE*"2rfPTUUUUr?   c                 t   t        j                         }|j                  dd       |j                  dd       |j                  ddd       t        j                         }|j                  dd       |j                  dd       |j                  ddd       t	        ||t
        t              dk(  sJ y )NrF   rG   rI   rL   badr   r!   r   r   s      r   testOneEdgeLabelDiffz#TestSimilarity.testOneEdgeLabelDiff  r   r?   c                 t   t        j                         }|j                  dd       |j                  dd       |j                  ddd       t        j                         }|j                  dd       |j                  dd       |j                  ddd       t	        ||t
        t              dk(  sJ y )NrF   rG   rI   rL   Zr   r!   r   r   s      r   testOneNodeLabelDiffz#TestSimilarity.testOneNodeLabelDiff  r   r?   c                    t        j                         }|j                  dd       |j                  dd       |j                  ddd       t        j                         }|j                  dd       |j                  dd       |j                  ddd       |j                  dd       t	        ||t
        t              dk(  sJ y )NrF   rG   rI   rL   rJ   r   r!   r   r   s      r   testOneExtraNodezTestSimilarity.testOneExtraNode  s    XXZ
Cs#
Cs#
CE*XXZ
Cs#
Cs#
CE*
Cs#"2rfPTUUUUr?   c                    t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  ddd       t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  ddd       |j                  ddd       t	        ||t
        t              dk(  sJ y )	NrF   rG   rI   rJ   rL   a-cr   r!   r   r   s      r   testOneExtraEdgezTestSimilarity.testOneExtraEdge  s    XXZ
Cs#
Cs#
Cs#
Cs#
CE*XXZ
Cs#
Cs#
Cs#
CE*
CE*"2rfPTUUUUr?   c                    t        j                         }|j                  dd       |j                  dd       |j                  ddd       t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  ddd       |j                  ddd       t	        ||t
        t              dk(  sJ y )	NrF   rG   rI   rL   rJ   r   r   r"   r   r   s      r   testOneExtraNodeAndEdgez&TestSimilarity.testOneExtraNodeAndEdge  s    XXZ
Cs#
Cs#
CE*XXZ
Cs#
Cs#
Cs#
CE*
CE*"2rfPTUUUUr?   c                    t               }t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j	                  ddd       |j	                  ddd       |j	                  ddd       t        ||t        t        	      d
k(  sJ y )NrF   rG   rI   rK   ErL   rN   d-er   r   rP   r   r0   rO   r2   r   r=   rD   r   s      r   
testGraph1zTestSimilarity.testGraph1      ^XXZ
Cs#
Cs#
Cs#
Cs#
CE*
CE*
CE*"2rfPTUUUUr?   c                    t               }t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j	                  ddd       |j	                  ddd       |j	                  ddd	       |j	                  ddd
       t        ||t        t              dk(  sJ y )NrF   rG   rI   rJ   rK   r   rL   rM   c-dzc-er   rZ   r  r   s      r   
testGraph2zTestSimilarity.testGraph2  s    ^XXZ
Cs#
Cs#
Cs#
Cs#
Cs#
CE*
CE*
CE*
CE*"2rfPTUUUUr?   c                 r   t               }t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j	                  ddd	       |j	                  ddd
       |j	                  ddd       |j	                  ddd       |j	                  ddd       |j	                  ddd       t        ||t        t              dk(  sJ y )NrF   rG   rI   rJ   rK   r   Fr   r   a-dr   zd-fzd-gze-br   rl   r  r   s      r   
testGraph3zTestSimilarity.testGraph3  s
   ^XXZ
Cs#
Cs#
Cs#
Cs#
Cs#
Cs#
Cs#
CE*
CE*
CE*
CE*
CE*
CE*"2rfPTVVVVr?   c                    t               }t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j	                  ddd       |j	                  ddd       |j	                  ddd       t        ||t        t        	      d
k(  sJ y )NrF   rG   rI   rJ   rK   rL   rM   r  r   r"   r  r   s      r   
testGraph4zTestSimilarity.testGraph4  r  r?   c                    t               }t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j	                  ddd       |j	                  ddd       |j	                  ddd       t        ||t        t        	      d
k(  sJ y )NrF   rG   rI   rJ   rK   rL   rM   r	  r   r"   r  r   s      r   testGraph4_azTestSimilarity.testGraph4_a  r  r?   c                    t               }t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j	                  ddd       |j	                  ddd       |j	                  ddd       t        ||t        t        	      d
k(  sJ y )NrF   rG   rI   rJ   rK   rL   rM   r   r   r!   r  r   s      r   testGraph4_bzTestSimilarity.testGraph4_b#  r  r?   simrank_similarityc           
         t        j                  d      }ddddddddddddddddddddddddddddddd} ||      }|j                         D ]$  \  }}|t        j                  ||   d      k(  r$J  t        j
                         }|j                  d	d
       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  g d       ddddddddddddddddddddddddddddddd} ||d      }|j                         D ]$  \  }}|t        j                  ||   d      k(  r$J  y )Nr   r!   퓭I?~$oC?퓭I?r   r!   r"   r   rZ   r   absr   UnivrG   ProfAr"   ProfBr   StudentArZ   StudentBr]   r   )r!   r   r"   rZ   )rZ   r"   )r   r           (-f?@<yX?gЏw?g$=?gPD?glɋݕ?gs,?皙?)importance_factor)r   r   r|   r   approxr   rO   r   )rd   r  r   expectedactualkvs          r    test_simrank_no_source_no_targetz/TestSimilarity.test_simrank_no_source_no_target5  s   NN1 %%%% &%%% &%%% &%%% &%%%;$
J $A&NN$DAqfQiT:::: %
 JJL	

1F
#	

1G
$	

1G
$	

1J
'	

1J
'	IJ !3@ST1!3@ST%%&& 3#61AST&&&%
( $A=NN$DAqfQiT:::: %r?   c                    t        j                  d      }dddddd} ||d      }|t        j                  |d	
      k(  sJ t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  g d       dddddd} ||dd      }|t        j                  |d	
      k(  sJ y )Nr   r!   r  r  r  r  r   r
   r   r  r  rG   r  r"   r  r   r  rZ   r  r  r   r!  r"  r#  r$  r
   r   r   r   r%  r   rO   r   rd   r  r   r&  r'  s        r   test_simrank_source_no_targetz,TestSimilarity.test_simrank_source_no_target  s    NN1!!!!
 $Aa06==T:::: JJL	

1F
#	

1G
$	

1G
$	

1J
'	

1J
'	IJS%7CDWX#AQG6==T::::r?   c                    t        j                  d      }t        j                  |t        t	        d                  }dddddd} ||d	      }|t        j                  |d
      k(  sJ t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  g d       t        t	        t        j                  |d      j                                     }t        j                  ||      }dddddd} ||dd      }|t        j                  |d
      k(  sJ y )Nr   abcder!   r  r  r  )abcdr   r3  r,  r   r  r   r  rG   r  r"   r  r   r  rZ   r  r  rH   r   r!  r"  )r  r  r  r  r  r#  r-  )r   r   relabel_nodesdict	enumerater   r%  r   rO   r   get_node_attributesvalues)rd   r  r   r&  r'  node_labelss         r   test_simrank_noninteger_nodesz,TestSimilarity.test_simrank_noninteger_nodes  sM   NN1QYw%7 89####
 $Ac26==T:::: JJL	

1F
#	

1G
$	

1G
$	

1J
'	

1J
'	IJ9R%;%;Aw%G%N%N%PQRQ, '+
 $AVL6==T::::r?   c                    t        j                  d      }d} ||dd      }|t        j                  |d      k(  sJ t        j                         }|j                  dd       |j                  dd	       |j                  d
d       |j                  dd       |j                  dd       |j                  g d       d} ||ddd
      }|t        j                  |d      k(  sJ y )Nr   r!   r   r
   targetr   r  r  rG   r  r"   r  r   r  rZ   r  r  r!  r#  )r$  r
   r@  gh㈵>r.  r/  s        r   test_simrank_source_and_targetz-TestSimilarity.test_simrank_source_and_target  s    NN1#Aa:6==T:::: JJL	

1F
#	

1G
$	

1G
$	

1J
'	

1J
'	IJ% $AQqQ6==T::::r?   algc                 |    t        j                  d      }t        j                  t         j                  ||d       y )Nr   r   )max_iterations)r   r   r   r   ExceededMaxIterations)rd   rB  r   s      r   test_simrank_max_iterationsz*TestSimilarity.test_simrank_max_iterations  s(    NN1b..QrJr?   c                     t        j                  d      }t        j                  t         j                  d      5  t        j
                  |d       d d d        y # 1 sw Y   y xY w)Nr   Source node 10 not in Gr   r   r,  r   r   r   r   r   r  r   s     r   test_simrank_source_not_foundz,TestSimilarity.test_simrank_source_not_found  @    NN1]]2??2KL!!!B/ MLL   AA%c                     t        j                  d      }t        j                  t         j                  d      5  t        j
                  |d       d d d        y # 1 sw Y   y xY w)Nr   zTarget node 10 not in Gr   r   )r@  rI  r   s     r   test_simrank_target_not_foundz,TestSimilarity.test_simrank_target_not_found  rK  rL  c                    t        j                  d      }dddddd}ddd	d	dd}t        j                  |d
      }|t        j                  |d      k(  sJ |t        j                  |d      k7  sJ |t        j                  |d      k(  sJ t         j
                  j                  |d
      }|t        j                  |d      k(  sJ |t        j                  |d      k7  sJ |t        j                  |d      k(  sJ y )Nr   r!   gLZ??gR9Y@?gR9Y@?r  r   IC?ΫcA?r   r,  Hz>r  r`   gMbP?)r   r   r  r   r%  
similarity_simrank_similarity_python)rd   r   expected_python_tol4expected_numpy_tol4r'  s        r   test_simrank_between_versionsz,TestSimilarity.test_simrank_between_versions  s   NN1  !!  
 !  !
 &&q3"fmmF&EEEE#v}}V'FFFF#v}}V'FFFF99!A9F#v}}V'FFFF"fmmF&EEEE"fmmF&EEEEr?   c                     t        j                  d      }t        j                  g dg dg dg dg dg      }t         j                  j                  |      }t        j                  j                  ||d       y )	Nr   r   rP  rQ  rQ  rP  )rP  r   rP  rQ  rQ  )rQ  rP  r   rP  rQ  )rQ  rQ  rP  r   rP  )rP  rQ  rQ  rP  r   rR  atolr   r   rU   arrayrS  _simrank_similarity_numpytestingassert_allcloserd   r   r&  r'  s       r   &test_simrank_numpy_no_source_no_targetz5TestSimilarity.test_simrank_numpy_no_source_no_target  sl    NN188;$&
N 88;


""8V$"?r?   c                     t        j                  d      }t        j                  g d      }t         j                  j                  |d      }t        j                  j                  ||d       y )Nr   rY  r   r,  rR  rZ  r\  ra  s       r   #test_simrank_numpy_source_no_targetz2TestSimilarity.test_simrank_numpy_source_no_target4  sW    NN188
 8818E


""8V$"?r?   c                     t        j                  d      }d}t         j                  j                  |dd      }t        j
                  j                  ||d       y )Nr   r   r   r?  rR  rZ  )r   r   rS  r^  rU   r_  r`  ra  s       r   $test_simrank_numpy_source_and_targetz3TestSimilarity.test_simrank_numpy_source_and_targetB  sI    NN1881Q8O


""8V$"?r?   c                 .   t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       ddddd}t        j                  |ddd	
      }||k(  sJ y )Nr   r!   r"   r   rZ   r   g      ?)r   r"   r!   rZ   r#   r/   r   r0   r2   panther_similarityrd   r   r&  sims       r   "test_panther_similarity_unweightedz1TestSimilarity.test_panther_similarity_unweightedH  s    HHJ	

1a	

1a	

1a	

1a	

1assu5##AqabAhr?   c                 D   t        j                         }|j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd	       |j                  dd
d       ddddd}t        j                  |dddd      }||k(  sJ y )Nv1v2r   )wv3r!   v4r"   r   v5g      ?r   g      ?)rq  rr  ro  rs  rp  r#   )r$   weightr%   rh  rj  s       r    test_panther_similarity_weightedz/TestSimilarity.test_panther_similarity_weightedS  s    HHJ	

4
#	

4
#	

4
#	

4
%	

4
#cDA##At3RPhr?   c                     t        j                         }|j                  g d       t        j                  t         j
                  d      5  t        j                  |d       d d d        y # 1 sw Y   y xY w)Nr]   r   )r   r   r\   r  rH  r   r   r,  )r   r0   r   r   r   r   ri  r   s     r   (test_panther_similarity_source_not_foundz7TestSimilarity.test_panther_similarity_source_not_found^  sL    HHJ	AB]]2??2KL!!!B/ MLLs   A..A7c                    t        j                         }|j                  t        d             t	        j
                  t         j                  d      5  t        j                  |d       d d d        y # 1 sw Y   y xY w)Nr   zAPanther similarity is not defined for the isolated source node 1.r   r!   r,  )r   r0   r1   ranger   r   NetworkXUnfeasibleri  r   s     r    test_panther_similarity_isolatedz/TestSimilarity.test_panther_similarity_isolatedd  sY    HHJ	q"]]!!U
 !!!A.	
 
 
s   A55A>	num_paths)r!   r   r   r
   r]   c                    t        j                  g d      }i }t        j                  ||d|      }t        |      }t	        |      |k(  sJ t        fd|D              sJ t        |         t        t        |            k(  sJ y )Nrw  r"   )r$   	index_mapr
   c              3   .   K   | ]  }|d    k(    ywr-   r.   )r)   r   r
   s     r   r+   zGTestSimilarity.test_generate_random_paths_with_start.<locals>.<genexpr>  s     15a1Q46>5s   )r   r0   r   r3   r'   r4   r   rz  )rd   r}  r
   r   r  r   r6   s     `    r   %test_generate_random_paths_with_startz4TestSimilarity.test_generate_random_paths_with_startm  s     HH=>	++
 X 5zY&&&151111 i'(Dy1A,BBBBr?   c                    i }d}d}t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       t        j                  ||||d      }g d	g d
g dg dg dg dg dg dg dg dg
}h dh dh dh dddhd}|t	        |      k(  sJ ||k(  sJ y )Nr   r"   r   r!   r   rZ   r#   r$   r  r%   )r   r   r   )rZ   r"   r!   )r"   r!   r   )r"   r   r   )r   r   r!   )rZ   r"   r   )r   r   r"   )r"   r!   r"   >   r   r"   r   rZ   r   ri   rj   r^   >   r!   r"   rZ   r   rj   r[   >   r!   r"   r   ri   rj   r^   r[   >   r   r   rZ   r   r^   ri   r  r   r0   r2   r   r3   rd   r  r}  r$   r   r6   r   expected_maps           r   %test_generate_random_paths_unweightedz4TestSimilarity.test_generate_random_paths_unweighted  s    		HHJ	

1a	

1a	

1a	

1a	

1a((ykYR
 
 (!$1v
 e,,,y(((r?   c                    i }d}d}t        j                         }|j                  ddd       |j                  ddd       |j                  dd	d
       |j                  ddd       |j                  ddd       |j                  dd	d       t        j                  ||||d      }g dg dg dg dg dg dg dg dg dg dg
}h dh dh dh dh dh dd }|t	        |      k(  sJ ||k(  sJ y )!Nr   ri   r3  r4  g333333?rt  r5  r   r6  r   r   gffffff?fg?g333333?r#   r  )r6  r5  r  r5  r6  r3  r4  )r   r5  r  r5  r  r5  r   )r6  r3  r4  r3  r4  r3  r5  )r4  r3  r6  r3  r4  r3  r4  )r6  r3  r4  r3  r4  r3  r6  )r6  r3  r4  r3  r4  r3  r4  )r  r5  r  r5  r  r5  r   )r6  r3  r6  r3  r4  r3  r4  )r   r5  r  r5  r   r5  r6  >   r   r"   r   rZ   r   ri   r^   r[   >   r   r!   r"   r   rj   r[   >   r   r!   rj   r[   >   r   r"   r   rZ   r   ri   r^   >   r!   rj   r[   )r6  r5  r  r3  r4  r   r  r  s           r   #test_generate_random_paths_weightedz2TestSimilarity.test_generate_random_paths_weighted  s   		HHJ	

3C
(	

3C
(	

3C
(	

3C
(	

3C
(	

3C
(((ykYR

 0/////////
 *#&&
 e,,,y(((r?   c                     t        j                  dg      }t        j                         }t        j                  ||      dk(  sJ y )Nr   r"   r   r   r   r   s      r   &test_one_node_one_loop_and_empty_graphz5TestSimilarity.test_one_node_one_loop_and_empty_graph  s8    ZZ!ZZ\%%b"-222r?   c                     t        j                  ddg      }t        j                  |t        j                               dk(  sJ t        j                  |t        j                               dk(  sJ y )Nr   r   )r   r   r   r   )rd   rf   s     r   'test_one_node_two_loops_and_empty_graphz6TestSimilarity.test_one_node_two_loops_and_empty_graph  sX    __ff-.%%b"**,71<<<%%b"//*;<AAAr?   c                     t        j                  ddg      }t        j                  |t        j                               dk(  sJ y )Nr   r   rZ   r  r   s     r   test_two_directed_loopsz&TestSimilarity.test_two_directed_loops  s6    JJ'(%%a6!;;;r?   c                    d\  }}t        j                         }|j                  ||f       |j                  ||fg       t        j                         }|j                  ||f       |j                  ||fg       ||fD ]>  }|D ]  }||j                  |   d<    |j
                  D ]  }||j
                  |   d<    @ d }t        j                  ||||      dk(  sJ t        j                  ||||      dk(  sJ y)zgG2 has edge (a,b) and G3 has edge (a,a) but node order for G2 is (a,b)
        while for G3 it is (b,a)r   r~   c                     | |k(  S r9   r.   )r   ys     r   
user_matchzETestSimilarity.test_symmetry_with_custom_matching.<locals>.user_match  s    6Mr?   r   r!   N)r   r0   r1   r   r{   r   r   )	rd   r3  r4  ro   rp   r   r}   r   r  s	            r   "test_symmetry_with_custom_matchingz1TestSimilarity.test_symmetry_with_custom_matching  s    1XXZ
1a&!
Aq6(#XXZ
1a&!
Aq6(#bA%&
6" WW%&
6"  	 ""2rjZX	
 ""2rjZX	
r?   c                    t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       t        j                  |ddddd      }t	        |      dkD  sJ d|vsJ t        d |D              sJ t        d	 |j                         D              sJ y
)z2Basic test for panther_vector_similarity function.r   r!   r"   r   rZ   r#   rK   r(  r$   r%   c              3   $   K   | ]  }|d v  
 yw)r!   r"   r   rZ   Nr.   r)   nodes     r   r+   zFTestSimilarity.test_panther_vector_similarity_basic.<locals>.<genexpr>  s     8CD4<'Cs   c              3   <   K   | ]  }d |cxk  xr dk  nc   ywr   r!   Nr.   r)   scores     r   r+   zFTestSimilarity.test_panther_vector_similarity_basic.<locals>.<genexpr>       =u1???   Nr   r0   r2   panther_vector_similarityr'   r4   r;  rd   r   rk  s      r   $test_panther_vector_similarity_basicz3TestSimilarity.test_panther_vector_similarity_basic  s    HHJ	

1a	

1a	

1a	

1a	

1a**1a1qrR3x!||||8C8888=

====r?   c                    t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       |j                  dd       t        j                  |ddddd      t	              dk(  sJ dvsJ t        fdd	D              sJ t        d
 j                         D              sJ y)z5Test panther_vector_similarity with unweighted graph.r   r!   r"   r   rZ   r#   r  c              3   &   K   | ]  }|v  
 y wr9   r.   r)   r  rk  s     r   r+   zKTestSimilarity.test_panther_vector_similarity_unweighted.<locals>.<genexpr>  s     8<443;<   r  c              3   <   K   | ]  }d |cxk  xr dk  nc   ywr  r.   r  s     r   r+   zKTestSimilarity.test_panther_vector_similarity_unweighted.<locals>.<genexpr>  r  r  Nr  r  s     @r   )test_panther_vector_similarity_unweightedz8TestSimilarity.test_panther_vector_similarity_unweighted
  s    HHJ	

1a	

1a	

1a	

1a	

1a**1a1qrR3x1}}||8<8888=

====r?   c           	         t        j                         }|j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd	       |j                  dd
d       t        j                  |dddddd      t	              dk(  sJ dvsJ t        d j                         D              sJ t        fddD              sJ y)z3Test panther_vector_similarity with weighted graph.rn  ro  r   r  rq  r!   rr  r"   r   rs  r   rZ   rt  r#   )rK   r(  r$   rt  r%   c              3   <   K   | ]  }d |cxk  xr dk  nc   ywr  r.   r  s     r   r+   zITestSimilarity.test_panther_vector_similarity_weighted.<locals>.<genexpr>)  r  r  c              3   &   K   | ]  }|v  
 y wr9   r.   r  s     r   r+   zITestSimilarity.test_panther_vector_similarity_weighted.<locals>.<genexpr>*  s     >+=443;+=r  )ro  rq  rr  Nr  r  s     @r   'test_panther_vector_similarity_weightedz6TestSimilarity.test_panther_vector_similarity_weighted  s    HHJ	

4a
(	

4a
(	

4a
(	

4c
*	

4a
(**tqA1XB
 3x1}}3=

====>+=>>>>r?   c                     t        j                         }|j                  g d       t        j                  t         j
                        5  t        j                  |d       ddd       y# 1 sw Y   yxY w)z=Test panther_vector_similarity with non-existent source node.rw  r   r,  N)r   r0   r   r   r   r   r  r   s     r   /test_panther_vector_similarity_source_not_foundz>TestSimilarity.test_panther_vector_similarity_source_not_found,  sH    HHJ	AB]]2??+((26 ,++s   A,,A5c                 "   t        j                         }|j                  t        d             |j	                  dd       t        j                  t         j                        5  t        j                  |d       ddd       y# 1 sw Y   yxY w)z9Test panther_vector_similarity with isolated source node.r   r   r!   r"   r,  N)	r   r0   r1   rz  r2   r   r   r{  r  r   s     r   'test_panther_vector_similarity_isolatedz6TestSimilarity.test_panther_vector_similarity_isolated4  s[    HHJ	q"	

1a]]2001((15 211s   $BBc                     t        j                  d      }t        j                  t         j                        5  t        j
                  |ddd       ddd       y# 1 sw Y   yxY w)z%Test raises when D > number of nodes.r   r   r   )rK   r(  Nr   ra   r   r   r{  r  r   s     r   *test_panther_vector_similarity_too_large_Dz9TestSimilarity.test_panther_vector_similarity_too_large_D=  sB    MM!]]2001((Aa8 211s   AA%c                     t        j                  d      }t        j                  t         j                        5  t        j
                  |dd       ddd       y# 1 sw Y   yxY w)z%Test raises when k > number of nodes.r   r   r   )r(  Nr  r   s     r   *test_panther_vector_similarity_too_large_kz9TestSimilarity.test_panther_vector_similarity_too_large_kD  s@    MM!]]2001((A3 211s   AA$c                     t        j                         }|j                  dd       t        j                  |dddd      }t	        |      dk(  sJ d|v sJ |d   dkD  sJ y)z7Test panther_vector_similarity with a very small graph.r   r!   r"   r#   )rK   r(  r%   N)r   r0   r2   r  r'   r  s      r   *test_panther_vector_similarity_small_graphz9TestSimilarity.test_panther_vector_similarity_small_graphK  s^    HHJ	

1a**1a1C3x1}}Cxx1vzzr?   c                     t        j                         }|j                  g d       t        j                  |dddd      }t        j                  |dddd      }||k(  sJ y)z4Test that results are deterministic with fixed seed.rw  r   r   r"   r#   )rK   r$   r%   N)r   r0   r   r  )rd   r   sim1sim2s       r   ,test_panther_vector_similarity_deterministicz;TestSimilarity.test_panther_vector_similarity_deterministicV  sX    HHJ	AB++AqA12N++AqA12Nt||r?   c                     t        j                  d       t        j                         }|j	                  g d       t        j
                  |dddd      }d|vsJ t        d |D              sJ y)	z/Test panther_similarity with string node names.r   r   r   r   r   rF   r"   r#   )r(  r$   r%   c              3   <   K   | ]  }t        |t                y wr9   
isinstancestrr  s     r   r+   zFTestSimilarity.test_panther_similarity_string_nodes.<locals>.<genexpr>j       9ST:dC(Sr  N)r   r   r   r0   r   ri  r4   r  s      r   $test_panther_similarity_string_nodesz3TestSimilarity.test_panther_similarity_string_nodesa  sb    G$HHJ	IJ##AsaQRH#~~9S9999r?   c                     t        j                  d       t        j                         }|j	                  g d       t        j
                  |ddddd      }d|vsJ t        d |D              sJ y	)
z6Test panther_vector_similarity with string node names.r   r  rF   r   r"   r#   r  c              3   <   K   | ]  }t        |t                y wr9   r  r  s     r   r+   zMTestSimilarity.test_panther_vector_similarity_string_nodes.<locals>.<genexpr>u  r  r  N)r   r   r   r0   r   r  r4   r  s      r   +test_panther_vector_similarity_string_nodesz:TestSimilarity.test_panther_vector_similarity_string_nodesl  se    G$HHJ	IJ**1cQ!QST#~~9S9999r?   c                    t        j                  d       t        j                  d      }dD ]  }t        j                  |d|d      }t        |      |k(  sJ d| dt        |       d	       d|vsJ d
       t        j                  |d|d      }t        |      |k(  sJ d| dt        |       d	       d|vrJ d
        y )Nr   r   )r!   r"   r   rZ   r   r   r!   r#   )r
   r(  r%   zpanther_similarity k=z
 returned z resultsz$Source node should not be in resultszpanther_vector_similarity k=)r   r   r   ra   ri  r'   r  )rd   r   k_valresult_pantherresult_vectors        r   5test_panther_similarity_k_parameter_returns_k_resultszDTestSimilarity.test_panther_similarity_k_parameter_returns_k_resultsw  s    G$MM#(E221Q%bQN~&%/ 'wj^9L8MXV/ N*R,RR*881TVWM}%. .ugZM@R?SS[\. M)Q+QQ) )r?   N)J__name__
__module____qualname__classmethodrW   rg   rq   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  
algorithmsrS  rT  simrank_algsr   markparametrizer*  r0  r=  rA  rF  rJ  rN  rW  rb  rd  rf  rl  ru  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r.   r?   r   rR   rR   G   sh   % %
P 04

#
J#
J1'YR0:0:0:00
	V	V	V
VVV
VVW$
V
V
V 	
  ;;L
 [[1<@K; AK;Z [[1<@; A;6 [[1<@"; A";H [[1<@; A;, [[UL1K 2K0
0
F<*@X@@		0/ [[[*5[[Xv.C / 6C*")H%)N3
B
<
:> > ?$7694			:	:Rr?   rR   )r   networkxr   networkx.algorithms.similarityr   r   r   networkx.generators.classicr   r   r   r	   r  r  r   filterwarningsr7   r=   rD   rP   rR   r.   r?   r   <module>r     s      
  ;/ 0 45 6.	R Rr?   