def main(graph_filename, graph_type=None, undirected=False): if undirected: graph = read_graph(graph_filename, graph_type, networkx.Graph()) else: graph = read_graph(graph_filename, graph_type, networkx.DiGraph()) pos = networkx.get_node_attributes(graph, "pos") if not pos: pos = networkx.spring_layout(graph) count = 0 colors = ["Reds_r", "Greens_r", "Blues_r", "Greys_r", "Purples_r"] unused = dict(pos) while unused: min_vertex, position = unused.items()[0] min_dist = position[0]**2 + position[1]**2 for vertex, position in unused.iteritems(): dist = position[0]**2 + position[1]**2 if dist < min_dist: min_vertex = vertex min_dist = dist path = networkx.single_source_shortest_path_length(graph, min_vertex) for vertex in path.iterkeys(): unused.pop(vertex, None) color_map = cmap = pyplot.get_cmap(colors[count % len(colors)]) vmin = float(min(path.itervalues())) vmax = float(max(path.itervalues())) if vmin == vmax: vmax += 2 vmax *= 1.5 networkx.draw_networkx_nodes(graph, pos, nodelist=path.keys(), node_size=50, node_color=path.values(), vmin=vmin, vmax=vmax, cmap=color_map) count += 1 networkx.draw_networkx_edges(graph, pos, arrows=not undirected) pyplot.axes().set_aspect('equal', 'datalim') pyplot.show()
def loadLearnedStructures(self): sizes = np.linspace(self.begin_size, self.end_size, self.n_points, dtype=int) parameters = '_'.join( [str(v).replace('.', '') for v in self.parameters.values()]) path = os.path.join(self.result_dir, 'structures', self.method, parameters + '_' + self.result_domain_str) list_structures = [] for i in range(self.n_restart): list_by_size = [] for size in sizes: name = 'sample' + str(i + 1).zfill(2) + '_size' + str(size) dag, var_names = gu.read_graph( os.path.join(path, name + '.dot')) ndag = otagr.NamedDAG(dag, var_names) list_by_size.append(ndag) # with open(os.path.join(path, name + '.dot'), 'r') as file: # arcs = file.read().replace('\n', '') # ndag = gu.fastNamedDAG(arcs) # list_by_size.append(ndag) list_structures.append(list_by_size) return np.reshape(list_structures, (self.n_restart, self.n_points)).transpose()
def learnDAG(sample, maxp=5, restart=20): names = list(sample.getDescription()) csvfile = tf.NamedTemporaryFile(delete=False) csvfilename = csvfile.name + '.csv' csvfile.close() dotfile = tf.NamedTemporaryFile(delete=False) dotfilename = dotfile.name + '.dot' dotfile.close() sample.exportToCSVFile(csvfilename, ',') start = time.time() os.system("Rscript learn_LGBN.R " + str(csvfilename) + ' ' + str(dotfilename) + ' ' + str(restart) + ' ' + str(maxp)) end = time.time() dag, names = read_graph(dotfilename) os.remove(csvfilename) os.remove(dotfilename) return NamedDAG(dag, names), start, end
def load_struct(self): # with open(self.structure_dir + self.data_structure, 'r') as file: # arcs = file.read().replace('\n', '') # return gu.fastNamedDAG(arcs) dag, names = gu.read_graph(self.structure_dir + self.data_structure + '.dot') return otagr.NamedDAG(dag, names)
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 plot_shd_diff(pdag_path, output_path): plt.clf() list_file = [e for e in pdag_path.iterdir() if e.is_file()] list_file.sort() shds = [] for i in range(len(list_file) - 1): mg1, names1 = gu.read_graph(list_file[i]) mg2, names2 = gu.read_graph(list_file[i + 1]) sc = gum.StructuralComparator() sc.compare(mg1, names1, mg2, names2) shds.append(sc.shd()) plt.plot(shds) plt.savefig(output_path.joinpath("shd_diff.pdf"), transparent=True)
def preprocess_gfiles(self, data_folder): files = glob.glob(data_folder) graphs = {} id = 1 for file in files: graphs[id] = graph_utils.read_graph(file) id += 1 return graphs
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 run(filename, cutoff_time, random_seed): seed(random_seed) graph = read_graph(filename) randomized_hill_climb( graph, # Generate the initial candidate as all vertices edge_deletion, filename, cutoff_time, random_seed)
def main(): graph = read_graph() print_graph(graph) tic = time.perf_counter() order, heights, lenghts = get_lgraph(graph) toc = time.perf_counter() print(f"Finding an ordering took {toc - tic:0.4f} seconds") if order: paint_lgraph(order, heights, lenghts)
def main(file=None): graph = read_graph(file) window = tk.Tk() HEIGHT = 500 WIDTH = HEIGHT UNIT = HEIGHT / (len(graph) + 2) c = tk.Canvas(height=HEIGHT, width=WIDTH, background='white') _ = Painter(c, graph, UNIT) window.mainloop()
def main(graph_filename, graph_type=None, undirected=False, databases=2): if undirected: graph = read_graph(graph_filename, graph_type, networkx.Graph()) else: graph = read_graph(graph_filename, graph_type, networkx.DiGraph()) pos = networkx.get_node_attributes(graph, "pos") if not pos: pos = networkx.spring_layout(graph) color_map = pyplot.cm.tab20 nodes = graph.nodes(data=False) colors = [node % databases for node in nodes] networkx.draw_networkx_nodes(graph, pos, nodelist=nodes, node_size=50, node_color=colors, cmap=color_map, vmin=0, vmax=databases) edges = graph.edges() colors = [pair[0] % databases for pair in edges] networkx.draw_networkx_edges(graph, pos, edgelist=edges, edge_color=colors, edge_cmap=color_map, edge_vmin=0, edge_vmax=databases, arrows=False) pyplot.axes().set_aspect('equal', 'datalim') pyplot.show()
def paint_lgraph_from_file(file: str = './graph.txt', master: any = None, verbose: bool = False): graph = read_graph(file=file) if verbose: print_graph(graph) tic = time.perf_counter() order, heights, lenghts = get_lgraph_parallel(graph, pe.try_order) toc = time.perf_counter() if verbose: print(f"Finding an ordering took {toc - tic:0.4f} seconds") if order: paint_lgraph(order, heights, lenghts, master=master)
self._count_constraint() def _capacity_constraints(self, graph, filename): """ # edge * # req """ with open(filename, 'a') as f: f.write("\\ Capacity Constraints\n") for edge in graph.edges: if graph.reqs: res = "{flow_edge} <= {edge_cap}\n".format( flow_edge=self.flows_e(edge, graph.reqs), edge_cap=edge.cap) f.write(res) self._count_constraint() @property def var_dict(self): return self.var if __name__ == '__main__': args = parser.parse_args() graph = graph_utils.read_graph(args.inputfile) if args.digraph: graph = graph_utils.convertToDiGraph(graph) lp = MultiCommMinCostFlowLP(graph, args.inputfile + ".lp", debug=args.debug) if args.debug: print(lp.var_dict)
def run(filename, cutoff_time, random_seed): seed(random_seed) graph = read_graph(filename) fast_vc(graph, filename, cutoff_time, random_seed)
def main(input_filename, output_filename=None, undirected=False, append_attributes=False, filters=None, verbose=0): print_message("Reading {}...".format(input_filename), verbose, 1) if undirected: graph = read_graph(input_filename, networkx.Graph()) else: graph = read_graph(input_filename, networkx.DiGraph()) print_message("done.\n", verbose, 1) print_message("Extracting attributes...", verbose, 1) measures = set() for vertex, attributes in graph.nodes_iter(data=True): measures.update(attributes.iterkeys()) print_message("done.\n", verbose, 1) parser = argparse.ArgumentParser() for measure in measures: if measure != "id" and measure != "label": flag = "--min-{}".format(measure) help_text = "filter with minimum {}".format( measure.replace("_", " ")) parser.add_argument(flag, type=float, metavar="val", help=help_text) flag = "--max-{}".format(measure) help_text = "filter with maximum {}".format( measure.replace("_", " ")) parser.add_argument(flag, type=float, metavar="val", help=help_text) args = parser.parse_args(filters) filters_dict = vars(args) filters_dict = { key: value for key, value in filters_dict.iteritems() if value } if filters_dict: filters_used = [ " {}:{}\n".format(key, value) for key, value in filters_dict.iteritems() ] print_message("Using Filters:\n" + "".join(filters_used), verbose, 2) print_message("Filtering vertices...", verbose, 1) print_message("\n", verbose, 3) for vertex, attributes in graph.nodes_iter(data=True): for name, value in attributes.iteritems(): key = "min_{}".format(name) if key in filters_dict: if value < filters_dict[key]: graph.remove_node(vertex) print_message(" {} {}\n".format(key, vertex), verbose, 3) break key = "max_{}".format(name) if key in filters_dict: if value > filters_dict[key]: graph.remove_node(vertex) print_message(" {} {}\n".format(key, vertex), verbose, 3) break print_message("done.\n", verbose, 1) if not output_filename: output_filename = "/dev/stdout" writer = RDFWriter() writer.write(graph, output_filename, append_attributes)
def main(): graph = read_graph() print_graph(graph) get_lgraph(graph)
def run(filename, cutoff_time, random_seed): seed(random_seed) graph = read_graph(filename) return greedy_vc(graph, filename, cutoff_time)
def main(source_file, destination_file=None, source_type=None, destination_type=None, undirected=False): if undirected: create_using = networkx.Graph else: create_using = networkx.DiGraph sys.stderr.write("Reading Graph...") sys.stderr.flush() graph = read_graph(source_file, source_type, create_using) sys.stderr.write("done.\n") sys.stderr.write("Degree...") sys.stderr.flush() degree = graph.degree() networkx.set_node_attributes(graph, "degree", degree) sys.stderr.write("done.\n") if not undirected: sys.stderr.write("In Degree...") sys.stderr.flush() degree = graph.in_degree() networkx.set_node_attributes(graph, "in_degree", degree) sys.stderr.write("done.\n") sys.stderr.write("Out Degree...") sys.stderr.flush() degree = graph.out_degree() networkx.set_node_attributes(graph, "out_degree", degree) sys.stderr.write("done.\n") sys.stderr.write("Page Rank...") sys.stderr.flush() pagerank = networkx.pagerank(graph) networkx.set_node_attributes(graph, "pagerank", pagerank) sys.stderr.write("done.\n") sys.stderr.write("Closeness Centrality...") sys.stderr.flush() closeness_centrality = networkx.closeness_centrality(graph) networkx.set_node_attributes(graph, "closeness_centrality", closeness_centrality) sys.stderr.write("done.\n") sys.stderr.write("Betweenness Centrality...") sys.stderr.flush() betweenness_centrality = networkx.betweenness_centrality(graph) networkx.set_node_attributes(graph, "betweenness_centrality", betweenness_centrality) sys.stderr.write("done.\n") sys.stderr.write("Eigenvector Centrality...") sys.stderr.flush() eigenvector_centrality = networkx.eigenvector_centrality(graph) networkx.set_node_attributes(graph, "eigenvector_centrality", eigenvector_centrality) sys.stderr.write("done.\n") if undirected: sys.stderr.write("Clustering Coefficent...") sys.stderr.flush() clustering = networkx.clustering(graph) networkx.set_node_attributes(graph, "clustering", clustering) sys.stderr.write("done.\n\n") sys.stderr.write("Writing Graph...") sys.stderr.flush() if destination_file: networkx.write_gml(graph, destination_file) else: networkx.write_gml(graph, "/dev/stdout") sys.stderr.write("done.\n")
def load_struct(self): dag, names = gu.read_graph(self.structure_dir + self.data_structure + '.dot') return otagr.NamedDAG(dag, names)
import sys from graph_utils import read_graph if __name__ == '__main__': print(read_graph(sys.argv[1]))
def run(filename, cutoff_time, random_seed=0): seed(random_seed) graph = read_graph(filename) return branch_bound(graph, filename, cutoff_time)