o
    3ήc[                     @   s   d dl Z d dlZd dlmZ dd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 ZdS )7    N)directedc                  C   s   t  } t  }t | }t |}t j|dd}t j|dd}t j|ddd}t j|dd}||ks5J ||ks;J ||ksAJ ||ksGJ ||ksMJ dS )	zD
    empty graphs should give hashes regardless of other params
    
edge_attr1	edge_attr
node_attr1	node_attrr   r   
   
iterationsN)nxempty_graphweisfeiler_lehman_graph_hash)G1G2h1h2h3h4h5h6 r   S/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/tests/test_graph_hashing.pytest_empty_graph_hash	   s   

r   c                  C   V   d} t | D ]"}tjd|  d| d}t|}t|}t|}||ks(J qdS )z
    A directed graph with no bi-directional edges should yield different a graph hash
    to the same graph taken as undirected if there are no hash collisions.
    r
   d   seedN)ranger   gn_graphto_undirectedr   )ri
G_directedG_undirected
h_directedh_undirectedr   r   r   test_directed       


r(   c                  C   b   t jdt jd} t j| dd |  D dd |  }t j| dd}t j|dd}||ks/J dS )	z
    A directed graph with no bi-directional edges should yield different a graph hash
    to the same graph taken with edge directions reversed if there are no hash collisions.
    Here we test a cycle graph which is the minimal counterexample
       create_usingc                 S      i | ]}|t |qS r   str.0nr   r   r   
<dictcomp>7       z!test_reversed.<locals>.<dictcomp>labelnamer   N)r   cycle_graphDiGraphset_node_attributesnodesreverser   G
G_reversedh
h_reversedr   r   r   test_reversed0      rC   c                  C   sz   d\} }d| }t d|d D ]+}tj| || d| d}t|dd | D }t|}t|}||ks:J qdS )	zt
    graph hashes should be invariant to node-relabeling (when the output is reindexed
    by the same mapping)
    r   r
         ?      r   c                 S      i | ]}|d | qS r   r2   ur   r   r   r4   J   r5   z#test_isomorphic.<locals>.<dictcomp>N)r   r   erdos_renyi_graphrelabel_nodesr<   r   )r3   r"   pr#   r   r   g1_hashg2_hashr   r   r   test_isomorphicA   s   

rS   c                  C   s$  d\} }d| }t d|d D ]}tj| || d| d}|jD ] \}}| d| d|| | d< | d| d	|| | d
< q tj|dd}tj|d
d}tj|dd}	||	ks\J ||	ksbJ ||kshJ t|dd | D }
tj|
dd}tj|
d
d}||ksJ ||ksJ qdS )a  
    Isomorphic graphs with differing edge attributes should yield different graph
    hashes if the 'edge_attr' argument is supplied and populated in the graph,
    and there are no hash collisions.
    The output should still be invariant to node-relabeling
    rE   rF   rG   ,  r   --1r   -2
edge_attr2r   Nc                 S   rI   rJ   r   rL   r   r   r   r4   n   r5   z-test_isomorphic_edge_attr.<locals>.<dictcomp>)r   r   rN   edgesr   rO   r<   r3   r"   rP   r#   r   abg1_hash_with_edge_attr1g1_hash_with_edge_attr2g1_hash_no_edge_attrr   g2_hash_with_edge_attr1g2_hash_with_edge_attr2r   r   r   test_isomorphic_edge_attrR   s6   rb   c                  C   >   t  } | ddddifddi fg tjtt j| dd dS zz
    If the 'edge_attr' argument is supplied but is missing from an edge in the graph,
    we should raise a KeyError
    rG      r   r[      r   N)r   Graphadd_edges_frompytestraisesKeyErrorr   r?   r   r   r   test_missing_edge_attr{   s   rm   c                  C   s  d\} }d| }t d|d D ]w}tj| || d| d}| D ]}| d|j| d< | d|j| d	< q!tj|dd
}tj|d	d
}tj|dd
}||ksSJ ||ksYJ ||ks_J t|dd | D }	tj|	dd
}
tj|	d	d
}||
ksJ ||ksJ qdS )a  
    Isomorphic graphs with differing node attributes should yield different graph
    hashes if the 'node_attr' argument is supplied and populated in the graph, and
    there are no hash collisions.
    The output should still be invariant to node-relabeling
    rE   rF   rG     r   rV   r   rW   
