def main(source_file, destination_file, source_type=None, destination_type=None, undirected=False): if undirected: create_using = networkx.Graph else: create_using = networkx.DiGraph graph = read_graph(source_file, source_type, create_using) write_graph(graph, destination_file, destination_type)
def main(vertex_count, neighbors, distance, undirected, append_attributes, output_file, output_type): if undirected: graph = generate_circle_graph(networkx.Graph(), vertex_count, neighbors, distance) else: graph = generate_circle_graph(networkx.DiGraph(), vertex_count, neighbors, distance) if not output_file: output_file = "/dev/stdout" if not output_type: output_type = "gml" write_graph(graph, output_file, output_type)
def CBN_PC(data, result_structure_path): print("CBN with PC") skeleton_path = result_structure_path.joinpath("skeleton") skeleton_path.mkdir(parents=True, exist_ok=True) pdag_path = result_structure_path.joinpath("pdag") pdag_path.mkdir(parents=True, exist_ok=True) dag_path = result_structure_path.joinpath("dag") dag_path.mkdir(parents=True, exist_ok=True) skeleton_file_name = "skeleton_" + str(size).zfill(7) + ".dot" skeleton_done = skeleton_path.joinpath(skeleton_file_name).exists() pdag_file_name = "pdag_" + str(size).zfill(7) + ".dot" pdag_done = pdag_path.joinpath(pdag_file_name).exists() dag_file_name = "dag_" + str(size).zfill(7) + ".dot" dag_done = dag_path.joinpath(dag_file_name).exists() alpha = 0.01 conditioningSet = 4 learner = otagr.ContinuousPC(data, conditioningSet, alpha) learner.setVerbosity(True) if not skeleton_done: skel = learner.learnSkeleton() gu.write_graph( skel, skeleton_path.joinpath("skeleton_" + str(size).zfill(7) + ".dot")) if not pdag_done: pdag = learner.learnPDAG() gu.write_graph( pdag, pdag_path.joinpath("pdag_" + str(size).zfill(7) + ".dot")) if not dag_done: dag = learner.learnDAG() gu.write_graph(dag, dag_path.joinpath("dag_" + str(size).zfill(7) + ".dot")) else: dag, names = gu.read_graph( dag_path.joinpath("dag_" + str(size).zfill(7) + ".dot")) dag = otagr.NamedDAG(dag, names) print("Learning parameters") factories = [ ot.KernelSmoothing(ot.Epanechnikov()), ot.BernsteinCopulaFactory() ] ot.Log.SetFile("log") ot.Log.Show(ot.Log.INFO) model = otagr.ContinuousBayesianNetworkFactory(factories, dag, alpha, conditioningSet, False).build(data) ot.Log.Show(ot.Log.INFO) return model
def write_struct(self, file, ndag): gu.write_graph(ndag, file + '.dot')
import numpy as np from pipeline import Pipeline import graph_utils as gu import elidan.hill_climbing as hc data = ot.Sample.ImportFromTextFile("../data/samples/dirichlet/alarm/sample01.csv", ',') # learner = cmiic.ContinuousMIIC(data, # cmode=cmiic.CModeType.Bernstein, # kmode=cmiic.KModeType.Naive) learner = otagrum.TabuList(data, 4, 5, 5) learner.setVerbosity(True) dag = learner.learnDAG() gu.write_graph(dag, "output_tabulist_gaussian.dot") # learner = otagrum.TabuList(data, 4, 5, 5) # learner.setCMode(otagrum.CorrectedMutualInformation.CModeTypes_Bernstein) # learner.setVerbosity(True) # dag = learner.learnDAG() # gu.write_graph(dag, "output_tabulist_bernstein.dot") t = hc.hill_climbing(data) names = list(data.getDescription()) gu.write_graph(otagrum.NamedDAG(t[1], names), "output_hc.dot") print("Final score hc: ", t[2]) # learner.use3off2() # pdag = learner.learnMixedStructure() # dag = learner.learnStructure()
# It would be nice to put this in the pipeline import pyAgrum as gum import otagrum as otagr import graph_utils as gu from pathlib import Path step = 1 start_size = 7 end_size = 7 restart = 1 density = 1.2 # Setting directories location and files directory = Path("../data/structures/generated/") directory.mkdir(parents=True, exist_ok=True) generator = gum.BNGenerator() gum.initRandom(10) for r in range(restart): for i in range(start_size, end_size + 1, step): print("Number of node :", i, flush=True) file_name = "size_" + str(i).zfill(3) + "_" + str(r + 1).zfill(2) n_arc = int(density * (i - 1)) bn = generator.generate(i, n_arc) ndag = otagr.NamedDAG(bn.dag(), bn.names()) gu.write_graph(ndag, directory.joinpath(file_name + '.dot'))