-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
267 lines (243 loc) · 12.5 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
import sys
import tensorflow as tf
from cluster_experiments import explore_clusters
from compute_figures_papers import plot_all_figures
from compute_persistence_diagrams_pgdl import compute_persistence_diagrams
from extract_statistics_from_persistence_diagrams import compute_statistics_from_persistence_diagrams
from model.sota_methods.get_sota_methods_measures import compute_sota_measures
possible_tasks = ["help", "compute_persistence_diagrams", "compute_sota", "compute_statistics", "compute_clusters",
"plot_figures"]
possible_pgdl_tasks = [1, 2, 4, 5, 6, 7, 8, 9]
# Set memory growth to true to avoid locating all the GPU memory at once.
# See https://www.tensorflow.org/guide/gpu#limiting_gpu_memory_growth
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
def execute_task():
if len(sys.argv) < 2:
print("Usage: python main.py <task> <task_args>. To see possible tasks and their arguments,"
" run python main.py help")
exit(1)
task = sys.argv[1]
if task not in possible_tasks:
print("Usage: python main.py <task> <task_args>. To see possible tasks and their arguments,"
" run python main.py help")
exit(1)
if task == "help":
print("Possible tasks:")
print("=====================================")
print("Compute persistence diagrams for a specific PGDL task. Usage:")
print("python main.py compute_persistence_diagrams <pgdl_folderpath> <number_of_the_task>"
" <neurons_sampled> <inputs_sampled> <number_of_persistence_diagrams_per_model> <max_ph_dim> "
"<output_folderpath>")
print("<pgdl_folderpath> is the path to the folder where the PGDL data is stored.")
print(f"<number_of_the_task> is an integer from the list {possible_pgdl_tasks} or 'all'. If 'all' is used, "
f"the persistence diagrams for all tasks will be computed.")
print("<neurons_sampled> is an integer determining the number of neurons sampled from the model.")
print("<inputs_sampled> is an integer determining the number of inputs sampled from the training dataset.")
print("<number_of_persistence_diagrams_per_model> is an integer determining the number of persistence diagrams"
" computed for each model.")
print("<max_ph_dim> is an integer determining the maximum homology dimension of the persistence diagrams.")
print("<output_folderpath> is a path to the folder where the persistence diagrams will be saved.")
print("=====================================")
print("Compute SOTA measures for a specific PGDL task. Usage:")
print("python main.py compute_sota <pgdl_folderpath> <number_of_the_task> <output_folderpath>")
print("<pgdl_folderpath> is the path to the folder where the PGDL data is stored.")
print(f"<number_of_the_task> is an integer from the list {possible_pgdl_tasks} or 'all'. If 'all' is used, "
f"the SOTA measures for all tasks will be computed.")
print("<output_folderpath> is a path to the folder where the SOTA measures will be saved.")
print("=====================================")
print("Compute statistics for a specific PGDL task. Usage:")
print("python main.py compute_statistics <pgdl_folderpath> <results_folderpath> <number_of_the_task> "
"<output_folderpath>")
print("<pgdl_folderpath> is the path to the folder where the PGDL data is stored.")
print("<results_folderpath> is a path to the folder where the results of the computations will be saved.")
print(f"<number_of_the_task> is an integer from the list {possible_pgdl_tasks} or 'all'. If 'all' is used, "
f"the statistics for all tasks will be computed.")
print("<output_folderpath> is a path to the folder where the statistics will be saved.")
print("=====================================")
print("Generate clusters using K-means and show an interactive plot with data associated to these clusters. "
"Also, computes mutual information on the clusters defined by the features selected in the config file. "
"Usage:")
print("python main.py compute_clusters <pgdl_folderpath> <number_of_the_task> "
"<output_folderpath>")
print("<pgdl_folderpath> is the path to the folder where the PGDL data is stored.")
print(f"<number_of_the_task> is an integer from the list {possible_pgdl_tasks}.")
print("<output_folderpath> is a path to the folder where the SOTA measures will be saved.")
print("=====================================")
print("Plot figures for the paper. Usage:")
print("python main.py plot_figures <pgdl_folderpath> <results_folderpath>")
print("<pgdl_folderpath> is the path to the folder where the PGDL data is stored.")
print("<results_folderpath> is the path to the folder where the results of the computations are saved.")
elif task == "compute_persistence_diagrams":
compute_persistence_diagrams_for_specific_pgdl_task()
elif task == "compute_statistics":
compute_statistics_for_specific_pgdl_task()
elif task == "compute_sota":
compute_sota_measures_for_specific_pgdl_task()
elif task == "compute_clusters":
compute_clusters_for_specific_pgdl_task()
elif task == "plot_figures":
plot_figures()
def plot_figures():
if len(sys.argv) != 4:
print("Wrong number of arguments. Usage: python main.py plot_figures <pgdl_folderpath> <results_folderpath>")
exit(1)
possible_errors = []
pgdl_folderpath = sys.argv[2]
# Check if the folder exists
if not os.path.isdir(pgdl_folderpath):
possible_errors.append(f"Folder {pgdl_folderpath} does not exist.")
results_folderpath = sys.argv[3]
# Check if the folder exists
if not os.path.isdir(results_folderpath):
possible_errors.append(f"Folder {results_folderpath} does not exist.")
if len(possible_errors) > 0:
print("Errors:")
for error in possible_errors:
print(error)
exit(1)
plot_all_figures(pgdl_folderpath, results_folderpath)
def compute_sota_measures_for_specific_pgdl_task():
if len(sys.argv) != 5:
print("Wrong number of arguments. Usage: python main.py compute_sota <pgdl_folderpath>"
" <number_of_the_task> <output_folderpath>")
exit(1)
possible_errors = []
pgdl_folderpath = sys.argv[2]
# Check if the folder exists
if not os.path.isdir(pgdl_folderpath):
possible_errors.append(f"Folder {pgdl_folderpath} does not exist.")
try:
task = int(sys.argv[3]) if sys.argv[3] != "all" else "all"
if task not in possible_pgdl_tasks and task != "all":
raise ValueError
except ValueError:
possible_errors.append(f"Parameter <number_of_the_task>: task number must be one of {possible_pgdl_tasks} "
f"or 'all'.")
output_folderpath = sys.argv[4]
if len(possible_errors) > 0:
print("Errors:")
for error in possible_errors:
print(error)
exit(1)
compute_sota_measures(pgdl_folderpath, task, output_folderpath)
def compute_statistics_for_specific_pgdl_task():
if len(sys.argv) != 6:
print("Wrong number of arguments. Usage: python main.py compute_statistics <pgdl_folderpath>"
" <results_folderpath> <number_of_the_task> <vectorization_name> <output_folderpath>")
exit(1)
possible_errors = []
pgdl_folderpath = sys.argv[2]
results_folderpath = sys.argv[3]
output_folderpath = sys.argv[5]
# Check if the folders exists
if not os.path.isdir(pgdl_folderpath):
possible_errors.append(f"Folder {pgdl_folderpath} does not exist.")
if not os.path.isdir(results_folderpath):
possible_errors.append(f"Folder {results_folderpath} does not exist.")
try:
task = int(sys.argv[4]) if sys.argv[4] != "all" else "all"
if task not in possible_pgdl_tasks and task != "all":
raise ValueError
except ValueError:
possible_errors.append(f"Parameter <number_of_the_task>: task number must be one of {possible_pgdl_tasks} "
f"or 'all'.")
if len(possible_errors) > 0:
print("Errors:")
for error in possible_errors:
print(error)
exit(1)
# Compute the statistics
compute_statistics_from_persistence_diagrams(pgdl_folderpath, results_folderpath, task, output_folderpath)
def compute_clusters_for_specific_pgdl_task():
if len(sys.argv) != 6:
print("Wrong number of arguments. Usage: python main.py compute_clusters <pgdl_folderpath>"
" <results_folderpath> <number_of_the_task> <output_folderpath>")
exit(1)
possible_errors = []
pgdl_folderpath = sys.argv[2]
results_folderpath = sys.argv[3]
output_folderpath = sys.argv[5]
# Check if the folders exists
if not os.path.isdir(pgdl_folderpath):
possible_errors.append(f"Folder {pgdl_folderpath} does not exist.")
if not os.path.isdir(results_folderpath):
possible_errors.append(f"Folder {results_folderpath} does not exist.")
try:
task = int(sys.argv[4])
if task not in possible_pgdl_tasks:
raise ValueError
except ValueError:
possible_errors.append(f"Parameter <number_of_the_task>: task number must be one of {possible_pgdl_tasks}.")
if len(possible_errors) > 0:
print("Errors:")
for error in possible_errors:
print(error)
exit(1)
# Compute the statistics
explore_clusters(pgdl_folderpath, results_folderpath, task, output_folderpath)
def compute_persistence_diagrams_for_specific_pgdl_task():
if len(sys.argv) != 9:
print("Wrong number of arguments. Usage: python main.py compute_persistence_diagrams <pgdl_folderpath>"
" <number_of_the_task> <neurons_sampled> <inputs_sampled> <number_of_persistence_diagrams_per_model> "
"<max_ph_dim> <output_folderpath>")
exit(1)
possible_errors = []
pgdl_folderpath = sys.argv[2]
# Check if the folder exists
if not os.path.isdir(pgdl_folderpath):
possible_errors.append(f"Folder {pgdl_folderpath} does not exist.")
try:
task = int(sys.argv[3]) if sys.argv[3] != "all" else "all"
if task not in possible_pgdl_tasks and task != "all":
raise ValueError
except ValueError:
possible_errors.append(f"Parameter <number_of_the_task>: task number must be one of {possible_pgdl_tasks} "
f"or 'all'.")
try:
neurons_sampled = int(sys.argv[4])
if neurons_sampled <= 0:
raise ValueError
except ValueError:
possible_errors.append("Parameter <neurons_sampled>: Neurons sampled must be a positive integer.")
try:
inputs_sampled = int(sys.argv[5])
if inputs_sampled <= 0:
raise ValueError
except ValueError:
possible_errors.append("Parameter <inputs_sampled>: Inputs sampled must be a positive integer.")
try:
number_of_persistence_diagrams_per_model = int(sys.argv[6])
if number_of_persistence_diagrams_per_model <= 0:
raise ValueError
except ValueError:
possible_errors.append("Parameter <number_of_persistence_diagrams_per_model>: Number of persistence diagrams "
"per model must be an integer.")
try:
max_ph_dim = int(sys.argv[7])
if max_ph_dim <= 0:
raise ValueError
except ValueError:
possible_errors.append("Parameter <max_ph_dim>: Max PH dimension must be a positive integer.")
output_folderpath = sys.argv[8]
if len(possible_errors) > 0:
print("Errors:")
for error in possible_errors:
print(error)
exit(1)
compute_persistence_diagrams(pgdl_folderpath, task, neurons_sampled, inputs_sampled,
number_of_persistence_diagrams_per_model, max_ph_dim, output_folderpath)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
execute_task()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/