-
Notifications
You must be signed in to change notification settings - Fork 37
/
basepmp.py
56 lines (45 loc) · 2.01 KB
/
basepmp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import unittest
import numpy
import os
import pcmdi_metrics
class PMPTest(unittest.TestCase):
def checkAllClose(self, a, b, rtol=1e-05, atol=1e-08):
if numpy.ma.allclose(a.filled(), b.filled(), rtol=rtol, atol=atol):
return True
else:
axes = a.getAxisList()
c = numpy.isclose(a.filled(), b.filled(), rtol=rtol, atol=atol)
w = numpy.argwhere(c==0)
for d in w:
print "Error for:",
for i,indx in enumerate(d):
print "%s, " % axes[i][indx],
print "(",
for indx in d:
print "%i," % indx,
print "). Test value %.3f vs expected value: %.3f" % (a[tuple(d)],b[tuple(d)])
return False
return True
def assertSimilarJsons(self, test_file, correct_file, rtol=1e-05, atol=1e-08, raiseOnError=True):
print "Comparing:",test_file, correct_file, "atol:",atol
T = pcmdi_metrics.io.base.JSONs([test_file], oneVariablePerFile=False)
test = T()
V = pcmdi_metrics.io.base.JSONs([correct_file], oneVariablePerFile=False)
valid = V()
self.assertEqual(test.shape, valid.shape)
tax = test.getAxisList()
cax = valid.getAxisList()
correct = True
for i in range(len(tax)):
if not tax[i].id == cax[i].id:
print "Axes index %i have different names, test is '%s' vs expected: '%s'" % (i,tax[i].id, cax[i].id)
correct = False
for j in range(len(tax[i])):
if not tax[i][j] == cax[i][j]:
print "Axes %s, differ at index %i, test value: %s vs expectedi value: %s" % (tax[i].id,j,tax[i][j], cax[i][j])
correct = False
if not self.checkAllClose(test, valid, rtol, atol):
correct = False
if not correct and raiseOnError:
raise Exception("jsons file %s differ from correct one: %s" % (test_file, correct_file))
return correct