Exemplo n.º 1
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.º 2
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.º 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_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.º 5
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))
 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.º 7
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
 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(numpy.array(range(number_of_elements))*1.)
         self.assertEqual(rows,matrix.get_number_of_rows())
         self.assertEqual(rows,matrix.row_length)
         self.assertEqual(rows,len(matrix))
Exemplo n.º 9
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.º 10
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.º 11
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.º 12
0
 def test_get_cluster_for_node(self):
     condensed_matrix = CondensedMatrix([1., 4.5, 8.5, 7.2, 
                                             4.5, 7.8, 6.7, 
                                                  3.6, 2.2, 
                                                       2.0])
     row_len = 5
     nodes_left = range(row_len)
     
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 0, nodes_left, 4.)),1)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 1, nodes_left, 4.)),1)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 2, nodes_left, 4.)),2)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 3, nodes_left, 4.)),2)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 4, nodes_left, 4.)),2)
     
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 0, nodes_left, 7.)),2)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 1, nodes_left, 7.)),3)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 2, nodes_left, 7.)),4)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 3, nodes_left, 7.)),2)
     self.assertEqual(len(condensed_matrix.get_neighbors_for_node( 4, nodes_left, 7.)),3)
     
     neig_4_3 = [2,4]
     neig = condensed_matrix.get_neighbors_for_node( 3, nodes_left, 4.)
     for i in range(len(neig_4_3)):
         self.assertEqual(neig[i],neig_4_3[i])
     
     neig_7_2 = [0,1,3,4]
     neig = condensed_matrix.get_neighbors_for_node( 2, nodes_left, 7.)
     for i in range(len(neig_7_2)):
         self.assertEqual(neig[i],neig_7_2[i])
Exemplo n.º 13
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.º 14
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.º 15
0
 def test_full_run(self):
     condensed_matrix = CondensedMatrix(matrix)
     cmax = condensed_matrix.calculateMax()
     alg = RandomAlgorithm.RandomClusteringAlgorithm(condensed_matrix)
     values = []
     calculator =  MeanMinimumDistanceCalculator(10)
     for i in range(2,20):
         clustering = alg.perform_clustering({
                                              "max_num_of_clusters":-1, 
                                              "num_clusters":i
                                              })
         values.append( calculator.evaluate(clustering, condensed_matrix, 30))
     self.assertTrue(max(values) < cmax)
Exemplo n.º 16
0
 def test_full_run(self):
     condensed_matrix = CondensedMatrix(matrix)
     cmax = condensed_matrix.calculateMax()
     alg = RandomAlgorithm.RandomClusteringAlgorithm(condensed_matrix)
     values = []
     calculator = MeanMinimumDistanceCalculator(10)
     for i in range(2, 20):
         clustering = alg.perform_clustering({
             "max_num_of_clusters": -1,
             "num_clusters": i
         })
         values.append(calculator.evaluate(clustering, condensed_matrix,
                                           30))
     self.assertTrue(max(values) < cmax)
