def clusterAnalysis(matrix, tolerance=10.): n = matrix.shape[0] clusters = [] covered = N.zeros((n, ), N.Int) while 1: pick = N.maximum.reduce(matrix) if N.maximum.reduce(pick) == 0.: break imax = N.argmax(pick) max_sim = N.maximum.reduce(matrix[imax]) cutoff = max_sim / tolerance mask = N.greater(matrix[imax], cutoff) similarity = N.sort(N.repeat(matrix[imax], mask)) mask[imax] = 1 clusters.append((similarity, mask)) covered = N.logical_or(covered, mask) mask = N.logical_not(mask) matrix = matrix * (mask[N.NewAxis, :] * mask[:, N.NewAxis]) if N.add.reduce(covered) < n: clusters.append((0., N.logical_not(covered))) return clusters
def axisAndAngle(self): """ @returns: the axis (a normalized vector) and angle (in radians). The angle is in the interval (-pi, pi] @rtype: (L{Scientific.Geometry.Vector}, C{float}) """ asym = -self.tensor.asymmetricalPart() axis = Geometry.Vector(asym[1, 2], asym[2, 0], asym[0, 1]) sine = axis.length() if abs(sine) > 1.e-10: axis = axis / sine projector = axis.dyadicProduct(axis) cosine = (self.tensor - projector).trace() / (3. - axis * axis) angle = angleFromSineAndCosine(sine, cosine) else: t = 0.5 * (self.tensor + Geometry.delta) i = N.argmax(t.diagonal().array) axis = (t[i] / N.sqrt(t[i, i])).asVector() angle = 0. if t.trace() < 2.: angle = N.pi return axis, angle
backbone = chain.backbone() for i in range(len(breaks)-1): residues = N.take(backbone, helix_indices[breaks[i]:breaks[i+1]]) helices.append(Collection(list(residues))) helices = [h for h in helices if len(h) > 4] #Collection(helices).view() residue_motion_vectors = [] helix_motion_vectors = [] for helix in helices: end_to_end = helix[0].centerOfMass()-helix[-1].centerOfMass() cms, inertia = helix.centerAndMomentOfInertia() moments, axes = inertia.diagonalization() axes = [a.asVector() for a in axes] helix_axis = axes[N.argmax([abs(end_to_end*v) for v in axes])] hmv = ParticleVector(universe) helix_motion_vectors.append(hmv) for residue in helix: mv = ParticleVector(universe) mv[residue.C_alpha] = helix_axis.cross(residue.C_alpha.position()-cms) hmv[residue.C_alpha] = helix_axis.cross(residue.C_alpha.position()-cms) residue_motion_vectors.append(mv) residue_subspace = Subspace(universe, residue_motion_vectors) helix_subspace = Subspace(universe, helix_motion_vectors) frequencies = [] projections = [] for m in modes[6:]: frequencies.append(m.frequency)
def findClusters(self, preferences, max_iterations=500, convergence = 50, damping=0.5): """ @param preferences: the preference values for the cluster identification. This can be either a single number, or a sequence with one value per item. @type preferences: C{float} or sequence of C{float} @param max_iterations: the number of iterations at which the algorithm is stopped even if there is no convergence. @type max_iterations: C{int} @param convergence: the number of iterations during which the cluster decomposition must remain stable before it is returned as converged. @type convergence: C{int} @param damping: a number between 0 and 1 that influences by fast affinity and responsibility values can change. @type damping: C{float} """ preferences = N.array(preferences) if len(preferences.shape) == 0: preferences = preferences + N.zeros((self.nitems,), N.Float) if len(preferences) != self.nitems: raise ValueError("Number of preferences != number of items") noise_scale = 1.e-12*(self.largest_similarity-self.smallest_similarity) s = N.concatenate([self.similarities, preferences]) for i in range(len(s)): s[i] += noise_scale*random.random() a = N.zeros(s.shape, N.Float) r = N.zeros(s.shape, N.Float) iterations_left = max_iterations convergence_count = 0 self.exemplar = N.zeros((self.nitems,), N.Int) while True: a, r = _affinityPropagation(self, s, a, r, damping) e = a + r exemplar = N.zeros((self.nitems,), N.Int) for i in range(self.nitems): ii, ik = self.e_indices[i] exemplar[i] = ik[N.argmax(N.take(e, ii))] if N.logical_and.reduce(exemplar == self.exemplar): convergence_count += 1 if convergence_count == convergence: break else: self.exemplar = exemplar iterations_left -= 1 if iterations_left == 0: raise ValueError("no convergence in %d iterations" % max_iterations) clusters = [] indices = N.arange(self.nitems) exemplar_indices = N.repeat(indices, self.exemplar == indices) for i in exemplar_indices: members = list(N.repeat(indices, self.exemplar == self.exemplar[i])) members.remove(i) members.insert(0, i) clusters.append([self.items[m] for m in members]) return clusters
backbone = chain.backbone() for i in range(len(breaks) - 1): residues = N.take(backbone, helix_indices[breaks[i]:breaks[i + 1]]) helices.append(Collection(list(residues))) helices = [h for h in helices if len(h) > 4] #Collection(helices).view() residue_motion_vectors = [] helix_motion_vectors = [] for helix in helices: end_to_end = helix[0].centerOfMass() - helix[-1].centerOfMass() cms, inertia = helix.centerAndMomentOfInertia() moments, axes = inertia.diagonalization() axes = [a.asVector() for a in axes] helix_axis = axes[N.argmax([abs(end_to_end * v) for v in axes])] hmv = ParticleVector(universe) helix_motion_vectors.append(hmv) for residue in helix: mv = ParticleVector(universe) mv[residue.C_alpha] = helix_axis.cross(residue.C_alpha.position() - cms) hmv[residue.C_alpha] = helix_axis.cross(residue.C_alpha.position() - cms) residue_motion_vectors.append(mv) residue_subspace = Subspace(universe, residue_motion_vectors) helix_subspace = Subspace(universe, helix_motion_vectors) frequencies = [] projections = []