Exemplo n.º 1
0
    def test_get_cluster_min_max_distances(self):
        # First test
        distance_matrix = CondensedMatrix([1., 0.7,
                                               0.3])

        decomposed_cluster = {"traj_0":[0],"traj_1":[1],"traj_2":[2]}

        expected_min_d, expected_max_d = [ 0.7, 0.3, 0.3], [ 1., 1., 0.7]

        min_d, max_d = OverlapCalculator.get_cluster_min_max_distances(decomposed_cluster, distance_matrix)

        numpy.testing.assert_array_almost_equal(min_d, expected_min_d, 8)
        numpy.testing.assert_array_almost_equal(max_d, expected_max_d, 8)

        #Second test
        distance_matrix = CondensedMatrix([1., 0.7, 2.,
                                               0.3, 1.,
                                                    0.7])


        decomposed_cluster = {"traj_0":[0,3],"traj_1":[1],"traj_2":[2]}

        expected_min_d, expected_max_d = [ 0.7,  0.7,  0.3,  0.3], [ 1., 1., 1.,  0.7]

        min_d, max_d =  OverlapCalculator.get_cluster_min_max_distances(decomposed_cluster, distance_matrix)

        numpy.testing.assert_array_almost_equal(min_d, expected_min_d, 8)
        numpy.testing.assert_array_almost_equal(max_d, expected_max_d, 8)
Exemplo n.º 2
0
    def test_item_access(self):
        condensed_matrix_1 = CondensedMatrix([1.0, 4.5, 7.2, 8.5, 4.5, 7.8])

        condensed_matrix_2 = CondensedMatrix([.0] * 6)

        complete_matrix = [[0.0, 1.0, 4.5, 7.2], [1.0, 0.0, 8.5, 4.5],
                           [4.5, 8.5, 0.0, 7.8], [7.2, 4.5, 7.8, 0.0]]

        row_len = condensed_matrix_1.row_length

        for i in range(row_len):
            for j in range(row_len):
                condensed_matrix_2[i, j] = complete_matrix[i][j]

        ## The access for a complete and a condensed matrix is exactly the same
        for i in range(row_len):
            for j in range(row_len):
                self.assertAlmostEqual(
                    condensed_matrix_1[i, j], complete_matrix[i][j],
                    6)  #-> there are some errors, as it stores floats instead
                # of doubles

        ## And we can build a condensed matrix as a complete matrix
        self.assertItemsEqual(condensed_matrix_1.get_data(),
                              condensed_matrix_2.get_data())
Exemplo n.º 3
0
    def data_section(self, parameters):
        """
        MPI implementation of the data protocol.
        """
        matrix_parameters = parameters["data"]["matrix"]

        self.load_trajectory(parameters)
        self.comm.Barrier()

        if self.rank == 0:
            self.calculate_matrix(parameters)
        else:
            self.matrixHandler = MatrixHandler(matrix_parameters)
            self.matrixHandler.distance_matrix = CondensedMatrix(
                [0.])  # fake matrix
        self.comm.Barrier()

        matrix_contents = list(
            self.comm.bcast(self.matrixHandler.distance_matrix.get_data(),
                            root=0))
        if self.rank != 0:
            self.matrixHandler.distance_matrix = CondensedMatrix(
                matrix_contents)

        if self.nprocs > 1:

            if self.rank == 0:
                if "filename" in matrix_parameters:
                    self.save_matrix(parameters)

            if self.rank == 1:
                if "image" in matrix_parameters:
                    self.plot_matrix(parameters)
        self.comm.Barrier()
Exemplo n.º 4
0
 def test_list_creation(self):
     MAX_ELEMENTS = 30
     DATA_LEN = (MAX_ELEMENTS * (MAX_ELEMENTS-1))/2
     numpy_matrix_data = numpy.abs(numpy.random.rand(DATA_LEN))
     np_matrix = CondensedMatrix(numpy_matrix_data)
     ls_matrix = CondensedMatrix(list(numpy_matrix_data))
     self.assertEqual(ls_matrix.row_length, MAX_ELEMENTS)
     self.assertEqual(np_matrix.row_length, MAX_ELEMENTS)
     numpy.testing.assert_almost_equal(numpy_matrix_data, ls_matrix.get_data(), 7)
     numpy.testing.assert_almost_equal(numpy_matrix_data, np_matrix.get_data(), 7)
     numpy.testing.assert_almost_equal(np_matrix.get_data(), ls_matrix.get_data(), 7)
