cli arguments python script
This commit is contained in:
		
							
								
								
									
										43
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								main.py
									
									
									
									
									
								
							| @@ -1,3 +1,5 @@ | |||||||
|  | #!/bin/python3 | ||||||
|  | import argparse | ||||||
| import subprocess | import subprocess | ||||||
| import re | import re | ||||||
| import numpy as np | import numpy as np | ||||||
| @@ -32,7 +34,7 @@ def run_pagerank(matrix_path, epsilon, alpha): | |||||||
|         match = re.search(r"Gauss-Seidel: (\d+) Iterations", output) |         match = re.search(r"Gauss-Seidel: (\d+) Iterations", output) | ||||||
|         data["gauss_seidel_iterations"] = int(match.group(1)) if match else None |         data["gauss_seidel_iterations"] = int(match.group(1)) if match else None | ||||||
|  |  | ||||||
|         match = re.search(r"Gauss-Seidel:: (\d+\.\d+) seconds", output) |         match = re.search(r"Gauss-Seidel: (\d+\.\d+) seconds", output) | ||||||
|         data["gauss_seidel_time"] = float(match.group(1)) if match else None |         data["gauss_seidel_time"] = float(match.group(1)) if match else None | ||||||
|  |  | ||||||
|         match = re.search(r"Difference norm Pagerank and Gauss-Seidel: (\d+\.\d+)", output) |         match = re.search(r"Difference norm Pagerank and Gauss-Seidel: (\d+\.\d+)", output) | ||||||
| @@ -47,11 +49,8 @@ def run_pagerank(matrix_path, epsilon, alpha): | |||||||
|         print(f"Error parsing output for alpha={alpha}: {e}") |         print(f"Error parsing output for alpha={alpha}: {e}") | ||||||
|         return None |         return None | ||||||
|  |  | ||||||
| def main(): |  | ||||||
|     matrix_path = "./out/input.mtx" |  | ||||||
|     epsilon = 1e-10 |  | ||||||
|     alpha_values = np.arange(0, 1, 0.1) |  | ||||||
|  |  | ||||||
|  | def generate_data(matrix_path, epsilon, alpha_values): | ||||||
|     pagerank_iterations = [] |     pagerank_iterations = [] | ||||||
|     gauss_seidel_iterations = [] |     gauss_seidel_iterations = [] | ||||||
|     pagerank_times = [] |     pagerank_times = [] | ||||||
| @@ -75,34 +74,54 @@ def main(): | |||||||
|             gauss_seidel_times.append(None) |             gauss_seidel_times.append(None) | ||||||
|             diff_norms.append(None) |             diff_norms.append(None) | ||||||
|  |  | ||||||
|  |     return (pagerank_iterations, gauss_seidel_iterations, pagerank_times, gauss_seidel_times, diff_norms) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def plot_curves(alpha_values, pagerank_iterations, gauss_seidel_iterations, pagerank_times, gauss_seidel_times, diff_norms): | ||||||
|     # plots |     # plots | ||||||
|     fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 12), sharex=True) |     fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 12), sharex=False) | ||||||
|  |  | ||||||
|     # 1: Number of iterations |     # 1: Number of iterations | ||||||
|     ax1.plot(alpha_values, pagerank_iterations, label="PageRank Iterations", color="blue") |     ax1.plot(alpha_values, pagerank_iterations, label="PageRank Iterations", color="blue") | ||||||
|     ax1.plot(alpha_values, gauss_seidel_iterations, label="Gauss-Seidel Iterations", color="red") |     ax1.plot(alpha_values, gauss_seidel_iterations, label="Gauß-Seidel Iterations", color="red") | ||||||
|  |     ax1.set_xlabel("α") | ||||||
|     ax1.set_ylabel("Number of Iterations") |     ax1.set_ylabel("Number of Iterations") | ||||||
|     ax1.set_title("Iterations vs. Alpha") |     ax1.set_title("Iterations vs. α") | ||||||
|     ax1.legend() |     ax1.legend() | ||||||
|     ax1.grid(True) |     ax1.grid(True) | ||||||
|  |  | ||||||
|     # 2: Execution time |     # 2: Execution time | ||||||
|     ax2.plot(alpha_values, pagerank_times, label="PageRank Time", color="blue") |     ax2.plot(alpha_values, pagerank_times, label="PageRank Time", color="blue") | ||||||
|     ax2.plot(alpha_values, gauss_seidel_times, label="Gauss-Seidel Time", color="red") |     ax2.plot(alpha_values, gauss_seidel_times, label="Gauß-Seidel Time", color="red") | ||||||
|  |     ax2.set_xlabel("α") | ||||||
|     ax2.set_ylabel("Time (seconds)") |     ax2.set_ylabel("Time (seconds)") | ||||||
|     ax2.set_title("Execution Time vs. Alpha") |     ax2.set_title("Execution Time vs. α") | ||||||
|     ax2.legend() |     ax2.legend() | ||||||
|     ax2.grid(True) |     ax2.grid(True) | ||||||
|  |  | ||||||
|     # 3: Difference norm |     # 3: Difference norm | ||||||
|     ax3.plot(alpha_values, diff_norms, label="Difference Norm", color="green") |     ax3.plot(alpha_values, diff_norms, label="Difference Norm", color="green") | ||||||
|     ax3.set_xlabel("Alpha") |     ax3.set_xlabel("α") | ||||||
|     ax3.set_ylabel("Norm Difference") |     ax3.set_ylabel("Norm Difference") | ||||||
|     ax3.set_title("Difference Norm between PageRank and Gauss-Seidel vs. Alpha") |     ax3.set_title("Difference Norm between PageRank and Gauß-Seidel vs. α") | ||||||
|     ax3.legend() |     ax3.legend() | ||||||
|     ax3.grid(True) |     ax3.grid(True) | ||||||
|  |  | ||||||
|     plt.show() |     plt.show() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def main(): | ||||||
|  |     parser = argparse.ArgumentParser(description='Pagerank vs Gauß Seidel execution time depending on α') | ||||||
|  |     parser.add_argument('--alpha', type=int, default=10, help='number of alpha to test in the range (0,1)') | ||||||
|  |     parser.add_argument('--epsilon', type=float, default=1e-7, help='epsilon value for the convergence') | ||||||
|  |     parser.add_argument('--matrix', type=str, default="out/input.mtx", help='path to the matrix file') | ||||||
|  |     args = parser.parse_args() | ||||||
|  |  | ||||||
|  |     alpha_values = np.arange(0, 1, (1/args.alpha)) | ||||||
|  |  | ||||||
|  |     (pagerank_iterations, gauss_seidel_iterations, pagerank_times, gauss_seidel_times, diff_norms) = generate_data(args.matrix, args.epsilon, alpha_values) | ||||||
|  |     plot_curves(alpha_values, pagerank_iterations, gauss_seidel_iterations, pagerank_times, gauss_seidel_times, diff_norms) | ||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     main() |     main() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user