From 1f2f9fcfef92e26ca918ab761d08fff043774d83 Mon Sep 17 00:00:00 2001 From: nicolas-zimmermann Date: Wed, 18 Sep 2019 17:09:32 +0200 Subject: [PATCH] added Conformations.small_kmedoids method to compute kmedoids --- src/projet8.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/projet8.py b/src/projet8.py index 8f1e702..c9464ee 100644 --- a/src/projet8.py +++ b/src/projet8.py @@ -36,8 +36,8 @@ class Conformations: """ self.df = pd.DataFrame() self.filename = filename - self.identity_dist = None - self.dissimilarity_dist = None + self.identity_dist = np.array(None) + self.dissimilarity_dist = np.array(None) for chain_name, chain in pbx.chains_from_files([filename]): dihedrals = chain.get_phi_psi_angles() pb_seq = pbx.assign(dihedrals) @@ -104,9 +104,41 @@ class Conformations: return dist - #def small_kmedoid(self, ncluster): - # pass - + def small_kmedoids(self, matrix, ncluster): + """ + Returns clusters and medoids computed with kmedoids on a distance matrix + Arguments : + - matrix : str, ('identity' or 'dissimilarity') + corresponding to the desired distance matrix to + computed + - ncluster number of clusters to be computed + """ + if matrix == 'identity': + + matrix = self.identity_dist + if matrix.all() == None: + print("Error : distance matrix from identity hasn't been " \ + "computed yet") + return + elif matrix == 'dissimilarity': + matrix = self.dissimilarity_dist + if matrix.all() == None: + print("Error : distance matrix from dissimilarity hasn't " \ + "been computed yet") + return + + if ncluster > matrix.shape[0]: + print("Error : number of desired clusters > number of objects") + return + + initial_medoids = np.random.randint(matrix.shape[0], size=ncluster) + kmed1 = kmedoids(matrix, initial_medoids, data_type='distance_matrix') + kmed1.process() + + clusters = kmed1.get_clusters() + medoids = kmed1.get_medoids() + + return (clusters, medoids) if __name__ == "__main__": if len(sys.argv) < 2: