Skip to content

Commit f24ba3e

Browse files
committed
Remove test helper assertListAlmostEqual.
Replace with numpy.allclose.
1 parent 46d429f commit f24ba3e

File tree

6 files changed

+32
-97
lines changed

6 files changed

+32
-97
lines changed

src/diffpy/Structure/tests/TestLattice.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ def setUp(self):
3232
self.places = 12
3333
return
3434

35-
def assertListAlmostEqual(self, l1, l2, places=None):
36-
"""wrapper for list comparison"""
37-
if places is None: places = self.places
38-
self.assertEqual(len(l1), len(l2))
39-
for i in range(len(l1)):
40-
self.assertAlmostEqual(l1[i], l2[i], places)
41-
return
42-
43-
4435
def test___init__(self):
4536
'''Check Lattice.__init__ processing of arguments.
4637
'''
@@ -188,9 +179,9 @@ def test_setLatBase(self):
188179
self.lattice.setLatPar(alpha=44, beta=66, gamma=88)
189180
self.assertNotEqual(numpy.all(base == self.lattice.base), True)
190181
self.lattice.setLatPar(alpha=60, beta=60, gamma=60)
191-
self.assertListAlmostEqual(base[0], self.lattice.base[0])
192-
self.assertListAlmostEqual(base[1], self.lattice.base[1])
193-
self.assertListAlmostEqual(base[2], self.lattice.base[2])
182+
self.assertTrue(numpy.allclose(base[0], self.lattice.base[0]))
183+
self.assertTrue(numpy.allclose(base[1], self.lattice.base[1]))
184+
self.assertTrue(numpy.allclose(base[2], self.lattice.base[2]))
194185
# try base checking
195186
self.assertRaises(LatticeError, self.lattice.setLatBase,
196187
[[1, 0, 0], [1,0,0], [0,0,1]])
@@ -228,7 +219,7 @@ def test_norm(self):
228219
'''check norm of a lattice vector.'''
229220
self.assertEqual(1, self.lattice.norm([1, 0, 0]))
230221
u = numpy.array([[3, 4, 0], [1, 1, 1]])
231-
self.assertListAlmostEqual([5, 3**0.5], self.lattice.norm(u))
222+
self.assertTrue(numpy.allclose([5, 3**0.5], self.lattice.norm(u)))
232223
self.lattice.setLatPar(gamma=120)
233224
self.assertAlmostEqual(1, self.lattice.norm([1, 1, 0]), self.places)
234225
return
@@ -242,7 +233,7 @@ def test_rnorm(self):
242233
hkl = [0.5, 0.3, 0.2]
243234
self.assertAlmostEqual(r.norm(hkl), L.rnorm(hkl), self.places)
244235
hkl5 = numpy.tile(hkl, (5, 1))
245-
self.assertListAlmostEqual(5 * [r.norm(hkl)], L.rnorm(hkl5))
236+
self.assertTrue(numpy.allclose(5 * [r.norm(hkl)], L.rnorm(hkl5)))
246237
return
247238

248239

@@ -257,9 +248,9 @@ def test_dist(self):
257248
self.assertAlmostEqual(d0, L.dist(v, u), self.places)
258249
u5 = numpy.tile(u, (5, 1))
259250
v5 = numpy.tile(v, (5, 1))
260-
self.assertListAlmostEqual(5 * [d0], L.dist(u, v5))
261-
self.assertListAlmostEqual(5 * [d0], L.dist(u5, v))
262-
self.assertListAlmostEqual(5 * [d0], L.dist(v5, u5))
251+
self.assertTrue(numpy.allclose(5 * [d0], L.dist(u, v5)))
252+
self.assertTrue(numpy.allclose(5 * [d0], L.dist(u5, v)))
253+
self.assertTrue(numpy.allclose(5 * [d0], L.dist(v5, u5)))
263254
return
264255

265256

