Пример #1
0
    def testSortCoordinatesDistance(self):
        """ Test the coordinate sorting wrt distances function. """
        # Lets, use these coordinates.
        coords = numpy.array([[2.0, 3.0, 0.0], [1.0, 2.0, 0.0],
                              [0.0, 0.0, 0.0], [-8.9, 12.0, 4.0]])
        types1 = ["B", "A", "C", "A"]
        types2 = ["D", "G", "C", "V"]

        # -----------------------------------------------------------------------------
        # Let the 3rd coordinates be the center.
        center = 2

        # Sort wrt distance, type, x, y, z
        sorted_coords, sorted_distances, sorted_types1, sorted_types2 = \
            sortCoordinatesDistance(coords, center, types1, types2)

        # Setup reference.
        ref_coords = numpy.array([[0.0, 0.0, 0.0],
                                  [1.0, 2.0, 0.0],
                                  [2.0, 3.0, 0.0],
                                  [-8.9, 12.0, 4.0]])
        ref_types1 = ["C", "A", "B", "A"]
        ref_types2 = ["C", "G", "D", "V"]
        ref_distances = [0.0, 2.2360679774997898, 3.6055512754639891, 15.46641522784126]

        # Check.
        self.assertTrue(numpy.allclose(sorted_coords, ref_coords))
        self.assertTrue(numpy.allclose(sorted_distances, ref_distances))
        self.assertListEqual(sorted_types1, ref_types1)
        self.assertListEqual(sorted_types2, ref_types2)

        # -----------------------------------------------------------------------------
        # Let the 1st coordinates be the center.
        center = 0

        # Sort wrt distance, type, x, y, z
        sorted_coords, sorted_distances, sorted_types1, sorted_types2 = \
            sortCoordinatesDistance(coords, center, types1, types2)

        # Setup reference.
        ref_coords = numpy.array([[2.0, 3.0, 0.0],
                                  [1.0, 2.0, 0.0],
                                  [0.0, 0.0, 0.0],
                                  [-8.9, 12.0, 4.0]])
        ref_types1 = ["B", "A", "C", "A"]
        ref_types2 = ["D", "G", "C", "V"]
        ref_distances = [0.0, 1.4142135623730951, 3.6055512754639891, 14.690473103341498]

        # Check.
        self.assertTrue(numpy.allclose(sorted_coords, ref_coords))
        self.assertTrue(numpy.allclose(sorted_distances, ref_distances))
        self.assertListEqual(sorted_types1, ref_types1)
        self.assertListEqual(sorted_types2, ref_types2)
Пример #2
0
    def testSortCoordinatesDistance(self):
        """ Test the coordinate sorting wrt distances function. """
        # Lets, use these coordinates.
        coords = numpy.array([[2.0, 3.0, 0.0], [1.0, 2.0, 0.0],
                              [0.0, 0.0, 0.0], [-8.9, 12.0, 4.0]])
        types1 = ["B", "A", "C", "A"]
        types2 = ["D", "G", "C", "V"]

        # -----------------------------------------------------------------------------
        # Let the 3rd coordinates be the center.
        center = 2

        # Sort wrt distance, type, x, y, z
        sorted_coords, sorted_distances, sorted_types1, sorted_types2 = \
            sortCoordinatesDistance(coords, center, types1, types2)

        # Setup reference.
        ref_coords = numpy.array([[0.0, 0.0, 0.0], [1.0, 2.0, 0.0],
                                  [2.0, 3.0, 0.0], [-8.9, 12.0, 4.0]])
        ref_types1 = ["C", "A", "B", "A"]
        ref_types2 = ["C", "G", "D", "V"]
        ref_distances = [
            0.0, 2.2360679774997898, 3.6055512754639891, 15.46641522784126
        ]

        # Check.
        self.assertTrue(numpy.allclose(sorted_coords, ref_coords))
        self.assertTrue(numpy.allclose(sorted_distances, ref_distances))
        self.assertListEqual(sorted_types1, ref_types1)
        self.assertListEqual(sorted_types2, ref_types2)

        # -----------------------------------------------------------------------------
        # Let the 1st coordinates be the center.
        center = 0

        # Sort wrt distance, type, x, y, z
        sorted_coords, sorted_distances, sorted_types1, sorted_types2 = \
            sortCoordinatesDistance(coords, center, types1, types2)

        # Setup reference.
        ref_coords = numpy.array([[2.0, 3.0, 0.0], [1.0, 2.0, 0.0],
                                  [0.0, 0.0, 0.0], [-8.9, 12.0, 4.0]])
        ref_types1 = ["B", "A", "C", "A"]
        ref_types2 = ["D", "G", "C", "V"]
        ref_distances = [
            0.0, 1.4142135623730951, 3.6055512754639891, 14.690473103341498
        ]

        # Check.
        self.assertTrue(numpy.allclose(sorted_coords, ref_coords))
        self.assertTrue(numpy.allclose(sorted_distances, ref_distances))
        self.assertListEqual(sorted_types1, ref_types1)
        self.assertListEqual(sorted_types2, ref_types2)
