-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptimization_nf.py
211 lines (187 loc) · 11 KB
/
optimization_nf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import json
import random
from algo_improved import *
import timeit
import sys
import argparse
import os
import re
NUM_INTERSECTION_UPPER_LIMIT = 10000
parser = argparse.ArgumentParser(description='graph visualization optimization algorithm')
parser.add_argument('-i', '--inFile', metavar="INPUT FILE NAME", action='store', type=str, required=True, help='input file name that we need to optimize coordinates')
parser.add_argument('-o', '--outDir', metavar="OUTPUT DIRECTORY", action='store', type=str, default=os.getcwd(), help="output directory for json files")
parser.add_argument('-r', '--rootDir', metavar="ROOT DIRECTORY", action='store', type=str, help="root directory for the project")
parser.add_argument('-a', '--artiDir', metavar="ARTIFICIAL DIRECTORY", action='store', type=str, help="the artificial directory")
args = parser.parse_args()
argsDict = vars(args)
inputFileName = argsDict["inFile"]
outputDirectory = argsDict['outDir'][:-1] if argsDict['outDir'].endswith("/") else argsDict['outDir']
rootDirectory = argsDict['rootDir'][:-1] if argsDict['rootDir'].endswith("/") else argsDict['rootDir']
artifactDirectory = argsDict['artiDir'][:-1] if argsDict['artiDir'].endswith("/") else argsDict['artiDir']
nodeMapToNum = {}
def main():
def core(THETA, LAMBDA):
with open(f"{rootDirectory}/output/{artifactDirectory}/{inputFileName}", 'r') as f:
rawData = json.load(f)
parsedNodeData = rawData["nodes"]
parsedLinksData = rawData["links"]
"""
1) Make each nodeName points to index of links that the source is that node using nodeIDMapToParsedLinkDataIndexSource
2) Make each nodeName points to index of link that the target is that node using nodeIDMapToParsedLinkDataIndexTarget
3) Make each nodeName points to index of node itself using nodeIDMapToParsedNodeDataIndex
"""
nodeIDMapToParsedLinkDataIndexSource = {}
nodeIDMapToParsedLinkDataIndexTarget = {}
nodeIDMapToParsedNodeDataIndex = {}
for index, node in enumerate(parsedNodeData):
nodeID = str(node["id"])
parsedNodeData[index].update({"id" : nodeID})
nodeIDMapToParsedNodeDataIndex[nodeID] = index
nodeIDMapToParsedLinkDataIndexSource[nodeID] = []
for index1, link in enumerate(parsedLinksData):
if nodeID == str(link["source"]["id"]):
nodeIDMapToParsedLinkDataIndexSource[nodeID].append(index1)
if nodeID == str(link['target']['id']):
nodeIDMapToParsedLinkDataIndexTarget[nodeID] = index1
"""end3Makes"""
"""Make each link in parsedLinksData point to specific object in parsedNodeData"""
for index, data in enumerate(parsedLinksData):
for indexNode, dataNode in enumerate(parsedNodeData):
if str(data["source"]["id"]) == str(dataNode["id"]):
parsedLinksData[index]["source"] = parsedNodeData[indexNode]
if str(data["target"]["id"]) == str(dataNode["id"]):
parsedLinksData[index]["target"] = parsedNodeData[indexNode]
"""endMake"""
"""
1) Make each linkID points to index of node that the source of that link is pointing to using linkIDMapToParsedNodeDataIndexSource
2) Make each linkID points to index of node that the target of that link is pointing to using linkIDMapToParsedNodeDataIndexTarget
3) Make each linkID points to index of itself using linkIDMapToParsedLinkDataIndex
"""
linkIDMapToParsedNodeDataIndexSource = {}
linkIDMapToParsedNodeDataIndexTarget = {}
linkIDMapToParsedLinkDataIndex = {}
for index, link in enumerate(parsedLinksData):
linkID = str(link["id"])
parsedLinksData[index].update({"id" : linkID})
linkIDMapToParsedLinkDataIndex[linkID] = index
for index1, node in enumerate(parsedNodeData):
if (link["source"]["id"] == node["id"]):
linkIDMapToParsedNodeDataIndexSource[linkID] = index1
if (link["target"]["id"] == node["id"]):
linkIDMapToParsedNodeDataIndexTarget[linkID] = index1
"""end3Makes"""
intersections = findIntersection(parsedLinksData)
temp = 0
D = {}
depth = {}
int_count = {}
"""create a dictionary that contains depth information for nodes"""
for node in parsedNodeData:
temp = findDepth(node, parsedLinksData, nodeIDMapToParsedLinkDataIndexTarget)[0]
depth[str(node["id"])] = temp
"""endCreate"""
start = timeit.default_timer()
"""assign each intersection object a value which is the mean of the depth of targets of the link."""
for intersection in intersections:
link1, link2 = intersection["id"].split("$")
## using hashTable instead of forloop search
link1 = parsedLinksData[linkIDMapToParsedLinkDataIndex[link1]]
link2 = parsedLinksData[linkIDMapToParsedLinkDataIndex[link2]]
node1 = link1["target"]
node2 = link2["target"]
if str(link1["id"]) in int_count:
int_count[str(link1["id"])] += 1
else:
int_count[str(link1["id"])] = 1
if str(link2["id"]) in int_count:
int_count[str(link2["id"])] += 1
else:
int_count[str(link2["id"])] = 1
# node1Importance = len(search(hashTable, str(node1["id"])))
# node2Importance = len(search(hashTable, str(node2["id"])))
node1Importance = depth[str(node1["id"])]
node2Importance = depth[str(node2["id"])]
D[intersection['id']] = sum([node1Importance, node2Importance])/2
D = dict(sorted(D.items(), key=lambda x: x[1], reverse=True))
"""end assignment"""
filteredIntersectionList = [(key, value) for key, value in D.items() if value >= 0]
number = len(filteredIntersectionList)
"""set upper limit to filteredIntersectionList if it exceeds originally"""
if number > NUM_INTERSECTION_UPPER_LIMIT:
number = NUM_INTERSECTION_UPPER_LIMIT
filteredIntersectionList = filteredIntersectionList[:NUM_INTERSECTION_UPPER_LIMIT]
"""end set"""
## need to investigate the priority so we can optimize it better
for intersection, _ in filteredIntersectionList:
print(json.dumps(
{"message": "------------------------------------------------------"}))
percentage = round(temp*100/number, 2)
print(json.dumps({"message": f"{percentage}%"}))
if (percentage != 0 and percentage < 100):
print(json.dumps(
{"message": f"expected finished time : {round((timeit.default_timer()- start)*(100 - percentage)/ percentage)}"}))
link1, link2 = intersection.split("$")
link1 = parsedLinksData[linkIDMapToParsedLinkDataIndex[link1]]
link2 = parsedLinksData[linkIDMapToParsedLinkDataIndex[link2]]
node1 = link1["target"]
node2 = link2["target"]
node1Depth = depth[str(node1["id"])]
node2Depth = depth[str(node2["id"])]
if node1Depth == node2Depth:
if int_count[str(link1["id"])] > int_count[str(link2["id"])]:
mainAlgo(node1, link1, parsedNodeData, parsedLinksData, THETA, LAMBDA,
nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, nodeIDMapToParsedNodeDataIndex,
linkIDMapToParsedNodeDataIndexSource, linkIDMapToParsedNodeDataIndexTarget, linkIDMapToParsedLinkDataIndex,
depth
)
elif int_count[str(link1["id"])] < int_count[str(link2["id"])]:
mainAlgo(node2, link2, parsedNodeData, parsedLinksData, THETA, LAMBDA,
nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, nodeIDMapToParsedNodeDataIndex,
linkIDMapToParsedNodeDataIndexSource, linkIDMapToParsedNodeDataIndexTarget, linkIDMapToParsedLinkDataIndex,
depth
)
else:
if link1["len"] > link2["len"]:
mainAlgo(node1, link1, parsedNodeData, parsedLinksData, THETA, LAMBDA,
nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, nodeIDMapToParsedNodeDataIndex,
linkIDMapToParsedNodeDataIndexSource, linkIDMapToParsedNodeDataIndexTarget, linkIDMapToParsedLinkDataIndex,
depth
)
elif link1['len'] == link2['len']:
choices = [(node1, link1), (node2, link2)]
randomSelection = random.choice([0, 1])
node, link = choices[randomSelection]
mainAlgo(node, link, parsedNodeData, parsedLinksData, THETA, LAMBDA,
nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, nodeIDMapToParsedNodeDataIndex,
linkIDMapToParsedNodeDataIndexSource, linkIDMapToParsedNodeDataIndexTarget, linkIDMapToParsedLinkDataIndex,
depth
)
else:
mainAlgo(node2, link2, parsedNodeData, parsedLinksData, THETA, LAMBDA,
nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, nodeIDMapToParsedNodeDataIndex,
linkIDMapToParsedNodeDataIndexSource, linkIDMapToParsedNodeDataIndexTarget, linkIDMapToParsedLinkDataIndex,
depth
)
elif node1Depth < node2Depth:
mainAlgo(node2, link2, parsedNodeData, parsedLinksData, THETA, LAMBDA,
nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, nodeIDMapToParsedNodeDataIndex,
linkIDMapToParsedNodeDataIndexSource, linkIDMapToParsedNodeDataIndexTarget, linkIDMapToParsedLinkDataIndex,
depth
)
else:
mainAlgo(node1, link1, parsedNodeData, parsedLinksData, THETA, LAMBDA,
nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, nodeIDMapToParsedNodeDataIndex,
linkIDMapToParsedNodeDataIndexSource, linkIDMapToParsedNodeDataIndexTarget, linkIDMapToParsedLinkDataIndex,
depth
)
temp += 1
stop = timeit.default_timer()
print(json.dumps({"message": f"Time: {stop-start}"}))
# print(json.dumps({"message": f"Time: {stop-start}"}))
# print(json.dumps({"nodes": parsedNodeData, "links": parsedLinksData}))
with open(f"{outputDirectory}/{re.match(r'(.*?).json', inputFileName).group(1)}_final.json", "w") as f:
json.dump({"nodes": parsedNodeData, "links": parsedLinksData}, f)
print("script is finished.")
core(1, 18)
if __name__ == '__main__':
main()