@@ -278,9 +269,9 @@ def test_angle(self):
278269
self.assertAlmostEqual(a0, L.angle(v, u), self.places)
279270
u5 = numpy.tile(u, (5, 1))
280271
v5 = numpy.tile(v, (5, 1))
281-
self.assertListAlmostEqual(5 * [a0], L.angle(u, v5))
282-
self.assertListAlmostEqual(5 * [a0], L.angle(u5, v))
283-
self.assertListAlmostEqual(5 * [a0], L.angle(v5, u5))
272+
self.assertTrue(numpy.allclose(5 * [a0], L.angle(u, v5)))
273+
self.assertTrue(numpy.allclose(5 * [a0], L.angle(u5, v)))
274+
self.assertTrue(numpy.allclose(5 * [a0], L.angle(v5, u5)))
284275
return
285276

286277

src/diffpy/Structure/tests/TestP_cif.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""
1818

1919
import unittest
20+
import numpy
2021

2122
from diffpy.Structure.tests.testutils import datafile
2223
from diffpy.Structure.Parsers.P_cif import P_cif, leading_float, getSymOp
@@ -209,14 +210,14 @@ def test_write_and_read(self):
209210
self.assertEqual(4, len(stru))
210211
a0 = stru[0]
211212
self.assertEqual('Cd', a0.element)
212-
self.assertListAlmostEqual([0.3334, 0.6667, 0.0], a0.xyz)
213+
self.assertTrue(numpy.allclose([0.3334, 0.6667, 0.0], a0.xyz))
213214
self.assertTrue(a0.anisotropy)
214215
self.assertAlmostEqual(0.01303, a0.U[0,0])
215216
self.assertAlmostEqual(0.01303, a0.U[1,1])
216217
self.assertAlmostEqual(0.01402, a0.U[2,2])
217218
a3 = stru[3]
218219
self.assertEqual('Se', a3.element)
219-
self.assertListAlmostEqual([0.6666, 0.333300, 0.87667], a3.xyz)
220+
self.assertTrue(numpy.allclose([0.6666, 0.333300, 0.87667], a3.xyz))
220221
self.assertAlmostEqual(0.015673, a3.U[0,0])
221222
self.assertAlmostEqual(0.015673, a3.U[1,1])
222223
self.assertAlmostEqual(0.046164, a3.U[2,2])
@@ -250,18 +251,6 @@ def test_getParser(self):
250251
self.assertEqual(4, len(grph2))
251252
return
252253

253-
########################################################################
254-
# helpers
255-
########################################################################
256-
257-
def assertListAlmostEqual(self, l1, l2, places=None):
258-
"""wrapper for list comparison"""
259-
if places is None: places = self.places
260-
self.assertEqual(len(l1), len(l2))
261-
for i in range(len(l1)):
262-
self.assertAlmostEqual(l1[i], l2[i], places)
263-
return
264-
265254
# End of class TestP_cif
266255

267256
if __name__ == '__main__':

src/diffpy/Structure/tests/TestP_discus.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,10 @@
2323
from diffpy.Structure import Structure, StructureFormatError
2424

2525

26-
def assertListAlmostEqual(self, l1, l2, places=None):
27-
"""wrapper for list comparison"""
28-
if places is None: places = self.places
29-
self.assertEqual(len(l1), len(l2))
30-
for i in range(len(l1)):
31-
self.assertAlmostEqual(l1[i], l2[i], places)
32-
33-
3426
##############################################################################
3527
class TestP_discus(unittest.TestCase):
3628
"""test Parser for PDFFit file format"""
3729

38-
assertListAlmostEqual = assertListAlmostEqual
39-
4030

4131
def setUp(self):
4232
self.stru = Structure()

src/diffpy/Structure/tests/TestP_pdffit.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,16 @@
1818

1919
import unittest
2020
import re
21+
import numpy
2122

2223
from diffpy.Structure.tests.testutils import datafile
2324
from diffpy.Structure import Structure, StructureFormatError
2425

2526

26-
def assertListAlmostEqual(self, l1, l2, places=None):
27-
"""wrapper for list comparison"""
28-
if places is None: places = self.places
29-
self.assertEqual(len(l1), len(l2))
30-
for i in range(len(l1)):
31-
self.assertAlmostEqual(l1[i], l2[i], places)
32-
33-
3427
##############################################################################
3528
class TestP_pdffit(unittest.TestCase):
3629
"""test Parser for PDFFit file format"""
3730