Exemplo n.º 5
0
 def test_get_intra_cluster_distances(self):
     matrix = CondensedMatrix(CH_table1)
     numpy.testing.assert_almost_equal(get_intra_cluster_distances(Cluster(None, [4,5]), matrix),[2.4494897427831779],5)
     numpy.testing.assert_almost_equal(get_intra_cluster_distances(Cluster(None, [1,3,5]), matrix),[2.4494897427831779, 3.8729833462074170, 3.8729833462074170],5)
     
     data = [0.0, 1.0, 1.0, 1.0,
                  1.0, 0.0, 0.0,
                       0.0, 0.0,
                            0.0]
     matrix = CondensedMatrix(data)
     expected_distance = 4
     self.assertEqual(expected_distance, numpy.sum(get_intra_cluster_distances(Cluster(None, range(5)), matrix)))
Exemplo n.º 6
0
    def calculate(cls, data_handler, matrix_params):
        data_array = data_handler.get_data().get_all_elements()
        if len(data_array.shape) == 1:  # 1 dimensional array
            distances = []
            for i in range(len(data_array) - 1):
                for j in range(i + 1, len(data_array)):
                    distances.append(abs(data_array[i] - data_array[j]))
            return CondensedMatrix(distances)

        else:
            return CondensedMatrix(
                scipy.spatial.distance.pdist(
                    data_handler.get_data().get_all_elements(), 'euclidean'))
