Modified the way group tests compare output

Instead of comparing text output files the tests are now done
on h5-files. The latter are compared with a finite precision
eliminating thus a problem of platform-dependence of the
results.
This commit is contained in:
Oleg E. Peil 2017-02-07 10:31:28 +01:00
parent e24933d3fe
commit 20be2e5826
11 changed files with 69 additions and 79 deletions

View File

@ -1,2 +1,3 @@
*.pyc
*.test
*.test.h5

View File

@ -5,6 +5,7 @@ Module defining a custom TestCase with extra functionality.
import unittest
import numpy as np
import difflib
from pytriqs.utility.h5diff import h5diff
class MyTestCase(unittest.TestCase):
"""
@ -54,4 +55,16 @@ class MyTestCase(unittest.TestCase):
if diff:
return self.fail("Files '%s' and '%s' differ"%(file1, file2))
def assertH5FileEqual(self, file1, file2):
"""
Compares two files using difflib.
Empty lines are ignored.
Files are assumed to be relatively small;
the data is truncated for files larger than MAX_SIZE bytes.
"""
try:
h5diff(file1, file2, precision=1e-6)
except RuntimeError as err:
if "FAILED" in err:
return self.fail("Files '%s' and '%s' differ"%(file1, file2))

View File

@ -1,10 +0,0 @@
density matrix: [[[[ 0.54239242 -0.00069107 0.00000946 -0.00067639 -0.00000019]
[-0.00069107 0.54295547 0.00000486 0.00082855 -0.00002492]
[ 0.00000946 0.00000486 0.57155757 0.00000454 0.00000002]
[-0.00067639 0.00082855 0.00000454 0.54666342 0.00002474]
[-0.00000019 -0.00002492 0.00000002 0.00002474 0.57284179]]]]
overlap matrix: [[[[ 1. -0. 0. 0. 0.]
[-0. 1. -0. -0. 0.]
[ 0. -0. 1. -0. -0.]
[ 0. -0. -0. 1. 0.]
[ 0. 0. -0. 0. 1.]]]]

Binary file not shown.

View File

@ -1,22 +0,0 @@
density matrix: [[[[ 0.50361386 0.01210591 -0.00005976 0.00166392 -0.00001898]
[ 0.01210591 0.48889987 0.00001956 -0.00197949 -0.00001589]
[-0.00005976 0.00001956 0.48077169 0.00006637 -0.00003311]
[ 0.00166392 -0.00197949 0.00006637 0.43066013 0.0001357 ]
[-0.00001898 -0.00001589 -0.00003311 0.0001357 0.48025246]]
[[ 0.5060551 0.00758141 -0.00005402 0.00663632 -0.00001664]
[ 0.00758141 0.47773011 0.00003639 -0.01034786 -0.00004147]
[-0.00005402 0.00003639 0.48077119 0.0000735 -0.00000922]
[ 0.00663632 -0.01034786 0.0000735 0.43934266 0.00013596]
[-0.00001664 -0.00004147 -0.00000922 0.00013596 0.48024702]]]]
overlap matrix: [[[[ 1. 0. -0. -0. 0.]
[ 0. 1. 0. 0. -0.]
[-0. 0. 1. -0. -0.]
[-0. 0. -0. 1. -0.]
[ 0. -0. -0. -0. 1.]]
[[ 1. 0. 0. -0. 0.]
[ 0. 1. -0. 0. -0.]
[ 0. -0. 1. 0. -0.]
[-0. 0. 0. 1. -0.]
[ 0. -0. -0. -0. 1.]]]]

Binary file not shown.

View File

@ -1,22 +0,0 @@
density matrix: [[[[ 0.56016062 0.01202778 -0.00006002 0.00164142 -0.0000189 ]
[ 0.01202778 0.5465868 0.0000203 -0.00199428 -0.00001706]
[-0.00006002 0.0000203 0.54446136 0.00006678 -0.00003108]
[ 0.00164142 -0.00199428 0.00006678 0.48839256 0.00013617]
[-0.0000189 -0.00001706 -0.00003108 0.00013617 0.54338373]]
[[ 0.56260435 0.00750293 -0.00005427 0.00661288 -0.00001657]
[ 0.00750293 0.53541612 0.0000371 -0.01036245 -0.00004262]
[-0.00005427 0.0000371 0.54445834 0.0000739 -0.00000814]
[ 0.00661288 -0.01036245 0.0000739 0.49707557 0.00013642]
[-0.00001657 -0.00004262 -0.00000814 0.00013642 0.54337993]]]]
overlap matrix: [[[[ 1. -0. -0. 0. 0.]
[-0. 1. 0. 0. 0.]
[-0. 0. 1. -0. 0.]
[ 0. 0. -0. 1. 0.]
[ 0. 0. 0. 0. 1.]]
[[ 1. -0. -0. -0. -0.]
[-0. 1. 0. 0. 0.]
[-0. 0. 1. -0. 0.]
[-0. 0. -0. 1. -0.]
[-0. 0. 0. -0. 1.]]]]

Binary file not shown.

View File

@ -5,7 +5,7 @@ import unittest
if __name__ == '__main__':
# suite = unittest.TestLoader().discover('./')
suite = unittest.TestLoader().discover('./', pattern='test_two*')
suite = unittest.TestLoader().discover('./', pattern='test_one*')
unittest.TextTestRunner(verbosity=2, buffer=True).run(suite)
# unittest.TextTestRunner(verbosity=2, buffer=False).run(suite)

View File

@ -9,6 +9,7 @@ from pytriqs.applications.dft.converters.plovasp.elstruct import ElectronicStruc
from pytriqs.applications.dft.converters.plovasp.inpconf import ConfigParameters
from pytriqs.applications.dft.converters.plovasp.proj_shell import ProjectorShell
from pytriqs.applications.dft.converters.plovasp.proj_group import ProjectorGroup
from pytriqs.archive import HDFArchive
import mytest
################################################################################
@ -47,15 +48,22 @@ class TestProjectorGroup(mytest.MyTestCase):
dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)
testout = _rpath + 'projortho.out.test'
with open(testout, 'wt') as f:
f.write("density matrix: %s\n"%(dens_mat))
f.write("overlap matrix: %s\n"%(overl))
# testout = _rpath + 'projortho.out.test'
# with open(testout, 'wt') as f:
# f.write("density matrix: %s\n"%(dens_mat))
# f.write("overlap matrix: %s\n"%(overl))
testout = _rpath + 'projortho.test.h5'
with HDFArchive(testout, 'w') as h5test:
h5test['density_matrix'] = dens_mat
h5test['overlap_matrix'] = overl
# FIXME: seems redundant, as 'overl' is written to the file anyway
self.assertEqual(overl, np.eye(5))
expected_file = _rpath + 'projortho.out'
self.assertFileEqual(testout, expected_file)
# expected_file = _rpath + 'projortho.out'
expected_file = _rpath + 'projortho.out.h5'
# self.assertFileEqual(testout, expected_file)
self.assertH5FileEqual(testout, expected_file)
# Scenario 2
def test_ortho_normion(self):
@ -64,14 +72,21 @@ class TestProjectorGroup(mytest.MyTestCase):
dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)
testout = _rpath + 'projortho.out.test'
with open(testout, 'wt') as f:
f.write("density matrix: %s\n"%(dens_mat))
f.write("overlap matrix: %s\n"%(overl))
# testout = _rpath + 'projortho.out.test'
# with open(testout, 'wt') as f:
# f.write("density matrix: %s\n"%(dens_mat))
# f.write("overlap matrix: %s\n"%(overl))
testout = _rpath + 'projortho.test.h5'
with HDFArchive(testout, 'w') as h5test:
h5test['density_matrix'] = dens_mat
h5test['overlap_matrix'] = overl
# FIXME: seems redundant, as 'overl' is written to the file anyway
self.assertEqual(overl, np.eye(5))
expected_file = _rpath + 'projortho.out'
self.assertFileEqual(testout, expected_file)
# expected_file = _rpath + 'projortho.out'
# self.assertFileEqual(testout, expected_file)
expected_file = _rpath + 'projortho.out.h5'
self.assertH5FileEqual(testout, expected_file)

View File

@ -9,6 +9,7 @@ from pytriqs.applications.dft.converters.plovasp.elstruct import ElectronicStruc
from pytriqs.applications.dft.converters.plovasp.inpconf import ConfigParameters
from pytriqs.applications.dft.converters.plovasp.proj_shell import ProjectorShell
from pytriqs.applications.dft.converters.plovasp.proj_group import ProjectorGroup
from pytriqs.archive import HDFArchive
import mytest
################################################################################
@ -50,16 +51,23 @@ class TestProjectorGroupTwoSite(mytest.MyTestCase):
dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)
testout = _rpath + 'projortho_2site.out.test'
with open(testout, 'wt') as f:
f.write("density matrix: %s\n"%(dens_mat))
f.write("overlap matrix: %s\n"%(overl))
# testout = _rpath + 'projortho_2site.out.test'
# with open(testout, 'wt') as f:
# f.write("density matrix: %s\n"%(dens_mat))
# f.write("overlap matrix: %s\n"%(overl))
testout = _rpath + 'projortho_2site.test.h5'
with HDFArchive(testout, 'w') as h5test:
h5test['density_matrix'] = dens_mat
h5test['overlap_matrix'] = overl
# FIXME: redundant
self.assertEqual(overl[0, 0, ...], np.eye(5))
self.assertEqual(overl[0, 1, ...], np.eye(5))
expected_file = _rpath + 'projortho_2site.out'
self.assertFileEqual(testout, expected_file)
# expected_file = _rpath + 'projortho_2site.out'
# self.assertFileEqual(testout, expected_file)
expected_file = _rpath + 'projortho_2site.out.h5'
self.assertH5FileEqual(testout, expected_file)
# Scenario 2
def test_ortho_normion(self):
@ -68,15 +76,22 @@ class TestProjectorGroupTwoSite(mytest.MyTestCase):
dens_mat, overl = self.proj_sh.density_matrix(self.el_struct)
testout = _rpath + 'projortho_normion.out.test'
with open(testout, 'wt') as f:
f.write("density matrix: %s\n"%(dens_mat))
f.write("overlap matrix: %s\n"%(overl))
# testout = _rpath + 'projortho_normion.out.test'
# with open(testout, 'wt') as f:
# f.write("density matrix: %s\n"%(dens_mat))
# f.write("overlap matrix: %s\n"%(overl))
testout = _rpath + 'projortho_normion.test.h5'
with HDFArchive(testout, 'w') as h5test:
h5test['density_matrix'] = dens_mat
h5test['overlap_matrix'] = overl
# FIXME: redundant
self.assertEqual(overl[0, 0, ...], np.eye(5))
self.assertEqual(overl[0, 1, ...], np.eye(5))
expected_file = _rpath + 'projortho_normion.out'
self.assertFileEqual(testout, expected_file)
# expected_file = _rpath + 'projortho_normion.out'
# self.assertFileEqual(testout, expected_file)
expected_file = _rpath + 'projortho_normion.out.h5'
self.assertH5FileEqual(testout, expected_file)