38-
assertListAlmostEqual = assertListAlmostEqual
39-
40-
4131
def setUp(self):
4232
self.stru = Structure()
4333
self.format = "pdffit"
@@ -59,10 +49,10 @@ def test_read_pdffit_ZnSb(self):
5949
s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
6050
stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
6151
f_lat = [12.309436, 12.309436, 12.392839, 90.0, 90.0, 120.0]
62-
self.assertListAlmostEqual(s_lat, f_lat)
52+
self.assertTrue(numpy.allclose(s_lat, f_lat))
6353
s_dcell = stru.pdffit['dcell']
6454
f_dcell = [0.000008, 0.000008, 0.000013, 0.0, 0.0, 0.0]
65-
self.assertListAlmostEqual(s_dcell, f_dcell)
55+
self.assertTrue(numpy.allclose(s_dcell, f_dcell))
6656
self.assertEqual(stru.pdffit['ncell'], [1,1,1,66])
6757
s_els = [a.element for a in stru]
6858
self.assertEqual(s_els, 36*['Zn']+30*['Sb'])
@@ -77,9 +67,9 @@ def test_read_pdffit_ZnSb(self):
7767
f_sigo = 0.0
7868
s_U = [ a0.U[i][i] for i in range(3) ]
7969
f_U = 3*[0.01]
80-
self.assertListAlmostEqual(s_xyz, f_xyz)
81-
self.assertListAlmostEqual(s_sigxyz, f_sigxyz)
82-
self.assertListAlmostEqual(s_U, f_U)
70+
self.assertTrue(numpy.allclose(s_xyz, f_xyz))
71+
self.assertTrue(numpy.allclose(s_sigxyz, f_sigxyz))
72+
self.assertTrue(numpy.allclose(s_U, f_U))
8373
self.assertAlmostEqual(s_o, f_o)
8474
self.assertAlmostEqual(s_sigo, f_sigo)
8575

src/diffpy/Structure/tests/TestParsers.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@
2020
import os
2121
import re
2222
import tempfile
23+
import numpy
2324

2425
from diffpy.Structure.tests.testutils import datafile
2526
from diffpy.Structure import Structure, StructureFormatError
2627
from diffpy.Structure import Lattice
2728
from diffpy.Structure import Atom
2829

29-
def assertListAlmostEqual(self, l1, l2, places=None):
30-
"""wrapper for list comparison"""
31-
if places is None: places = self.places
32-
self.assertEqual(len(l1), len(l2))
33-
for i in range(len(l1)):
34-
self.assertAlmostEqual(l1[i], l2[i], places)
35-
3630
##############################################################################
3731
class TestP_xyz(unittest.TestCase):
3832
"""test Parser for xyz file format"""
@@ -184,13 +178,6 @@ def setUp(self):
184178
self.format = "pdb"
185179
self.places = 3
186180

187-
def assertListAlmostEqual(self, l1, l2, places=None):
188-
"""wrapper for list comparison"""
189-
if places is None: places = self.places
190-
self.assertEqual(len(l1), len(l2))
191-
for i in range(len(l1)):
192-
self.assertAlmostEqual(l1[i], l2[i], places)
193-
194181
def test_read_pdb_arginine(self):
195182
"""check reading of arginine PDB file"""
196183
stru = self.stru
@@ -205,7 +192,7 @@ def test_read_pdb_arginine(self):
205192
f_lat = [1.0, 1.0, 1.0, 90.0, 90.0, 90.0]
206193
self.assertEqual(s_lat, f_lat)
207194
a0 = stru[0]
208-
self.assertListAlmostEqual(a0.xyz, [0.735, 2.219, 1.389])
195+
self.assertTrue(numpy.allclose(a0.xyz, [0.735, 2.219, 1.389]))
209196