node_attr2r   Nc                 S   rI   rJ   r   rL   r   r   r   r4      r5   z-test_isomorphic_node_attr.<locals>.<dictcomp>)r   r   rN   r<   r   rO   r3   r"   rP   r#   r   rM   g1_hash_with_node_attr1g1_hash_with_node_attr2g1_hash_no_node_attrr   g2_hash_with_node_attr1g2_hash_with_node_attr2r   r   r   test_isomorphic_node_attr   s6   rv   c                  C   H   t  } | dddifdi fg | g d tjtt j| dd dS zy
    If the 'node_attr' argument is supplied but is missing from a node in the graph,
    we should raise a KeyError
    rG   r   r[   re   ))rG   re   )re   rf   )rf   rG   )rG      r   N)r   rg   add_nodes_fromrh   ri   rj   rk   r   rl   r   r   r   test_missing_node_attr   s   r{   c                  C   s  d\} }d| }t d|d D ]}tj| || d| d}| D ]}| d|j| d< | d|j| d	< q!|jD ] \}}| d
| d|| | d< | d
| d|| | d< q;tj|ddd}tj|dd	d}	tj|dd	d}
t|}||ksJ |	|ksJ ||	ksJ |
|	ksJ |
|ksJ t|dd | D }tj|ddd}tj|dd	d}||ksJ |	|ksJ qdS )a  
    Isomorphic graphs with differing node attributes should yield different graph
    hashes if the 'node_attr' and 'edge_attr' argument is supplied and populated in
    the graph, and there are no hash collisions.
    The output should still be invariant to node-relabeling
    rE   rF   rG     r   rV   r   rW   ro   rU   r   rX   r	   c                 S   rI   rJ   r   rL   r   r   r   r4      r5   z;test_isomorphic_edge_attr_and_node_attr.<locals>.<dictcomp>N)r   r   rN   r<   rY   r   rO   r3   r"   rP   r#   r   rM   r[   r\   g1_hash_edge1_node1g1_hash_edge2_node2g1_hash_edge1_node2g1_hash_no_attrr   g2_hash_edge1_node1g2_hash_edge2_node2r   r   r   'test_isomorphic_edge_attr_and_node_attr   sF   
r   c                  C   s   d\} }d| }t d|d D ]0}tj| || d| d}t|}tj|dd}||ks/J t|dks7J t|dks?J qd	S )
d
    The hash string lengths should be as expected for a variety of graphs and
    digest sizes
    rE   rF   rG     r       digest_size@   N)r   r   rN   r   len)r3   r"   rP   r#   r?   h16h32r   r   r   test_digest_size   s   
r   c                    s   t  fdd|  D S )z
    returns True if that each hash sequence in 'a' is a prefix for
    the corresponding sequence indexed by the same node in 'b'.
    c                 3   s,    | ]\}} | d t | |kV  qd S Nr   )r2   nodehashesr\   r   r   	<genexpr>  s   * z"is_subiteration.<locals>.<genexpr>)allitems)r[   r\   r   r   r   is_subiteration   s   r   c                    s.   |d   fddt fdd|  D S )a  
    returns True if all hex digest sizes are the expected length in a node:subgraph-hashes
    dictionary. Hex digest string length == 2 * bytes digest length since each pair of hex
    digits encodes 1 byte (https://docs.python.org/3/library/hashlib.html)
    re   c                    s   t  fdd| D S )Nc                 3   s    | ]	}t | kV  qd S r   r   )r2   xhexdigest_sizer   r   r         z<hexdigest_sizes_correct.<locals>.<lambda>.<locals>.<genexpr>)r   )lr   r   r   <lambda>  r5   z)hexdigest_sizes_correct.<locals>.<lambda>c                 3   s    | ]} |V  qd S r   r   r2   r   )list_digest_sizes_correctr   r   r     s    z*hexdigest_sizes_correct.<locals>.<genexpr>)r   values)r[   r   r   )r   r   r   hexdigest_sizes_correct  s   r   c                  C   s   t  } t | }t j| dd}t j| dd}t j| dd}t j| dd}|i ks+J |i ks1J |i ks7J |i ks=J |i ksCJ dS )	zZ "
    empty graphs should give empty dict subgraph hashes regardless of other params
    r   r   r   re   r   r   r   N)r   r   !weisfeiler_lehman_subgraph_hashes)r?   subgraph_hashes1subgraph_hashes2subgraph_hashes3subgraph_hashes4subgraph_hashes5r   r   r   test_empty_graph_subgraph_hash  s   
r   c                  C   r   )z
    A directed graph with no bi-directional edges should yield different subgraph hashes
    to the same graph taken as undirected, if all hashes don't collide.
    r
   r   r   N)r   r   r    r!   r   )r"   r#   r$   r%   directed_subgraph_hashesundirected_subgraph_hashesr   r   r   test_directed_subgraph_hash&  r)   r   c                  C   r*   )	z
    A directed graph with no bi-directional edges should yield different subgraph hashes
    to the same graph taken with edge directions reversed if there are no hash collisions.
    Here we test a cycle graph which is the minimal counterexample
    r+   r,   c                 S   r.   r   r/   r1   r   r   r   r4   =  r5   z/test_reversed_subgraph_hash.<locals>.<dictcomp>r6   r7   r   N)r   r9   r:   r;   r<   r=   r   r>   r   r   r   test_reversed_subgraph_hash6  rD   r   c                  C   s   d\} }d| }t d|d D ]2}tj| || d| d}t|dd | D }t|}t|}|dd | D ksAJ qd	S )
