Commit f18e7ea3 authored by Alexander Lercher's avatar Alexander Lercher

applied oo

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