Commit ef2f767f authored by Alexander Lercher's avatar Alexander Lercher

Extracted methods

parent 1efb006e
import json import json
nodeIds = []
destIds = [] def do():
clusterlabels = []
destclusterlabel = []
cluster = []
labalvlues = []
def classify():
df_nodes = load_values() df_nodes = load_values()
for row in df_nodes: ret_val = init(df_nodes)
nodeIds = ret_val['nodeIds']
clusterlabels = ret_val['clusterlabels']
destIds = ret_val['destIds']
clusterlabels = classify_input(nodeIds, clusterlabels)
labelvals = calc_cluster_num(clusterlabels)
cluster = cluster_with_labels(nodeIds, clusterlabels, labelvals)
cluster = remove_duplicates(cluster)
destclusterlabel = cluster_dest_ids(labelvals, cluster, destIds)
def load_values():
with open("mult_in_out_large.json", "r") as json_file:
df_nodes = json.load(json_file)
return df_nodes
def init(df_nodes):
nodeIds = []
clusterlabels = []
destIds = []
for row in df_nodes:
for j in range(len(row['TransactionFrom'])): for j in range(len(row['TransactionFrom'])):
print(" Input Ids: ", row['TransactionFrom'][j]) print(" Input Ids: ", row['TransactionFrom'][j])
nodeIds.append(row['TransactionFrom']) nodeIds.append(row['TransactionFrom'])
print("This is nodes: ", nodeIds) print("This is nodes: ", nodeIds)
for row in df_nodes: for row in df_nodes:
destIds.append(row['TransactionTo']) destIds.append(row['TransactionTo'])
for row in range(len(nodeIds)): for row in range(len(nodeIds)):
print(nodeIds[row]) print(nodeIds[row])
print("Finish InputIDs") print("Finish InputIDs")
i = 0 i = 0
for row in range(len(nodeIds)): for row in range(len(nodeIds)):
clusterlabels.append(row) clusterlabels.append(row)
i += 1 i += 1
print(i) print(i)
return {'nodeIds': nodeIds,
'clusterlabels': clusterlabels,
'destIds': destIds}
def classify_input(nodeIds, clusterlabels):
"""" classifying Inputs""" """" classifying Inputs"""
"""" Labaling inputs""" """" Labaling inputs"""
for row in range(len(nodeIds)): for row in range(len(nodeIds)):
...@@ -55,42 +76,43 @@ def classify(): ...@@ -55,42 +76,43 @@ def classify():
clusterlabels[row2] = clusterlabels[row1] clusterlabels[row2] = clusterlabels[row1]
clusterlabels[row] = clusterlabels[row1] clusterlabels[row] = clusterlabels[row1]
print(clusterlabels) print(clusterlabels)
print("cluster labels:", len(clusterlabels)) print("cluster labels:", len(clusterlabels))
print("NodeIDs: ", len(nodeIds)) print("NodeIDs: ", len(nodeIds))
return clusterlabels
def calc_cluster_num(clusterlabels):
"""" Calculating the number of clusters""" """" Calculating the number of clusters"""
clusternum = 1 labelvals = []
labalvlues.append(clusterlabels[0])
labelvals.append(clusterlabels[0])
for row in range(len(clusterlabels)): for row in range(len(clusterlabels)):
flag = True flag = True
for row1 in range(len(labalvlues)): for row1 in range(len(labelvals)):
if(clusterlabels[row]== labalvlues[row1]): if(clusterlabels[row]== labelvals[row1]):
flag = False flag = False
if (flag): if (flag):
clusternum = + 1 labelvals.append(clusterlabels[row])
labalvlues.append(clusterlabels[row])
print("label values (source Ids in the network): ", labalvlues, " and the number of clusters is: ", len(labalvlues))
print("label values (source Ids in the network): ", labelvals, " and the number of clusters is: ", len(labelvals))
return labelvals
def cluster_with_labels(nodeIds, clusterlabels, labelvals):
"""" clustering Ids according to their labels""" """" clustering Ids according to their labels"""
cluster = []
for row in range(len(labalvlues)): for row in range(len(labelvals)):
cluster.append([]) cluster.append([])
for row3 in range(len(nodeIds)): for row3 in range(len(nodeIds)):
if (labalvlues[row] == clusterlabels[row3]): if (labelvals[row] == clusterlabels[row3]):
cluster[row].extend(nodeIds[row3]) cluster[row].extend(nodeIds[row3])
print("clusters: ", cluster) print("clusters: ", cluster)
return cluster
def remove_duplicates(cluster):
""" Removing duplicating items in cluster""" """ Removing duplicating items in cluster"""
flag = True flag = True
...@@ -112,28 +134,26 @@ def classify(): ...@@ -112,28 +134,26 @@ def classify():
break break
print("cluster:", cluster) print("cluster:", cluster)
return cluster
def cluster_dest_ids(labelvals, cluster, destIds):
"""" Clustering Destination Ids """ """" Clustering Destination Ids """
destclusterlabel = []
for row in range(len(destIds)): for row in range(len(destIds)):
destclusterlabel.append([]) destclusterlabel.append([])
for row2 in range(len(destIds[row])): for row2 in range(len(destIds[row])):
flag = True flag = True
for rownum in range(len(labalvlues)): for rownum in range(len(labelvals)):
for row1 in range(len(cluster[rownum])): for row1 in range(len(cluster[rownum])):
if(destIds[row][row2]== cluster[rownum][row1]): if(destIds[row][row2]== cluster[rownum][row1]):
destclusterlabel[row].append(labalvlues[rownum]) destclusterlabel[row].append(labelvals[rownum])
flag = False flag = False
if(flag): if(flag):
destclusterlabel.append(destIds[row][row2]) destclusterlabel.append(destIds[row][row2])
print("destination labels (destination Ids): ", destclusterlabel) print("destination labels (destination Ids): ", destclusterlabel)
def load_values(): return destclusterlabel
with open("mult_in_out_large.json", "r") as json_file: \ No newline at end of file
df_nodes = json.load(json_file)
return df_nodes
\ 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