z
    the subgraph hashes should be invariant to node-relabeling when the output is reindexed
    by the same mapping and all hashes don't collide.
    rE   rF   rG   rH   r   c                 S   rI   rJ   r   rL   r   r   r   r4   P  r5   z1test_isomorphic_subgraph_hash.<locals>.<dictcomp>c                 S      i | ]	\}}d | |qS rJ   r   r2   kvr   r   r   r4   U  s    N)r   r   rN   rO   r<   r   r   )r3   r"   rP   r#   r   r   g1_subgraph_hashesg2_subgraph_hashesr   r   r   test_isomorphic_subgraph_hashG  s   

r   c                  C   s@  d\} }d| }t d|d D ]}tj| || d| d}|jD ] \}}| d| d|| | d< | d| d	|| | d
< q tj|dd}tj|d
d}tj|dd}	||	ks\J ||	ksbJ ||kshJ t|dd | D }
tj|
dd}tj|
d
d}|dd | D ksJ |dd | D ksJ qdS )a  
    Isomorphic graphs with differing edge attributes should yield different subgraph
    hashes if the 'edge_attr' argument is supplied and populated in the graph, and
    all hashes don't collide.
    The output should still be invariant to node-relabeling
    rE   rF   rG   rT   r   rU   rV   r   rW   rX   r   Nc                 S   rI   rJ   r   rL   r   r   r   r4   t  r5   z;test_isomorphic_edge_attr_subgraph_hash.<locals>.<dictcomp>c                 S   r   rJ   r   r   r   r   r   r4   }      c                 S   r   rJ   r   r   r   r   r   r4     r   )r   r   rN   rY   r   rO   r<   r   rZ   r   r   r   'test_isomorphic_edge_attr_subgraph_hashX  s>   r   c                  C   rc   rd   )r   rg   rh   ri   rj   rk   r   rl   r   r   r   $test_missing_edge_attr_subgraph_hash  s
   

r   c                  C   s.  d\} }d| }t d|d D ]}tj| || d| d}| D ]}| d|j| d< | d|j| d	< q!tj|dd
}tj|d	d
}tj|dd
}||ksSJ ||ksYJ ||ks_J t|dd | D }	tj|	dd
}
tj|	d	d
}|dd |
 D ksJ |dd | D ksJ qdS )a  
    Isomorphic graphs with differing node attributes should yield different subgraph
    hashes if the 'node_attr' argument is supplied and populated in the graph, and
    all hashes don't collide.
    The output should still be invariant to node-relabeling
    rE   rF   rG   rn   r   rV   r   rW   ro   r   Nc                 S   rI   rJ   r   rL   r   r   r   r4     r5   z;test_isomorphic_node_attr_subgraph_hash.<locals>.<dictcomp>c                 S   r   rJ   r   r   r   r   r   r4     r   c                 S   r   rJ   r   r   r   r   r   r4     r   )r   r   rN   r<   r   rO   r   rp   r   r   r   'test_isomorphic_node_attr_subgraph_hash  s>   r   c                  C   rw   rx   )r   rg   rz   rh   ri   rj   rk   r   rl   r   r   r   $test_missing_node_attr_subgraph_hash  s   

r   c                  C   s  d\} }d| }t d|d D ]}tj| || d| d}| D ]}| d|j| d< | d|j| d	< q!|jD ] \}}| d
| d|| | d< | d
| d|| | d< q;tj|ddd}tj|dd	d}	tj|dd	d}
t|}||ksJ |	|ksJ ||	ksJ |
|	ksJ |
|ksJ t|dd | D }tj|ddd}tj|dd	d}|dd | D ksJ |	dd | D ksJ qdS )a  
    Isomorphic graphs with differing node attributes should yield different subgraph
    hashes if the 'node_attr' and 'edge_attr' argument is supplied and populated in
    the graph, and all hashes don't collide
    The output should still be invariant to node-relabeling
    rE   rF   rG   r|   r   rV   r   rW   ro   rU   r   rX   r	   c                 S   rI   rJ   r   rL   r   r   r   r4     r5   zItest_isomorphic_edge_attr_and_node_attr_subgraph_hash.<locals>.<dictcomp>c                 S   r   rJ   r   r   r   r   r   r4     r   c                 S   r   rJ   r   r   r   r   r   r4     r   N)r   r   rN   r<   rY   r   rO   r   r}   r   r   r   5test_isomorphic_edge_attr_and_node_attr_subgraph_hash  sN   
