# Simple python3 script to compare output with a reference output.
# Usage: python3 compareOutputs.py testOutput.h5 testRefOutput.h5

import sys
import h5py
import numpy as np

if len(sys.argv) != 3:
    print("Expected two arguments. Output and reference output file.")
    sys.exit(1)

filename = sys.argv[1]
ref_filename = sys.argv[2]

f = h5py.File(filename, 'r')
ref_f = h5py.File(ref_filename, 'r')

out = np.array(f['output_data'])
out_ref = np.array(ref_f['output_data'])

if out.shape != out_ref.shape:
    print("The files do not contain the same number of outputs.")
    print("The output size: {0}.".format(out.shape[0]))
    print("The reference size: {0}.".format(out_ref.shape[0]))

    sys.exit(1)

ref_value = np.copy(out_ref)
ref_value[ref_value == 0.0] = 1.0

error = (out_ref - out) / ref_value

maximal_error = np.amax(error)

print("Maximal error between the output and the reference is {0}.".format(maximal_error))
if maximal_error < 10**(-6):
    print("OK:Output seems to match the reference.")
    sys.exit(0)

print("Failure:Output does not match the reference.")
maximal_error = np.amax(error, axis=1)
print(maximal_error.shape)

for i in range(0, 5):
    print("Image", i)
    print("Expected:", end="")
    for j in range(0, 10):
        print(out_ref[i, j], end = " ")
    print("\nGot:", end="")
    for j in range(0, 10):
        print(out[i, j], end=" ")
    print("\nMaximal error:", maximal_error[i], "\n")

sys.exit(1)

[(15, 'h5py.File', 'h5py.File', False, 'import h5py\n'), (16, 'h5py.File', 'h5py.File', False, 'import h5py\n'), (18, 'numpy.array', 'np.array', True, 'import numpy as np\n'), (19, 'numpy.array', 'np.array', True, 'import numpy as np\n'), (28, 'numpy.copy', 'np.copy', True, 'import numpy as np\n'), (33, 'numpy.amax', 'np.amax', True, 'import numpy as np\n'), (41, 'numpy.amax', 'np.amax', True, 'import numpy as np\n'), (54, 'sys.exit', 'sys.exit', False, 'import sys\n'), (10, 'sys.exit', 'sys.exit', False, 'import sys\n'), (26, 'sys.exit', 'sys.exit', False, 'import sys\n'), (38, 'sys.exit', 'sys.exit', False, 'import sys\n')]