# 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, 4, 15, 28), 'h5py.File', 'h5py.File', ({(15, 14, 15, 22): 'filename', (15, 24, 15, 27): '"""r"""'}, {}), "(filename, 'r')", False, 'import h5py\n'), ((16, 8, 16, 36), 'h5py.File', 'h5py.File', ({(16, 18, 16, 30): 'ref_filename', (16, 32, 16, 35): '"""r"""'}, {}), "(ref_filename, 'r')", False, 'import h5py\n'), ((18, 6, 18, 32), 'numpy.array', 'np.array', ({(18, 15, 18, 31): "f['output_data']"}, {}), "(f['output_data'])", True, 'import numpy as np\n'), ((19, 10, 19, 40), 'numpy.array', 'np.array', ({(19, 19, 19, 39): "ref_f['output_data']"}, {}), "(ref_f['output_data'])", True, 'import numpy as np\n'), ((28, 12, 28, 28), 'numpy.copy', 'np.copy', ({(28, 20, 28, 27): 'out_ref'}, {}), '(out_ref)', True, 'import numpy as np\n'), ((33, 16, 33, 30), 'numpy.amax', 'np.amax', ({(33, 24, 33, 29): 'error'}, {}), '(error)', True, 'import numpy as np\n'), ((41, 16, 41, 38), 'numpy.amax', 'np.amax', (), '', True, 'import numpy as np\n'), ((54, 0, 54, 11), 'sys.exit', 'sys.exit', ({(54, 9, 54, 10): '(1)'}, {}), '(1)', False, 'import sys\n'), ((10, 4, 10, 15), 'sys.exit', 'sys.exit', ({(10, 13, 10, 14): '(1)'}, {}), '(1)', False, 'import sys\n'), ((26, 4, 26, 15), 'sys.exit', 'sys.exit', ({(26, 13, 26, 14): '(1)'}, {}), '(1)', False, 'import sys\n'), ((38, 4, 38, 15), 'sys.exit', 'sys.exit', ({(38, 13, 38, 14): '(0)'}, {}), '(0)', False, 'import sys\n')]