def test_input_output(): l = [nx.Graph([(1,2)]),nx.Graph([(3,4)])] U = nx.disjoint_union_all(l) assert_equal(len(l),2) C = nx.compose_all(l) assert_equal(len(l),2) l = [nx.Graph([(1,2)]),nx.Graph([(1,2)])] R = nx.intersection_all(l) assert_equal(len(l),2)
def test_input_output(): l = [nx.Graph([(1, 2)]), nx.Graph([(3, 4)])] U = nx.disjoint_union_all(l) assert len(l) == 2 C = nx.compose_all(l) assert len(l) == 2 l = [nx.Graph([(1, 2)]), nx.Graph([(1, 2)])] R = nx.intersection_all(l) assert len(l) == 2
def test_intersection_all_multigraph_attributes(): g = nx.MultiGraph() g.add_edge(0, 1, key=0) g.add_edge(0, 1, key=1) g.add_edge(0, 1, key=2) h = nx.MultiGraph() h.add_edge(0, 1, key=0) h.add_edge(0, 1, key=3) gh = nx.intersection_all([g, h]) assert_equal( set(gh.nodes()) , set(g.nodes()) ) assert_equal( set(gh.nodes()) , set(h.nodes()) ) assert_equal( sorted(gh.edges()) , [(0,1)] ) assert_equal( sorted(gh.edges(keys=True)) , [(0,1,0)] )
def test_intersection_all_multigraph_attributes(): g = nx.MultiGraph() g.add_edge(0, 1, key=0) g.add_edge(0, 1, key=1) g.add_edge(0, 1, key=2) h = nx.MultiGraph() h.add_edge(0, 1, key=0) h.add_edge(0, 1, key=3) gh = nx.intersection_all([g, h]) assert set(gh.nodes()) == set(g.nodes()) assert set(gh.nodes()) == set(h.nodes()) assert sorted(gh.edges()) == [(0, 1)] assert sorted(gh.edges(keys=True)) == [(0, 1, 0)]
def test_intersection_all(): G=nx.Graph() H=nx.Graph() R=nx.Graph() G.add_nodes_from([1,2,3,4]) G.add_edge(1,2) G.add_edge(2,3) H.add_nodes_from([1,2,3,4]) H.add_edge(2,3) H.add_edge(3,4) R.add_nodes_from([1,2,3,4]) R.add_edge(2,3) R.add_edge(4,1) I=nx.intersection_all([G,H,R]) assert_equal( set(I.nodes()) , set([1,2,3,4]) ) assert_equal( sorted(I.edges()) , [(2,3)] )
def test_intersection_all_attributes_different_node_sets(): g = nx.Graph() g.add_node(0, x=4) g.add_node(1, x=5) g.add_edge(0, 1, size=5) g.graph["name"] = "g" h = g.copy() g.add_node(2) h.graph["name"] = "h" h.graph["attr"] = "attr" h.nodes[0]["x"] = 7 gh = nx.intersection_all([g, h]) assert set(gh.nodes()) == set(h.nodes()) assert sorted(gh.edges()) == sorted(g.edges())
def test_intersection_all(): G = nx.Graph() H = nx.Graph() R = nx.Graph() G.add_nodes_from([1, 2, 3, 4]) G.add_edge(1, 2) G.add_edge(2, 3) H.add_nodes_from([1, 2, 3, 4]) H.add_edge(2, 3) H.add_edge(3, 4) R.add_nodes_from([1, 2, 3, 4]) R.add_edge(2, 3) R.add_edge(4, 1) I = nx.intersection_all([G, H, R]) assert set(I.nodes()) == set([1, 2, 3, 4]) assert sorted(I.edges()) == [(2, 3)]
def test_intersection_all_different_node_sets(): G = nx.Graph() H = nx.Graph() R = nx.Graph() G.add_nodes_from([1, 2, 3, 4, 6, 7]) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(6, 7) H.add_nodes_from([1, 2, 3, 4]) H.add_edge(2, 3) H.add_edge(3, 4) R.add_nodes_from([1, 2, 3, 4, 8, 9]) R.add_edge(2, 3) R.add_edge(4, 1) R.add_edge(8, 9) I = nx.intersection_all([G, H, R]) assert set(I.nodes()) == {1, 2, 3, 4} assert sorted(I.edges()) == [(2, 3)]
def test_intersection_all_attributes(): g = nx.Graph() g.add_node(0, x=4) g.add_node(1, x=5) g.add_edge(0, 1, size=5) g.graph['name'] = 'g' h = g.copy() h.graph['name'] = 'h' h.graph['attr'] = 'attr' h.nodes[0]['x'] = 7 gh = nx.intersection_all([g, h]) assert set(gh.nodes()) == set(g.nodes()) assert set(gh.nodes()) == set(h.nodes()) assert sorted(gh.edges()) == sorted(g.edges()) h.remove_node(0) pytest.raises(nx.NetworkXError, nx.intersection, g, h)
def test_intersection_all_attributes(): g = nx.Graph() g.add_node(0, x=4) g.add_node(1, x=5) g.add_edge(0, 1, size=5) g.graph['name'] = 'g' h = g.copy() h.graph['name'] = 'h' h.graph['attr'] = 'attr' h.node[0]['x'] = 7 gh = nx.intersection_all([g, h]) assert_equal( set(gh.nodes()) , set(g.nodes()) ) assert_equal( set(gh.nodes()) , set(h.nodes()) ) assert_equal( sorted(gh.edges()) , sorted(g.edges()) ) h.remove_node(0) assert_raises(nx.NetworkXError, nx.intersection, g, h)
def intersection(G, H): """Returns a new graph that contains only the nodes and the edges that exist in both G and H. Parameters ---------- G,H : graph A NetworkX graph. G and H can have different node sets but must be both graphs or both multigraphs. Raises ------ NetworkXError If one is a MultiGraph and the other one is a graph. Returns ------- GH : A new graph with the same type as G. Notes ----- Attributes from the graph, nodes, and edges are not copied to the new graph. If you want a new graph of the intersection of G and H with the attributes (including edge data) from G use remove_nodes_from() as follows >>> G = nx.path_graph(3) >>> H = nx.path_graph(5) >>> R = G.copy() >>> R.remove_nodes_from(n for n in G if n not in H) >>> R.remove_edges_from(e for e in G.edges if e not in H.edges) Examples -------- >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)]) >>> H = nx.Graph([(0, 3), (1, 2), (2, 3)]) >>> R = nx.intersection(G, H) >>> R.nodes NodeView((0, 1, 2)) >>> R.edges EdgeView([(1, 2)]) """ return nx.intersection_all([G, H])
def test_mixed_type_intersection(): G = nx.Graph() H = nx.MultiGraph() I = nx.Graph() U = nx.intersection_all([G,H,I])
def test_empty_intersection_all(): nx.intersection_all([])
def test_empty_intersection_all(): with pytest.raises(ValueError): nx.intersection_all([])
def test_mixed_type_intersection(): G = nx.Graph() H = nx.MultiGraph() I = nx.Graph() U = nx.intersection_all([G, H, I])
for nodoi in list(redes['AP-MS']): if nodoi not in commonNodes: redes['AP-MS'].remove_node(nodoi) for nodoi in list(redes['LIT']): if nodoi not in commonNodes: redes['LIT'].remove_node(nodoi) redes['Y2H'].number_of_nodes() redes['AP-MS'].number_of_nodes() redes['LIT'].number_of_nodes() #operaciones entre sets redesAll = nx.intersection_all([redes['Y2H'], redes['AP-MS'], redes['LIT']]) ne_redesAll = redesAll.number_of_edges() redesY2H_AP = nx.intersection(redes['Y2H'], redes['AP-MS']) ne_redesY2H_AP = redesY2H_AP.number_of_edges() ne_onlyY2H_AP = ne_redesY2H_AP - ne_redesAll redesY2H_LIT = nx.intersection(redes['Y2H'], redes['LIT']) ne_redesY2H_LIT = redesY2H_LIT.number_of_edges() ne_onlyY2H_LIT = ne_redesY2H_LIT - ne_redesAll redesAP_LIT = nx.intersection(redes['AP-MS'], redes['LIT']) ne_redesAP_LIT = redesAP_LIT.number_of_edges() ne_onlyAP_LIT = ne_redesAP_LIT - ne_redesAll neY2H = redes['Y2H'].number_of_edges()
def join(source, target, joinBy): ''' returns a graph of a single 'or' condition which may contain inner 'and' conditions ''' conn = sqlite3.connect('data.db') cur = conn.cursor() intersectGraphs = [] if source == target: for i in range(len(joinBy)): # "and" conditions (intersections) # assumes that there has to be an "attr" composeGraphs = [] if joinBy[i]['value'] == []: a = list( cur.execute(f''' SELECT GROUP_CONCAT({source}), {joinBy[i]["attr"]} FROM T GROUP BY {joinBy[i]["attr"]} ''')) for tup in a: nodes = [x.strip() for x in tup[0].split(',')] composeGraphs.append(nx.complete_graph(nodes)) else: li = joinBy[i]['value'] for val in li: a = list( cur.execute( f''' SELECT GROUP_CONCAT({source}), {joinBy[i]["attr"]} FROM T WHERE {joinBy[i]["attr"]}=? GROUP BY {joinBy[i]["attr"]} ''', [val])) nodes = [x.strip() for x in a[0][0].split(',')] composeGraphs.append(nx.complete_graph(nodes)) # adding a graph from a single joinBy condition intersectGraphs.append(nx.compose_all(composeGraphs)) else: for i in range(len(joinBy)): if joinBy[i]['attr'] == '': pairs = list(cur.execute(f'SELECT {source}, {target} from T')) else: if joinBy[i]['value'] == []: li = list( cur.execute(f''' SELECT DISTINCT {joinBy[i]["attr"]} FROM T ''')) else: li = joinBy[i]['value'] for val in li: pairs = list( cur.execute( f''' SELECT {source}, {target} FROM T WHERE {joinBy[i]["attr"]}=? ''', [val])) temp = nx.Graph() for p in pairs: temp.add_node(p[0], type=source, group=1, color="blue") temp.add_node(p[1], type=target, group=2, color="red") temp.add_edges_from(pairs) intersectGraphs.append(temp) for i in range(len(intersectGraphs)): if i > 0: intersectGraphs[i].add_nodes_from(intersectGraphs[i - 1]) G = nx.intersection_all(intersectGraphs) G.remove_nodes_from(list(nx.isolates(G))) return G
def test_mixed_type_intersection(): with pytest.raises(nx.NetworkXError): G = nx.Graph() H = nx.MultiGraph() I = nx.Graph() U = nx.intersection_all([G, H, I])
def test_empty_intersection_all(): nx.intersection_all([])