Last active
December 20, 2016 09:48
-
-
Save pspoerri/15c0461c81c4ae2d04c4 to your computer and use it in GitHub Desktop.
Serialbox Compare Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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