Files
TDT4200/exercise7/compare.py
2025-11-03 11:24:17 +01:00

49 lines
1.1 KiB
Python

import sys
import numpy as np
class bcolors:
OKGREEN = "\033[92m"
FAIL = "\033[91m"
ENDC = "\033[0m"
def main() -> None:
error_margin = 1e-4
if len(sys.argv) != 3:
print("Usage: python plot_difference.py file1.dat file2.dat")
sys.exit(1)
file1, file2 = sys.argv[1], sys.argv[2]
M = 128
N = 128
data1 = load_data(file1, M, N)
data2 = load_data(file2, M, N)
# Compute difference
diff = data2 - data1
if (diff > error_margin).any():
print(
f"\n{bcolors.FAIL}Data files {file1} and {file2} differ by more than {error_margin}{bcolors.ENDC}\n"
)
else:
print(
f"\n{bcolors.OKGREEN}Data files {file1} and {file2} are identical within the margin of {error_margin}{bcolors.ENDC}\n"
)
def load_data(filename: str, M: int, N: int) -> np.ndarray:
data = np.fromfile(filename, dtype=np.float64)
if data.size != M * N:
raise ValueError(f"File {filename} does not contain M*N={M*N} entries")
return data.reshape((M, N))
if __name__ == "__main__":
main()