Пример #1
0
 def inference_dag(self):
     """
     This is a DAG created from the original but has been altered
     to accomodate `do-calculus`.
     """
     infer_dag = DAG(self.dag.df.copy())
     logging.debug(
         f"constructing copy of original DAG nodes: {infer_dag.nodes}")
     for n1, n2 in self.dag.edges:
         if n2 not in self.do_dict.keys():
             infer_dag.add_edge(n1, n2)
         else:
             logging.debug(
                 f"edge {n1} -> {n2} ignored because of do operator")
     logging.debug(f"original DAG copied")
     return infer_dag
Пример #2
0
def simple_dag():
    df = pd.DataFrame({
        "a": [1, 1, 1, 1, 0, 0, 0, 0],
        "b": [0, 1, 0, 1, 1, 1, 1, 0],
        "c": [0, 0, 1, 0, 0, 1, 0, 1]
    })
    return DAG(df).add_edge("a", "b").add_edge("a", "c").add_edge("c", "b")
Пример #3
0
def basic_dag():
    df = pd.DataFrame({"a": [1, 1, 1, 1, 0, 0, 0, 0],
                       "b": [0, 1, 0, 1, 1, 1, 1, 0],
                       "c": [0, 0, 1, 0, 0, 1, 0, 1],
                       "d": [1, 1, 0, 1, 0, 0, 0, 0],
                       "e": [1, 1, 1, 1, 0, 0, 0, 0]})
    return DAG(df)
Пример #4
0
def test_parent_child1(small_df):
    dag = DAG(small_df).add_edge("a", "b").add_edge("c",
                                                    "b").add_edge("a", "c")
    assert set(dag.children("a")) == {"b", "c"}
    assert set(dag.children("b")) == set()
    assert set(dag.children("c")) == {"b"}
    assert set(dag.parents("a")) == set()
    assert set(dag.parents("b")) == {"a", "c"}
    assert set(dag.parents("c")) == {"a"}
Пример #5
0
def test_copy(small_df):
    dag1 = DAG(small_df).add_edge("a", "b").add_edge("c", "b")
    dag2 = DAG(small_df).add_edge("a", "b").add_edge("c",
                                                     "b").add_edge("a", "c")
    dag1_copy = dag1.copy()
    dag2_copy = dag2.copy()
    assert set(dag1_copy.edges) == set(dag1.edges)
    assert set(dag2_copy.edges) == set(dag2.edges)
    assert set(dag1_copy.nodes) == set(dag1.nodes)
    assert set(dag2_copy.nodes) == set(dag2.nodes)
    assert dag1.df.equals(dag1_copy.df)
    assert dag2.df.equals(dag2_copy.df)
Пример #6
0
import logging
import pandas as pd

from brent.graph import DAG
from brent.query import Query

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s [%(filename)s:%(funcName)s:%(lineno)d] %(levelname)s - %(message)s',
)

df = pd.DataFrame({"a": [1, 1, 1, 1, 0, 0, 0, 0],
                   "b": [0, 1, 0, 1, 1, 1, 1, 0],
                   "c": [0, 0, 1, 0, 0, 1, 0, 1]})
dag = DAG(df).add_edge("a", "b").add_edge("a", "c").add_edge("c", "b")
logging.debug(f"parents of c: {dag.parents('c')}")
for node in dag.nodes:
    logging.debug(f"confirming parents({node}) = {dag.parents(node)}")
print(Query(dag).infer())
Пример #7
0
def dag_with_two_values():
    return (DAG(make_fake_df(nodes=4))
            .add_edge("a", "d")
            .add_edge("b", "d")
            .add_edge("a", "b")
            .add_edge("b", "c"))
Пример #8
0
def dag_with_four_values():
    return (DAG(make_fake_df(nodes=4, rows=2000, values=4))
            .add_edge("a", "d")
            .add_edge("b", "d")
            .add_edge("a", "b")
            .add_edge("b", "c"))
Пример #9
0
def test_dag_remains_dag(small_df):
    dag = DAG(small_df).add_edge("a", "b").add_edge("b", "c")
    with pytest.raises(ValueError):
        assert dag.add_edge("c", "a")
Пример #10
0
def test_connections2(small_df):
    dag = DAG(small_df).add_edge("a", "b").add_edge("c", "b")
    assert set(dag.connections("a")) == {"b"}
    assert set(dag.connections("b")) == {"a", "c"}
    assert set(dag.connections("c")) == {"b"}
Пример #11
0
def test_origin_nodes_2(small_df):
    dag = DAG(small_df).add_edge("a", "b").add_edge("c", "b")
    assert dag.origin_nodes == ("a", "c")
Пример #12
0
def dag():
    return (DAG(make_fake_df(7)).add_edge("e", "a").add_edge(
        "e",
        "d").add_edge("a", "d").add_edge("b", "d").add_edge("a", "b").add_edge(
            "a", "c").add_edge("b", "c").add_edge("c", "f").add_edge("g", "f"))