Clustering is very simple. The example generates some random points in clusters and extracts the clusters. To cluster density, configurations or particles, replace the IMP.statistics.Vector3DEmbedding with a IMP.statistics.Vector3DEmbedding with a IMP::statistics::HighDensityEmbedding, IMP::statistics::ConfigurationSetXYZEmbedding or a IMP::statistics::ParticleEmbedding.
import IMP.algebra
import IMP.statistics
# generate some clusters of points
vs= []
centers=(IMP.algebra.Vector3D(0,0,0),
IMP.algebra.Vector3D(10,15,20),
IMP.algebra.Vector3D(60,30,12))
for i in range(0,3):
for j in range(0,100):
vs.append(IMP.algebra.get_random_vector_in(IMP.algebra.Sphere3D(centers[i], 10)))
# cluster them into 3 clusters
3, 1000)
# print out the cluster results
print c.get_cluster_center(0)
print c.get_cluster_center(1)
print c.get_cluster_center(2)
|
This simple example shows how to write an IMP.statistics.Metric in python.
import IMP.statistics
import math
import random
class MyMetric(IMP.statistics.Metric):
"""Define a metric on a list of floating point numbers based on their difference"""
def __init__(self, nums):
"""Store the list of numbers to measure distances between"""
IMP.statistics.Metric.__init__(self, "MyMetric%1%")
self._nums=nums
def get_distance(self, i, j):
"""Return the magnitude of the distance between the ith and jth number"""
return math.fabs(self._nums[i]-self._nums[j])
def get_number_of_items(self):
return len(self._nums)
mm= MyMetric([random.uniform(0,1) for i in range(0,15)])
cc= IMP.statistics.create_centrality_clustering(mm, .1, 3)
print cc.get_number_of_clusters()
|