def test_copy(): node_a = 'A' node_b = 'B' node_c = 'C' node_d = 'D' node_e = 'E' nodes1 = set([node_a, node_b, node_c]) frozen_nodes1 = frozenset(nodes1) nodes2 = set([node_a, node_d]) frozen_nodes2 = frozenset(nodes2) nodes3 = set([node_d, node_e]) frozen_nodes3 = frozenset(nodes3) common_attrib = {'weight': 2, 'sink': False} hyperedges = hyperedges = [nodes1, nodes2, nodes3] H = UndirectedHypergraph() H.add_node("A", root=True) hyperedge_names = \ H.add_hyperedges(hyperedges, common_attrib, color='white') new_H = H.copy() assert new_H._node_attributes == H._node_attributes assert new_H._hyperedge_attributes == H._hyperedge_attributes assert new_H._star == H._star assert new_H._node_set_to_hyperedge == H._node_set_to_hyperedge
def test_copy(): node_a = 'A' node_b = 'B' node_c = 'C' node_d = 'D' node_e = 'E' nodes1 = set([node_a, node_b, node_c]) frozen_nodes1 = frozenset(nodes1) nodes2 = set([node_a, node_d]) frozen_nodes2 = frozenset(nodes2) nodes3 = set([node_d, node_e]) frozen_nodes3 = frozenset(nodes3) common_attrib = {'weight': 2, 'sink': False} hyperedges = hyperedges = [nodes1, nodes2, nodes3] H = UndirectedHypergraph() H.add_node("A", root=True) hyperedge_names = \ H.add_hyperedges(hyperedges, common_attrib, color='white') new_H = H.copy() assert new_H._node_attributes == H._node_attributes assert new_H._hyperedge_attributes == H._hyperedge_attributes assert new_H._star == H._star assert new_H._node_set_to_hyperedge == H._node_set_to_hyperedge
def from_networkx_graph(nx_graph): """Returns an UndirectedHypergraph object that is the graph equivalent of the given NetworkX Graph object. :param nx_graph: the NetworkX undirected graph object to transform. :returns: UndirectedHypergraph -- H object equivalent to the NetworkX undirected graph. :raises: TypeError -- Transformation only applicable to undirected NetworkX graphs """ import networkx as nx if not isinstance(nx_graph, nx.Graph): raise TypeError("Transformation only applicable to undirected \ NetworkX graphs") G = UndirectedHypergraph() for node in nx_graph.nodes(): G.add_node(node, copy.copy(nx_graph.nodes[node])) for edge in nx_graph.edges(): G.add_hyperedge([edge[0], edge[1]], copy.copy(nx_graph[edge[0]][edge[1]])) return G
def from_networkx_graph(nx_graph): """Returns an UndirectedHypergraph object that is the graph equivalent of the given NetworkX Graph object. :param nx_graph: the NetworkX undirected graph object to transform. :returns: UndirectedHypergraph -- H object equivalent to the NetworkX undirected graph. :raises: TypeError -- Transformation only applicable to undirected NetworkX graphs """ import networkx as nx if not isinstance(nx_graph, nx.Graph): raise TypeError("Transformation only applicable to undirected \ NetworkX graphs") G = UndirectedHypergraph() for node in nx_graph.nodes_iter(): G.add_node(node, copy.copy(nx_graph.node[node])) for edge in nx_graph.edges_iter(): G.add_hyperedge([edge[0], edge[1]], copy.copy(nx_graph[edge[0]][edge[1]])) return G
def test_get_node_attributes(): node_a = 'A' node_b = 'B' node_c = 'C' attrib_c = {'alt_name': 1337} node_d = 'D' attrib_d = {'label': 'black', 'sink': True} # Test adding unadded nodes with various attribute settings H = UndirectedHypergraph() H.add_node(node_a) H.add_node(node_b, source=True) H.add_node(node_c, attrib_c) H.add_node(node_d, attrib_d, sink=False) attrs = H.get_node_attributes(node_b) assert attrs['source'] is True # Try requesting an invalid node try: H.get_node_attributes("E") assert False except ValueError: pass except BaseException as e: assert False, e
def test_get_node_attributes(): node_a = 'A' node_b = 'B' node_c = 'C' attrib_c = {'alt_name': 1337} node_d = 'D' attrib_d = {'label': 'black', 'sink': True} # Test adding unadded nodes with various attribute settings H = UndirectedHypergraph() H.add_node(node_a) H.add_node(node_b, source=True) H.add_node(node_c, attrib_c) H.add_node(node_d, attrib_d, sink=False) attrs = H.get_node_attributes(node_b) assert attrs['source'] is True # Try requesting an invalid node try: H.get_node_attributes("E") assert False except ValueError: pass except BaseException as e: assert False, e
def test_add_hyperedge(): node_a = 'A' node_b = 'B' node_c = 'C' node_d = 'D' nodes1 = set([node_a, node_b, node_c]) frozen_nodes1 = frozenset(nodes1) attrib = {'weight': 6, 'color': 'black'} H = UndirectedHypergraph() H.add_node(node_a, label=1337) hyperedge_name = H.add_hyperedge(nodes1, attrib, weight=5) assert hyperedge_name == 'e1' # Test that all hyperedge attributes are correct assert H._hyperedge_attributes[hyperedge_name]['nodes'] == nodes1 assert H._hyperedge_attributes[hyperedge_name]['__frozen_nodes'] == \ frozen_nodes1 assert H._hyperedge_attributes[hyperedge_name]['weight'] == 5 assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black' # Test that compose_hyperedge list contains the correct info assert frozen_nodes1 in H._node_set_to_hyperedge assert hyperedge_name == H._node_set_to_hyperedge[frozen_nodes1] # Test that the stars contain the correct info for node in frozen_nodes1: assert hyperedge_name in H._star[node] # Test that adding same hyperedge will only update attributes new_attrib = {'weight': 10} H.add_hyperedge(nodes1, new_attrib) assert H._hyperedge_attributes[hyperedge_name]['weight'] == 10 assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black' try: H.add_hyperedge(set()) assert False except ValueError: pass except BaseException as e: assert False, e
def test_add_hyperedge(): node_a = 'A' node_b = 'B' node_c = 'C' node_d = 'D' nodes1 = set([node_a, node_b, node_c]) frozen_nodes1 = frozenset(nodes1) attrib = {'weight': 6, 'color': 'black'} H = UndirectedHypergraph() H.add_node(node_a, label=1337) hyperedge_name = H.add_hyperedge(nodes1, attrib, weight=5) assert hyperedge_name == 'e1' # Test that all hyperedge attributes are correct assert H._hyperedge_attributes[hyperedge_name]['nodes'] == nodes1 assert H._hyperedge_attributes[hyperedge_name]['__frozen_nodes'] == \ frozen_nodes1 assert H._hyperedge_attributes[hyperedge_name]['weight'] == 5 assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black' # Test that compose_hyperedge list contains the correct info assert frozen_nodes1 in H._node_set_to_hyperedge assert hyperedge_name == H._node_set_to_hyperedge[frozen_nodes1] # Test that the stars contain the correct info for node in frozen_nodes1: assert hyperedge_name in H._star[node] # Test that adding same hyperedge will only update attributes new_attrib = {'weight': 10} H.add_hyperedge(nodes1, new_attrib) assert H._hyperedge_attributes[hyperedge_name]['weight'] == 10 assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black' try: H.add_hyperedge(set()) assert False except ValueError: pass except BaseException as e: assert False, e
def test_add_node(): node_a = 'A' node_b = 'B' node_c = 'C' attrib_c = {'alt_name': 1337} node_d = 'D' attrib_d = {'label': 'black', 'sink': True} # Test adding unadded nodes with various attribute settings H = UndirectedHypergraph() H.add_node(node_a) H.add_node(node_b, source=True) H.add_node(node_c, attrib_c) H.add_node(node_d, attrib_d, sink=False) assert node_a in H._node_attributes assert H._node_attributes[node_a] == {} assert node_b in H._node_attributes assert H._node_attributes[node_b]['source'] is True assert node_c in H._node_attributes assert H._node_attributes[node_c]['alt_name'] == 1337 assert node_d in H._node_attributes assert H._node_attributes[node_d]['label'] == 'black' assert H._node_attributes[node_d]['sink'] is False # Test adding a node that has already been added H.add_nodes(node_a, common=False) assert H._node_attributes[node_a]['common'] is False # Pass in bad (non-dict) attribute try: H.add_node(node_a, ["label", "black"]) assert False except AttributeError: pass except BaseException as e: assert False, e
def test_add_node(): node_a = 'A' node_b = 'B' node_c = 'C' attrib_c = {'alt_name': 1337} node_d = 'D' attrib_d = {'label': 'black', 'sink': True} # Test adding unadded nodes with various attribute settings H = UndirectedHypergraph() H.add_node(node_a) H.add_node(node_b, source=True) H.add_node(node_c, attrib_c) H.add_node(node_d, attrib_d, sink=False) assert node_a in H._node_attributes assert H._node_attributes[node_a] == {} assert node_b in H._node_attributes assert H._node_attributes[node_b]['source'] is True assert node_c in H._node_attributes assert H._node_attributes[node_c]['alt_name'] == 1337 assert node_d in H._node_attributes assert H._node_attributes[node_d]['label'] == 'black' assert H._node_attributes[node_d]['sink'] is False # Test adding a node that has already been added H.add_nodes(node_a, common=False) assert H._node_attributes[node_a]['common'] is False # Pass in bad (non-dict) attribute try: H.add_node(node_a, ["label", "black"]) assert False except AttributeError: pass except BaseException as e: assert False, e