cli arguments python script

This commit is contained in:
2025-05-14 20:35:53 +02:00
parent 4dd7fea3c5
commit d5c8f3bfc9

43
main.py
View File

@@ -1,3 +1,5 @@
#!/bin/python3
import argparse
import subprocess
import re
import numpy as np
@@ -32,7 +34,7 @@ def run_pagerank(matrix_path, epsilon, alpha):
match = re.search(r"Gauss-Seidel: (\d+) Iterations", output)
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
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}")
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 = []
gauss_seidel_iterations = []
pagerank_times = []
@@ -75,34 +74,54 @@ def main():
gauss_seidel_times.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
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
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_title("Iterations vs. Alpha")
ax1.set_title("Iterations vs. α")
ax1.legend()
ax1.grid(True)
# 2: Execution time
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_title("Execution Time vs. Alpha")
ax2.set_title("Execution Time vs. α")
ax2.legend()
ax2.grid(True)
# 3: Difference norm
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_title("Difference Norm between PageRank and Gauss-Seidel vs. Alpha")
ax3.set_title("Difference Norm between PageRank and Gauß-Seidel vs. α")
ax3.legend()
ax3.grid(True)
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__":
main()