def setUp(self): # Load the object # Imports from opan.xyz import OpanXYZ # Create the object self.xyz = OpanXYZ(path=self.file_name) # Long messages self.longMessage = True
def setUp(self): # Load the object # Imports from opan.xyz import OpanXYZ # Create the object self.xyz = OpanXYZ(atom_syms=self.good_direct_atoms, coords=self.good_direct_geom) # Long messages self.longMessage = True
def test_XYZ_BadUsageBadNoneParamsWithIters(self): from opan.xyz import OpanXYZ # Load the test file x = OpanXYZ(path=self.file_name) # Check error on multiple Nones passed with self.assertRaises(ValueError): [d for d in x.dist_iter(None, None, 1)] # Check error on None passed with iterable with self.assertRaises(ValueError): [d for d in x.dist_iter(None, [0, 1, 2], 3)]
def test_XYZ_BadUsageBadNoneParamsWithIters(self): from opan.xyz import OpanXYZ # Load the test file x = OpanXYZ(path=self.file_name) # Check error on multiple Nones passed with self.assertRaises(ValueError): [d for d in x.dist_iter(None, None, 1)] # Check error on None passed with iterable with self.assertRaises(ValueError): [d for d in x.dist_iter(None, [0,1,2], 3)]
def test_XYZ_BadUsageNoneInAngleIterAtom(self): # Confirm that an error is thrown when using angle_iter called with # 'error_invalid=True', when 'None' is passed as one of the 'at_n' # parameters. from opan.xyz import OpanXYZ # Load the test file x = OpanXYZ(path=self.file_name) # Check that error is not raised when error_invalid == False, but # *is* raised when error_invalid == True. try: [a for a in x.angle_iter(0,0,None,1, invalid_error=False)] except Exception: self.fail("Unexpected error raised in 'angle_iter' call") with self.assertRaises(ValueError): [a for a in x.angle_iter(0, 0, None, 1, invalid_error=True)]
def test_XYZ_BadUsageBadDisplIndices(self): from opan.xyz import OpanXYZ # Load the test file x = OpanXYZ(path=self.file_name) # Try invalid arguments to dist_single, in both positions self.assertRaises(IndexError, x.displ_single, 0, 1000000, 0) self.assertRaises(IndexError, x.displ_single, 0, 0, 1000000)
def test_XYZ_BadUsageNoneInAngleIterAtom(self): # Confirm that an error is thrown when using angle_iter called with # 'error_invalid=True', when 'None' is passed as one of the 'at_n' # parameters. from opan.xyz import OpanXYZ # Load the test file x = OpanXYZ(path=self.file_name) # Check that error is not raised when error_invalid == False, but # *is* raised when error_invalid == True. try: [a for a in x.angle_iter(0, 0, None, 1, invalid_error=False)] except Exception: self.fail("Unexpected error raised in 'angle_iter' call") with self.assertRaises(ValueError): [a for a in x.angle_iter(0, 0, None, 1, invalid_error=True)]
def test_XYZ_AltFileDataFirstCuAtomnum(self): from opan.xyz import OpanXYZ from opan.error import XYZError try: xyz = OpanXYZ(path=(self.file_name + self.names.Cu_1st_as_atomnum)) except XYZError: # pragma: no cover self.fail("XYZ import failed when success was expected.") self.assertEqual(xyz.atom_syms[0].upper(), self.good_direct_atoms[0])
def test_XYZ_BadUsageBadDihedIndices(self): from opan.xyz import OpanXYZ # Load the test file x = OpanXYZ(path=self.file_name) # Try invalid arguments to dihed_single, in all four positions self.assertRaises(IndexError, x.dihed_single, 0, 1000000, 1, 2, 3) self.assertRaises(IndexError, x.dihed_single, 0, 0, 1000, 2, 3) self.assertRaises(IndexError, x.dihed_single, 0, 0, 1, 20000, 3) self.assertRaises(IndexError, x.dihed_single, 0, 0, 1, 2, 3000000) # Test for catch on duplicate indices self.assertRaises(ValueError, x.dihed_single, 0, 0, 0, 0, 0)
def test_XYZ_BadUsageBadAngleIndices(self): from opan.xyz import OpanXYZ # Load the test file x = OpanXYZ(path=self.file_name) # Try invalid arguments to angle_single, in all three positions self.assertRaises(IndexError, x.angle_single, 0, 1000000, 1, 2) self.assertRaises(IndexError, x.angle_single, 0, 0, 1000000, 2) self.assertRaises(IndexError, x.angle_single, 0, 0, 1, 200000) # Try inputs where at_1 == at_2 or at_3 == at_2 self.assertRaises(ValueError, x.angle_single, 0, 1, 1, 2) self.assertRaises(ValueError, x.angle_single, 0, 1, 2, 2)
def test_XYZ_BadUsageReInitFileData(self): # Should pitch a fit if someone uses the private file method to try to # reinit an OpanXYZ instance from opan.error import XYZError from opan.xyz import OpanXYZ # Should work fine xyz = OpanXYZ(path=self.file_name) # Should raise error to re-init with same contents. self.assertErrorAndTypecode(XYZError, xyz._load_file, XYZError.OVERWRITE, XYZ_path=self.file_name)
def test_XYZ_BadUsageLinearDihedAngle(self): import os from opan.xyz import OpanXYZ from opan.error import XYZError # Construct path to benzene test file bz_path = os.path.join(os.path.pardir, 'test', 'resource', 'inertia', 'C6H6_Planar.xyz') # Load benzene test file from inside inertia x = OpanXYZ(path=bz_path) # From testing, the (6,0,3) H-C-C angle is 180 degrees self.assertErrorAndTypecode(XYZError, x.dihed_single, XYZError.DIHED, 0, 6, 0, 3, 4)
def test_XYZ_BadUsageReInitDirectData(self): # Should pitch a fit if someone uses the private data method to try to # reinit an OpanXYZ instance from opan.error import XYZError from opan.xyz import OpanXYZ # Should work fine xyz = OpanXYZ(coords=self.good_direct_geom, atom_syms=self.good_direct_atoms) # Should raise error to re-init with same contents. self.assertErrorAndTypecode(XYZError, xyz._load_data, XYZError.OVERWRITE, coords=self.good_direct_geom, atom_syms=self.good_direct_atoms)
class TestOpanXYZGoodDirectData(SuperOpanXYZ): # Confirming sanity of an OpanXYZ generated directly from data. # There are checks here of some of the generated resuls when using # 'x_iter' methods with 'None'. def setUp(self): # Load the object # Imports from opan.xyz import OpanXYZ # Create the object self.xyz = OpanXYZ(atom_syms=self.good_direct_atoms, coords=self.good_direct_geom) # Long messages self.longMessage = True def test_XYZ_GoodDirectDataNumAtoms(self): self.assertEqual(self.xyz.num_atoms, len(self.good_direct_atoms)) def test_XYZ_GoodDirectDataNumGeoms(self): self.assertEqual(self.xyz.num_geoms, 1) def test_XYZ_GoodDirectDataNumCoords(self): self.assertEqual(self.xyz.geoms[0].shape[0], self.xyz.num_atoms * 3) def test_XYZ_GoodDirectDataCoords(self): for i in range(self.good_direct_geom[0].shape[0]): self.assertAlmostEqual(self.xyz.geoms[0][i], self.good_direct_geom[0][i], delta=1e-8, msg="Coordinates element (" + str(i) + ')') def test_XYZ_GoodDirectDataIterO1Dist(self): for tup in zip( self.good_direct_O1Dist, self.xyz.dist_iter(0,1,None), range(self.good_direct_O1Dist.shape[0]) ): self.assertAlmostEqual(tup[0], tup[1], delta=1e-5, msg="Distance between O1 and atom #" + str(tup[2]) + " (" + self.xyz.atom_syms[tup[2]].capitalize() + ")") def test_XYZ_GoodDirectDataIterCuO1Angle(self): for tup in zip( self.good_direct_CuO1Angle, self.xyz.angle_iter(0,0,1,None), range(self.good_direct_CuO1Angle.shape[0]) ): self.assertAlmostEqual(tup[0], tup[1], delta=5e-3, msg="Angle Cu-O-X with atom #" + str(tup[2]) + " (" + self.xyz.atom_syms[tup[2]].capitalize() + ")") def test_XYZ_GoodDirectDataIterDihed(self): for tup in zip( self.good_direct_Dihed, self.xyz.dihed_iter(0,None, 1,0,2), range(self.good_direct_Dihed.shape[0]) ): self.assertAlmostEqual(tup[0], tup[1], delta=5e-3, msg="Dihedral with atom #" + str(tup[2]) + " (" + self.xyz.atom_syms[tup[2]].capitalize() + ")")
class TestOpanXYZGoodFileData(SuperOpanXYZ): # Ensuring importing a known OpenBabel xyz file with good data reports # the correct geometric parameters, etc. @classmethod def setUpClass(cls): from opan.test.utils import setUpTestDir # Set up the directory and add the good file setUpTestDir(cls.testdir) # Write the file with open(cls.file_name, 'w') as f: f.write(cls.file_text_good) @classmethod def tearDownClass(cls): import os from opan.test.utils import tearDownTestDir # Delete the xyz file os.remove(cls.file_name) # Remove the test directory tearDownTestDir(cls.testdir) def setUp(self): # Load the object # Imports from opan.xyz import OpanXYZ # Create the object self.xyz = OpanXYZ(path=self.file_name) # Long messages self.longMessage = True def test_XYZ_GoodFileDataNumAtoms(self): self.assertEqual(self.xyz.num_atoms, self.num_atoms) def test_XYZ_GoodFileDataNumGeoms(self): self.assertEqual(self.xyz.num_geoms, self.num_geoms) def test_XYZ_GoodFileDataAtomSyms(self): for i in range(len(self.xyz.atom_syms)): self.assertEqual(self.xyz.atom_syms[i], self.atom_syms[i], msg="Coordinates element (" + str(i) + ')') def test_XYZ_GoodFileDataCoords(self): for g in range(len(self.geoms)): for i in range(self.geoms[g].shape[0]): self.assertAlmostEqual(self.xyz.geoms[g][i], self.geoms[g][i], delta=1e-6, msg="Geometry #" + str(g) + ", coordinate element #" + str(i)) def test_XYZ_GoodFileDataDistances(self): for t in zip(self.dist_Cu_O, self.xyz.dist_iter(None, 0, 1), range(self.dist_Cu_O.shape[0])): self.assertAlmostEqual(t[0], t[1], delta=1e-5, msg="Cu-O distance mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) for t in zip(self.dist_O_H1, self.xyz.dist_iter(None, 1, 2), range(self.dist_O_H1.shape[0])): self.assertAlmostEqual(t[0], t[1], delta=1e-5, msg="O-H1 distance mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) def test_XYZ_GoodFileDataAngles(self): for t in zip(self.angle_Cu_O_H1, self.xyz.angle_iter(None, 0, 1, 2), range(self.angle_Cu_O_H1.shape[0])): self.assertAlmostEqual(t[0], t[1], delta=1e-2, msg="Cu-O-H1 angle mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) def test_XYZ_GoodFileDataDihedrals(self): for t in zip(self.dihed_H2_O_Cu_H1, self.xyz.dihed_iter(None, 3, 1, 0, 2), range(self.dihed_H2_O_Cu_H1.shape[0])): self.assertAlmostEqual( t[0], t[1], delta=1e-2, msg="H2-Cu-O-H1 dihedral mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) def test_XYZ_GoodFileDataIterGeom(self): import numpy as np idxs = [1, 4, 8] for t in zip( np.array(self.geoms)[idxs], self.xyz.geom_iter(idxs), range(len(idxs))): for i in range(t[0].shape[0]): self.assertAlmostEqual(t[0][i], t[1][i], delta=1e-6, msg="Geometry #" + str(t[2]) + \ ", coordinate element #" + str(i)) def test_XYZ_GoodFileDataDisplacements(self): for t in zip(self.displ_Cu_O, self.xyz.displ_iter(None, 0, 1), range(len(self.displ_Cu_O))): for i in range(self.displ_Cu_O[0].shape[0]): self.assertAlmostEqual( t[0][i], t[1][i], delta=1e-6, msg="Displacement #{0}, dimension #{1}".format(t[2], i)) def test_XYZ_GoodFileDataZeroDisplacement(self): # Have to use assertTrue b/c numpy confuses assertEqual &c. self.assertTrue(all(self.xyz.displ_single(0, 0, 0) == [0.0, 0.0, 0.0]))
class TestOpanXYZGoodDirectData(SuperOpanXYZ): # Confirming sanity of an OpanXYZ generated directly from data. # There are checks here of some of the generated resuls when using # 'x_iter' methods with 'None'. def setUp(self): # Load the object # Imports from opan.xyz import OpanXYZ # Create the object self.xyz = OpanXYZ(atom_syms=self.good_direct_atoms, coords=self.good_direct_geom) # Long messages self.longMessage = True def test_XYZ_GoodDirectDataNumAtoms(self): self.assertEqual(self.xyz.num_atoms, len(self.good_direct_atoms)) def test_XYZ_GoodDirectDataNumGeoms(self): self.assertEqual(self.xyz.num_geoms, 1) def test_XYZ_GoodDirectDataNumCoords(self): self.assertEqual(self.xyz.geoms[0].shape[0], self.xyz.num_atoms * 3) def test_XYZ_GoodDirectDataCoords(self): for i in range(self.good_direct_geom[0].shape[0]): self.assertAlmostEqual(self.xyz.geoms[0][i], self.good_direct_geom[0][i], delta=1e-8, msg="Coordinates element (" + str(i) + ')') def test_XYZ_GoodDirectDataIterO1Dist(self): for tup in zip(self.good_direct_O1Dist, self.xyz.dist_iter(0, 1, None), range(self.good_direct_O1Dist.shape[0])): self.assertAlmostEqual( tup[0], tup[1], delta=1e-5, msg="Distance between O1 and atom #" + str(tup[2]) + " (" + self.xyz.atom_syms[tup[2]].capitalize() + ")") def test_XYZ_GoodDirectDataIterCuO1Angle(self): for tup in zip(self.good_direct_CuO1Angle, self.xyz.angle_iter(0, 0, 1, None), range(self.good_direct_CuO1Angle.shape[0])): self.assertAlmostEqual( tup[0], tup[1], delta=5e-3, msg="Angle Cu-O-X with atom #" + str(tup[2]) + " (" + self.xyz.atom_syms[tup[2]].capitalize() + ")") def test_XYZ_GoodDirectDataIterDihed(self): for tup in zip(self.good_direct_Dihed, self.xyz.dihed_iter(0, None, 1, 0, 2), range(self.good_direct_Dihed.shape[0])): self.assertAlmostEqual( tup[0], tup[1], delta=5e-3, msg="Dihedral with atom #" + str(tup[2]) + " (" + self.xyz.atom_syms[tup[2]].capitalize() + ")")
class TestOpanXYZGoodFileData(SuperOpanXYZ): # Ensuring importing a known OpenBabel xyz file with good data reports # the correct geometric parameters, etc. @classmethod def setUpClass(cls): from opan.test.utils import setUpTestDir # Set up the directory and add the good file setUpTestDir(cls.testdir) # Write the file with open(cls.file_name, 'w') as f: f.write(cls.file_text_good) @classmethod def tearDownClass(cls): import os from opan.test.utils import tearDownTestDir # Delete the xyz file os.remove(cls.file_name) # Remove the test directory tearDownTestDir(cls.testdir) def setUp(self): # Load the object # Imports from opan.xyz import OpanXYZ # Create the object self.xyz = OpanXYZ(path=self.file_name) # Long messages self.longMessage = True def test_XYZ_GoodFileDataNumAtoms(self): self.assertEqual(self.xyz.num_atoms, self.num_atoms) def test_XYZ_GoodFileDataNumGeoms(self): self.assertEqual(self.xyz.num_geoms, self.num_geoms) def test_XYZ_GoodFileDataAtomSyms(self): for i in range(len(self.xyz.atom_syms)): self.assertEqual(self.xyz.atom_syms[i], self.atom_syms[i], msg="Coordinates element (" + str(i) + ')') def test_XYZ_GoodFileDataCoords(self): for g in range(len(self.geoms)): for i in range(self.geoms[g].shape[0]): self.assertAlmostEqual(self.xyz.geoms[g][i], self.geoms[g][i], delta=1e-6, msg="Geometry #" + str(g) + ", coordinate element #" + str(i)) def test_XYZ_GoodFileDataDistances(self): for t in zip(self.dist_Cu_O, self.xyz.dist_iter(None, 0, 1), range(self.dist_Cu_O.shape[0])): self.assertAlmostEqual(t[0], t[1], delta=1e-5, msg="Cu-O distance mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) for t in zip(self.dist_O_H1, self.xyz.dist_iter(None, 1, 2), range(self.dist_O_H1.shape[0])): self.assertAlmostEqual(t[0], t[1], delta=1e-5, msg="O-H1 distance mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) def test_XYZ_GoodFileDataAngles(self): for t in zip(self.angle_Cu_O_H1, self.xyz.angle_iter(None, 0, 1, 2), range(self.angle_Cu_O_H1.shape[0])): self.assertAlmostEqual(t[0], t[1], delta=1e-2, msg="Cu-O-H1 angle mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) def test_XYZ_GoodFileDataDihedrals(self): for t in zip(self.dihed_H2_O_Cu_H1, self.xyz.dihed_iter(None, 3, 1, 0, 2), range(self.dihed_H2_O_Cu_H1.shape[0])): self.assertAlmostEqual(t[0], t[1], delta=1e-2, msg="H2-Cu-O-H1 dihedral mismatch at geom #" + str(t[2]) + ": " + str(t[0:2])) def test_XYZ_GoodFileDataIterGeom(self): import numpy as np idxs = [1,4,8] for t in zip(np.array(self.geoms)[idxs], self.xyz.geom_iter(idxs), range(len(idxs)) ): for i in range(t[0].shape[0]): self.assertAlmostEqual(t[0][i], t[1][i], delta=1e-6, msg="Geometry #" + str(t[2]) + \ ", coordinate element #" + str(i)) def test_XYZ_GoodFileDataDisplacements(self): for t in zip(self.displ_Cu_O, self.xyz.displ_iter(None, 0, 1), range(len(self.displ_Cu_O)) ): for i in range(self.displ_Cu_O[0].shape[0]): self.assertAlmostEqual(t[0][i], t[1][i], delta=1e-6, msg="Displacement #{0}, dimension #{1}".format(t[2], i)) def test_XYZ_GoodFileDataZeroDisplacement(self): # Have to use assertTrue b/c numpy confuses assertEqual &c. self.assertTrue(all(self.xyz.displ_single(0,0,0) == [0.0, 0.0, 0.0]))