# 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)

[((343, 367), 'h5py.File', 'h5py.File', (['filename', '"""r"""'], {}), "(filename, 'r')\n", (352, 367), False, 'import h5py\n'), ((376, 404), 'h5py.File', 'h5py.File', (['ref_filename', '"""r"""'], {}), "(ref_filename, 'r')\n", (385, 404), False, 'import h5py\n'), ((412, 438), 'numpy.array', 'np.array', (["f['output_data']"], {}), "(f['output_data'])\n", (420, 438), True, 'import numpy as np\n'), ((449, 479), 'numpy.array', 'np.array', (["ref_f['output_data']"], {}), "(ref_f['output_data'])\n", (457, 479), True, 'import numpy as np\n'), ((727, 743), 'numpy.copy', 'np.copy', (['out_ref'], {}), '(out_ref)\n', (734, 743), True, 'import numpy as np\n'), ((832, 846), 'numpy.amax', 'np.amax', (['error'], {}), '(error)\n', (839, 846), True, 'import numpy as np\n'), ((1107, 1129), 'numpy.amax', 'np.amax', (['error'], {'axis': '(1)'}), '(error, axis=1)\n', (1114, 1129), True, 'import numpy as np\n'), ((1444, 1455), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (1452, 1455), False, 'import sys\n'), ((275, 286), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (283, 286), False, 'import sys\n'), ((702, 713), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (710, 713), False, 'import sys\n'), ((1024, 1035), 'sys.exit', 'sys.exit', (['(0)'], {}), '(0)\n', (1032, 1035), False, 'import sys\n')]