def test_add_nodes(): node_a = 'A' node_b = 'B' node_c = 'C' attrib_c = {'alt_name': 1337} node_d = 'D' attrib_d = {'label': 'black', 'sink': True} common_attrib = {'common': True, 'source': False} node_list = [node_a, (node_b, {'source': False}), (node_c, attrib_c), (node_d, attrib_d)] # Test adding unadded nodes with various attribute settings H = UndirectedHypergraph() H.add_nodes(node_list, common_attrib) assert node_a in H._node_attributes assert H._node_attributes[node_a] == common_attrib assert node_b in H._node_attributes assert H._node_attributes[node_b]['source'] is False 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 True node_set = H.get_node_set() assert node_set == set(['A', 'B', 'C', 'D']) assert len(node_set) == len(node_list) for node in H.node_iterator(): assert node in node_set
def to_graph_decomposition(H): """Returns an UndirectedHypergraph object that has the same nodes (and corresponding attributes) as the given H, except that for all hyperedges in the given H, each node in the hyperedge is pairwise connected to every other node also in that hyperedge in the new H. Said another way, each of the original hyperedges are decomposed in the new H into cliques (aka the "2-section" or "clique graph"). :param H: the H to decompose into a graph. :returns: UndirectedHypergraph -- the decomposed H. :raises: TypeError -- Transformation only applicable to undirected Hs """ if not isinstance(H, UndirectedHypergraph): raise TypeError("Transformation only applicable to \ undirected Hs") G = UndirectedHypergraph() nodes = [(node, H.get_node_attributes(node_attributes)) for node in G.node_iterator()] G.add_nodes(nodes) edges = [(node_a, node_b) for hyperedge_id in H.hyperedge_id_iterator() for node_a in H.get_hyperedge_nodes(hyperedge_id) for node_b in H.get_hyperedge_nodes(hyperedge_id) if node_a != node_b] G.add_hyperedges(edges) return G
def to_graph_decomposition(H): """Returns an UndirectedHypergraph object that has the same nodes (and corresponding attributes) as the given H, except that for all hyperedges in the given H, each node in the hyperedge is pairwise connected to every other node also in that hyperedge in the new H. Said another way, each of the original hyperedges are decomposed in the new H into cliques (aka the "2-section" or "clique graph"). :param H: the H to decompose into a graph. :returns: UndirectedHypergraph -- the decomposed H. :raises: TypeError -- Transformation only applicable to undirected Hs """ if not isinstance(H, UndirectedHypergraph): raise TypeError("Transformation only applicable to \ undirected Hs") G = UndirectedHypergraph() nodes = [(node, H.get_node_attributes(node_attributes)) for node in G.node_iterator()] G.add_nodes(nodes) edges = [(node_a, node_b) for hyperedge_id in H.hyperedge_id_iterator() for node_a in H.get_hyperedge_nodes(hyperedge_id) for node_b in H.get_hyperedge_nodes(hyperedge_id) if node_a != node_b] G.add_hyperedges(edges) return G
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
def test_add_nodes(): node_a = 'A' node_b = 'B' node_c = 'C' attrib_c = {'alt_name': 1337} node_d = 'D' attrib_d = {'label': 'black', 'sink': True} common_attrib = {'common': True, 'source': False} node_list = [ node_a, (node_b, { 'source': False }), (node_c, attrib_c), (node_d, attrib_d) ] # Test adding unadded nodes with various attribute settings H = UndirectedHypergraph() H.add_nodes(node_list, common_attrib) assert node_a in H._node_attributes assert H._node_attributes[node_a] == common_attrib assert node_b in H._node_attributes assert H._node_attributes[node_b]['source'] is False 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 True node_set = H.get_node_set() assert node_set == set(['A', 'B', 'C', 'D']) assert len(node_set) == len(node_list) for node in H.node_iterator(): assert node in node_set
'pants_on_fire_c', 'venue' ]) df1 = pd.read_csv( "C:/Users/user/PycharmProjects/dataset/yelp-dataset/result.csv", names=['subject', 'edge']) # df.info() # print(df.head(10)) # Initialize an empty hypergraph H = UndirectedHypergraph() # NODE node_list = df['id'] node_list1 = tuple(node_list) # EDGE node_subject = df1['edge'] # attribute_list = {"f1": df['speaker'], # "f2": df['job'], # "f3": df['state']} # economy = df.loc[df['subject'] == 'economy'] # # list = economy['id'] hyperedge_list = (node_subject) edge_list = list(zip(hyperedge_list, hyperedge_list.index)) # print(hyperedge_list) node = H.add_nodes(node_list1) hyperedge_ids = H.add_hyperedges(edge_list) print(node)