Exemplo n.º 7
0
    def test_cluster_cohesion_without_prototype(self):

        distances = CondensedMatrix([1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
        clusters_1 = [
            Cluster(None, elements=[0, 1]),
            Cluster(None, elements=[2]),
            Cluster(None, elements=[3, 4])
        ]

        clusters_2 = [
            Cluster(None, elements=[0, 2, 4]),
            Cluster(None, elements=[1, 3])
        ]

        cohesion_calctor = CohesionCalculator()

        self.assertEqual(
            cohesion_calctor.evaluate_cluster(clusters_1[0], distances), 0.5)
        self.assertEqual(
            cohesion_calctor.evaluate_cluster(clusters_1[1], distances), 0.)
        self.assertEqual(
            cohesion_calctor.evaluate_cluster(clusters_1[2], distances), 5.0)
        self.assertEqual(
            cohesion_calctor.evaluate_cluster(clusters_2[0], distances), 5.0)
        self.assertEqual(
            cohesion_calctor.evaluate_cluster(clusters_2[1], distances), 3.0)
Exemplo n.º 8
0
    def test_cluster_cohe_sep_wo_prot_eval(self):
        distances = CondensedMatrix([1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
        clusters_1 = [
            Cluster(None, elements=[0, 1]),
            Cluster(None, elements=[2]),
            Cluster(None, elements=[3, 4])
        ]

        clusters_2 = [
            Cluster(None, elements=[0, 2, 4]),
            Cluster(None, elements=[1, 3])
        ]

        clusterization_1 = Clustering(clusters_1)
        clusterization_2 = Clustering(clusters_2)
        sep_calctor = SeparationCalculator()

        self.assertEqual(
            sep_calctor.cluster_separation(clusters_1[0], clusterization_1, 1.,
                                           distances), 27.0)
        self.assertEqual(
            sep_calctor.cluster_separation(clusters_1[1], clusterization_1, 1.,
                                           distances), 24.0)
        self.assertEqual(
            sep_calctor.cluster_separation(clusters_1[2], clusterization_1, 1.,
                                           distances), 37.0)
        self.assertEqual(
            sep_calctor.cluster_separation(clusters_2[0], clusterization_2, 1.,
                                           distances), 34.0)
        self.assertEqual(
            sep_calctor.cluster_separation(clusters_2[1], clusterization_2, 1.,
                                           distances), 34.0)
Exemplo n.º 9
0
    def test_cluster_mixed_cohesion_wo_prot(self):

        distances = CondensedMatrix([1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
        clusters_1 = [
            Cluster(None, elements=[0, 1]),
            Cluster(None, elements=[2]),
            Cluster(None, elements=[3, 4])
        ]

        clusters_2 = [
            Cluster(None, elements=[0, 2, 4]),
            Cluster(None, elements=[1, 3])
        ]

        sep_calctor = SeparationCalculator()

        self.assertEqual(
            sep_calctor._SeparationCalculator__between_cluster_distance(
                clusters_1[0], clusters_1[1], distances), 7.0)
        self.assertEqual(
            sep_calctor._SeparationCalculator__between_cluster_distance(
                clusters_1[0], clusters_1[2], distances), 20.0)
        self.assertEqual(
            sep_calctor._SeparationCalculator__between_cluster_distance(
                clusters_1[1], clusters_1[2], distances), 17.0)
        self.assertEqual(
            sep_calctor._SeparationCalculator__between_cluster_distance(
                clusters_2[0], clusters_2[1], distances), 34.0)
Exemplo n.º 10
0
    def test_gromos_seeding(self):
        points = [(0, 0), (0, 1), (0, -1), (1, 0), (3, 0), (3, 1), (6, 0),
                  (7, 0), (7, 1), (7, -1)]
        #          Maximum distance of connected components is 1, for 1.5 it may discover 3 clusters.
        #          For 3.2 it may discover only 2
        #
        #         1       5         8
        #         |       |         |
        #         0 - 3   4     6 - 7
        #         |                 |
        #         2                 9
        #

        matrix = CondensedMatrix(pdist(points))
        kmed_alg = KMedoidsAlgorithm(matrix, rand_seed=10)

        # With a small cutoff we get all 3 connected components
        numpy.testing.assert_array_equal(
            kmed_alg.gromos_seeding(3, 1.4), [0, 7, 4]
        )  # if it's 1.5 we'll have 6 instead of 7 as medoid (as it is the first one to have 3 neighbours)

        # With a bigger cutoff it has to try to find the minimum cutoff for 3 clusters, then 6 is returned instead of 7
        numpy.testing.assert_array_equal(kmed_alg.gromos_seeding(3, 3.2),
                                         [3, 6, 5])

        # This one is regression
        numpy.testing.assert_array_equal(kmed_alg.gromos_seeding(2, 3.2),
                                         [4, 7])

        # This one should return a random sequence, so is only testable because of the rand_seed
        numpy.testing.assert_array_equal(kmed_alg.gromos_seeding(2, 0), [5, 3])
Exemplo n.º 11
0
    def setUpClass(cls):
        cls.separated_decomposed_clusters = {
            "mixed": {
                "cluster_1": {
                    "traj_A": [0, 1, 2, 3, 4],
                    "traj_B": [5, 6, 7, 8, 9],
                    "traj_C": [10, 11, 12, 13, 14]
                }
            },
            "pure": {
                "cluster_2": {
                    "traj_A": [0, 1, 2, 3, 4]
                },
                "cluster_3": {
                    "traj_B": [5, 6, 7, 8, 9]
                }
            }
        }

        # 4 points forming a square with another point in its center
        square_points = numpy.array([[0, 0], [0, 2], [2, 0], [2, 2], [1, 1]])

        # move the square to the right and up-right
        square_points_2 = square_points + numpy.array([0, 5])
        square_points_3 = square_points + numpy.array([5, 0])

        cls.square_points = square_points.tolist()
        cls.square_points.extend(square_points_2.tolist())
        cls.square_points.extend(square_points_3.tolist())
        cls.matrix = CondensedMatrix(
            scipy.spatial.distance.pdist(cls.square_points))
Exemplo n.º 12
0
def create_matrices(data, verbose=False):
    """
    Creates all the matrices for the observations (datasets) and returns both.
    """
    condensed_matrices = {}
    all_observations = {}
    for dataset_name in data.all_datasets:
        dataset = data.all_datasets[dataset_name]
        # Creating the matrix
        observations = dataset_loading_2D(dataset,
                                          data.scale_factor[dataset_name])

        all_observations[dataset_name] = observations
        condensed_matrix_data = distance.pdist(observations)
        condensed_matrix = CondensedMatrix(condensed_matrix_data)
        condensed_matrices[dataset_name] = condensed_matrix
        if verbose:
            print "Matrix for %s:" % dataset_name
            print "-----------------------"
            print "Max dist. = ", condensed_matrix.calculateMax()
            print "Min dist. = ", condensed_matrix.calculateMin()
            print "Mean dist. = ", condensed_matrix.calculateMean()
            print "Variance = ", condensed_matrix.calculateVariance()
            print "-----------------------\n"
    return condensed_matrices, all_observations
Exemplo n.º 13
0
    def calculate(cls, data_handler, matrix_parameters):
        """
        Will generate the CondensedMatrix filled with the all vs all geometric center distances of the "body_selection"
        coordinates (which will usually be a ligand).

        @param trajectory_handler: The handler containing selection strings, pdb info and coordsets.
        @param matrix_parameters: The creation parameters (from the initial script).

        @return: The created distance matrix.
        """

        coords_type = matrix_parameters.get_value("type",
                                                  default_value="COORDINATES")
        builder = None

        if coords_type == "COORDINATES":
            print "using coords"
            builder = euclideanDistanceBuilder

        elif coords_type == "DIHEDRALS":
            print "using dihedrals"
            builder = DihedralEuclideanDistanceBuilder

        coordinates = builder.build(data_handler, matrix_parameters)
        distances = builder.calc_distances(coordinates)

        return CondensedMatrix(distances)
Exemplo n.º 14
0
 def setUpClass(cls):
     cls.matrix = CondensedMatrix(squared_CH_table1)
      
     cls.clusterings = [Clustering([Cluster(None, [0,1,2,3]), Cluster(None, [4,5])]),
                         Clustering([Cluster(None, [0,1]), Cluster(None, [2,3]), Cluster(None, [4,5])])]
     update_medoids(cls.clusterings[0], cls.matrix)
     update_medoids(cls.clusterings[0], cls.matrix)
    def build(cls, trajectory_handler, matrix_creation_parameters):
        """
        Will generate the CondensedMatrix filled with the all vs all geometric center distances of the "body_selection"
        coordinates (which will usually be a ligand).

        @param trajectory_handler: The handler containing selection strings, pdb info and coordsets.
        @param matrix_creation_parameters: The creation parameters (from the initial script).

        @return: The created distances matrix.
        """

        # Build calculator with fitting coordinate sets ...
        fit_selection_coordsets = trajectory_handler.getSelection(
            trajectory_handler.fitting_selection)

        # and calculation coordsets (we want them to be moved along with the fitting ones)
        body_selection_coordsets = trajectory_handler.getSelection(
            trajectory_handler.calculation_selection)

        calculator = RMSDCalculator(
            calculatorType="QTRFIT_OMP_CALCULATOR",
            fittingCoordsets=fit_selection_coordsets,
            calculationCoordsets=body_selection_coordsets)

        # Superpose iteratively (will modify all coordinates)
        calculator.iterativeSuperposition()

        # Working coordinates are changed to the body coordinates (to be used later for instance
        # with clustering metrics)
        trajectory_handler.setWorkingCoordinates(
            trajectory_handler.calculation_selection)
        distances = cls.calculate_geom_center(body_selection_coordsets)
        matrix = CondensedMatrix(distances)
        return matrix
Exemplo n.º 16
0
def get_submatrix(old_matrix, elements):
    """
    Gets the distances of the elements in 'elements' to build a new distance matrix.
    
    @param old_matrix: The initial matrix.
    @param elements: An array of elements of old_matrix (old_matrix.row_length >= len(elements), 
    max(elements)>=old_matrix.row_length)
    
    @return: The new (probably smaller) matrix.
    """
    #     This is not working. TODO: Review getting data from matrix once is changed
    #     N = len(elements)
    #     new_matrix = CondensedMatrix([1.]*((N*(N-1)/2)))
    #     for i in range(len(elements)-1):
    #         e_i = elements[i]
    #         for j in range(i+1,len(elements)):
    #             e_j = elements[j]
    #             print i, j
    #             new_matrix[i, j] = old_matrix[e_i, e_j]
    #     return new_matrix
    N = len(elements)
    inner_data = numpy.zeros((N * (N - 1)) / 2)
    k = 0
    for i in range(len(elements) - 1):
        e_i = elements[i]
        for j in range(i + 1, len(elements)):
            e_j = elements[j]
            inner_data[k] = old_matrix[e_i, e_j]
            k += 1
    return CondensedMatrix(inner_data)
Exemplo n.º 17
0
def shrink_matrix(this_matrix, to_have_this_size):
    max_dim = to_have_this_size
    row_dim = this_matrix.row_length

    box_size = int(math.ceil(row_dim / max_dim))
    number_of_boxes = int(math.ceil(row_dim / box_size))
    box_mid = int(math.ceil(box_size / 2.))
    max_dim = number_of_boxes * box_size
    print "Box size, mid", box_size, box_mid, max_dim, row_dim, number_of_boxes

    tmp_condensed = CondensedMatrix(
        numpy.zeros(int((number_of_boxes * (number_of_boxes - 1)) / 2.),
                    dtype=numpy.float))

    for k in range(0, max_dim, box_size):
        for l in range(0, max_dim, box_size):
            values = []
            for i in range(-box_mid, box_mid):
                for j in range(-box_mid, box_mid):
                    if (k + i > 0 and l + j > 0 and k + i < max_dim
                            and l + j < max_dim):
                        if k + i == l + j:
                            values.append(0)
                        else:
                            values.append(this_matrix[k + i, l + j])
            if (k / box_size != l / box_size):
                if values != []:
                    tmp_condensed[int(k / box_size),
                                  int(l / box_size)] = numpy.mean(values)
                else:
                    tmp_condensed[int(k / box_size), int(l / box_size)] = 0
    return tmp_condensed
Exemplo n.º 18
0
def get_rmsds_matrix(conforms, mode, sup, cores):
    print "Mode:", mode, "Superposition:", sup, "Number of cores:", cores

    if (mode == "cpu_serial" and not sup):
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            "NOSUP_OMP_CALCULATOR", conforms)

    elif (mode == "cpu_omp" and not sup):
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            "NOSUP_OMP_CALCULATOR", conforms)
        calculator.setNumberOfOpenMPThreads(int(cores))

    elif (mode == "cpu_omp" and sup):
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            "QCP_OMP_CALCULATOR", conforms)
        calculator.setNumberOfOpenMPThreads(int(cores))

    elif (mode == "cuda" and sup):
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            "QCP_CUDA_MEM_CALCULATOR", conforms)

    else:
        print "Wrong values to pyRMSD ! Please Fix"
        exit()

    rmsd = calculator.pairwiseRMSDMatrix()
    rmsd_matrix = CondensedMatrix(rmsd)
    inner_data = rmsd_matrix.get_data()
    np.save("Distances_Matrix.data", inner_data)

    return inner_data
Exemplo n.º 19
0
    def test_eps_neighborhood(self):
        distances = CondensedMatrix([
            1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 1., 1., 1., 1.,
            1., 3., 1., 1., 1., 1., 2., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
            2., 1.
        ])
        dbscan_alg = DBSCANAlgorithm(distances)

        self.assertItemsEqual(
            dbscan_alg._DBSCANAlgorithm__eps_neighborhood(6, 3),
            [0, 1, 2, 3, 4, 5, 7, 8])
        self.assertItemsEqual(
            dbscan_alg._DBSCANAlgorithm__eps_neighborhood(6, 2),
            [0, 1, 3, 4, 5, 7, 8])
        self.assertItemsEqual(
            dbscan_alg._DBSCANAlgorithm__eps_neighborhood(6, 1), [0, 4, 5, 7])
        self.assertItemsEqual(
            dbscan_alg._DBSCANAlgorithm__eps_neighborhood(6, 1.), [0, 4, 5, 7])

        self.assertItemsEqual(distances.element_neighbors_within_radius(6, 3),
                              [0, 1, 2, 3, 4, 5, 7, 8])
        self.assertItemsEqual(distances.element_neighbors_within_radius(6, 2),
                              [0, 1, 3, 4, 5, 7, 8])
        self.assertItemsEqual(distances.element_neighbors_within_radius(6, 1),
                              [0, 4, 5, 7])
        self.assertItemsEqual(distances.element_neighbors_within_radius(6, 1.),
                              [0, 4, 5, 7])
Exemplo n.º 20
0
def get_rmsds_matrix(conforms, mode, sup, cores, symm_groups=None):
    print("Mode:", mode, "Superposition:", sup, "Number of cores:", cores)

    if (mode == "cpu_serial" and not sup) or (mode == "cpu_omp" and not sup):
        calculator_name = "NOSUP_OMP_CALCULATOR"

    elif mode == "cpu_omp" and sup:
        calculator_name = "QCP_OMP_CALCULATOR"

    elif mode == "cuda" and sup:
        calculator_name = "QCP_CUDA_MEM_CALCULATOR"
    else:
        print("Wrong values to pyRMSD ! Please Fix")
        exit()

    if symm_groups:
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            calculator_name,
            fittingCoordsets=conforms,
            calculationCoordsets=conforms,
            calcSymmetryGroups=symm_groups)
    else:
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            calculator_name, conforms)

    # additionally set number of cores for parallel calculator
    if mode == "cpu_omp":
        calculator.setNumberOfOpenMPThreads(int(cores))

    rmsd = calculator.pairwiseRMSDMatrix()
    rmsd_matrix = CondensedMatrix(rmsd)
    inner_data = rmsd_matrix.get_data()
    np.save("Distances_Matrix.data", inner_data)

    return inner_data
