Commit f18e7ea3 authored by Alexander Lercher's avatar Alexander Lercher

applied oo

parent ef2f767f
import json
class HyperGraph:
def do():
df_nodes = load_values()
cluster_labels = []
dest_cluster_labels = []
label_values = []
ret_val = init(df_nodes)
nodeIds = ret_val['nodeIds']
clusterlabels = ret_val['clusterlabels']
destIds = ret_val['destIds']
clusterlabels = classify_input(nodeIds, clusterlabels)
def __init__(self):
pass
labelvals = calc_cluster_num(clusterlabels)
def classify(self):
df_nodes = self.load_values()
cluster = cluster_with_labels(nodeIds, clusterlabels, labelvals)
ret_val = self.init(df_nodes)
nodeIds = ret_val['nodeIds']
clusterlabels = ret_val['clusterlabels']
destIds = ret_val['destIds']
clusterlabels = self.classify_input(nodeIds, clusterlabels)
cluster = remove_duplicates(cluster)
labelvals = self.calc_cluster_num(clusterlabels)
destclusterlabel = cluster_dest_ids(labelvals, cluster, destIds)
cluster = self.cluster_with_labels(nodeIds, clusterlabels, labelvals)
def load_values():
with open("mult_in_out_large.json", "r") as json_file:
df_nodes = json.load(json_file)
cluster = self.remove_duplicates(cluster)
return df_nodes
destclusterlabel = self.cluster_dest_ids(labelvals, cluster, destIds)
def init(df_nodes):
nodeIds = []
clusterlabels = []
destIds = []
self.cluster_labels = clusterlabels
self.dest_cluster_labels = destclusterlabel
self.labelvals = labelvals
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)
def load_values(self):
with open("mult_in_out_large.json", "r") as json_file:
df_nodes = json.load(json_file)
for row in df_nodes:
destIds.append(row['TransactionTo'])
return df_nodes
for row in range(len(nodeIds)):
print(nodeIds[row])
def init(self, df_nodes):
nodeIds = []
clusterlabels = []
destIds = []
print("Finish InputIDs")
i = 0
for row in range(len(nodeIds)):
clusterlabels.append(row)
i += 1
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)
print(i)
return {'nodeIds': nodeIds,
'clusterlabels': clusterlabels,
'destIds': destIds}
for row in df_nodes:
destIds.append(row['TransactionTo'])
def classify_input(nodeIds, clusterlabels):
"""" classifying Inputs"""
"""" Labaling inputs"""
for row in range(len(nodeIds)):
for row in range(len(nodeIds)):
print(nodeIds[row])
for rown in range(len(nodeIds[row])):
print("Finish InputIDs")
i = 0
for row in range(len(nodeIds)):
clusterlabels.append(row)
i += 1
for row1 in range(len(nodeIds)):
for rown1 in range(len(nodeIds[row1])):
if(nodeIds[row][rown]==nodeIds[row1][rown1]):
# print("row: ",row,"row1: ",row1)
if(row < row1):
for row2 in clusterlabels:
if( clusterlabels[row1]== clusterlabels[row2]):
clusterlabels[row2]=clusterlabels[row]
clusterlabels[row1] = clusterlabels[row]
print(i)
return {'nodeIds': nodeIds,
'clusterlabels': clusterlabels,
'destIds': destIds}
else:
for row2 in clusterlabels:
if (clusterlabels[row] == clusterlabels[row2]):
clusterlabels[row2] = clusterlabels[row1]
clusterlabels[row] = clusterlabels[row1]
def classify_input(self, nodeIds, clusterlabels):
"""" classifying Inputs"""
"""" Labaling inputs"""
for row in range(len(nodeIds)):
print(clusterlabels)
print("cluster labels:", len(clusterlabels))
print("NodeIDs: ", len(nodeIds))
return clusterlabels
for rown in range(len(nodeIds[row])):
def calc_cluster_num(clusterlabels):
"""" Calculating the number of clusters"""
labelvals = []
for row1 in range(len(nodeIds)):
for rown1 in range(len(nodeIds[row1])):
if(nodeIds[row][rown]==nodeIds[row1][rown1]):
# print("row: ",row,"row1: ",row1)
if(row < row1):
for row2 in clusterlabels:
if( clusterlabels[row1]== clusterlabels[row2]):
clusterlabels[row2]=clusterlabels[row]
clusterlabels[row1] = clusterlabels[row]
labelvals.append(clusterlabels[0])
for row in range(len(clusterlabels)):
flag = True
for row1 in range(len(labelvals)):
if(clusterlabels[row]== labelvals[row1]):
flag = False
if (flag):
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(labelvals)):
cluster.append([])
for row3 in range(len(nodeIds)):
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
while(flag):
for row in range(len(cluster)):
flag= False
for row1 in range(len(cluster[row])):
flag= False
for row2 in range (len(cluster[row])):
if(row1 != row2):
if(cluster[row][row1] == cluster[row][row2]):
del cluster[row][row2]
flag=True
break
if(flag):
break
if(flag):
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])):
else:
for row2 in clusterlabels:
if (clusterlabels[row] == clusterlabels[row2]):
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(self, clusterlabels):
"""" Calculating the number of clusters"""
labelvals = []
labelvals.append(clusterlabels[0])
for row in range(len(clusterlabels)):
flag = True
for rownum in range(len(labelvals)):
for row1 in range(len(cluster[rownum])):
for row1 in range(len(labelvals)):
if(clusterlabels[row]== labelvals[row1]):
flag = False
if (flag):
labelvals.append(clusterlabels[row])
if(destIds[row][row2]== cluster[rownum][row1]):
destclusterlabel[row].append(labelvals[rownum])
flag = False
if(flag):
destclusterlabel.append(destIds[row][row2])
print("label values (source Ids in the network): ", labelvals, " and the number of clusters is: ", len(labelvals))
print("destination labels (destination Ids): ", destclusterlabel)
return labelvals
return destclusterlabel
\ No newline at end of file
def cluster_with_labels(self, nodeIds, clusterlabels, labelvals):
"""" clustering Ids according to their labels"""
cluster = []
for row in range(len(labelvals)):
cluster.append([])
for row3 in range(len(nodeIds)):
if (labelvals[row] == clusterlabels[row3]):
cluster[row].extend(nodeIds[row3])
print("clusters: ", cluster)
return cluster
def remove_duplicates(self, cluster):
""" Removing duplicating items in cluster"""
flag = True
while(flag):
for row in range(len(cluster)):
flag= False
for row1 in range(len(cluster[row])):
flag= False
for row2 in range (len(cluster[row])):
if(row1 != row2):
if(cluster[row][row1] == cluster[row][row2]):
del cluster[row][row2]
flag=True
break
if(flag):
break
if(flag):
break
print("cluster:", cluster)
return cluster
def cluster_dest_ids(self, 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(labelvals)):
for row1 in range(len(cluster[rownum])):
if(destIds[row][row2]== cluster[rownum][row1]):
destclusterlabel[row].append(labelvals[rownum])
flag = False
if(flag):
destclusterlabel.append(destIds[row][row2])
print("destination labels (destination Ids): ", destclusterlabel)
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