Exemplo n.º 17
0
 def test_intermediate_iteration_1(self):
     """
     Replication of an intermediate result from an actual run.
     """
     condensed_matrix = CondensedMatrix([2.2360679774997898, 2.2360679774997898, 3.6055512754639891, 31.144823004794873, 4.2426406871192848, 33.241540277189323, 4.0, 28.442925306655784, 6.4031242374328485, 34.525353003264136, 6.7082039324993694, 6.0827625302982193, 32.756678708318397, 29.832867780352597, 23.086792761230392, 17.464249196572979, 35.902646142032481, 29.732137494637012, 33.837848631377263, 31.016124838541646, 20.124611797498108, 25.0, 21.095023109728988, 23.086792761230392, 22.360679774997898, 29.832867780352597, 24.186773244895647, 25.709920264364882, 28.792360097775937, 32.649655434629018, 32.140317359976393, 36.055512754639892, 34.713109915419565, 35.227829907617071, 36.013886210738214, 36.013886210738214, 3.1622776601683795, 5.0990195135927845, 29.068883707497267, 2.2360679774997898, 31.144823004794873, 3.6055512754639891, 26.305892875931811, 7.2111025509279782, 32.388269481403292, 5.0990195135927845, 5.8309518948453007, 30.594117081556711, 27.658633371878661, 20.880613017821101, 15.231546211727817, 33.734255586865999, 27.513632984395208, 31.622776601683793, 28.792360097775937, 18.384776310850235, 23.021728866442675, 20.0, 21.587033144922902, 21.095023109728988, 27.802877548915689, 23.021728866442675, 24.331050121192877, 27.202941017470888, 30.870698080866262, 31.016124838541646, 34.481879299133332, 33.376638536557273, 34.058772731852805, 35.014282800023196, 35.128336140500593, 2.0, 32.015621187164243, 4.1231056256176606, 34.058772731852805, 2.2360679774997898, 29.154759474226502, 4.2426406871192848, 35.227829907617071, 5.6568542494923806, 4.0, 33.376638536557273, 30.413812651491099, 23.53720459187964, 17.720045146669349, 36.496575181789318, 30.083217912982647, 34.205262752974143, 31.32091952673165, 18.867962264113206, 24.083189157584592, 19.235384061671343, 21.540659228538015, 20.615528128088304, 29.0, 22.360679774997898, 24.041630560342615, 27.313000567495326, 31.384709652950431, 30.265491900843113, 34.539832078341085, 32.984845004941285, 33.376638536557273, 34.058772731852805, 34.0, 34.014702703389901, 6.0827625302982193, 36.055512754639892, 3.6055512754639891, 31.144823004794873, 3.1622776601683795, 37.215588131856791, 7.2111025509279782, 4.4721359549995796, 35.355339059327378, 32.388269481403292, 25.495097567963924, 19.646882704388499, 38.470768123342687, 32.015621187164243, 36.138621999185304, 33.241540277189323, 20.0, 25.45584412271571, 19.646882704388499, 22.360679774997898, 21.189620100417091, 30.413812651491099, 22.803508501982758, 24.698178070456937, 28.178005607210743, 32.449961479175904, 30.594117081556711, 35.341194094144583, 33.526109228480422, 33.734255586865999, 34.23448553724738, 34.058772731852805, 28.0, 2.2360679774997898, 31.016124838541646, 3.6055512754639891, 35.057096285916209, 4.2426406871192848, 28.160255680657446, 32.140317359976393, 4.1231056256176606, 4.4721359549995796, 9.8488578017961039, 15.524174696260024, 6.4031242374328485, 7.6157731058639087, 8.0622577482985491, 8.2462112512353212, 26.627053911388696, 23.345235059857504, 34.132096331752024, 30.610455730027933, 33.015148038438355, 23.323807579381203, 35.0, 33.301651610693426, 31.89043743820395, 30.528675044947494, 40.311288741492746, 36.359317925395686, 39.204591567825318, 41.868842830916641, 44.598206241955516, 45.967379738244816, 30.016662039607269, 3.1622776601683795, 25.079872407968907, 7.2801098892805181, 31.144823004794873, 3.0, 5.0, 29.274562336608895, 26.305892875931811, 19.416487838947599, 13.601470508735444, 32.388269481403292, 25.96150997149434, 30.083217912982647, 27.202941017470888, 16.15549442140351, 20.808652046684813, 18.027756377319946, 19.416487838947599, 19.026297590440446, 25.612496949731394, 21.0, 22.203603311174518, 25.0, 28.635642126552707, 29.0, 32.280024783137947, 31.256999216175569, 32.015621187164243, 33.060550509633082, 33.241540277189323, 33.0, 5.0990195135927845, 37.013511046643494, 2.2360679774997898, 30.066592756745816, 34.058772731852805, 3.1622776601683795, 5.0, 11.401754250991379, 17.262676501632068, 4.4721359549995796, 7.810249675906654, 7.0710678118654755, 8.0622577482985491, 27.784887978899608, 24.083189157584592, 35.355339059327378, 31.622776601683793, 34.132096331752024, 23.600847442411894, 36.055512754639892, 34.205262752974143, 32.526911934581186, 30.805843601498726, 41.036569057366385, 36.61966684720111, 39.698866482558415, 42.449970553582247, 45.254833995939045, 46.690470119715009, 28.0178514522438, 4.1231056256176606, 34.058772731852805, 3.6055512754639891, 2.2360679774997898, 32.140317359976393, 29.154759474226502, 22.203603311174518, 16.278820596099706, 35.227829907617071, 28.635642126552707, 32.756678708318397, 29.832867780352597, 16.643316977093239, 21.931712199461309, 17.11724276862369, 19.313207915827967, 18.439088914585774, 26.870057685088806, 20.223748416156685, 21.840329667841555, 25.079872407968907, 29.154759474226502, 28.160255680657446, 32.310988842807021, 30.805843601498726, 31.256999216175569, 32.015621187164243, 32.015621187164243, 32.0, 6.0827625302982193, 25.019992006393608, 29.017236257093817, 4.4721359549995796, 2.2360679774997898, 6.324555320336759, 12.165525060596439, 7.6157731058639087, 5.0, 7.2111025509279782, 6.0827625302982193, 23.021728866442675, 19.849433241279208, 30.528675044947494, 27.018512172212592, 29.410882339705484, 20.124611797498108, 31.400636936215164, 29.732137494637012, 28.42534080710379, 27.294688127912362, 36.796738985948195, 33.120990323358392, 35.805027579936315, 38.418745424597091, 41.109609582188931, 42.449970553582247, 38.013155617496423, 7.0710678118654755, 3.1622776601683795, 36.055512754639892, 33.060550509633082, 26.076809620810597, 20.09975124224178, 39.11521443121589, 32.388269481403292, 36.496575181789318, 33.541019662496844, 18.384776310850235, 24.207436873820409, 17.088007490635061, 20.248456731316587, 18.788294228055936, 29.206163733020468, 20.248456731316587, 22.360679774997898, 26.076809620810597, 30.610455730027933, 27.892651361962706, 33.120990323358392, 31.016124838541646, 31.048349392520048, 31.400636936215164, 31.144823004794873, 31.0, 35.0, 2.2360679774997898, 5.0990195135927845, 12.041594578792296, 18.027756377319946, 2.2360679774997898, 7.2111025509279782, 5.3851648071345037, 7.0710678118654755, 27.730849247724095, 23.600847442411894, 35.341194094144583, 31.384709652950431, 34.0, 22.671568097509269, 35.846896657869841, 33.837848631377263, 31.89043743820395, 29.832867780352597, 40.459856648287818, 35.608987629529715, 38.897300677553446, 41.725292090050132, 44.598206241955516, 46.097722286464439, 4.0, 29.017236257093817, 26.019223662515376, 19.026297590440446, 13.038404810405298, 32.062439083762797, 25.317977802344327, 29.427877939124322, 26.476404589747453, 13.416407864998739, 18.439088914585774, 15.033296378372908, 16.492422502470642, 16.031219541881399, 23.345235059857504, 18.0, 19.235384061671343, 22.135943621178654, 25.942243542145693, 26.0, 29.410882339705484, 28.284271247461902, 29.017236257093817, 30.066592756745816, 30.265491900843113, 33.015148038438355, 30.016662039607269, 23.021728866442675, 17.029386365926403, 36.055512754639892, 29.274562336608895, 33.376638536557273, 30.413812651491099, 15.620499351813308, 21.2602916254693, 15.297058540778355, 17.888543819998318, 16.763054614240211, 26.248809496813376, 18.439088914585774, 20.248456731316587, 23.706539182259394, 28.0178514522438, 26.305892875931811, 30.870698080866262, 29.120439557122072, 29.427877939124322, 30.066592756745816, 30.0, 3.0, 10.0, 16.0, 3.1622776601683795, 5.0, 4.0, 5.0, 25.495097567963924, 21.400934559032695, 33.105890714493697, 29.154759474226502, 31.76476034853718, 20.615528128088304, 33.61547262794322, 31.622776601683793, 29.732137494637012, 27.802877548915689, 38.288379438153292, 33.600595232822883, 36.796738985948195, 39.597979746446661, 42.449970553582247, 43.931765272977593, 7.0, 13.0, 6.0827625302982193, 3.1622776601683795, 5.0, 4.0, 22.825424421026653, 19.104973174542799, 30.413812651491099, 26.627053911388696, 29.154759474226502, 18.867962264113206, 31.064449134018133, 29.206163733020468, 27.586228448267445, 26.076809620810597, 36.069377593742864, 31.906112267087632, 34.828149534535996, 37.536648758246919, 40.311288741492746, 41.725292090050132, 6.0, 13.038404810405298, 6.7082039324993694, 10.770329614269007, 8.0622577482985491, 17.029386365926403, 14.7648230602334, 24.413111231467404, 21.213203435596427, 23.430749027719962, 16.278820596099706, 25.495097567963924, 24.083189157584592, 23.323807579381203, 23.086792761230392, 31.400636936215164, 28.792360097775937, 30.886890422961002, 33.286633954186478, 35.805027579936315, 37.013511046643494, 19.026297590440446, 12.369316876852981, 16.492422502470642, 13.601470508735444, 13.038404810405298, 13.038404810405298, 19.798989873223331, 17.4928556845359, 19.209372712298546, 16.278820596099706, 21.400934559032695, 20.591260281974002, 20.880613017821101, 22.022715545545239, 28.178005607210743, 27.294688127912362, 28.460498941515414, 30.463092423455635, 32.649655434629018, 33.61547262794322, 7.2801098892805181, 4.2426406871192848, 6.7082039324993694, 27.856776554368238, 23.323807579381203, 35.468295701936398, 31.304951684997057, 34.014702703389901, 21.931712199461309, 35.777087639996637, 33.61547262794322, 31.400636936215164, 29.0, 40.0, 34.713109915419565, 38.209946349085598, 41.109609582188931, 44.045431091090478, 45.607017003965517, 4.1231056256176606, 1.4142135623730951, 20.615528128088304, 16.401219466856727, 28.231188426986208, 24.186773244895647, 26.832815729997478, 15.811388300841896, 28.653097563788805, 26.627053911388696, 24.758836806279895, 23.021728866442675, 33.301651610693426, 28.844410203711913, 31.89043743820395, 34.655446902326915, 37.483329627982627, 38.948684188300895, 3.0, 24.041630560342615, 19.235384061671343, 31.622776601683793, 27.313000567495326, 30.083217912982647, 17.691806012954132, 31.780497164141408, 29.529646120466801, 27.202941017470888, 24.758836806279895, 35.805027579936315, 30.479501308256342, 33.97057550292606, 36.878177829171548, 39.824615503479755, 41.400483088968905, 21.189620100417091, 16.643316977093239, 28.792360097775937, 24.596747752497688, 27.313000567495326, 15.620499351813308, 29.068883707497267, 26.92582403567252, 24.839484696748443, 22.803508501982758, 33.421549934136806, 28.600699292150182, 31.827660925679098, 34.655446902326915, 37.536648758246919, 39.05124837953327, 6.324555320336759, 7.6157731058639087, 4.4721359549995796, 6.4031242374328485, 11.180339887498949, 8.4852813742385695, 7.6157731058639087, 9.0553851381374173, 12.529964086141668, 15.231546211727817, 16.278820596099706, 16.124515496597098, 17.720045146669349, 19.697715603592208, 20.591260281974002, 13.038404810405298, 8.2462112512353212, 11.180339887498949, 5.0, 12.649110640673518, 10.295630140987001, 8.6023252670426267, 9.0, 16.970562748477139, 14.317821063276353, 16.124515496597098, 18.601075237738275, 21.2602916254693, 22.627416997969522, 5.0990195135927845, 2.2360679774997898, 17.11724276862369, 3.1622776601683795, 5.6568542494923806, 10.0, 15.264337522473747, 11.045361017187261, 16.401219466856727, 13.928388277184119, 14.142135623730951, 15.033296378372908, 15.297058540778355, 3.0, 12.041594578792296, 4.4721359549995796, 3.1622776601683795, 5.8309518948453007, 10.63014581273465, 10.770329614269007, 13.0, 12.0, 13.341664064126334, 15.231546211727817, 16.124515496597098, 15.033296378372908, 2.2360679774997898, 3.6055512754639891, 7.810249675906654, 13.038404810405298, 10.04987562112089, 14.422205101855956, 12.369316876852981, 13.0, 14.317821063276353, 14.866068747318506, 16.031219541881399, 13.152946437965905, 9.8488578017961039, 7.2111025509279782, 18.357559750685819, 13.038404810405298, 16.278820596099706, 19.209372712298546, 22.203603311174518, 23.853720883753127, 3.1622776601683795, 7.6157731058639087, 13.0, 8.0, 13.45362404707371, 10.770329614269007, 11.045361017187261, 12.165525060596439, 12.649110640673518, 4.4721359549995796, 9.8488578017961039, 7.6157731058639087, 10.816653826391969, 9.0553851381374173, 10.198039027185569, 12.083045973594572, 13.038404810405298, 5.3851648071345037, 8.6023252670426267, 7.2801098892805181, 7.6157731058639087, 10.0, 12.727922061357855, 14.212670403551895, 12.369316876852981, 5.8309518948453007, 9.4339811320566032, 12.529964086141668, 15.652475842498529, 17.464249196572979, 9.2195444572928871, 4.4721359549995796, 3.1622776601683795, 4.4721359549995796, 5.6568542494923806, 5.0, 8.0622577482985491, 11.180339887498949, 13.152946437965905, 3.1622776601683795, 6.324555320336759, 8.2462112512353212, 3.1622776601683795, 5.0990195135927845, 2.0])
     remaining_nodes = [4, 8, 9, 15, 16, 18, 20, 21, 22, 26, 29, 30, 32, 36]
     clustered_nodes = [7, 0, 1, 2, 3, 5, 11, 12, 13, 6, 10, 14, 17, 19, 25, 23, 24, 27, 28, 34, 31, 33, 35]
     (node, len_neig) = condensed_matrix.choose_node_with_higher_cardinality(remaining_nodes,4.0)
     self.assertEqual(len_neig,1)
     self.assertNotIn(node, clustered_nodes)
     neigbours = condensed_matrix.get_neighbors_for_node(node,remaining_nodes,4.0)
     self.assertEqual(len(neigbours),len_neig)
     expected_neightbours = [8]
     for i in range(len(neigbours)):
         self.assertEqual(neigbours[i],expected_neightbours[i])
