
    Vh)              	          d dl mZ d dlZd dlZd Zd Zd Zd Zd Z	 ej                  d	      d
        Z ej                  d	      d        Z ej                  d	      d        Z ej                  d	      d        Z ej                  d	      d        Z ej                         d        Z ej                         d        Z ej                         d        Z ej                         d        Z ej                         d        Zej*                  j-                  d e        e        e        e        e	       g      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#y)&    )combinationsNc                      t        j                  dt         j                        } d| j                  d<   t        j                  |        | S )z$Return a path graph of length three.   )create_usingpathname)nx
path_graphDiGraphgraphfreezeGs    [/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/tests/test_d_separation.pyr
   r
      s3    
abjj1AAGGFOIIaLH    c                      t        j                  d      } | j                  ddg       t        j                  |        | S )zReturn a three node fork graph.forkr   r      r      r	   r   add_edges_fromr   r   s    r   
fork_graphr      s3    


Aff%&IIaLHr   c                      t        j                  d      } | j                  ddg       t        j                  |        | S )z5Return a collider/v-structure graph with three nodes.colliderr   r   )r   r   r   r   s    r   collider_graphr      s3    



#Aff%&IIaLHr   c                      t        j                  d      } | j                  g d       t        j                  |        | S )z&Return a simply Naive Bayes PGM graph.naive_bayesr   )r   r   )r   r   )r      r   r   s    r   naive_bayes_graphr"       s/    


&A56IIaLHr   c                      t        j                  d      } | j                  g d       t        j                  |        | S )zReturn the 'Asia' PGM graph.asiar   ))r$   tuberculosis)smokingcancer)r&   
bronchitis)r%   either)r'   r)   )r)   xray)r)   dyspnea)r(   r+   r   r   s    r   
asia_graphr,   (   s5    


A		
 IIaLHr   r
   r   c                      t               S Nr
    r   r   path_graph_fixturer1   ;   
    <r   r   c                      t               S r.   r   r0   r   r   fork_graph_fixturer5   @   r2   r   r   c                      t               S r.   r   r0   r   r   collider_graph_fixturer8   E   s    r   r"   c                      t               S r.   )r"   r0   r   r   naive_bayes_graph_fixturer:   J   s    r   r,   c                      t               S r.   r,   r0   r   r   asia_graph_fixturer=   O   r2   r   c                  8    g d} t        j                  |       }|S )N)ABCrA   rA   D)rE   E)rA   F)r   rF   r	   r   	edge_listr   s     r   large_collider_graphrK   T   s    XI


9AHr   c                  8    g d} t        j                  |       }|S )N)r?   rA   rC   rD   rE   rC   rH   rI   s     r   chain_and_fork_graphrO   [   s    @I


9AHr   c                  6    dg} t        j                  |       }|S )Nr?   rH   rI   s     r   no_separating_set_graphrQ   b   s    I


9AHr   c                  8    g d} t        j                  |       }|S )N)r?   )rC   r@   rB   rH   rI   s     r   large_no_separating_set_graphrS   i       4I


