Python networkx.compose() Examples
The following are 23
code examples of networkx.compose().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: tree.py From fragile with MIT License | 6 votes |
def compose(self, other: "NetworkxTree") -> None: """ Compose the graph of another :class:`BaseNetworkxTree` with the graph of \ the current instance. Composition is the simple union of the node sets and edge sets. The node sets of ``self.data`` and ``other.data`` do not need to be \ disjoint. The data in ``other`` takes precedence over the data in the current instance. Args: other: Instance of :class:`BaseNetworkxTree` that will be composed \ with the current instance. Returns: None """ self.data = nx.compose(self.data, other.data)
Example #2
Source File: test_binary.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_compose_multigraph(): G=nx.MultiGraph() G.add_edge(1,2,key=0) G.add_edge(1,2,key=1) H=nx.MultiGraph() H.add_edge(3,4,key=0) H.add_edge(3,4,key=1) GH=nx.compose(G,H) assert_equal( set(GH) , set(G)|set(H)) assert_equal( set(GH.edges(keys=True)) , set(G.edges(keys=True))|set(H.edges(keys=True))) H.add_edge(1,2,key=2) GH=nx.compose(G,H) assert_equal( set(GH) , set(G)|set(H)) assert_equal( set(GH.edges(keys=True)) , set(G.edges(keys=True))|set(H.edges(keys=True)))
Example #3
Source File: test_binary.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_compose_multigraph(): G = nx.MultiGraph() G.add_edge(1, 2, key=0) G.add_edge(1, 2, key=1) H = nx.MultiGraph() H.add_edge(3, 4, key=0) H.add_edge(3, 4, key=1) GH = nx.compose(G, H) assert_equal(set(GH), set(G) | set(H)) assert_equal(set(GH.edges(keys=True)), set(G.edges(keys=True)) | set(H.edges(keys=True))) H.add_edge(1, 2, key=2) GH = nx.compose(G, H) assert_equal(set(GH), set(G) | set(H)) assert_equal(set(GH.edges(keys=True)), set(G.edges(keys=True)) | set(H.edges(keys=True)))
Example #4
Source File: test_binary.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_compose_multigraph(): G=nx.MultiGraph() G.add_edge(1,2,key=0) G.add_edge(1,2,key=1) H=nx.MultiGraph() H.add_edge(3,4,key=0) H.add_edge(3,4,key=1) GH=nx.compose(G,H) assert_equal( set(GH) , set(G)|set(H)) assert_equal( set(GH.edges(keys=True)) , set(G.edges(keys=True))|set(H.edges(keys=True))) H.add_edge(1,2,key=2) GH=nx.compose(G,H) assert_equal( set(GH) , set(G)|set(H)) assert_equal( set(GH.edges(keys=True)) , set(G.edges(keys=True))|set(H.edges(keys=True)))
Example #5
Source File: gengraph.py From gnn-model-explainer with Apache License 2.0 | 6 votes |
def join_graph(G1, G2, n_pert_edges): """ Join two graphs along matching nodes, then perturb the resulting graph. Args: G1, G2: Networkx graphs to be joined. n_pert_edges: number of perturbed edges. Returns: A new graph, result of merging and perturbing G1 and G2. """ assert n_pert_edges > 0 F = nx.compose(G1, G2) edge_cnt = 0 while edge_cnt < n_pert_edges: node_1 = np.random.choice(G1.nodes()) node_2 = np.random.choice(G2.nodes()) F.add_edge(node_1, node_2) edge_cnt += 1 return F
Example #6
Source File: test_recognition.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): self.T1 = self.graph() self.T2 = self.graph() self.T2.add_node(1) self.T3 = self.graph() self.T3.add_nodes_from(range(5)) edges = [(i, i + 1) for i in range(4)] self.T3.add_edges_from(edges) self.T5 = self.multigraph() self.T5.add_nodes_from(range(5)) edges = [(i, i + 1) for i in range(4)] self.T5.add_edges_from(edges) self.T6 = self.graph() self.T6.add_nodes_from([6, 7]) self.T6.add_edge(6, 7) self.F1 = nx.compose(self.T6, self.T3) self.N4 = self.graph() self.N4.add_node(1) self.N4.add_edge(1, 1) self.N5 = self.graph() self.N5.add_nodes_from(range(5)) self.N6 = self.graph() self.N6.add_nodes_from(range(3)) self.N6.add_edges_from([(0, 1), (1, 2), (2, 0)]) self.NF1 = nx.compose(self.T6, self.N6)
Example #7
Source File: test_binary.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_mixed_type_compose(): G = nx.Graph() H = nx.MultiGraph() U = nx.compose(G,H)
Example #8
Source File: exportnx.py From GraphiPy with MIT License | 5 votes |
def create_from_pd(self, pd_graph, nx_graph=None, directional=False): nodes_df = pd_graph.get_nodes() edges_df = pd_graph.get_edges() # Create graph from edgelist dataframes if nx_graph is None: if directional: nx_graph = nx.DiGraph() else: nx_graph = nx.Graph() for key in edges_df: new_graph = nx.from_pandas_edgelist( edges_df[key], source="Source", target="Target", edge_attr=True) nx_graph = nx.compose(nx_graph, new_graph) # Add node attributes for key in nodes_df: df = nodes_df[key] for index, row in df.iterrows(): _id = row["Id"] node = nx_graph.node[_id] for attr in row.keys(): node[attr] = row[attr] return nx_graph
Example #9
Source File: all.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def compose_all(graphs, name=None): """Return the composition of all graphs. Composition is the simple union of the node sets and edge sets. The node sets of the supplied graphs need not be disjoint. Parameters ---------- graphs : list List of NetworkX graphs name : string Specify name for new graph Returns ------- C : A graph with the same type as the first graph in list Notes ----- It is recommended that the supplied graphs be either all directed or all undirected. Graph, edge, and node attributes are propagated to the union graph. If a graph attribute is present in multiple graphs, then the value from the last graph in the list with that attribute is used. """ graphs = iter(graphs) C = next(graphs) for H in graphs: C = nx.compose(C, H, name=name) return C
Example #10
Source File: test_recognition.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def setUp(self): self.T1 = self.graph() self.T2 = self.graph() self.T2.add_node(1) self.T3 = self.graph() self.T3.add_nodes_from(range(5)) edges = [(i,i+1) for i in range(4)] self.T3.add_edges_from(edges) self.T5 = self.multigraph() self.T5.add_nodes_from(range(5)) edges = [(i,i+1) for i in range(4)] self.T5.add_edges_from(edges) self.T6 = self.graph() self.T6.add_nodes_from([6,7]) self.T6.add_edge(6,7) self.F1 = nx.compose(self.T6, self.T3) self.N4 = self.graph() self.N4.add_node(1) self.N4.add_edge(1,1) self.N5 = self.graph() self.N5.add_nodes_from(range(5)) self.N6 = self.graph() self.N6.add_nodes_from(range(3)) self.N6.add_edges_from([(0,1),(1,2),(2,0)]) self.NF1 = nx.compose(self.T6,self.N6)
Example #11
Source File: test_binary.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_mixed_type_compose(): G = nx.Graph() H = nx.MultiGraph() U = nx.compose(G, H)
Example #12
Source File: test_tree.py From fragile with MIT License | 5 votes |
def random_powerlaw(): g = networkx.DiGraph() t = networkx.random_powerlaw_tree(500, gamma=3, tries=1000, seed=160290) return networkx.compose(g, t)
Example #13
Source File: all.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def compose_all(graphs): """Returns the composition of all graphs. Composition is the simple union of the node sets and edge sets. The node sets of the supplied graphs need not be disjoint. Parameters ---------- graphs : list List of NetworkX graphs Returns ------- C : A graph with the same type as the first graph in list Raises ------ ValueError If `graphs` is an empty list. Notes ----- It is recommended that the supplied graphs be either all directed or all undirected. Graph, edge, and node attributes are propagated to the union graph. If a graph attribute is present in multiple graphs, then the value from the last graph in the list with that attribute is used. """ if not graphs: raise ValueError('cannot apply compose_all to an empty list') graphs = iter(graphs) C = next(graphs) for H in graphs: C = nx.compose(C, H) return C
Example #14
Source File: test_binary.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_mixed_type_compose(): G = nx.Graph() H = nx.MultiGraph() U = nx.compose(G,H)
Example #15
Source File: test_tree.py From fragile with MIT License | 5 votes |
def test_compose_not_crashes(self, tree): other = NetworkxTree() other.data = small_tree() tree.compose(other)
Example #16
Source File: all.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def compose_all(graphs, name=None): """Return the composition of all graphs. Composition is the simple union of the node sets and edge sets. The node sets of the supplied graphs need not be disjoint. Parameters ---------- graphs : list List of NetworkX graphs name : string Specify name for new graph Returns ------- C : A graph with the same type as the first graph in list Notes ----- It is recommended that the supplied graphs be either all directed or all undirected. Graph, edge, and node attributes are propagated to the union graph. If a graph attribute is present in multiple graphs, then the value from the last graph in the list with that attribute is used. """ graphs = iter(graphs) C = next(graphs) for H in graphs: C = nx.compose(C, H, name=name) return C
Example #17
Source File: test_recognition.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.T1 = self.graph() self.T2 = self.graph() self.T2.add_node(1) self.T3 = self.graph() self.T3.add_nodes_from(range(5)) edges = [(i,i+1) for i in range(4)] self.T3.add_edges_from(edges) self.T5 = self.multigraph() self.T5.add_nodes_from(range(5)) edges = [(i,i+1) for i in range(4)] self.T5.add_edges_from(edges) self.T6 = self.graph() self.T6.add_nodes_from([6,7]) self.T6.add_edge(6,7) self.F1 = nx.compose(self.T6, self.T3) self.N4 = self.graph() self.N4.add_node(1) self.N4.add_edge(1,1) self.N5 = self.graph() self.N5.add_nodes_from(range(5)) self.N6 = self.graph() self.N6.add_nodes_from(range(3)) self.N6.add_edges_from([(0,1),(1,2),(2,0)]) self.NF1 = nx.compose(self.T6,self.N6)
Example #18
Source File: queries_to_graph.py From kglib with Apache License 2.0 | 5 votes |
def combine_2_graphs(graph1, graph2): """ Combine two graphs into one. Do this by recognising common nodes between the two. Args: graph1: Graph to compare graph2: Graph to compare Returns: Combined graph """ for node, data in graph1.nodes(data=True): if graph2.has_node(node): data2 = graph2.nodes[node] if data2 != data: raise ValueError((f'Found non-matching node properties for node {node} ' f'between graphs {graph1} and {graph2}:\n' f'In graph {graph1}: {data}\n' f'In graph {graph2}: {data2}')) for sender, receiver, keys, data in graph1.edges(data=True, keys=True): if graph2.has_edge(sender, receiver, keys): data2 = graph2.edges[sender, receiver, keys] if data2 != data: raise ValueError((f'Found non-matching edge properties for edge {sender, receiver, keys} ' f'between graphs {graph1} and {graph2}:\n' f'In graph {graph1}: {data}\n' f'In graph {graph2}: {data2}')) return nx.compose(graph1, graph2)
Example #19
Source File: execute.py From conda-concourse-ci with BSD 3-Clause "New" or "Revised" License | 5 votes |
def collect_tasks(path, folders, matrix_base_dir, channels=None, steps=0, test=False, max_downstream=5, variant_config_files=None, platform_filters=None, clobber_sections_file=None, append_sections_file=None, pass_throughs=None, skip_existing=True): # runs = ['test'] # not testing means build and test # if not test: # runs.insert(0, 'build') runs = ['build'] task_graph = nx.DiGraph() parsed_cli_args = _parse_python_numpy_from_pass_throughs(pass_throughs) config = conda_build.api.Config(clobber_sections_file=clobber_sections_file, append_sections_file=append_sections_file, skip_existing=skip_existing, **parsed_cli_args) platform_filters = ensure_list(platform_filters) if platform_filters else ['*'] for run in runs: platforms = parse_platforms(matrix_base_dir, run, platform_filters) # loop over platforms here because each platform may have different dependencies # each platform will be submitted with a different label for platform in platforms: index_key = '-'.join([platform['platform'], str(platform['arch'])]) config.variants = get_package_variants(path, config, platform.get('variants')) config.channel_urls = channels or [] config.variant_config_files = variant_config_files or [] conda_resolve = Resolve(get_build_index(subdir=index_key, bldpkgs_dir=config.bldpkgs_dir, channel_urls=channels)[0]) # this graph is potentially different for platform and for build or test mode ("run") g = construct_graph(path, worker=platform, folders=folders, run=run, matrix_base_dir=matrix_base_dir, conda_resolve=conda_resolve, config=config) # Apply the build label to any nodes that need (re)building or testing expand_run(g, config=config.copy(), conda_resolve=conda_resolve, worker=platform, run=run, steps=steps, max_downstream=max_downstream, recipes_dir=path, matrix_base_dir=matrix_base_dir) # merge this graph with the main one task_graph = nx.compose(task_graph, g) collapse_noarch_python_nodes(task_graph) return task_graph
Example #20
Source File: test_binary.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_union_and_compose(): K3 = complete_graph(3) P3 = path_graph(3) G1 = nx.DiGraph() G1.add_edge('A', 'B') G1.add_edge('A', 'C') G1.add_edge('A', 'D') G2 = nx.DiGraph() G2.add_edge('1', '2') G2.add_edge('1', '3') G2.add_edge('1', '4') G = union(G1, G2) H = compose(G1, G2) assert_edges_equal(G.edges(), H.edges()) assert_false(G.has_edge('A', 1)) assert_raises(nx.NetworkXError, nx.union, K3, P3) H1 = union(H, G1, rename=('H', 'G1')) assert_equal(sorted(H1.nodes()), ['G1A', 'G1B', 'G1C', 'G1D', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) H2 = union(H, G2, rename=("H", "")) assert_equal(sorted(H2.nodes()), ['1', '2', '3', '4', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) assert_false(H1.has_edge('NB', 'NA')) G = compose(G, G) assert_edges_equal(G.edges(), H.edges()) G2 = union(G2, G2, rename=('', 'copy')) assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) assert_equal(sorted(G2.neighbors('copy4')), []) assert_equal(sorted(G2.neighbors('copy1')), ['copy2', 'copy3', 'copy4']) assert_equal(len(G), 8) assert_equal(number_of_edges(G), 6) E = disjoint_union(G, G) assert_equal(len(E), 16) assert_equal(number_of_edges(E), 12) E = disjoint_union(G1, G2) assert_equal(sorted(E.nodes()), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) G = nx.Graph() H = nx.Graph() G.add_nodes_from([(1, {'a1': 1})]) H.add_nodes_from([(1, {'b1': 1})]) R = compose(G, H) assert_equal(R.nodes, {1: {'a1': 1, 'b1': 1}})
Example #21
Source File: test_binary.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def test_union_and_compose(): K3=complete_graph(3) P3=path_graph(3) G1=nx.DiGraph() G1.add_edge('A','B') G1.add_edge('A','C') G1.add_edge('A','D') G2=nx.DiGraph() G2.add_edge('1','2') G2.add_edge('1','3') G2.add_edge('1','4') G=union(G1,G2) H=compose(G1,G2) assert_edges_equal(G.edges(),H.edges()) assert_false(G.has_edge('A',1)) assert_raises(nx.NetworkXError, nx.union, K3, P3) H1=union(H,G1,rename=('H','G1')) assert_equal(sorted(H1.nodes()), ['G1A', 'G1B', 'G1C', 'G1D', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) H2=union(H,G2,rename=("H","")) assert_equal(sorted(H2.nodes()), ['1', '2', '3', '4', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) assert_false(H1.has_edge('NB','NA')) G=compose(G,G) assert_edges_equal(G.edges(),H.edges()) G2=union(G2,G2,rename=('','copy')) assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) assert_equal(G2.neighbors('copy4'),[]) assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4']) assert_equal(len(G),8) assert_equal(number_of_edges(G),6) E=disjoint_union(G,G) assert_equal(len(E),16) assert_equal(number_of_edges(E),12) E=disjoint_union(G1,G2) assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Example #22
Source File: test_binary.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def test_union_and_compose(): K3=complete_graph(3) P3=path_graph(3) G1=nx.DiGraph() G1.add_edge('A','B') G1.add_edge('A','C') G1.add_edge('A','D') G2=nx.DiGraph() G2.add_edge('1','2') G2.add_edge('1','3') G2.add_edge('1','4') G=union(G1,G2) H=compose(G1,G2) assert_edges_equal(G.edges(),H.edges()) assert_false(G.has_edge('A',1)) assert_raises(nx.NetworkXError, nx.union, K3, P3) H1=union(H,G1,rename=('H','G1')) assert_equal(sorted(H1.nodes()), ['G1A', 'G1B', 'G1C', 'G1D', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) H2=union(H,G2,rename=("H","")) assert_equal(sorted(H2.nodes()), ['1', '2', '3', '4', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) assert_false(H1.has_edge('NB','NA')) G=compose(G,G) assert_edges_equal(G.edges(),H.edges()) G2=union(G2,G2,rename=('','copy')) assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) assert_equal(sorted(G2.neighbors('copy4')),[]) assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4']) assert_equal(len(G),8) assert_equal(number_of_edges(G),6) E=disjoint_union(G,G) assert_equal(len(E),16) assert_equal(number_of_edges(E),12) E=disjoint_union(G1,G2) assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) G = nx.Graph() H = nx.Graph() G.add_nodes_from([(1, {'a1': 1})]) H.add_nodes_from([(1, {'b1': 1})]) R = compose(G, H) assert_equal(R.nodes, {1: {'a1': 1, 'b1': 1}})
Example #23
Source File: apiutils.py From aurum-datadiscovery with MIT License | 4 votes |
def absorb_provenance(self, drs, annotate_and_edges=False, annotate_or_edges=False): """ Merge provenance of the input parameter into self, *not* the data. :param drs: :return: """ def annotate_union_edges(label): # Find nodes that intersect (those that will contain add_edges) my_data = set(self.data) merging_data = set(drs.data) disjoint = my_data.intersection( merging_data) # where a union is created # Now find the incoming edges to these nodes in each of the drs's node_and_edges = [] for el in disjoint: input_edges1 = self._provenance.prov_graph().in_edges(el, data=True) input_edges2 = drs._provenance.prov_graph().in_edges(el, data=True) node_and_edges.append((input_edges1, input_edges2)) # Now locate the nodes in the merged prov graph and annotate edges # with AND for input1, input2 in node_and_edges: for src1, tar1, dic1 in input1: # this is the edge information edge_data = merge[src1][tar1] for e in edge_data: # we iterate over each edge # we assign the new metadata as data assigned to the # edge edge_data[e][label] = 1 for src2, tar2, dic2 in input2: edge_data = merge[src2][tar2] for e in edge_data: edge_data[e][label] = 1 # Reset ranking self._ranked = False # Get prov graph of merging prov_graph_of_merging = drs.get_provenance().prov_graph() # Compose into my prov graph merge = nx.compose(self._provenance.prov_graph(), prov_graph_of_merging) if annotate_and_edges: annotate_union_edges('AND') if annotate_or_edges: annotate_union_edges('OR') # Rewrite my prov graph to the new merged one and return self._provenance.swap_p_graph(merge) return self