Skip to content

Commit 0be9e28

Browse files
committed
Clean and docs for benchmark methods
1 parent 835b5a7 commit 0be9e28

1 file changed

Lines changed: 94 additions & 22 deletions

File tree

run_full_benchmark.py

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222

2323
def parse_npz_file(file_path, has_dn=False):
24+
"""
25+
Parses a .npz file containing adjacency matrices and returns a list of NetworkX graphs.
26+
27+
Args:
28+
file_path (str): The path to the .npz file.
29+
has_dn (bool): Whether the decycling numbers are included in the file or not.
30+
31+
Returns:
32+
list: A list of NetworkX graphs.
33+
"""
2434
graphs = []
2535
try:
2636
with np.load(file_path, allow_pickle=True) as data:
@@ -41,31 +51,42 @@ def parse_npz_file(file_path, has_dn=False):
4151
return graphs
4252

4353

44-
def run_method_get_time_and_result(method_func, graph_data, result_queue):
54+
def run_method_get_result_and_time(method_func, graph_data, result_queue):
55+
"""
56+
Runs a method function on the given graph data, and adds the computed value and execution time to the result queue.
57+
58+
Args:
59+
method_func (function): The method function to run.
60+
graph_data (networkx.Graph): The graph to process.
61+
result_queue (multiprocessing.Queue): The queue to store the result and execution time.
62+
63+
Returns:
64+
None
65+
"""
4566
try:
4667
start_time = time.perf_counter()
4768
result_value = method_func(graph_data)
4869
end_time = time.perf_counter()
49-
result_queue.put((end_time - start_time, result_value))
70+
result_queue.put((result_value, end_time - start_time))
5071
except Exception as e:
5172
result_queue.put(e)
5273

5374

5475
def run_method_get_result(method_func, graph_data, result_queue):
55-
try:
56-
result = method_func(graph_data)
57-
result_queue.put(result)
58-
except Exception as e:
59-
result_queue.put(e)
76+
"""
77+
Runs a method function on the given graph data, and adds the computed value to the result queue.
6078
79+
Args:
80+
method_func (function): The method function to run.
81+
graph_data (networkx.Graph): The graph to process.
82+
result_queue (multiprocessing.Queue): The queue to store the result and execution time.
6183
62-
def run_approx_method_get_result_and_time(method_func, graph_data, result_queue):
84+
Returns:
85+
None
86+
"""
6387
try:
64-
start_time = time.perf_counter()
65-
k = method_func(graph_data)
66-
end_time = time.perf_counter()
67-
duration = end_time - start_time
68-
result_queue.put((k, duration))
88+
result = method_func(graph_data)
89+
result_queue.put(result)
6990
except Exception as e:
7091
result_queue.put(e)
7192

@@ -76,7 +97,19 @@ def benchmark_exact_exec_time(
7697
timeout_seconds,
7798
output_filename,
7899
):
79-
100+
"""
101+
Benchmarks the exact execution time of given methods on graphs stored in .npz files within a directory.
102+
The results are written to an output file.
103+
104+
Args:
105+
directory_path (str): The path to the directory containing .npz files.
106+
methods_list (list): A list of method functions to benchmark.
107+
timeout_seconds (int): The timeout in seconds for each graph processing.
108+
output_filename (str): The name of the output file to store benchmark results.
109+
110+
Returns:
111+
None
112+
"""
80113
benchmark_start_time = time.perf_counter()
81114

