Commit b19dc287 authored by Alexander Lercher's avatar Alexander Lercher

Call hypergraph impl

parent f18e7ea3
import networkx as nx import networkx as nx
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from collections import Counter from collections import Counter
import HyperGraph as hg from HyperGraph import HyperGraph
import warnings import warnings
# pip install networkx # pip install networkx
...@@ -13,9 +13,18 @@ import warnings ...@@ -13,9 +13,18 @@ import warnings
class SemanticLinking: class SemanticLinking:
def __init__(self): hg: HyperGraph = None
hg.classify() df_nodes = []
destf_nodes = []
def __init__(self):
warnings.filterwarnings('ignore')
self.hg = HyperGraph()
self.hg.classify()
self.df_nodes = self.hg.cluster_labels
self.destf_nodes = self.hg.dest_cluster_labels
def _color_network(self, G): def _color_network(self, G):
"""Colors the network so that neighboring nodes all have distinct colors. """Colors the network so that neighboring nodes all have distinct colors.
...@@ -30,7 +39,6 @@ class SemanticLinking: ...@@ -30,7 +39,6 @@ class SemanticLinking:
coloring[color] = set([node]) coloring[color] = set([node])
return coloring return coloring
def _labeling_complete(self, labeling, G): def _labeling_complete(self, labeling, G):
"""Determines whether or not LPA is done. """Determines whether or not LPA is done.
...@@ -42,7 +50,6 @@ class SemanticLinking: ...@@ -42,7 +50,6 @@ class SemanticLinking:
return all(labeling[v] in self._most_frequent_labels(v, labeling, G) return all(labeling[v] in self._most_frequent_labels(v, labeling, G)
for v in G if len(G[v]) > 0) for v in G if len(G[v]) > 0)
def _most_frequent_labels(self, node, labeling, G): def _most_frequent_labels(self, node, labeling, G):
"""Returns a set of all labels with maximum frequency in `labeling`. """Returns a set of all labels with maximum frequency in `labeling`.
...@@ -58,7 +65,6 @@ class SemanticLinking: ...@@ -58,7 +65,6 @@ class SemanticLinking:
max_freq = max(freqs.values()) max_freq = max(freqs.values())
return {label for label, freq in freqs.items() if freq == max_freq} return {label for label, freq in freqs.items() if freq == max_freq}
def _update_label(self, node, labeling, G): def _update_label(self, node, labeling, G):
"""Updates the label of a node using the Prec-Max tie breaking algorithm """Updates the label of a node using the Prec-Max tie breaking algorithm
...@@ -75,16 +81,8 @@ class SemanticLinking: ...@@ -75,16 +81,8 @@ class SemanticLinking:
labeling[node] = max(high_labels) labeling[node] = max(high_labels)
warnings.filterwarnings('ignore')
#G = nx.DiGraph(directed=True)
G = nx.MultiDiGraph(day="Stackoverflow") G = nx.MultiDiGraph(day="Stackoverflow")
df_nodes = hg.clusterlabels
destf_nodes = hg.destclusterlabel
color_map = {1: '#f09494', 2: '#eebcbc', 3: '#72bbd0', 4: '#91f0a1', 5: '#629fff', 6: '#bcc2f2', color_map = {1: '#f09494', 2: '#eebcbc', 3: '#72bbd0', 4: '#91f0a1', 5: '#629fff', 6: '#bcc2f2',
7: '#eebcbc', 8: '#f1f0c0', 9: '#d2ffe7', 10: '#caf3a6', 11: '#ffdf55', 12: '#ef77aa', 7: '#eebcbc', 8: '#f1f0c0', 9: '#d2ffe7', 10: '#caf3a6', 11: '#ffdf55', 12: '#ef77aa',
13: '#d6dcff', 14: '#d2f5f0'} 13: '#d6dcff', 14: '#d2f5f0'}
...@@ -102,24 +100,23 @@ class SemanticLinking: ...@@ -102,24 +100,23 @@ class SemanticLinking:
labeling = {} labeling = {}
def drawedges(self): def drawedges(self):
"""drawing edges in graph""" """drawing edges in graph"""
labelvalues = self.hg.label_values
for drow in range(len(self.df_nodes)): for drow in range(len(self.df_nodes)):
for row in range(len(self.destf_nodes[drow])): for row in range(len(self.destf_nodes[drow])):
self.G.add_edge(self.df_nodes[drow], self.destf_nodes[drow][row]) self.G.add_edge(self.df_nodes[drow], self.destf_nodes[drow][row])
for row in range(len(hg.labalvlues)): for row in range(len(labelvalues)):
for row1 in range(len(hg.labalvlues)): for row1 in range(len(labelvalues)):
self.weight1.append(self.G.number_of_edges(hg.labalvlues[row], hg.labalvlues[row1])) self.weight1.append(self.G.number_of_edges(labelvalues[row], labelvalues[row1]))
print("The number of coccurance from node ", hg.labalvlues[row],"to node ", hg.labalvlues[row1], ": ", self.weight1[row1]) print("The number of coccurance from node ", labelvalues[row],"to node ", labelvalues[row1], ": ", self.weight1[row1])
self.G.__setattr__('weight', self.weight1) self.G.__setattr__('weight', self.weight1)
def dolabeling(self): def dolabeling(self):
"""label_propagation_communities(G) """ """label_propagation_communities(G) """
coloring = self._color_network(self.G) coloring = self._color_network(self.G)
# Create a unique label for each node in the graph # Create a unique label for each node in the graph
labeling = {v: k for k, v in enumerate(self.G)} labeling = {v: k for k, v in enumerate(self.G)}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment