def buildGraph(filePath): G = nx.DiGraph() file = open(filePath, "r") for line in file: x, y = line.rstrip("\n").split('\t') G.add_edge(x, y) return G
def roget_graph(): """Return the thesaurus graph from the roget.dat example in the Stanford Graph Base. """ # open file roget_dat.txt.gz fh = gzip.open("roget_dat.txt.gz", "r") G = nx.DiGraph() for line in fh.readlines(): line = line.decode() if line.startswith("*"): # skip comments continue if line.startswith(" "): # this is a continuation line, append line = oldline + line if line.endswith("\\\n"): # continuation line, buffer, goto next oldline = line.strip("\\\n") continue (headname, tails) = line.split(":") # head numfind = re.compile(r"^\d+") # re to find the number of this word head = numfind.findall(headname)[0] # get the number G.add_node(head) for tail in tails.split(): if head == tail: print("skipping self loop", head, tail, file=sys.stderr) G.add_edge(head, tail) return G
def recipes_to_graph(recipes): """ Given a list of recipes, will create a func dep graph for each which it will then compose. :param recipes: A list of recipe object :return: A combined graph where the 'starts' attribute for each node has been set """ result_graph = nx.DiGraph() for r in recipes: # Turn recipe to graph and get top nodes r_graph = r.to_DiGraph() top_nodes = get_top_nodes(r_graph) top_nodes = [x[0] for x in top_nodes ] # Get only names of nodes not attributes # Iterate over each node in graph for node in r_graph.nodes(data=True): work = node[0] attributes = node[1] # If node is a top node, it has its starts attribute set to the recipe name attributes['starts'] = set() if work in top_nodes: attributes['starts'].add(r.name) # If this node is already in the result_graph we store elements from its start attributes # in the r_graph node, as to retain it after compose. if work in result_graph: res_set = result_graph.node[work]['starts'] attributes['starts'].update(res_set) # Attributes of r_graph take presedence over result_graph's result_graph = nx.compose(result_graph, r_graph) return result_graph
def test_directed_star(self): G = nx.DiGraph() G.add_edge(1, 2, weight=0.5) G.add_edge(1, 3, weight=0.5) grc = nx.global_reaching_centrality assert_equal(grc(G, weight="weight", normalized=False), 0.5) assert_equal(grc(G, weight="weight", normalized=True), 1)
def solve_mincost_problem_for_expenses(expenses, people, debug=False): if debug: print(expenses) nodes = defaultdict(lambda: 0) G = nx.DiGraph() for idx, expense in enumerate(expenses): whopaid = expense["whopaid"] whoshouldpay = expense["whoshouldpay"] #total = functools.reduce(lambda accu, that: that["amount"] + accu, whopaid, 0) total = sum(that["amount"] for that in whopaid) for v in people: if v in whoshouldpay: if whoshouldpay[v] is not None: idealcontrib = whoshouldpay[v] else: notnone = [x for x in whoshouldpay.values() if x is not None] idealcontrib = Fraction(total - sum(notnone), len(whoshouldpay) - len(notnone)) else: idealcontrib = 0 actualcontrib = sum([w["amount"] for w in expense["whopaid"] if w["personId"] == v]) diff = actualcontrib - idealcontrib #print(actualcontrib, idealcontrib, diff) nodes[v] += diff; for i, v in nodes.items(): G.add_node(i, demand=v) G.add_weighted_edges_from(x + (1,) for x in itertools.permutations(people, 2)) flowCost, flowDict = nx.network_simplex(G) return flowDict
def depend(ctx, flavor, qualifiers, format, output, full, package, version): ''' Product dependency information for the given product. ''' format = format or os.path.splitext(output)[1][1:] if format not in ['raw','dot']: raise RuntimeError, 'Unknown format: "%s"' % format repos = [ups.repos.UpsRepo(pdir) for pdir in ctx.obj['PRODUCTS']] tree = ups.repos.squash_trees(repos) flavor = flavor or repos[0].uc.flavor() seed = make_product(package, version, qualifiers, flavor) subtree = nx.DiGraph() subtree.add_node(seed) subtree.add_edges_from(nx.bfs_edges(tree, seed)) # this is a minimal rep if full: sg = tree.subgraph(subtree.nodes()) subtree.add_edges_from(sg.edges()) if format == 'dot': from . import dot text = dot.simple(subtree) open(output,'wb').write(text) # raw print '%d nodes, %d edges' % (len(subtree.nodes()), len(subtree.edges()))
def create_network_from_json(nwjson): graph = nx.DiGraph() print "Number of nodes: %s" % str(len(nwjson["nodes"])) for pointjs in nwjson["nodes"]: nodeattr = pointjs["attr"] nodeattr["dataPointId"] = pointjs["dataPointId"] graph.add_node(pointjs["id"], **nodeattr) print "Number of links: %s" % str(len(nwjson["links"])) for linkjs in nwjson["links"]: linkattr = linkjs["attr"] linkattr["id"] = linkjs["id"] linkattr["isDirectional"] = linkjs["isDirectional"] graph.add_edge(linkjs["source"], linkjs["target"], **linkattr) nodeAttrDescriptors = [ load_attr_from_json(attrjs) for attrjs in nwjson["nodeAttrDescriptors"] ] linkAttrDescriptors = [ load_attr_from_json(attrjs) for attrjs in nwjson["linkAttrDescriptors"] ] nw = Network(nwjson["id"], nwjson["datasetId"], nwjson["projectId"], nwjson["name"], nwjson['description'], graph, nodeAttrDescriptors, linkAttrDescriptors, nwjson.get("clusterInfo", {}), nwjson["networkInfo"], nwjson["generatorInfo"], nwjson.get("nodesFile", None), nwjson.get("linksFile", None), get_time(nwjson, "createdAt", "dateCreated"), get_time(nwjson, "modifiedAt", "dateModified")) return nw
def tech_graph(h, technician_skill_set, technician_nodes, customer_nodes, customer_machine_types): """ Purpose, Create a graph that includes the technician node and all the customer location where the technician can install Input, h, int: technician under consideration technician_skill_set, list: the skillset of each technician technician_nodes, dict: nodes of technicians customer_nodes, dict: nodes of customers customer_machine_types, list: machine type of each customer order/request Output, G_tech_h, networkx DiGraph for technician h """ G_tech_h = nx.DiGraph() G_tech_h.add_nodes_from( {key: technician_nodes[key] for key in [len(customer_nodes) + 1 + h]}) customers_to_add = [] for i in customer_nodes: if technician_skill_set[h][customer_machine_types[i - 1]]: customers_to_add.append(i) G_tech_h.add_nodes_from( {key: customer_nodes[key] for key in customers_to_add}) return G_tech_h
def __init__(self, node_id_map, transition_mat, workers): self.look_back_list = node_id_map self.T = transition_mat self.workers = workers self.rec_G = nx.to_networkx_graph( self.T, create_using=nx.DiGraph() ) # reconstructed "directed" "weighted" graph based on transition matrix
def _parse_and_add_graph_tag(self, nk_tag): if nk_tag.attrib['isDirected'] == 'true': g = nx.DiGraph() else: g = nx.Graph() g.graph['sourceType'] = nk_tag.attrib['sourceType'] g.graph['source'] = nk_tag.attrib['source'] g.graph['targetType'] = nk_tag.attrib['targetType'] g.graph['target'] = nk_tag.attrib['target'] g.graph['id'] = nk_tag.attrib['id'] g.graph['isDirected'] = nk_tag.attrib['isDirected'] == 'true' g.graph['allowSelfLoops'] = nk_tag.attrib['allowSelfLoops'] == 'true' g.graph['isBinary'] = nk_tag.attrib['isBinary'] == 'true' #for attrib_key in nk_tag.attrib: # g.graph[attrib_key] = format_prop(nk_tag.attrib[attrib_key]) link_list = list() if g.graph['isBinary']: for link in nk_tag.iterfind('link'): link_list.append( (link.attrib['source'], link.attrib['target'])) g.add_edges_from(link_list) else: for link in nk_tag.iterfind('link'): weight = float( link.attrib['value']) if 'value' in link.attrib else 1.0 link_list.append( (link.attrib['source'], link.attrib['target'], weight)) g.add_weighted_edges_from(link_list) self.networks[nk_tag.attrib['id']] = g
def graph_from_xml(method_el): insns = insns_from_xml(method_el) bbs = basic_blocks(method_el) graph = nx.DiGraph() bb_entry_points = {} for bb in bbs: bb_entry_points[bb.entry_point_index()] = bb # 0. Connect sequential basic blocks for i, bb in enumerate(bbs): tail = bb.tail_instruction() if tail.follows_sequential_instruction(): if (i + 1) in range(len(bbs)): graph.add_edge(bb, bbs[i + 1]) # 1. Connect branches for bb in bbs: tail = bb.tail_instruction() if tail.has_target(): dest = bb_entry_points[tail.target_index()] graph.add_edge(bb, dest) # 2. Connect blocks within try blocks to handler entry points handlers = exception_handlers(method_el) for start, end in handlers: for bb in bbs: if bb.index() in range(start, end + 1): for index in handlers[(start, end)]: graph.add_edge(bb, bb_entry_points[index]) return graph
def sample_graph(): """Return a sample graph to be used by different iterator tests.""" graph = nx.DiGraph() graph.add_edge('a', 'b') graph.add_edge('a', 'e') graph.add_edge('b', 'c') graph.add_edge('c', 'b') graph.add_edge('c', 'd') return graph
def test_relabel_selfloop(): G = nx.DiGraph([(1, 1), (1, 2), (2, 3)]) G = nx.relabel_nodes(G, {1: 'One', 2: 'Two', 3: 'Three'}, copy=False) assert sorted(G.nodes()) == ['One', 'Three', 'Two'] G = nx.MultiDiGraph([(1, 1), (1, 2), (2, 3)]) G = nx.relabel_nodes(G, {1: 'One', 2: 'Two', 3: 'Three'}, copy=False) assert sorted(G.nodes()) == ['One', 'Three', 'Two'] G = nx.MultiDiGraph([(1, 1)]) G = nx.relabel_nodes(G, {1: 0}, copy=False) assert sorted(G.nodes()) == [0]
def __init__(self, base_graph=None): if not base_graph: self.G = nx.DiGraph() else: self.G = base_graph self.carrier_count = 0 self.meter_count = 0 self.cw_count = 0 self.iw_count = 0 self.temp = nx.Graph()
def plot(self): drawableGraph = nx.DiGraph() drawableGraph.add_nodes_from(self.graph.vertices) for v in self.graph.vertices: adjs = self.graph.edges[v] for a in adjs: drawableGraph.add_edge(v, a) plt.subplot(111) nx.draw(drawableGraph, with_labels=True, node_color='blue', node_size=50, width=1) plt.show()
def read_graph(): ''' Reads the input network in networkx. ''' if args.weighted: G = nx.read_edgelist(args.input, nodetype=int, data=(('weight', float), ), create_using=nx.DiGraph()) else: G = nx.read_edgelist(args.input, nodetype=int, create_using=nx.DiGraph()) for edge in G.edges(): G[edge[0]][edge[1]]['weight'] = 1 if not args.directed: G = G.to_undirected() return G
def cycles(model, limit=6, threshold=0.01): """ :param model: O dicionário contendo informações de transições de telas para um conjunto de usuários. :param limit: Um valor máximo de caminho. Geralmente caminhos com valores maior que 10 tem uma probabilidade muito pequena e raramente são escolhidos :param threshold: Um valor mínimo de probabilidade para caminhos. Este parâmetro evita que caminhos muito longos, ou seja, com baixa probabilidade continuem a ser buscados, reduzindo assim o tempo de execução do algoritmo :return: Um novo model contendo as informações das telas dos caminhos gerados e seus respectivos custos """ count_graph = model['model'] probabilities_graph = {} for key in count_graph: transitions = count_graph[key] total = sum(transitions.values()) features = {} for feature in transitions: features[feature] = {'weight': float(transitions[feature]) / total} probabilities_graph[key] = features graph = {} for key in list(probabilities_graph.keys()): graph[key] = list(probabilities_graph[key].keys()) G = nx.DiGraph(probabilities_graph) all_simple_cycles = list(simple_cycles(G, limit=limit, threshold=threshold)) all_cycles_sorted, cost_of_all_cycles = cost_of_cycles(probabilities_graph, all_simple_cycles, threshold=threshold) all_frames = [] for cycle in all_cycles_sorted: all_frames += cycle all_frames = list(set(all_frames)) model = [] for i in range(len(all_cycles_sorted)): path = all_cycles_sorted[i] path_as_int = [] for index in range(len(path)): path_as_int.append(all_frames.index(path[index])) model.append({"cost": cost_of_all_cycles[i], "path": path_as_int}) return model, all_frames
def ReorderEvents(event_lst): if len(event_lst) == 0: return [] node_id = 0 graph = nx.DiGraph() connectors = {} last_event_per_cpu = {} last_userspace_event_per_thread = {} first_userspace_event_per_thread = {} # 1st pass: link all events belongs to the same core and same thread for evt in event_lst: graph.add_node(node_id, data=evt) if evt.cpu != 9: # kernel events if evt.cpu in last_event_per_cpu: graph.add_edge(last_event_per_cpu[evt.cpu], node_id) last_event_per_cpu[evt.cpu] = node_id else: # user space events if evt.pid in last_userspace_event_per_thread: graph.add_edge(last_userspace_event_per_thread[evt.pid], node_id) last_userspace_event_per_thread[evt.pid] = node_id if evt.pid not in first_userspace_event_per_thread: first_userspace_event_per_thread[evt.pid] = node_id node_id += 1 # if all events are in one core, there is no need to do reordering if len(last_event_per_cpu.keys()) < 2 and len(first_userspace_event_per_thread.keys()) == 0: return event_lst # 2nd pass: extract all connector events for n in graph: LinkOtherEvents(n, connectors, graph) # 3rd pass: find all the events which the connector links to for n in graph: LinkOtherEvents(n, connectors, graph) # Do topological sort try: sorted_lst = nx.topological_sort(graph) except: sys.stderr.write('\n-----Found a cycle when doing reordering of the following events.-----\n') sys.stderr.write('\n'.join([str(e) for e in event_lst])) return event_lst if sorted_lst is None: return event_lst for n in sorted_lst: LinkUserSpaceEvents(n, graph, first_userspace_event_per_thread, last_userspace_event_per_thread) sorted_lst = nx.topological_sort(graph) if sorted_lst is None: return event_lst new_lst = [] for n in sorted_lst: new_lst.append(graph.node[n]['data']) return new_lst
def __init__( self, node_id_map, transition_mat, workers, parallel_walks=10): # recommend: parallel_walks = number_walks self.look_back_list = node_id_map # if memory error due to python multiprocessor module, plz reduce parallel_walks self.T = transition_mat self.workers = workers self.parallel_walks = parallel_walks self.rec_G = nx.to_networkx_graph( self.T, create_using=nx.DiGraph() ) # reconstructed "directed" "weighted" graph based on transition matrix
def create_dataset_from_json(dsjson): graph = nx.DiGraph() for pointjs in dsjson["datapoints"]: graph.add_node(pointjs["id"], **pointjs["attr"]) attrDescriptors = [ load_attr_from_json(attrjs) for attrjs in dsjson["attrDescriptors"] ] ds = Dataset(dsjson["id"], dsjson["projectId"], dsjson["name"], graph, attrDescriptors, dsjson["sourceInfo"], dsjson.get("datapointsFile", None), get_time(dsjson, "createdAt", "dateCreated"), get_time(dsjson, "modifiedAt", "dateModified")) return ds
def has_loop(edges, threshold=2): """check if a list of edges representing a directed graph contains a loop args: edges: list of edge sets representing a directed graph i.e. [(1, 2), (2, 1)] threshold: min number of nodes contained in loop returns: bool """ g = nx.DiGraph() g.add_edges_from(edges) return any( len(comp) >= threshold for comp in strongly_connected_components(g))
def __nested_remixgroup(dg1, remix_group): print "============= nested remix_group ================ \n" dg2 = nx.DiGraph() data = json.loads(remix_group.networkx_data) dg2.add_nodes_from(data['nodes']) dg2.add_edges_from(data['edges']) print "========== MERGED GROUP NODES: " + str(dg1.nodes()) # FIXME: this combines the graphs correctly # recheck the time-bound concept dg1 = nx.compose(dg1, dg2) print dg1.nodes() return dg1
def handle(self, *args, **options): # 1) Get all the sounds that have remixes cursor = connection.cursor() cursor.execute(""" SELECT src.from_sound_id AS from, src.to_sound_id AS to, snd.created AS created FROM sounds_sound_sources src, sounds_sound snd WHERE src.to_sound_id = snd.id ORDER BY snd.created ASC """) # 2) Create directed graph dg = nx.DiGraph() for row in cursor: dg.add_edge(row[0], row[1]) # 3) Add date to nodes for sorting (FIXME: how can we avoid this query???) """ for node in dg.nodes(): cursor.execute("SELECT snd.created, snd.original_filename, au.username " \ "FROM sounds_sound snd, auth_user au WHERE au.id=snd.user_id AND snd.id = %s", [node]) temp = cursor.fetchone() dg.add_node(node, {'date':temp[0], 'nodeName': temp[1], 'username': temp[2]}) """ dg = _create_nodes(dg) # print dg.nodes(data=True) # 4) Find weakly connected components (single direction) subgraphs = nx.weakly_connected_component_subgraphs(dg) node_list = [] # 5) delete all remixgroup objects to recalculate RemixGroup.objects.all().delete() # 6) Loop through all connected graphs in the dataset # and create the groups for sg in subgraphs: # _create_and_save_remixgroup(sg_idx, sg) _create_and_save_remixgroup(sg, RemixGroup()) """
def copy_graph(G): new_graph = nx.DiGraph() node_mapping = {} for node in G.nodes(): new_node = copy.deepcopy(node) node_mapping[node] = new_node new_graph.add_node(new_node) new_edges = [] for (n1, n2) in G.edges(): new_edges.append((node_mapping[n1], node_mapping[n2])) new_graph.add_edges_from(new_edges) return new_graph
def LoadGraph(location): ''' Function reads in a Json file containing a graph and associated edge metadata and returns a Networkx directed graph and a dictionary containing the associated edge metadata. :param location: path and file name where json file is stored :return: the function outputs a list of lists where list[0] "metadata" contains a dictionary where the keys are the NETS edges and te values are the associated metadata needed to recreate the original OWL representation and list[1] contains an directed OWL-NET graph ''' graph = nx.DiGraph() data = json.load(open(location)) edge_metadata = data['metadata'] graph.add_nodes_from(data['network']['nodes']) graph.add_edges_from(data['network']['edges']) return edge_metadata, graph
def create_network_new(dataset, networkName): graph = nx.DiGraph() nodeAttrDescriptors = [] nodeAttrDescriptors.append( AttrDescriptor("OriginalX", "OriginalX", 'float', "Original_layout", None, None, False, False)) nodeAttrDescriptors.append( AttrDescriptor("OriginalY", "OriginalY", 'float', "Original_layout", None, None, False, False)) nodeAttrDescriptors.append( AttrDescriptor("OriginalColor", "OriginalColor", 'color', "Original_layout", None, None, False, False)) nodeAttrDescriptors.append( AttrDescriptor("OriginalSize", "OriginalSize", 'float', "Original_layout", None, None, False, False)) nodeAttrDescriptors.append( AttrDescriptor("OriginalLabel", "OriginalLabel", 'string', "Original_layout", None, None, False, False)) linkAttrDescriptors = [] linkAttrDescriptors.append( AttrDescriptor("OriginalColor", "OriginalColor", 'color', "Original_layout", None, None, False, False)) linkAttrDescriptors.append( AttrDescriptor("OriginalSize", "OriginalSize", 'float', "Original_layout", None, None, False, False)) linkAttrDescriptors.append( AttrDescriptor("OriginalLabel", "OriginalLabel", 'string', "Original_layout", None, None, False, False)) nw = Network(str(ObjectId()), dataset.id, dataset.projectId, "network", "network for " + dataset.name, graph, nodeAttrDescriptors, linkAttrDescriptors, {}, {}, {}, None, None, datetime.datetime.utcnow(), datetime.datetime.utcnow()) if networkName is not None: nw.name = networkName else: nw.genName = True mongoman.create_network_new(nw) return nw
def test_directed_weighted(self): G = nx.DiGraph() G.add_edge("A", "B", weight=5) G.add_edge("B", "C", weight=1) G.add_edge("B", "D", weight=0.25) G.add_edge("D", "E", weight=1) denom = len(G) - 1 A_local = sum([5, 3, 2.625, 2.0833333333333]) / denom B_local = sum([1, 0.25, 0.625]) / denom C_local = 0 D_local = sum([1]) / denom E_local = 0 local_reach_ctrs = [A_local, C_local, B_local, D_local, E_local] max_local = max(local_reach_ctrs) expected = sum(max_local - lrc for lrc in local_reach_ctrs) / denom grc = nx.global_reaching_centrality actual = grc(G, normalized=False, weight='weight') assert almost_equal(expected, actual, places=7)
def handle(self, *args, **options): logger.info("Starting to create remix grooups") # 1) Get all the sounds that have remixes cursor = connection.cursor() cursor.execute(""" SELECT src.from_sound_id AS from, src.to_sound_id AS to, snd.created AS created FROM sounds_sound_sources src, sounds_sound snd WHERE src.to_sound_id = snd.id ORDER BY snd.created ASC """) # 2) Create directed graph dg = nx.DiGraph() for row in cursor: dg.add_edge(row[0], row[1]) # 3) Create nodes with dates and metadata dg = _create_nodes(dg) # 4) Find weakly connected components (single direction) subgraphs = nx.weakly_connected_component_subgraphs(dg) # 5) delete all remixgroup objects to recalculate RemixGroup.objects.all().delete() # 6) Loop through all connected graphs in the dataset and create the groups n_groups_created = 0 for sg in subgraphs: _create_and_save_remixgroup(sg, RemixGroup()) n_groups_created += 1 logger.info("Finished createing remix grooups (%i groups created)" % n_groups_created)
def read_networkx_graph(driver, label, directed=True): if label is None or label == '': raise Exception("You must provide a valid graph label") print("Reading networkx graph labeled %s" % label) nodes = """ MATCH (n:`%s`) RETURN n.id as id, apoc.map.removeKey(n{.*}, 'id') as props """ % label rels = """ MATCH (a:`%s`)-[r:`%s`]->(b:`%s`) RETURN a.id as a, b.id as b, apoc.map.removeKey(r{.*}, 'id') as props """ % (label, label, label) G = None if directed: G = nx.DiGraph() else: G = nx.Graph() with driver.session() as session: # print(nodes) node_results = session.read_transaction(generic_worker(nodes)) for n in node_results: G.add_node(n['id'], **n['props']) # print(rels) edge_results = session.read_transaction(generic_worker(rels)) for e in edge_results: G.add_edge(e['a'], e['b'], **e['props']) print("Read graph %s" % G) return label, G
def get_max_cut_star_5(): n = 5 V = np.arange(0, n, 1) E = [ (0, 1, 3.0), (1, 2, 2.0), (2, 3, 2.0), (3, 4, 3.0), (4, 0, 1.0), (0, 3, 3.0), (1, 0, 3.0), (2, 1, 2.0), (3, 2, 2.0), (4, 3, 3.0), (0, 4, 1.0), (3, 0, 3.0), ] G = nx.DiGraph() G.add_nodes_from(V) G.add_weighted_edges_from(E) return G