Exemplo n.º 18
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.º 19
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.º 20
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.º 21
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.º 22
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.º 23
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.º 24
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.º 25
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
    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.º 27
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)
Exemplo n.º 28
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.º 29
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.º 30
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.º 31
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.º 32
0
    def createMatrix(self, pdb_coordsets, calculator="QCP_OMP_CALCULATOR"):
        """
        Generates a condensed matrix given a set of coordinates (usually representing a trajectory) and
        a calculator definition.

        @param pdb_coordsets: Input coordinates in Prody format.
        @param calculator: One of the possible available calculator types.
        
        @return: A consensed matrix with the pairwise RMSD matrix.
        """
        print "Calculating matrix..."
        rmsd = pyRMSD.RMSDCalculator.RMSDCalculator(
            pdb_coordsets, calculator).pairwiseRMSDMatrix()
        self.distance_matrix = CondensedMatrix(rmsd)
        self.distance_matrix.recalculateStatistics()
        self.__save_statistics()
        print " Done\n"
        return self.distance_matrix
Exemplo n.º 33
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.º 34
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))
Exemplo n.º 35
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.º 36
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.º 37
0
def get_rmsds_matrix(conforms,  mode,  sup,  cores, symm_groups=None):

    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"
        print("we are using QCP_OMP to compute RMSD")
    elif mode == "cuda" and sup:
        calculator_name = "QCP_CUDA_MEM_CALCULATOR"
    else:
        print("Wrong values to pyRMSD ! Please Fix")
        exit()

    if symm_groups:
        print("We have ambiguity.")
        s1 = parse_symm_groups_for_pyrmsd(symm_groups)
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            calculator_name,
            fittingCoordsets=conforms,
            calcSymmetryGroups=s1,
            fitSymmetryGroups=s1)

    else:
        calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
            calculator_name, conforms)

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

    if not symm_groups:
        rmsd = calculator.pairwiseRMSDMatrix()
    else:
        rmsd = []
        for i in range(len(conforms) - 1):
            rmsd += list(calculator.oneVsFollowing(i))
    rmsd_matrix = CondensedMatrix(rmsd)
    inner_data = rmsd_matrix.get_data()
    np.save("Distances_Matrix.data", inner_data)

    return inner_data