82115
if not os.path.isdir(directory_path):
@@ -116,7 +149,7 @@ def benchmark_exact_exec_time(
116149
graphs_in_file = parse_npz_file(filepath)
117150
total_graphs = len(graphs_in_file)
118151
if total_graphs == 0:
119-
# Skip le fichier
152+
# Skip file
120153
continue
121154
f_out.write(f" File loaded: {total_graphs} graphs.\n")
122155

@@ -140,7 +173,7 @@ def benchmark_exact_exec_time(
140173
for _ in range(3):
141174
result_queue = multiprocessing.Queue()
142175
p = multiprocessing.Process(
143-
target=run_method_get_time_and_result,
176+
target=run_method_get_result_and_time,
144177
args=(method, graph, result_queue),
145178
)
146179
p.start()
@@ -161,7 +194,7 @@ def benchmark_exact_exec_time(
161194
error_occurred = True
162195
break
163196
else:
164-
duration, val = result
197+
val, duration = result
165198
run_times.append(duration)
166199
if current_graph_result_value is None:
167200
current_graph_result_value = val
@@ -272,6 +305,20 @@ def benchmark_approximation_quality(
272305
timeout_seconds,
273306
output_filename,
274307
):
308+
"""
309+
Benchmarks the execution time and approximation quality of given methods against the computed exact value on graphs stored
310+
in .npz files within a directory.
311+
312+
Args:
313+
directory_path (str): The path to the directory containing .npz files.
314+
approx_methods_list (list): A list of approximation method functions to benchmark.
315+
exact_method_func (function): The exact method function to compute the reference value.
316+
timeout_seconds (int): The timeout in seconds for each graph processing.
317+
output_filename (str): The name of the output file to store benchmark results.
318+
319+
Returns:
320+
None
321+
"""
275322
benchmark_start_time = time.perf_counter()
276323

277324
if not os.path.isdir(directory_path):
@@ -333,7 +380,7 @@ def benchmark_approximation_quality(
333380
p_exact.terminate()
334381
p_exact.join()
335382
skipped_graphs_exact_fail += 1
336-
# Skip ce graphe
383+
# Skip graph
337384
continue
338385

339386
try:
@@ -357,7 +404,7 @@ def benchmark_approximation_quality(
357404
for _ in range(3):
358405
approx_queue = multiprocessing.Queue()
359406
p_approx = multiprocessing.Process(
360-
target=run_approx_method_get_result_and_time,
407+
target=run_method_get_result_and_time,
361408
args=(method, graph, approx_queue),
362409
)
363410
p_approx.start()
@@ -522,6 +569,19 @@ def benchmark_approximation_quality_with_dn(
522569
timeout_seconds,
523570
output_filename,
524571
):
572+
"""
573+
Benchmarks the execution time and approximation quality of given methods against known decycling numbers on graphs
574+
stored in .npz files within a directory. The decycling numbers are stored in the files.
575+
576+
Args:
577+
directory_path (str): The path to the directory containing .npz files.
578+
approx_methods_list (list): A list of approximation method functions to benchmark.
579+
timeout_seconds (int): The timeout in seconds for each graph processing.
580+
output_filename (str): The name of the output file to store benchmark results.
581+
582+
Returns:
583+
None
584+
"""
525585
benchmark_start_time = time.perf_counter()
526586

527587
if not os.path.isdir(directory_path):
@@ -581,7 +641,7 @@ def benchmark_approximation_quality_with_dn(
581641
for _ in range(3):
582642
approx_queue = multiprocessing.Queue()
583643
p_approx = multiprocessing.Process(
584-
target=run_approx_method_get_result_and_time,
644+
target=run_method_get_result_and_time,
585645
args=(method, graph, approx_queue),
586646
)
587647
p_approx.start()
@@ -747,7 +807,19 @@ def benchmark_approx_comparison(
747807
timeout_seconds,
748808
output_filename="benchmark_comparison_results.txt",
749809
):
750-
810+
"""
811+
Benchmarks the execution time and compares the best approximation frequency for given approximation methods
812+
on graphs stored in .npz files within a directory.
813+
814+
Args:
815+
directory_path (str): The path to the directory containing .npz files.
816+
methods_list (list): A list of approximation method functions to benchmark.
817+
timeout_seconds (int): The timeout in seconds for each graph processing.
818+
output_filename (str): The name of the output file to store benchmark results.
819+
820+
Returns:
821+
None
822+
"""
751823
benchmark_start_time = time.perf_counter()
752824

753825
if not os.path.isdir(directory_path):
@@ -816,7 +888,7 @@ def benchmark_approx_comparison(
816888
for _ in range(3):
817889
q = multiprocessing.Queue()
818890
p = multiprocessing.Process(
819-
target=run_approx_method_get_result_and_time,
891+
target=run_method_get_result_and_time,
820892
args=(method, graph, q),
821893
)
822894
p.start()

0 commit comments

Comments
 (0)