r   c                  C   s   d\} }d| }t d|d D ]_}tj| || d| d}tj|dd}tj|dd}tj|d	d}td
d | D s?J tdd | D sLJ tdd | D sYJ t||s`J t||sgJ t||snJ qdS )z
    All nodes should have the correct number of subgraph hashes in the output when
    using degree as initial node labels
    Subsequent iteration depths for the same graph should be additive for each node
    rE   rF   rG   iX  r   rf   r   ry   r+   c                 s       | ]	}t |d kV  qdS rf   Nr   r   r   r   r   r     r   z'test_iteration_depth.<locals>.<genexpr>c                 s   r   ry   Nr   r   r   r   r   r     r   c                 s   r   r+   Nr   r   r   r   r   r     r   N)r   r   rN   r   r   r   r   )r3   r"   rP   r#   r?   depth3depth4depth5r   r   r   test_iteration_depth  s   r   c            
      C   s  d\} }d| }t d|d D ]x}tj| || d| d}|jD ]\}}| d| d|| | d< q tj|dd	d
}tj|ddd
}tj|ddd
}	tdd | D sXJ tdd | D seJ tdd |	 D srJ t||syJ t||	sJ t||	sJ qdS )a  
    All nodes should have the correct number of subgraph hashes in the output when
    setting initial node labels empty and using an edge attribute when aggregating
    neighborhoods.
    Subsequent iteration depths for the same graph should be additive for each node
    rE   rF   rG   i  r   rU   rV   r   rf   )r   r   ry   r+   c                 s   r   r   r   r   r   r   r   r   2  r   z1test_iteration_depth_edge_attr.<locals>.<genexpr>c                 s   r   r   r   r   r   r   r   r   3  r   c                 s   r   r   r   r   r   r   r   r   4  r   N)r   r   rN   rY   r   r   r   r   )
r3   r"   rP   r#   r?   r[   r\   r   r   r   r   r   r   test_iteration_depth_edge_attr  s,   r   c            	      C   s
  d\} }d| }t d|d D ]s}tj| || d| d}| D ]}| d|j| d< q!tj|ddd	}tj|dd
d	}tj|ddd	}tdd | D sSJ tdd | D s`J tdd | D smJ t||stJ t||s{J t||sJ qdS )z
    All nodes should have the correct number of subgraph hashes in the output when
    setting initial node labels to an attribute.
    Subsequent iteration depths for the same graph should be additive for each node
    rE   rF   rG   i   r   rV   r   rf   )r   r   ry   r+   c                 s   r   r   r   r   r   r   r   r   S  r   z1test_iteration_depth_node_attr.<locals>.<genexpr>c                 s   r   r   r   r   r   r   r   r   T  r   c                 s   r   r   r   r   r   r   r   r   U  r   N)r   r   rN   r<   r   r   r   r   )	r3   r"   rP   r#   r?   rM   r   r   r   r   r   r   test_iteration_depth_node_attr;  s,   r   c                  C   s<  d\} }d| }t d|d D ]}tj| || d| d}| D ]}| d|j| d< q!|jD ]\}}| d| d|| | d	< q1tj|d	dd
d}tj|d	ddd}	tj|d	ddd}
tdd | D slJ tdd |	 D syJ tdd |
 D sJ t||	sJ t|	|
sJ t||
sJ qdS )a!  
    All nodes should have the correct number of subgraph hashes in the output when
    setting initial node labels to an attribute and also using an edge attribute when
    aggregating neighborhoods.
    Subsequent iteration depths for the same graph should be additive for each node
    rE   rF   rG   i  r   rV   r   rU   r   rf   )r   r   r   ry   r+   c                 s   r   r   r   r   r   r   r   r   x  r   z6test_iteration_depth_node_edge_attr.<locals>.<genexpr>c                 s   r   r   r   r   r   r   r   r   y  r   c                 s   r   r   r   r   r   r   r   r   z  r   N)	r   r   rN   r<   rY   r   r   r   r   )r3   r"   rP   r#   r?   rM   r[   r\   r   r   r   r   r   r   #test_iteration_depth_node_edge_attr\  s0   r   c                  C   s   d\} }d| }t d|d D ].}tj| || d| d}t|}tj|dd}||ks/J t|ds6J t|ds=J qd	S )
r   rE   rF   rG   r   r   r   r      N)r   r   rN   r   r   )r3   r"   rP   r#   r?   digest_size16_hashesdigest_size32_hashesr   r   r   test_digest_size_subgraph_hash  s   
r   )ri   networkxr   networkx.generatorsr   r   r(   rC   rS   rb   rm   rv   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s:    )
)2--6"!%