Exemplo n.º 38
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.º 39
0
class testMatrixStatistics(unittest.TestCase):
    
    def setUp(self):
        random.seed(12345)
        num_elems = 50*49/2;
        self.contents = random.sample(xrange(num_elems+1),num_elems)
        self.condensedMatrix = CondensedMatrix(self.contents)
        
    def test_mean(self):
        self.assertAlmostEquals(self.condensedMatrix.calculateMean(), numpy.mean(self.contents))#, delta = 1)
    
    def test_variance(self):
        self.assertAlmostEquals(self.condensedMatrix.calculateVariance(), numpy.var(self.contents))#, delta = 1)
    
    def test_skewness(self):
        self.assertAlmostEquals(self.condensedMatrix.calculateSkewness(), -0.000733274016748)#scipy.stats.skew(self.contents))#d, delta = 1)
    
    def test_kurtosis(self):
        self.assertAlmostEquals(self.condensedMatrix.calculateKurtosis(), -1.20119577613)#scipy.stats.kurtosis(self.contents))#, delta = 1)
    
    def test_max(self):
        self.assertAlmostEquals(self.condensedMatrix.calculateMax(), numpy.max(self.contents))#, delta = 1)

    def test_min(self):
        self.assertAlmostEquals(self.condensedMatrix.calculateMin(), numpy.min(self.contents))#, delta = 1)
