o
    3ήc                     @   s  d dl mZ d dlZd dlZdd Zdd Zdd Zd	d
 Zdd Z	ej
dddd Zej
dddd Zej
dddd Zej
d
ddd Zej
dddd Zejde e e e e	 g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S )+    )combinationsNc                  C   s(   t jdt jd} d| jd< t |  | S )z$Return a path graph of length three.   )create_usingpathname)nx
path_graphDiGraphgraphfreezeG r   R/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/tests/test_d_separation.pyr      s   

r   c                  C   (   t jdd} | ddg t |  | S )zReturn a three node fork graph.forkr   r      r      r   r	   add_edges_fromr   r   r   r   r   
fork_graph      
r   c                  C   r   )z5Return a collider/v-structure graph with three nodes.colliderr   r   )r   r   r   r   r   r   r   collider_graph   r   r   c                  C   (   t jdd} | g d t |  | S )z&Return a simply Naive Bayes PGM graph.naive_bayesr   )r   r   )r   r   )r      r   r   r   r   r   naive_bayes_graph    r   r    c                  C   r   )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   r   r   r   
asia_graph(   s   
r)   r   c                   C      t  S Nr   r   r   r   r   path_graph_fixture;      r-   c                   C   r*   r+   r   r   r   r   r   fork_graph_fixture@   r.   r0   c                   C   r*   r+   r   r   r   r   r   collider_graph_fixtureE   r.   r2   c                   C   r*   r+   )r    r   r   r   r   naive_bayes_graph_fixtureJ   r.   r3   c                   C   r*   r+   r)   r   r   r   r   asia_graph_fixtureO   r.   r5   r
   c                 C   sP   | j D ]"}t| |}| j t| | |h | }t| |h||s%J qdS )z8Test that the Markov condition holds for each PGM graph.N)nodessetpredecessorsr   descendantsd_separated)r
   nodeparentsnon_descendantsr   r   r   test_markov_conditionT   s
   
r>   c                 C   6   t | dhdhdhsJ t | dhdhi rJ dS )z2Example-based test of d-separation for path_graph.r   r   r   Nr   r:   r,   r   r   r   test_path_graph_dsep`      rA   c                 C   r?   )z2Example-based test of d-separation for fork_graph.r   r   r   Nr@   r/   r   r   r   test_fork_graph_dsepf   rB   rC   c                 C   s6   t | dhdhi sJ t | dhdhdhrJ dS )z6Example-based test of d-separation for collider_graph.r   r   r   Nr@   r1   r   r   r   test_collider_graph_dsepl   s   rD   c                 C   sP   t tdddD ]\}}t| |h|hdhsJ t| |h|hi r%J qdS )z9Example-based test of d-separation for naive_bayes_graph.r      r   r   N)r   ranger   r:   )r    uvr   r   r   test_naive_bayes_dsepr   s   rI   c                 C   sB   t | ddhddhddhsJ t | ddhdhddhsJ d	S )
z2Example-based test of d-separation for asia_graph.r!   r#   r(   r'   r%   r&   r"   r$   Nr@   r4   r   r   r   test_asia_graph_dsepy   s   rJ   c                  C   V   t tj tdtj} t| dhdhdh W d   dS 1 s$w   Y  dS )z{
    Test that undirected graphs are not supported.

    d-separation does not apply in the case of undirected graphs.
    r   r   r   r   N)pytestraisesr   NetworkXNotImplementedr   Graphr:   gr   r   r   (test_undirected_graphs_are_not_supported      "rR   c                  C   rK   )zr
    Test that cycle graphs should cause erroring.

    This is because PGMs assume a directed acyclic graph.
    r   r   r   r   N)rL   rM   r   NetworkXErrorcycle_graphr	   r:   rP   r   r   r   test_cyclic_graphs_raise_error   rS   rV   c                 C   sH   t tj t| dhdhdh W d   dS 1 sw   Y  dS )zJ
    Test that graphs that have invalid nodes passed in raise errors.
    r   r   r   N)rL   rM   r   NodeNotFoundr:   r4   r   r   r   test_invalid_nodes_raise_error   s   "rX   )	itertoolsr   rL   networkxr   r   r   r   r    r)   fixturer-   r0   r2   r3   r5   markparametrizer>   rA   rC   rD   rI   rJ   rR   rV   rX   r   r   r   r   <module>   s>    