9AHr   c                  8    g d} t        j                  |       }|S )N)r?   rB   )rC   rE   rH   rI   s     r   collider_trek_graphrV   p   rT   r   r   c                     | j                   D ]a  }t        | j                  |            }| j                   t        j                  | |      z
  |hz
  |z
  }t        j
                  | |h||      raJ  y)z8Test that the Markov condition holds for each PGM graph.N)nodessetpredecessorsr	   descendantsis_d_separator)r   nodeparentsnon_descendantss       r   test_markov_conditionr`   w   so      Je((./++ud(CCtfLwV  IIIJr   c                     t        j                  | dhdhdh      sJ t        j                  | dhdht                     rJ y)z2Example-based test of d-separation for path_graph.r   r   r   Nr	   r\   rY   r/   s    r   test_path_graph_dseprc      E    Z!qcA3777  aS1#su====r   c                     t        j                  | dhdhdh      sJ t        j                  | dhdht                     rJ y)z2Example-based test of d-separation for fork_graph.r   r   r   Nrb   r4   s    r   test_fork_graph_dseprf      rd   r   c                     t        j                  | dhdht                     sJ t        j                  | dhdhdh      rJ y)z6Example-based test of d-separation for collider_graph.r   r   r   Nrb   r7   s    r   test_collider_graph_dseprh      sE    ^aS1#su===  !qcA3????r   c                     t        t        dd      d      D ]F  \  }}t        j                  | |h|hdh      sJ t        j                  | |h|ht	                     sFJ  y)z9Example-based test of d-separation for naive_bayes_graph.r      r   r   N)r   ranger	   r\   rY   )r"   uvs      r   test_naive_bayes_dseprn      sf    U1a[!, I1  !2QC!qcBBB$$%6aS#%HHHIr   c                     t        j                  | ddhddhddh      sJ t        j                  | ddhdhddh      sJ y	)
z2Example-based test of d-separation for asia_graph.r$   r&   r+   r*   r(   r)   r%   r'   N)r	   r\   r<   s    r   test_asia_graph_dseprp      sa    VY')V)<|X>V   ^X.F@S  r   c                  :   t        j                  dt         j                        } t        j                  t         j
                        5  t        j                  | dhdhdh       ddd       t        j                  t         j
                        5  t        j                  | dhdhdh       ddd       t        j                  t         j
                        5  t        j                  | dhdh       ddd       y# 1 sw Y   xY w# 1 sw Y   [xY w# 1 sw Y   yxY w)z
    Test that undirected graphs are not supported.

    d-separation and its related algorithms do not apply in
    the case of undirected graphs.
    r   r   r   r   N)	r	   r
   GraphpytestraisesNetworkXNotImplementedr\   is_minimal_d_separatorfind_minimal_d_separatorgs    r   (test_undirected_graphs_are_not_supportedrz      s     	a"A	r00	1 ,
!aS1#s+,	r00	1 4
!!!aS1#s34	r00	1 1
##AsQC01 1	, ,4 41 1s$   C9DD9DDDc                  :   t        j                  dt         j                        } t        j                  t         j
                        5  t        j                  | dhdhdh       ddd       t        j                  t         j
                        5  t        j                  | dhdh       ddd       t        j                  t         j
                        5  t        j                  | dhdhdh       ddd       y# 1 sw Y   xY w# 1 sw Y   ]xY w# 1 sw Y   yxY w)zr
    Test that cycle graphs should cause erroring.

    This is because PGMs assume a directed acyclic graph.
    r   r   r   r   N)	r	   cycle_graphr   rs   rt   NetworkXErrorr\   rw   rv   rx   s    r   test_cyclic_graphs_raise_errorr~      s     	q"**%A	r''	( ,
!aS1#s+,	r''	( 1
##AsQC01	r''	( 4
!!!aS1#s34 4	, ,1 14 4s$   C9DD9DDDc                    t        j                  t        j                        5  t        j                  | dhdhdh       ddd       t        j                  t        j                        5  t        j                  | ddd       ddd       t        j                  t        j                        5  t        j
                  | dhdhdh       ddd       t        j                  t        j                        5  t        j
                  | ddd       ddd       t        j                  t        j                        5  t        j                  | dhdh       ddd       t        j                  t        j                        5  t        j                  | dd       ddd       y# 1 sw Y   bxY w# 1 sw Y   +xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)zJ
    Test that graphs that have invalid nodes passed in raise errors.
    r   r   r   N)rs   rt   r	   NodeNotFoundr\   rv   rw   r<   s    r   test_invalid_nodes_raise_errorr      sk   
 
r	' 5
*qcA345	r	' /
*aA./	r	' =
!!*qcA3<=	r	' 7
!!*aA67	r	' :
##JaS9:	r	' 6
##J156 65 5/ /= =7 7: :6 6sG   F +F-/F:6G:G?G F*-F7:GGGG'c                    t        j                  t        j                        5  t        j                  | ddd       ddd       t        j                  t        j                        5  t        j                  | ddd       ddd       t        j                  t        j                        5  t        j                  | ddd       ddd       t        j                  t        j                        5  t        j                  | ddd       ddd       t        j                  t        j                        5  t        j
                  | dd       ddd       t        j                  t        j                        5  t        j
                  | ddd       ddd       t        j                  t        j                        5  t        j
                  | ddd       ddd       t        j                  t        j                        5  t        j                  | ddt                      ddd       t        j                  t        j                        5  t        j                  | ddt               d       ddd       t        j                  t        j                        5  t        j                  | ddt               d       ddd       y# 1 sw Y   xY w# 1 sw Y   UxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w# 1 sw Y   AxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)zC
    Test that error is raised when node sets aren't disjoint.
    r   r   Nr   included)rs   rt   r	   r}   r\   rw   rv   rY   r7   s    r   &test_nondisjoint_node_sets_raise_errorr      sp    
r''	( 3
.!Q23	r''	( 3
.!Q23	r''	( 3
.!Q23	r''	( 3
.!Q23	r''	( :
##NAq9:	r''	( F
##NAq1EF	r''	( F
##NAq1EF	r''	( ?
!!.!Q>?	r''	( K
!!.!QJK	r''	( K
!!.!QJK K%3 33 33 33 3: :F FF F? ?K KK Ksw   K(K,K!0K.4K;7L<L!L"#L/#L;KK!K+.K8;LLL"L,/L8;Mc                    t        j                  | dhdht                     rJ t        j                  | dd      }t        j                  | dd|      sJ t        j                  | dd|      sJ t        j                  | ddhddh|      sJ |dhk(  sJ t        j                  |dhdht                     rJ t        j                  |dd      }t        j                  |dd|      sJ |dhk(  sJ |j                  dh      }t        j                  |dd|      rJ t        j                  |dhdht                     rJ t        j                  |dd      J t        j                  |dhdhdh      rJ t        j                  |dd      J t        j                  |ddd      ddhk(  sJ t        j                  |dddd      	 J y )	NrA   rF   r@   r   rE   rC   r   )r   
restricted)r	   r\   rY   rw   rv   union)rK   rO   rQ   rS   rV   ZminZnotmins          r   test_is_minimal_d_separatorr      s      !5useSUKKK &&';S#FD13TBBB$$%93TJJJ$$%9C:SzSWXXXC5==
   !5useSUKKK&&';S#FD $$%93TJJJC5==jj#G(()=sCQQQ   !83%#NNN&&'>SIQQQ   !>useTTT&&'Dc3OWWW &&':CsSX   
 	##cCC	
 		r   c                     t        j                         } | j                  g d       t        j                  | dhdhdh      rJ t        j                  | dddh      rJ t        j                  | ddt                     rJ y)zATest that is_minimal_d_separator checks for d-separation as well.)r?   )r@   rF   rM   rD   rN   )rE   rG   )rF   rE   )rF   rG   rC   rG   rE   N)r	   r   r   r\   rv   rY   rx   s    r   'test_is_minimal_d_separator_checks_dsepr   7  s    


A		
   SEC53%888 ((Cse<<<((Cce<<<<r   c                     t         j                  j                  j                  }| }ddh}h d} ||||dh      h dk(  sJ  ||||t	                     |k(  sJ y )NrG   rE   >   r@   rA   rC   rE   rG   rA   >   rA   rE   rG   )r	   
algorithmsd_separation
_reachablerY   )rK   	reachablery   x	ancestorss        r   test__reachabler   N  s`    **55IA	c
A)IQ9se,???Q9ce,	999r   )$	itertoolsr   rs   networkxr	   r
   r   r   r"   r,   fixturer1   r5   r8   r:   r=   rK   rO   rQ   rS   rV   markparametrizer`   rc   rf   rh   rn   rp   rz   r~   r   r   r   r   r   r0   r   r   <module>r      s   "  & \" # \" # %& ' () * \" #           \:<!13D3F
UJ	J>>@I1 46&K4BJ=.:r   