Пример #3
0
    def __sortCoordinatesElementsAndMoveVectors(self):
        """
        Private helper to sort the validated coordinate input,
        and update the element order and move_vector indexing accordingly.
        """
        # Set up the original indexing.
        original_indexing = range(len(self._coordinates))

        # Sort, and co-sort coordinates and indices.
        (sorted_coords, dummy_distances, sorted_types_before, sorted_types_after, new_to_old_index) = \
            sortCoordinatesDistance(coordinates = self._coordinates,
                                    center      = 0,
                                    types1      = self._elements_before,
                                    types2      = self._elements_after,
                                    co_sort     = original_indexing)

        # Fix the move vectors.
        if len(self._move_vectors) > 0:
            old_to_new_index = []
            for i in range(len(new_to_old_index)):
                old_to_new_index.append(new_to_old_index.index(i))

            # Fixt the move vector indexing.
            move_vector_index = []
            for v in self._move_vectors:
                move_vector_index.append(old_to_new_index[v[0]])

            # Setup and sort the backmapping.
            help_index = range(len(move_vector_index))
            to_sort = numpy.array(zip(help_index, move_vector_index))
            sorted_indices = to_sort[numpy.argsort(to_sort[:, 1])]

            # Construct the new move vectors.
            new_move_vectors = []
            for idx in sorted_indices:
                new_move_vectors.append(
                    (idx[1], self._move_vectors[idx[0]][1]))

            # Set the move vectors.
            self._move_vectors = new_move_vectors

        # Set the new data on the class.
        self._coordinates = sorted_coords
        self._elements_before = sorted_types_before
        self._elements_after = sorted_types_after
Пример #4
0
    def __sortCoordinatesElementsAndMoveVectors(self):
        """
        Private helper to sort the validated coordinate input,
        and update the element order and move_vector indexing accordingly.
        """
        # Set up the original indexing.
        original_indexing = range(len(self.__coordinates))

        # Sort, and co-sort coordinates and indices.
        (sorted_coords, dummy_distances, sorted_types_before, sorted_types_after, new_to_old_index) = \
            sortCoordinatesDistance(coordinates = self.__coordinates,
                                    center      = 0,
                                    types1      = self.__elements_before,
                                    types2      = self.__elements_after,
                                    co_sort     = original_indexing)

        # Fix the move vectors.
        if len(self.__move_vectors) > 0:
            old_to_new_index = []
            for i in range(len(new_to_old_index)):
                old_to_new_index.append(new_to_old_index.index(i))

            # Fixt the move vector indexing.
            move_vector_index = []
            for v in self.__move_vectors:
                move_vector_index.append( old_to_new_index[v[0]] )

            # Setup and sort the backmapping.
            help_index = range(len(move_vector_index))
            to_sort = numpy.array(zip(help_index, move_vector_index))
            sorted_indices = to_sort[numpy.argsort(to_sort[:,1])]

            # Construct the new move vectors.
            new_move_vectors = []
            for idx in sorted_indices:
                new_move_vectors.append( (idx[1], self.__move_vectors[idx[0]][1]) )

            # Set the move vectors.
            self.__move_vectors = new_move_vectors

        # Set the new data on the class.
        self.__coordinates = sorted_coords
        self.__elements_before = sorted_types_before
        self.__elements_after = sorted_types_after
Пример #5
0
    def testSortCoordinatesDistance(self):
        """ Test the coordinate sorting wrt distances function. """
        # Lets, use these coordinates.
        coords = numpy.array([[2.0,1.0,0.0],[1.0,2.0,0.0],[0.0,0.0,0.0],[-8.9,12.0,4.0]])
        types  = ["B","A","C","A"]
        center = 2

        # Sort - wrt distance,type,x,y,z
        (sorted_coords, sorted_distances, sorted_types, co_sorted_types) = sortCoordinatesDistance(coords, center, types)

        # Setup references.
        ref_coords = numpy.array([[0.0,0.0,0.0],[1.0,2.0,0.0],[2.0,1.0,0.0],[-8.9,12.0,4.0]])
        ref_types = ["C","A","B","A"]
        ref_distances=numpy.array([numpy.linalg.norm(c) for c in ref_coords])

        # Check.
        self.assertAlmostEqual(numpy.linalg.norm(sorted_distances-ref_distances), 0.0, 10)
        self.assertAlmostEqual(numpy.linalg.norm(sorted_coords-ref_coords), 0.0, 10)
        self.assertEqual(sorted_types, ref_types)
    def testSortCoordinatesDistance(self):
        """ Test the coordinate sorting wrt distances function. """
        # Lets, use these coordinates.
        coords = numpy.array([[2.0, 1.0, 0.0], [1.0, 2.0, 0.0],
                              [0.0, 0.0, 0.0], [-8.9, 12.0, 4.0]])
        types = ["B", "A", "C", "A"]
        center = 2

        # Sort - wrt distance,type,x,y,z
        (sorted_coords, sorted_distances, sorted_types,
         co_sorted_types) = sortCoordinatesDistance(coords, center, types)

        # Setup references.
        ref_coords = numpy.array([[0.0, 0.0, 0.0], [1.0, 2.0, 0.0],
                                  [2.0, 1.0, 0.0], [-8.9, 12.0, 4.0]])
        ref_types = ["C", "A", "B", "A"]
        ref_distances = numpy.array([numpy.linalg.norm(c) for c in ref_coords])

        # Check.
        self.assertAlmostEqual(
            numpy.linalg.norm(sorted_distances - ref_distances), 0.0, 10)
        self.assertAlmostEqual(numpy.linalg.norm(sorted_coords - ref_coords),
                               0.0, 10)
        self.assertEqual(sorted_types, ref_types)