From f525513491c996bd1d45cd5d1bd3f9c4a61c9674 Mon Sep 17 00:00:00 2001 From: Oliver Jahn Date: Thu, 11 Apr 2024 14:00:08 -0400 Subject: [PATCH] Compare digits --- .../MITgcmutils/tests/test_diagnostics.py | 41 +++++++++++++------ utils/python/MITgcmutils/tox.ini | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/utils/python/MITgcmutils/tests/test_diagnostics.py b/utils/python/MITgcmutils/tests/test_diagnostics.py index f8500d03ac..c841fe98f0 100644 --- a/utils/python/MITgcmutils/tests/test_diagnostics.py +++ b/utils/python/MITgcmutils/tests/test_diagnostics.py @@ -15,6 +15,21 @@ def test_readstats(): assert totals['DETADT2'].shape == (4, 5) assert locals['THETA'][1, 4, 1] == 8.4845124949601 + +def cmp(a, b): + av = .5*(np.abs(a) + np.abs(b)) + if np.any(np.isnan(av)): + return -1 + d = a - b + w = np.where(av) + np.divide.at(d, w, av[w]) + ld = np.zeros_like(d) + 99 + w = np.where(d) + np.place(ld, d, -np.log10(d[w])) + n = np.round(ld.clip(0,99)).astype(int).min() + return n + + def compare_stats(fname): locals, totals, itrs = mit.readstats(fname) lname = fname[:-4]+'.locals.pkl' @@ -23,22 +38,24 @@ def compare_stats(fname): loc = pickle.load(f) with open(tname, 'rb') as f: tot = pickle.load(f) + if type(tot) == type({}): - for k in tot: - idxs = list(zip(*np.where(totals[k] != tot[k]))) - assert len(idxs) == 0 + keys = list(tot) else: - if tot.size != 0: - idxs = list(zip(*np.where(totals != tot))) - assert len(idxs) == 0 + keys = tot.dtype.names + for k in keys: + if tot[k].size != 0: + n = cmp(totals[k], tot[k]) + assert n == 99 + if type(loc) == type({}): - for k in loc: - idxs = list(zip(*np.where(locals[k] != loc[k]))) - assert len(idxs) == 0 + keys = list(loc) else: - if loc.size != 0: - idxs = list(zip(*np.where(locals != loc))) - assert len(idxs) == 0 + keys = loc.dtype.names + for k in keys: + if loc[k].size != 0: + n = cmp(locals[k], loc[k]) + assert n == 99 def test_readstats5(): compare_stats('diagstats/offline_exf_seaice/tr_run.dyn_paralens/iceStDiag.0000000000.txt') diff --git a/utils/python/MITgcmutils/tox.ini b/utils/python/MITgcmutils/tox.ini index af2e325921..a7c4191b6a 100644 --- a/utils/python/MITgcmutils/tox.ini +++ b/utils/python/MITgcmutils/tox.ini @@ -3,7 +3,7 @@ envlist = py37,py38,py39,py310,py311,py312 [testenv] install_command = {toxinidir}/tests/pipinstall.py {opts} -- {packages} -allowlist_externals = /home/runner/work/MITgcm/MITgcm/utils/python/MITgcmutils/tests/pipinstall.py +allowlist_externals = /home/runner/work/MITgcm/MITgcm/utils/python/MITgcmutils/tests/pipinstall.py,/home/jahn/ci/MITgcm/utils/python/MITgcmutils/tests/pipinstall.py usedevelop = True extras = plot