Commit 4a9cd21f authored by Alexander Lercher's avatar Alexander Lercher

Removed global variables

parent b19dc287
...@@ -17,13 +17,24 @@ class SemanticLinking: ...@@ -17,13 +17,24 @@ class SemanticLinking:
df_nodes = [] df_nodes = []
destf_nodes = [] destf_nodes = []
G: nx.MultiDiGraph = None
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',
13: '#d6dcff', 14: '#d2f5f0'}
def __init__(self): def __init__(self):
warnings.filterwarnings('ignore') warnings.filterwarnings('ignore')
# init HyperGraph
self.hg = HyperGraph() self.hg = HyperGraph()
self.hg.classify() self.hg.classify()
self.df_nodes = self.hg.cluster_labels self.df_nodes = self.hg.cluster_labels
self.destf_nodes = self.hg.dest_cluster_labels self.destf_nodes = self.hg.dest_cluster_labels
# init visual graph
self.G = nx.MultiDiGraph(day="Stackoverflow")
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.
...@@ -77,31 +88,12 @@ class SemanticLinking: ...@@ -77,31 +88,12 @@ class SemanticLinking:
elif len(high_labels) > 1: elif len(high_labels) > 1:
# Prec-Max # Prec-Max
if labeling[node] not in high_labels: if labeling[node] not in high_labels:
labeling[node] = max(high_labels)
labeling[node] = max(high_labels)
G = nx.MultiDiGraph(day="Stackoverflow")
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',
13: '#d6dcff', 14: '#d2f5f0'}
i=0
graphedge = []
weigth = []
sourcedestination = []
source = []
dest = []
edge_width = []
weight1 = []
node_adjacencies = []
labeling = {}
def drawedges(self): def drawedges(self):
"""drawing edges in graph""" """drawing edges in graph"""
labelvalues = self.hg.label_values labelvalues = self.hg.label_values
weight1 = []
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])):
...@@ -109,16 +101,18 @@ class SemanticLinking: ...@@ -109,16 +101,18 @@ class SemanticLinking:
for row in range(len(labelvalues)): for row in range(len(labelvalues)):
for row1 in range(len(labelvalues)): for row1 in range(len(labelvalues)):
self.weight1.append(self.G.number_of_edges(labelvalues[row], labelvalues[row1])) weight1.append(self.G.number_of_edges(labelvalues[row], labelvalues[row1]))
print("The number of coccurance from node ", labelvalues[row],"to node ", labelvalues[row1], ": ", self.weight1[row1]) print("The number of coccurance from node ", labelvalues[row],"to node ", labelvalues[row1], ": ", weight1[row1])
self.G.__setattr__('weight', self.weight1) #self.G.__setattr__('weight', weight1)
self.G.weight = weight1
return 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)}
print("lable value: ", labeling.values()) print("lable value: ", labeling.values())
while not self._labeling_complete(labeling, self.G): while not self._labeling_complete(labeling, self.G):
...@@ -129,19 +123,25 @@ class SemanticLinking: ...@@ -129,19 +123,25 @@ class SemanticLinking:
self._update_label(n, labeling, self.G) self._update_label(n, labeling, self.G)
for label in set(labeling.values()): for label in set(labeling.values()):
print("lable value: ", labeling.values()) print("lable value: ", labeling.values())
self.labeling = labeling
return labeling
def findigneighbors(self): def findigneighbors(self):
""" findig nodes' adjecencies""" """ findig nodes' adjecencies"""
node_text = [] node_text = []
node_adjacencies = []
for node, adjacencies in enumerate(self.G.adjacency()): for node, adjacencies in enumerate(self.G.adjacency()):
self.node_adjacencies.append(len(adjacencies[1])) node_adjacencies.append(len(adjacencies[1]))
node_text.append('# of connections: '+str(len(adjacencies[1]))) node_text.append('# of connections: '+str(len(adjacencies[1])))
self.G.color = self.node_adjacencies self.G.color = node_adjacencies
return node_adjacencies
def result(self, weight1, labeling, node_adjacencies):
weigth = []
edge_width = []
def result(self):
plt.figure(figsize=(25, 25)) plt.figure(figsize=(25, 25))
options = { options = {
'with_labels': True, 'with_labels': True,
...@@ -154,18 +154,19 @@ class SemanticLinking: ...@@ -154,18 +154,19 @@ class SemanticLinking:
d = nx.degree_centrality(self.G) d = nx.degree_centrality(self.G)
d_list = list(d.values()) d_list = list(d.values())
print("node centrality: ", d_list) print("node centrality: ", d_list)
print("node adjacencies: ", self.node_adjacencies) print("node adjacencies: ", node_adjacencies)
for row in range(len(self.weigth)): for row in range(len(weigth)):
self.edge_width.append([]) edge_width.append([])
for drow in range(len(self.weigth[row])): for drow in range(len(weigth[row])):
self.edge_width[row].append(self.weigth[row][drow]) edge_width[row].append(weigth[row][drow])
node_size = [v * 80 for v in d.values()] # setting node size based on node centrality node_size = [v * 80 for v in d.values()] # setting node size based on node centrality
edge_width = [row * 0.5 for row in self.weight1] edge_width = [row * 0.5 for row in weight1]
print("Nodes' Degree: ", nx.degree(self.G)) print("Nodes' Degree: ", nx.degree(self.G))
print("Nodes' Betweeness ", nx.edge_betweenness_centrality(self.G)) print("Nodes' Betweeness ", nx.edge_betweenness_centrality(self.G))
print("Nodes' Betweeness-centrality: ", nx.betweenness_centrality(self.G)) print("Nodes' Betweeness-centrality: ", nx.betweenness_centrality(self.G))
# TODO extract method
""" """
Using the spring layout : Using the spring layout :
- k controls the distance between the nodes and varies between 0 and 1 - k controls the distance between the nodes and varies between 0 and 1
...@@ -180,9 +181,8 @@ class SemanticLinking: ...@@ -180,9 +181,8 @@ class SemanticLinking:
pos_nodes = nx.spring_layout(self.G, k=0.25, iterations=50) pos_nodes = nx.spring_layout(self.G, k=0.25, iterations=50)
nx.draw(self.G, pos_nodes, node_color=node_adjacencies, node_size=node_size, width=2, arrowstyle='->',
nx.draw(self.G, pos_nodes, node_color=self.node_adjacencies, node_size=node_size, width=2, arrowstyle='->', arrowsize=10, weight=weight1, edge_color='gray', **options)
arrowsize=10, weight=self.weight1, edge_color='gray', **options)
edge_labels = nx.get_edge_attributes(self.G, 'weight') edge_labels = nx.get_edge_attributes(self.G, 'weight')
...@@ -190,18 +190,23 @@ class SemanticLinking: ...@@ -190,18 +190,23 @@ class SemanticLinking:
for node, coords in pos_nodes.items(): for node, coords in pos_nodes.items():
pos_attrs[node] = (coords[0], coords[1] + 0.02) pos_attrs[node] = (coords[0], coords[1] + 0.02)
nx.draw_networkx_edge_labels(self.G, pos_nodes, edge_labels=edge_labels, font_size=10, font_color='red') nx.draw_networkx_edge_labels(self.G, pos_nodes, edge_labels=edge_labels, font_size=10, font_color='red')
nx.draw_networkx_labels(self.G, pos_attrs, labels=self.labeling, font_size=10, font_color='red') nx.draw_networkx_labels(self.G, pos_attrs, labels=labeling, font_size=10, font_color='red')
ax = plt.gca() ax = plt.gca()
ax.collections[0].set_edgecolor("#555555") ax.collections[0].set_edgecolor("#555555")
plt.show() plt.show()
def main(self): def main(self):
self.drawedges() weight1 = self.drawedges()
self.dolabeling()
self.findigneighbors() labeling = self.dolabeling()
self.result()
node_adjacencies = self.findigneighbors()
self.result(weight1, labeling, node_adjacencies)
linking = SemanticLinking() if __name__ == '__main__':
linking.main() linking = SemanticLinking()
\ No newline at end of file linking.main()
\ No newline at end of file
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