Пример #1
0
    def test_superpose(self):
        # create a few test sets with random data points, including degenerate
        # situations. (e.g. one point, two points, linear points)
        references = [  # a list of 2-tuples: (points, degenerate)
            (numpy.random.normal(0, 5, (n, 3)), False) for n in xrange(4, 40)
        ] + [
            #(numpy.array([[0,0,1]], float), True),
            #(numpy.array([[0,0,0],[0,0,1]], float), True),
            #(numpy.array([[0,0,0],[0,0,1],[0,0,2]], float), True),
            #(numpy.array([[0,0,0],[0,0,1],[0,0,2],[0,0,4]], float), True),
            #(numpy.random.normal(0, 5, (3, 3)), True)
        ]

        # Do a random transformation on the points
        randomized = []
        for points, degenerate in references:
            #points[:] -= points.mean(axis=0)
            axis = random_unit(3)
            angle = numpy.random.uniform(0, numpy.pi * 2)
            transformation = Complete()
            transformation.set_rotation_properties(angle, axis, False)
            transformation.t[:] = numpy.random.normal(0, 5, 3)
            randomized.append(
                (numpy.array([transformation.vector_apply(p)
                              for p in points]), transformation))

        for (ref_points, degenerate), (tr_points,
                                       transf) in zip(references, randomized):
            check_transf = superpose(ref_points, tr_points)
            # check whether the rotation matrix is orthogonal
            self.assertArraysAlmostEqual(
                numpy.dot(check_transf.r, check_transf.r.transpose()),
                numpy.identity(3, float), 1e-5)
            # first check whether check_transf brings the tr_points back to the ref_points
            check_points = numpy.dot(
                tr_points, check_transf.r.transpose()) + check_transf.t
            self.assertArraysAlmostEqual(ref_points,
                                         check_points,
                                         1e-5,
                                         doabs=True)
            if not degenerate:
                # if the set of points is not degenerate, check whether transf and check_transf
                # are each other inverses
                tmp = Complete()
                tmp.apply_before(transf)
                tmp.apply_before(check_transf)
                self.assertArraysAlmostEqual(
                    numpy.dot(transf.r, check_transf.r),
                    numpy.identity(3, float), 1e-5)
                self.assertArrayAlmostZero(tmp.t, 1e-5)

        # Add some distortion to the transformed points
        randomized = []
        for tr_points, transf in randomized:
            tr_points[:] += numpy.random.normal(0, 1.0, len(tr_points))

        # Do a blind test
        for (ref_points, degenerate), (tr_points,
                                       transf) in zip(references, randomized):
            superpose(ref_points, tr_points)
Пример #2
0
 def setUp(self):
     self.test_transformations = []
     for i in xrange(20):
         test_transformation = Complete()
         test_transformation.set_rotation_properties(
             random.random() * math.pi * 2, numpy.random.uniform(-3, 3, 3),
             random.sample([True, False], 1)[0])
         test_transformation.t = numpy.random.uniform(-3, 3, 3)
         self.test_transformations.append(test_transformation)
Пример #3
0
    def __init__(self, affected_atoms, transformation, molecule=None):
        self.affected_atoms = affected_atoms
        self.transformation = Complete()
        if isinstance(transformation, Rotation):
            self.transformation.r[:] = transformation.r
        if isinstance(transformation, Translation):
            self.transformation.t[:] = transformation.t

        if molecule is not None:
            for i in affected_atoms:
                molecule.coordinates[i] = transformation.vector_apply(
                    molecule.coordinates[i])
Пример #4
0
 def read_from_file(cls, filename):
     """Construct a MolecularDistortion object from a file"""
     with open(filename) as f:
         lines = list(line for line in f if line[0] != '#')
     r = []
     t = []
     for line in lines[:3]:
         values = list(float(word) for word in line.split())
         r.append(values[:3])
         t.append(values[3])
     transformation = Complete(r, t)
     affected_atoms = set(int(word) for word in lines[3].split())
     return cls(affected_atoms, transformation)
Пример #5
0
 def read_from_file(cls, filename):
     f = file(filename)
     lines = list(line for line in f if line[0] != '#')
     f.close()
     r = []
     t = []
     for line in lines[:3]:
         values = list(float(word) for word in line.split())
         r.append(values[:3])
         t.append(values[3])
     transformation = Complete()
     transformation.r[:] = r
     transformation.t[:] = t
     affected_atoms = set(int(word) for word in lines[3].split())
     return cls(affected_atoms, transformation)
Пример #6
0
    def do_test(self, coordinates, masses, expected_is):
        reference = MolecularDescriptorTV1(coordinates, masses)
        self.assert_(reference.inversion_symmetric == expected_is)

        for counter in xrange(3):
            transformation = Complete()
            transformation.set_rotation_properties(
                random.uniform(0, 2 * math.pi),
                numpy.random.uniform(-1, 1, (3, )),
                False,
            )
            transformation.t = numpy.random.uniform(-5, 5, (3, ))
            new_coordinates = numpy.array([
                transformation.vector_apply(coordinate)
                for coordinate in coordinates
            ], float)
            new_descriptor = MolecularDescriptorTV1(new_coordinates, masses)
            self.assert_(reference.compare_structure(new_descriptor))
            self.assert_(not reference.compare_global_rotation(new_descriptor))
            self.assert_(
                not reference.compare_global_translation(new_descriptor))
Пример #7
0
 def compute_transformation(self, connection):
     transformation = Complete(self.rotation2.r, connection.t)
     del connection.t
     return transformation