Exemplo n.º 21
0
    def test_get_neighbors_for_node(self):
        condensed_matrix = CondensedMatrix(
            [0.2, 1.0, 0.3, .0, 0.5, 0.6, 0.7, 0.9, 0.8, 0.4])

        self.assertItemsEqual([0, 2],
                              condensed_matrix.get_neighbors_for_node(
                                  1, range(5), 0.5))
Exemplo n.º 22
0
    def test_do_combine(self):
        a = numpy.array([ 1., 4.,
                              7.])
        
        b = numpy.array([ 2., 3., 
                              1.])

        matrices = {
                    "a": CondensedMatrix(a),
                    "b": CondensedMatrix(b)
        }
        
        # Test sum
        operation = ["add","a","b"]
        c = combine(operation, matrices)
        numpy.testing.assert_array_equal( c.get_data(), [ 3.,  7.,  8.])
        
        # Test subtraction
        operation = ["sub","a","b"]
        c = combine(operation, matrices)
        numpy.testing.assert_array_equal( c.get_data(), [-1.,  1.,  6.])
        
        # Test scalar mult
        operation = ["mult",2,"a"]
        c = combine(operation, matrices)
        numpy.testing.assert_array_equal( c.get_data(), [  2.,   8.,  14.])
        
        # Test scalar mult conmutativity
        operation = ["mult","a",2]
        c = combine(operation, matrices)
        numpy.testing.assert_array_equal( c.get_data(), [  2.,   8.,  14.])
        
        # Test error conditions
        operation = ["mult",2,2]
        print "Error means OK-> ",
        self.assertRaises(SystemExit, combine, operation, matrices)
        operation = ["mult","a","a"]
        print "Error means OK-> ",
        self.assertRaises(SystemExit, combine, operation, matrices)
        operation = ["mult","dummy","a"]
        print "Error means OK-> ",
        self.assertRaises(SystemExit, combine, operation, matrices)

        # Test recursivity
        operation = ["mult",["add","a","b"], 2]
        c = combine(operation, matrices)
        numpy.testing.assert_array_equal( c.get_data(),[  6., 14.,  16.] )
