-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
201 lines (174 loc) · 8.06 KB
/
main.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
import argparse
import os
import sys
# DeepWalk + vis
from tests.DeepWalk_TSNE_test import DeepWalk_TSNE_test
from tests.DeepWalk_TSGNE_test import DeepWalk_TSGNE_test
from tests.DeepWalk_PCA_test import DeepWalk_PCA_test
# Node2Vec + vis
from tests.Node2Vec_TSNE_test import Node2Vec_TSNE_test
from tests.Node2Vec_TSGNE_test import Node2Vec_TSGNE_test
from tests.Node2Vec_PCA_test import Node2Vec_PCA_test
# SDNE + vis
from tests.SDNE_TSNE_test import SDNE_TSNE_test
from tests.SDNE_TSGNE_test import SDNE_TSGNE_test
from tests.SDNE_PCA_test import SDNE_PCA_test
# ShortestPath + vis
from tests.ShortestPath_TSNE_test import ShortestPath_TSNE_test
from tests.ShortestPath_TSGNE_test import ShortestPath_TSGNE_test
from tests.ShortestPath_PCA_test import ShortestPath_PCA_test
# LEE + vis
from tests.LEE_TSNE_test import LEE_TSNE_test
from tests.LEE_TSGNE_test import LEE_TSGNE_test
from tests.LEE_PCA_test import LEE_PCA_test
# GLEE + vis
from tests.GLEE_TSNE_test import GLEE_TSNE_test
from tests.GLEE_TSGNE_test import GLEE_TSGNE_test
from tests.GLEE_PCA_test import GLEE_PCA_test
# SPLEE + vis
from tests.SPLEE_TSNE_test import SPLEE_TSNE_test
from tests.SPLEE_TSGNE_test import SPLEE_TSGNE_test
from tests.SPLEE_PCA_test import SPLEE_PCA_test
# RandomEmbed + vis (for testing purpose only)
from tests.RandomEmbed_TSGNE_test import RandomEmbed_TSGNE_test
if __name__ == "__main__":
### ========== ========== ========== ========== ========== ###
### EMBEDDING AND VISUALIZING METHODS ###
### ========== ========== ========== ========== ========== ###
### V V V V V V V V V V V V V V V V V V V V V V V V V V V ###
EMBED_METHODS = {
"deepwalk": "DeepWalk",
"node2vec": "Node2Vec",
"sdne": "SDNE",
"shortestpath": "ShortestPath",
"lee": "LEE",
"glee": "GLEE",
"splee": "SPLEE",
"randomembed": "RandomEmbed",
}
VIS_METHODS = {
"t-sne": "t-SNE",
"t-sgne": "t-SGNE",
"pca": "PCA",
}
### default settings
dataset_folder = os.path.join(os.path.dirname(__file__), "datasets")
image_folder = os.path.join(os.path.dirname(__file__), "images")
### set up argument parser
parser = argparse.ArgumentParser()
parser.add_argument("--data", help="name of the dataset to use", default="lastfm")
parser.add_argument(
"--embed",
help=f"name of the graph embedding method to use, choices are {list(EMBED_METHODS.keys())}",
default="splee")
parser.add_argument(
"--vis",
help=f"name of the visualization method to use, choices are {list(VIS_METHODS.keys())}",
default="t-sne")
# Below are some less used options. Feel free to tune them.
parser.add_argument("--dim", help="dimension of the high-dimensional embedding", type=int, default=128)
# According to sklearn implemnetation, k should be calculated using perplexity (k = min(n_samples - 1, int(3.0 * self.perplexity + 1)))
parser.add_argument("--k", help="k neighbors to use for the knn graph construction", type=int, default=10)
parser.add_argument("--seed", help="random seed", type=int, default=20220804) #TODO: fix a random seed for reproducibility
parser.add_argument("--image_format", help="image format", default="png")
parser.add_argument("--description", help="extra description of current test", default="")
parser.add_argument("--knn_mode", help="The mode of knn matrix constructed from graph ('connectivity' or 'distance')", default="distance")
# whether or not to apply NMI / RI metrics on the final result
parser.add_argument("--eval", help="whether or not to evaluate the result (calculate density / NMI / RI)", type=int, default=1)
### parse arguments
args = parser.parse_args()
config = dict()
for arg in vars(args):
config[arg] = getattr(args, arg)
config["dataset_folder"] = os.path.join(dataset_folder, config["data"])
config["image_folder"] = os.path.join(image_folder, config["data"])
config["edgeset"] = os.path.join(config["dataset_folder"], f"{config['data']}_edgelist.txt")
config["featureset"] = os.path.join(config["dataset_folder"], f"{config['data']}_labels.txt")
if not os.path.exists(config["image_folder"]):
os.makedirs(config["image_folder"])
print(f"Created folder: {config['image_folder']}")
### A A A A A A A A A A A A A A A A A A A A A A A A A A A ###
### ========== ========== ========== ========== ========== ###
### END OF EMBEDDING AND VISUALIZING METHODS ###
### ========== ========== ========== ========== ========== ###
if config["embed"].lower() not in EMBED_METHODS:
print(f"{config['embed']} is not a valid embedding method. Valid methods are:", end=" ")
for name in EMBED_METHODS.values():
print(name, end=" ")
print()
sys.exit(1)
else:
config["embed"] = EMBED_METHODS[config["embed"].lower()]
if config["vis"].lower() not in VIS_METHODS:
print(f"{config['vis']} is not a valid visualization method. Valid methods are:", end=" ")
for name in VIS_METHODS.values():
print(name, end=" ")
print()
sys.exit(1)
else:
config["vis"] = VIS_METHODS[config["vis"].lower()]
if config["image_format"] not in ["png", "jpg", "jpeg", "webp", "svg", "pdf", "eps", "json"]:
print(f"{config['image_format']} is not a valid image format. Valid formats are: png, pdf")
sys.exit(1)
### ========== ========== ========== ========== ========== ###
### CALLING EMBEDDING AND VISUALIZING METHODS ###
### ========== ========== ========== ========== ========== ###
### V V V V V V V V V V V V V V V V V V V V V V V V V V V ###
if config["embed"] == "DeepWalk":
if config["vis"] == "t-SNE":
DeepWalk_TSNE_test(config)
elif config["vis"] == "t-SGNE":
DeepWalk_TSGNE_test(config)
elif config["vis"] == "PCA":
DeepWalk_PCA_test(config)
elif config["embed"] == "Node2Vec":
if config["vis"] == "t-SNE":
Node2Vec_TSNE_test(config)
elif config["vis"] == "t-SGNE":
Node2Vec_TSGNE_test(config)
elif config["vis"] == "PCA":
Node2Vec_PCA_test(config)
elif config["embed"] == "SDNE":
if config["vis"] == "t-SNE":
SDNE_TSNE_test(config)
elif config["vis"] == "t-SGNE":
SDNE_TSGNE_test(config)
elif config["vis"] == "PCA":
SDNE_PCA_test(config)
elif config["embed"] == "ShortestPath":
if config["vis"] == "t-SNE":
ShortestPath_TSNE_test(config)
elif config["vis"] == "t-SGNE":
ShortestPath_TSGNE_test(config)
elif config["vis"] == "PCA":
ShortestPath_PCA_test(config)
elif config["embed"] == "LEE":
if config["vis"] == "t-SNE":
LEE_TSNE_test(config)
elif config["vis"] == "t-SGNE":
LEE_TSGNE_test(config)
elif config["vis"] == "PCA":
LEE_PCA_test(config)
elif config["embed"] == "GLEE":
if config["vis"] == "t-SNE":
GLEE_TSNE_test(config)
elif config["vis"] == "t-SGNE":
GLEE_TSGNE_test(config)
elif config["vis"] == "PCA":
GLEE_PCA_test(config)
elif config["embed"] == "SPLEE":
if config["vis"] == "t-SNE":
SPLEE_TSNE_test(config)
elif config["vis"] == "t-SGNE":
SPLEE_TSGNE_test(config)
elif config["vis"] == "PCA":
SPLEE_PCA_test(config)
elif config["embed"] == "RandomEmbed":
if config["vis"] == "t-SGNE":
RandomEmbed_TSGNE_test(config)
else:
NotImplementedError("Random embedding + other visualization is not implemented yet")
### A A A A A A A A A A A A A A A A A A A A A A A A A A A ###
### ========== ========== ========== ========== ========== ###
### END OF CALLING EMBEDDING AND VISUALIZING METHODS ###
### ========== ========== ========== ========== ========== ###