Exemplo n.º 40
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.º 41
0
    def createMatrix(self, pdb_coordsets, calculator = "QCP_OMP_CALCULATOR"):
        """
        Generates a condensed matrix given a set of coordinates (usually representing a trajectory) and
        a calculator definition.

        @param pdb_coordsets: Input coordinates in Prody format.
        @param calculator: One of the possible available calculator types.
        
        @return: A consensed matrix with the pairwise RMSD matrix.
        """
        print "Calculating matrix..."
        rmsd = pyRMSD.RMSDCalculator.RMSDCalculator(pdb_coordsets, calculator).pairwiseRMSDMatrix()
        self.distance_matrix = CondensedMatrix(rmsd)
        self.distance_matrix.recalculateStatistics()
        self.__save_statistics()
        print " Done\n"
        return self.distance_matrix
Exemplo n.º 42
0
 def test_choose_node_with_higher_cardinality(self):
     condensed_matrix = CondensedMatrix([1., 4.5, 8.5, 7.2, 
                                             4.5, 7.8, 6.7, 
                                                  3.6, 2.2, 
                                                       2.0])
     nodes = range(5)
     self.assertEqual( condensed_matrix.choose_node_with_higher_cardinality( nodes, 4.),\
                       (2,2))
     
     nodes = range(1,5)
     self.assertEqual( condensed_matrix.choose_node_with_higher_cardinality( nodes, 4.),\
                       (2,2))
     
     nodes = [2,0,3]
     self.assertEqual( condensed_matrix.choose_node_with_higher_cardinality( nodes, 4.),\
                      (2,1))
     
     nodes = range(5)
     self.assertEqual( condensed_matrix.choose_node_with_higher_cardinality( nodes, 7.),\
                       (2,4))
      
     nodes = [2,0,3]
     self.assertEqual( condensed_matrix.choose_node_with_higher_cardinality(  nodes, 7.),\
                      (2,2))