Exemplo n.º 23
0
 def setUpClass(cls):
     cls.parameters = ProtocolParameters.get_default_params("data/params.json")
     distances = [94, 6, 43, 14, 96,
                     18, 59, 54, 69,
                         56, 96, 69,
                             54, 50,
                                  8]
     cls.matrix_1 = CondensedMatrix(distances)
Exemplo n.º 24
0
    def test_update_medoids(self):
        points = [(0, 0), (0, 1), (0, -1), (1, 0), (6, 0), (7, 0), (7, 1),
                  (7, -1)]

        matrix = CondensedMatrix(pdist(points))
        kmed_alg = KMedoidsAlgorithm(matrix)
        kmed_alg.class_list = [0, 0, 0, 0, 1, 1, 1, 1]
        numpy.testing.assert_array_equal(kmed_alg.update_medoids(), [0, 5])
Exemplo n.º 25
0
 def test_update_medois(self):
     clusters = [Cluster(None, [1,2]),Cluster(None, [3,4]), Cluster(None, [5])]
     clustering = Clustering(clusters)
     matrix = CondensedMatrix(squared_CH_table1)
     update_medoids(clustering, matrix)
     for c in clusters:
         self.assertNotEqual(c.prototype, None)
     
     self.assertItemsEqual([c.prototype for c in clusters], [1,3,5])
