class TestFindRotation(unittest.TestCase): def setUp(self): self.natoms = 11 # self.x0 = _utils.random_configuration(3*11) # self.x1 = _utils.random_configuration(3*11) # self.policy = TransformPolicyAtomicCluster() self.measure = MeasureCartesian() # # self.rot = findrotation_kabsch(self.x0, self.x1, align_com=True) def basic_test(self, x0, x1): x0bk = x0.copy() x1bk = x1.copy() rot = findrotation_kabsch(x0, x1) # rot = findrotation_kearsley(x0, x1) self.assertTrue((x0 == x0bk).all()) self.assertTrue((x1 == x1bk).all()) self.policy.rotate(x1, rot) self.assertLessEqual(self.measure.get_dist(x0, x1), self.measure.get_dist(x0bk, x1bk)) def test1(self): """align two random configurations""" x0 = _utils.random_configuration(3 * self.natoms) x1 = _utils.random_configuration(3 * self.natoms) # subtract center of mass com = self.measure.get_com(x0) self.policy.translate(x0, -com) com = self.measure.get_com(x1) self.policy.translate(x1, -com) self.basic_test(x0, x1) def test2(self): """align two configurations that should match exactly """ x0 = _utils.random_configuration(3 * self.natoms) com = self.measure.get_com(x0) self.policy.translate(x0, -com) x1 = x0.copy() self.policy.rotate(x1, _utils.random_rotation()) self.basic_test(x0, x1) self.assertLess(np.abs(x0 - x1).max(), 1e-3)
class TestFindRotation(unittest.TestCase): def setUp(self): self.natoms = 11 # self.x0 = _utils.random_configuration(3*11) # self.x1 = _utils.random_configuration(3*11) # self.policy = TransformPolicyAtomicCluster() self.measure = MeasureCartesian() # # self.rot = findrotation_kabsch(self.x0, self.x1, align_com=True) def basic_test(self, x0, x1): x0bk = x0.copy() x1bk = x1.copy() rot = findrotation_kabsch(x0, x1) # rot = findrotation_kearsley(x0, x1) self.assertTrue((x0 == x0bk).all()) self.assertTrue((x1 == x1bk).all()) self.policy.rotate(x1, rot) self.assertLessEqual(self.measure.get_dist(x0, x1), self.measure.get_dist(x0bk, x1bk)) def test1(self): """align two random configurations""" x0 = _utils.random_configuration(3 * self.natoms) x1 = _utils.random_configuration(3 * self.natoms) # subtract center of mass com = self.measure.get_com(x0) self.policy.translate(x0, -com) com = self.measure.get_com(x1) self.policy.translate(x1, -com) self.basic_test(x0, x1) def test2(self): """align two configurations that should match exactly """ x0 = _utils.random_configuration(3 * self.natoms) com = self.measure.get_com(x0) self.policy.translate(x0, -com) x1 = x0.copy() self.policy.rotate(x1, _utils.random_rotation()) self.basic_test(x0, x1) self.assertLess(np.abs(x0-x1).max(), 1e-3)
class TestTransformation(unittest.TestCase): def setUp(self): self.natoms = 11 self.policy = TransformPolicyAtomicCluster() self.transform_list = [self.invert, self.rrot, self.rtrans, self.rperm] def rrot(self, tform, coords): """apply random rotation to both coords and transformation object""" mx = _utils.random_mx() self.policy.rotate(coords, mx) tform.rotate(mx) def rtrans(self, tform, coords): """apply random translation to both coords and transformation object""" xyz = _utils.random_translation() self.policy.translate(coords, xyz) tform.translate(xyz) def invert(self, tform, coords): """apply inversion to both coords and transformation object""" tform.invert() self.policy.invert(coords) def rperm(self, tform, coords): """apply random permutation to both coords and transformation object""" perm = _utils.random_permutation(len(coords) / 3) tform.permute(perm) self.policy.permute(coords, perm) def apply_tests(self, f): """test that that a transofrmation object can reproduce the transformation in f""" x = _utils.random_configuration(self.natoms * 3) tform = TransformCluster3D(nsites=self.natoms) xbkup = x.copy() x2 = x.copy() # do transormation f(tform, x) tform.apply(x2) # x2 and x should be the same self.assertLess(np.abs(x - x2).max(), 1e-3) def fchain(self, flist): """return a function which applies all of the functions in flist to the input""" def function_chain(tform, x): for f in reversed(flist): # print f.__name__ f(tform, x) return function_chain def test_1(self): """run do_tests() on transformations """ for f in self.transform_list: self.apply_tests(f) def test_2(self): """run do_tests() on all combinations of length 2 of the transformations """ for flist in itertools.product(self.transform_list, repeat=2): tform = self.fchain(flist) self.apply_tests(tform) def test_3(self): """run do_tests() on all combinations of length 3 of the transformations """ for flist in itertools.product(self.transform_list, repeat=3): tform = self.fchain(flist) self.apply_tests(tform) def test_4(self): """run do_tests() on all combinations of length 3 of the transformations """ for flist in itertools.product(self.transform_list, repeat=4): tform = self.fchain(flist) self.apply_tests(tform) def test_10(self): """run do_tests() on combinations of length 10 of the transformations """ maxiter = 2000 i = 0 for flist in itertools.product(self.transform_list, repeat=10): tform = self.fchain(flist) self.apply_tests(tform) i += 1 if i > maxiter: break
class TestTransformation(unittest.TestCase): def setUp(self): self.natoms = 11 self.policy = TransformPolicyAtomicCluster() self.transform_list = [self.invert, self.rrot, self.rtrans, self.rperm] def rrot(self, tform, coords): """apply random rotation to both coords and transformation object""" mx = _utils.random_mx() self.policy.rotate(coords, mx) tform.rotate(mx) def rtrans(self, tform, coords): """apply random translation to both coords and transformation object""" xyz = _utils.random_translation() self.policy.translate(coords, xyz) tform.translate(xyz) def invert(self, tform, coords): """apply inversion to both coords and transformation object""" tform.invert() self.policy.invert(coords) def rperm(self, tform, coords): """apply random permutation to both coords and transformation object""" perm = _utils.random_permutation(len(coords) / 3) tform.permute(perm) self.policy.permute(coords, perm) def apply_tests(self, f): """test that that a transofrmation object can reproduce the transformation in f""" x = _utils.random_configuration(self.natoms * 3) tform = TransformCluster3D(nsites=self.natoms) xbkup = x.copy() x2 = x.copy() # do transormation f(tform, x) tform.apply(x2) #x2 and x should be the same self.assertLess(np.abs(x - x2).max(), 1e-3) def fchain(self, flist): """return a function which applies all of the functions in flist to the input""" def function_chain(tform, x): for f in reversed(flist): # print f.__name__ f(tform, x) return function_chain def test_1(self): """run do_tests() on transformations """ for f in self.transform_list: self.apply_tests(f) def test_2(self): """run do_tests() on all combinations of length 2 of the transformations """ for flist in itertools.product(self.transform_list, repeat=2): tform = self.fchain(flist) self.apply_tests(tform) def test_3(self): """run do_tests() on all combinations of length 3 of the transformations """ for flist in itertools.product(self.transform_list, repeat=3): tform = self.fchain(flist) self.apply_tests(tform) def test_4(self): """run do_tests() on all combinations of length 3 of the transformations """ for flist in itertools.product(self.transform_list, repeat=4): tform = self.fchain(flist) self.apply_tests(tform) def test_10(self): """run do_tests() on combinations of length 10 of the transformations """ maxiter = 2000 i = 0 for flist in itertools.product(self.transform_list, repeat=10): tform = self.fchain(flist) self.apply_tests(tform) i += 1 if i > maxiter: break