Skip to content

Instantly share code, notes, and snippets.

@pspoerri
Last active December 20, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pspoerri/15c0461c81c4ae2d04c4 to your computer and use it in GitHub Desktop.
Save pspoerri/15c0461c81c4ae2d04c4 to your computer and use it in GitHub Desktop.
Serialbox Compare Arrays
import numpy as np
def match_arrays(a, b, threshold=0.0):
diff = np.abs(a - b)
if diff.min() <= diff.max() and diff.max() <= threshold:
return True, ""
return False, "abs(a-b): min= {min} max= {max}".format(min=diff.min(), max=diff.max())
def compare_dicts(a, b, fun, ident=""):
for k, v in b.items():
if k not in a.keys():
print("{ident}{key} is not in a".format(ident=ident, key=k))
for k, v in a.items():
if k not in b.keys():
print("{ident}{key} is not in b".format(ident=ident, key=k))
continue
print("{ident}Comparing {key}".format(ident=ident, key=k))
if isinstance(v, dict):
compare_dicts(v, b[k], fun, ident + " ")
continue
va = v
vb = b[k]
if type(va) is not type(vb):
print("{ident}{key} type mismatch: type(a)={ta} type(b)={tb}".format(ident=ident, key=k,
ta=str(type(va)),
tb=str(type(vb))))
validates = False
try:
(validates, result) = fun(va, vb)
except Exception as e:
validates = False
result = "Received an error: type({type}) with message {msg}".format(type=type(e), msg=str(e))
if validates:
continue
print("{ident}{key} does not validate: {result}".format(ident=ident, key=k, result=result))
from serialbox import *
ser_all = Serializer('/scratch/spoerrip/testdata/COSMO_1_January_reduce_case2')
ser_red = Serializer('/scratch/spoerrip/testdata/COSMO_1_January_reduce_case2_latersteps2')
compare_dicts(ser_all, ser_red, match_arrays)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment