def test_sklearn_affinity_propagation(self, ens1): pytest.importorskip('sklearn') cc1 = encore.cluster([ens1]) cc2 = encore.cluster([ens1], method=encore.AffinityPropagation()) assert len(cc1) == len(cc2), \ "Native and sklearn implementations of affinity "\ "propagation don't agree: mismatch in number of "\ "clusters: {0} {1}".format(len(cc1), len(cc2))
def test_clustering_AffinityPropagation_direct(self, ens1): pytest.importorskip('sklearn') method = encore.AffinityPropagation() distance_matrix = encore.get_distance_matrix(ens1) cluster_assignment, details = method(distance_matrix) expected_value = 7 assert len(set(cluster_assignment)) == expected_value, \ "Unexpected result: {0}".format(cluster_assignment)
def test_sklearn_affinity_propagation(self): cc1 = encore.cluster([self.ens1]) cc2 = encore.cluster([self.ens1], method=encore.AffinityPropagation()) assert_equal(len(cc1), len(cc2), err_msg="Native and sklearn implementations of affinity " "propagation don't agree: mismatch in number of " "clusters: {0} {1}".format(len(cc1), len(cc2)))
def affinity_propagation(self, preference=-6.0): '''Performing Affinity Propagation clustering of AMOEBA-run Trp-Cage folding trajectory:- Default parameter values from MDAnalysis - damping=0.9, max_iter=500, convergence_iter=50 Preference reduced to -10 from -1 to reflect local homogenity within the trajectory''' print("Performing Affinity Propagation with input preference = %f" % preference) clust = encore.cluster(self.univ_cut, method=encore.AffinityPropagation( preference=preference, verbose=True)) centroids = [cluster.centroid * self.ncut for cluster in clust] ids = [cluster.id for cluster in clust] print( "Clustering complete! - %d clusters formed with average size = %d frames" % (len(ids), np.average([cluster.size for cluster in clust]))) coords_centroids = np.zeros((len(centroids), 304, 3), dtype=np.float64) coords_centroids[:, :, :] = [ self.coords_protein[centroids[i], :, :] for i in np.arange(0, len(centroids)) ] protein = self.univ2.select_atoms("protein") univ_centroids = mda.Merge(protein) univ_centroids.load_new(coords_centroids, format=MemoryReader) ref = mda.Universe("folded.pdb") nframes = len(univ_centroids.trajectory) alignment = align.AlignTraj(univ_centroids, ref, select='protein and name CA', in_memory=True, verbose=True) alignment.run() idvscenter = { 'Cluster ID': [ids[i] for i in range(0, len(ids))], 'Centroid Time (ps)': [centroids[i] * 10 for i in range(0, len(centroids))], 'Cluster Size': [cluster.size for cluster in clust] } idtable = pd.DataFrame(data=idvscenter) #Visualisation representation set for Trp-Cage - will expand to general proteins later view = nglview.show_mdanalysis(univ_centroids) view.add_cartoon(selection="protein") view.add_licorice('TRP') view.add_licorice('PRO') view.center(selection='protein', duration=nframes) view.player.parameters = dict(frame=True) return view, idtable, univ_centroids
def test_clustering_AffinityPropagation_direct(self): method = encore.AffinityPropagation() distance_matrix = encore.get_distance_matrix(self.ens1) cluster_assignment, details = method(distance_matrix) expected_value = 7 assert_equal( len(set(cluster_assignment)), expected_value, err_msg="Unexpected result: {0}".format(cluster_assignment))
def test_clustering_two_different_methods(self, ens1): pytest.importorskip('sklearn') cluster_collection = encore.cluster( [ens1], method=[ encore.AffinityPropagation(preference=-7.5), encore.DBSCAN(min_samples=2) ]) assert len(cluster_collection[0]) == len(cluster_collection[1]), \ "Unexpected result: {0}".format(cluster_collection)
def test_clustering_two_different_methods(self): cluster_collection = encore.cluster( [self.ens1], method=[ encore.AffinityPropagation(preference=-7.5), encore.DBSCAN(min_samples=2) ]) print(cluster_collection) print(cluster_collection) assert_equal( len(cluster_collection[0]), len(cluster_collection[1]), err_msg="Unexpected result: {0}".format(cluster_collection))