Commit ef2f767f authored by Alexander Lercher's avatar Alexander Lercher

Extracted methods

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