Ejemplo n.º 1
0
 def learn_fci(self, df, tabu_edges):
     """This function is used to learn model using FCI"""
     from pycausal.pycausal import pycausal as pc
     from pycausal import search as s
     from pycausal import prior as p
     pc = pc()
     pc.start_vm()
     forbid = [list(i) for i in tabu_edges]
     prior = p.knowledge(forbiddirect=forbid)
     tetrad = s.tetradrunner()
     tetrad.getAlgorithmParameters(algoId='fci', testId='fisher-z-test')
     tetrad.run(algoId='fci',
                dfs=df,
                testId='fisher-z-test',
                depth=-1,
                maxPathLength=-1,
                completeRuleSetUsed=False,
                verbose=False)
     edges = tetrad.getEdges()
     dot_str = pc.tetradGraphToDot(tetrad.getTetradGraph())
     graph = pydot.graph_from_dot_data(dot_str)
     # graph[0].write_pdf(fname)
     pc.stop_vm()
     return edges
df = pd.read_table(data_dir, sep="\t")

from pycausal.pycausal import pycausal as pc
pc = pc()
pc.start_vm()

from pycausal import search as s
tetrad = s.tetradrunner()
tetrad.run(algoId='fges',
           dfs=df,
           scoreId='sem-bic',
           dataType='continuous',
           maxDegree=-1,
           faithfulnessAssumed=True,
           verbose=True)

print(tetrad.getNodes())
print(tetrad.getEdges())

graph = tetrad.getTetradGraph()
print('Graph BIC: {}'.format(graph.getAttribute('BIC')))
nodes = graph.getNodes()
for i in range(nodes.size()):
    node = nodes.get(i)
    print('Node {} BIC: {}'.format(node.getName(), node.getAttribute('BIC')))

dot_str = pc.tetradGraphToDot(graph)
graphs = pydot.graph_from_dot_data(dot_str)
graphs[0].write_svg('fges-continuous.svg')

pc.stop_vm()
#!/usr/local/bin/python


import os
import pandas as pd
import pydot
from IPython.display import SVG

data_dir = os.path.join(os.getcwd(), 'data', 'audiology.txt')
df = pd.read_table(data_dir, sep="\t")

from pycausal.pycausal import pycausal as pc
pc = pc()
pc.start_vm(java_max_heap_size = '100M')

from pycausal import search as s
tetrad = s.tetradrunner()
tetrad.run(algoId = 'fges', dfs = df, scoreId = 'bdeu', dataType = 'discrete',
           structurePrior = 1.0, samplePrior = 1.0, maxDegree = 3, faithfulnessAssumed = True, verbose = True)

tetrad.getNodes()
tetrad.getEdges()

dot_str = pc.tetradGraphToDot(tetrad.getTetradGraph())
graphs = pydot.graph_from_dot_data(dot_str)
graphs[0].write_svg('fges-discrete.svg')

pc.stop_vm()
import pandas as pd
import pydot
from IPython.display import SVG

data_dir = os.path.join(os.getcwd(), 'data', 'charity.txt')
df = pd.read_table(data_dir, sep="\t")

from pycausal.pycausal import pycausal as pc
pc = pc()
pc.start_vm()

from pycausal import search as s
tetrad = s.tetradrunner()
tetrad.run(algoId='fges',
           dfs=df,
           scoreId='sem-bic',
           dataType='continuous',
           penaltyDiscount=2,
           maxDegree=-1,
           faithfulnessAssumed=True,
           verbose=True)

tetrad.getNodes()
tetrad.getEdges()

dot_str = pc.tetradGraphToDot(tetrad.getTetradGraph())
graphs = pydot.graph_from_dot_data(dot_str)
graphs[0].write_svg('fges-continuous.svg')

pc.stop_vm()