Exemplo n.º 43
0
import random
import numpy
from pyRMSD.condensedMatrix import CondensedMatrix
import pyRMSD.benchmark.alias.condensedMatrix as PythonCondensedMatrix
from pyRMSD.benchmark.alias.neighbourOps import choose_node_with_higher_cardinality,\
    get_neighbors_for_node
 
if __name__ == '__main__':
    
    print "Creating data..."
    row_size = 20000
    matrix_elem_size = row_size*(row_size-1)/2
    contents = numpy.array(random.sample(xrange(matrix_elem_size+1),matrix_elem_size))
    float_contents = contents / float(max(contents))
    del contents
    matrix = CondensedMatrix(float_contents)
    matrix2 = PythonCondensedMatrix.CondensedMatrix(float_contents)
    remaining_nodes = range(row_size)

    print "======================================"
    print "'get_neighbors_for_node' benchmark"
    print "======================================"
    time_start = time.time()
    neighbors1 = matrix.get_neighbors_for_node(1,remaining_nodes, 0.5)
    time_end = time.time()
    print "Fast Neighbor search for Fast matrix took %.3fs"%(time_end-time_start)
    
    time_start = time.time()
    neighbors2 = matrix2.get_neighbors_for_node(1,remaining_nodes, 0.5)
    time_end = time.time()
    print "Slow Neighbor search for Slow matrix took %.3fs"%(time_end-time_start)