Exemplo n.º 26
0
 def test_get_number_of_rows(self):
     random.seed()
     for i in range(20):  #@UnusedVariable
         rows = random.randint(1, 1000)
         number_of_elements = (rows * (rows - 1)) / 2
         matrix = CondensedMatrix(range(number_of_elements))
         self.assertEqual(rows, matrix.get_number_of_rows())
         self.assertEqual(rows, matrix.row_length)
         self.assertEqual(rows, len(matrix))
Exemplo n.º 27
0
 def test_force_sparsity(self):
     data = numpy.array([1., 4., 6., 2., 5.,
                             3., 9., 7., 2.,
                                 4., 1., 1.,
                                      9.,3.,
                                         8.])
     W  = CondensedMatrix(data)
     SpectralTools.force_sparsity(W)
     W_expected = [ 0., 0., 6., 0., 5., 0., 9., 7., 0., 0., 0., 0., 9., 0., 8.]
     numpy.testing.assert_array_equal(W_expected, W.get_data())
Exemplo n.º 28
0
 def test_calculate_biased_medoid(self):
     condensed_matrix = CondensedMatrix([1.0, 4.5, 7.2, 6.7, 
                                              8.5, 4.5, 3.6, 
                                                   7.8, 2.2, 
                                                        2.0]) 
     c = Cluster(None,[0,2,3,4])
     interesting_elements = [3,4,0]
     self.assertEquals(4, c.calculate_biased_medoid(condensed_matrix,interesting_elements))
     interesting_elements = [4,2,3]
     self.assertEquals(4,c.calculate_biased_medoid(condensed_matrix,interesting_elements))
Exemplo n.º 29
0
 def test_kth_elements(self):
     distances = CondensedMatrix([
         17.46, 9.21, 4.47, 3.16, 4.47, 5.65, 12.36, 5.83, 9.43, 12.52,
         15.65, 5., 8.06, 11.18, 13.15, 3.16, 6.35, 8.24, 3.16, 5.10, 2.
     ])
     buffer = numpy.empty(distances.row_length)
     expected = [3.1600000000000001, 5.0999999999999996]
     result = kth_elements_distance(4, numpy.array([2, 4]), buffer,
                                    distances)
     numpy.testing.assert_array_almost_equal(expected, result, decimal=3)
Exemplo n.º 30
0
 def test_mini_evaluation(self):
     calculator = MeanMinimumDistanceCalculator(10)
     clusters = [
         Cluster(None, elements=[0, 1, 2]),
         Cluster(None, elements=[3, 4])
     ]
     triangle = [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]
     distances = CondensedMatrix(triangle)
     clustering = Clustering(clusters)
     self.assertEqual(7.0, calculator.evaluate(clustering, distances, 20))