210197
def test_rwStr_pdb_CdSe(self):
211198
"""check conversion to PDB file format"""
@@ -224,14 +211,14 @@ def test_rwStr_pdb_CdSe(self):
224211
s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
225212
stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
226213
f_lat = [ 4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0 ]
227-
self.assertListAlmostEqual(s_lat, f_lat)
214+
self.assertTrue(numpy.allclose(s_lat, f_lat, atol=5e-4))
228215
a0 = stru[0]
229216
s_Uii = [ a0.U[i,i] for i in range(3) ]
230217
f_Uii = [ 0.01303035, 0.01303035, 0.01401959 ]
231-
self.assertListAlmostEqual(s_Uii, f_Uii)
218+
self.assertTrue(numpy.allclose(s_Uii, f_Uii, atol=5e-4))
232219
s_sigUii = [ a0.sigU[i,i] for i in range(3) ]
233220
f_sigUii = [ 0.00011127, 0.00011127, 0.00019575 ]
234-
self.assertListAlmostEqual(s_sigUii, f_sigUii)
221+
self.assertTrue(numpy.allclose(s_sigUii, f_sigUii, atol=5e-4))
235222
s_title = stru.title
236223
f_title = "Cell structure file of CdSe #186"
237224
self.assertEqual(s_title, f_title)
@@ -247,12 +234,6 @@ def setUp(self):
247234
self.format = "xcfg"
248235
self.places = 6
249236

250-
def assertListAlmostEqual(self, l1, l2, places=None):
251-
"""wrapper for list comparison"""
252-
if places is None: places = self.places
253-
self.assertEqual(len(l1), len(l2))
254-
for i in range(len(l1)):
255-
self.assertAlmostEqual(l1[i], l2[i], places)
256237

257238
def test_read_xcfg(self):
258239
"""check reading of BubbleRaft XCFG file"""
@@ -265,7 +246,9 @@ def test_read_xcfg(self):
265246
s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
266247
stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
267248
f_lat = [127.5, 119.5, 3.0, 90.0, 90.0, 90.0]
268-
self.assertListAlmostEqual(s_lat, f_lat)
249+
self.assertTrue(numpy.allclose(s_lat, f_lat))
250+
return
251+
269252

270253
def test_rwStr_xcfg_CdSe(self):
271254
"""check conversion to XCFG file format"""
@@ -280,11 +263,11 @@ def test_rwStr_xcfg_CdSe(self):
280263
s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
281264
stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
282265
f_lat = [ 4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0 ]
283-
self.assertListAlmostEqual(s_lat, f_lat)
266+
self.assertTrue(numpy.allclose(s_lat, f_lat))
284267
a0 = stru[0]
285268
s_Uii = [ a0.U[i,i] for i in range(3) ]
286269
f_Uii = [ 0.01303035, 0.01303035, 0.01401959 ]
287-
self.assertListAlmostEqual(s_Uii, f_Uii)
270+
self.assertTrue(numpy.allclose(s_Uii, f_Uii))
288271

289272
# End of TestP_xcfg
290273

src/diffpy/Structure/tests/TestStructure.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ def setUp(self):
5151
return
5252

5353

54-
def assertListAlmostEqual(self, l1, l2, places=None):
55-
"""wrapper for list comparison"""
56-
if places is None: places = self.places
57-
self.assertEqual(len(l1), len(l2))
58-
for i in range(len(l1)):
59-
self.assertAlmostEqual(l1[i], l2[i], places)
60-
61-
6254
def test___init__(self):
6355
"""check Structure.__init__()
6456
"""
@@ -170,9 +162,9 @@ def test_placeInLattice(self):
170162
new_lattice = Lattice(.5, .5, .5, 90, 90, 60)
171163
stru.placeInLattice(new_lattice)
172164
a0 = stru[0]
173-
self.assertListAlmostEqual(a0.xyz, 3*[0.0])
165+
self.assertTrue(numpy.allclose(a0.xyz, [0.0, 0.0, 0.0]))
174166
a1 = stru[1]
175-
self.assertListAlmostEqual(a1.xyz, [2.0, 0.0, 2.0])
167+
self.assertTrue(numpy.allclose(a1.xyz, [2.0, 0.0, 2.0]))
176168

177169
# def test_read(self):
178170
# """check Structure.read()

0 commit comments

Comments
 (0)