Exemplo n.º 44
0
 def setUp(self):
     random.seed(12345)
     num_elems = 50*49/2;
     self.contents = random.sample(xrange(num_elems+1),num_elems)
     self.condensedMatrix = CondensedMatrix(self.contents)
Exemplo n.º 45
0
class MatrixHandler(object):
    
    def __init__(self, statistics_folder=None):
        """
        Class constructor.

        @param statistics_folder: Path to the folder that will contain the statistics summary. If None, 
        no statistics will be saved.
        """
        self.distance_matrix = None
        self.statistics_folder = statistics_folder
        
    def getMatrix(self):
        """
        @return: The inner condensed matrix.
        """
        return self.distance_matrix

    def createMatrix(self, pdb_coordsets, calculator = "QCP_OMP_CALCULATOR"):
        """
        Generates a condensed matrix given a set of coordinates (usually representing a trajectory) and
        a calculator definition.

        @param pdb_coordsets: Input coordinates in Prody format.
        @param calculator: One of the possible available calculator types.
        
        @return: A consensed matrix with the pairwise RMSD matrix.
        """
        print "Calculating matrix..."
        rmsd = pyRMSD.RMSDCalculator.RMSDCalculator(pdb_coordsets, calculator).pairwiseRMSDMatrix()
        self.distance_matrix = CondensedMatrix(rmsd)
        self.distance_matrix.recalculateStatistics()
        self.__save_statistics()
        print " Done\n"
        return self.distance_matrix
        
    def saveMatrix(self, matrix_file_without_extension):
        """
        Saves the internal matrix to a file. It adds the extension '.bin' to that name.
        
        @param matrix_file_without_extension: Is the matrix file name without any 
        extension ('.bin' will be added).
        """
        print "Writing matrix data (in "+matrix_file_without_extension+".bin) ..."
        MatrixHandler.save_matrix(matrix_file_without_extension, self.distance_matrix)
        print " Done\n"
    
    def loadMatrix(self, matrix_file_without_extension):
        """
        Loads a condensed matrix from disk.
        
        @param matrix_file_without_extension: Is the matrix file name without any 
        extension ('.bin' will be added).
        """
        print "Loading matrix data from "+matrix_file_without_extension+".bin ..."
        self.distance_matrix = MatrixHandler.load_matrix(matrix_file_without_extension)
        self.distance_matrix.recalculateStatistics()
        MatrixHandler.save_statistics(self.statistics_folder, self.distance_matrix)
        print " Done\n"
    
    @classmethod
    def save_matrix(cls, matrix_file_without_extension, distance_matrix):
        """
        Saves the internal matrix to a file. It adds the extension '.bin' to that name.
        
        @param matrix_file_without_extension: Is the matrix file name without any 
        extension ('.bin' will be added).
        @param distance_matrix: The matrix to be saved.
        """
        numpy.save(matrix_file_without_extension,distance_matrix.get_data())
    
    @classmethod
    def load_matrix(cls, matrix_file_without_extension):
        """
        Loads a condensed matrix from disk. <ATT> Without the cast to list, the matrix 
        is not loaded properly.</ATT>
        
        @param matrix_file_without_extension: Is the matrix file name without any 
        extension ('.bin' will be added).
        
        @return: The loaded condensed matrix.
        """
        return CondensedMatrix(list(numpy.load(matrix_file_without_extension+".npy")))
    
    @classmethod
    def save_statistics(cls, statistics_folder, distance_matrix):
        """
        Saves a file with some statistics of the internal matrix.
        
        @param statistics_folder: Folder where the file 'statistics.json' will be stored.
        @param distance_matrix: The distance matrix from which the statistics are calculated.
        """
        if statistics_folder is not None:
            stats_dic = {}
            stats_dic["Minimum"] =  distance_matrix.calculateMin() 
            stats_dic["Maximum"] =  distance_matrix.calculateMax()
            stats_dic["Mean"] =     distance_matrix.calculateMean()
            stats_dic["Std. Dev."] =distance_matrix.calculateVariance()
            stats_dic["Skewness"] = distance_matrix.calculateSkewness()
            stats_dic["Kurtosis"] = distance_matrix.calculateKurtosis()
            open( os.path.join(statistics_folder,"statistics.json"),"w").write(json.dumps(stats_dic,indent=4, separators=(',', ': ')))
            return os.path.join(statistics_folder,"statistics.json")
        return None