def Qparameter(parts, distfunc=None): """ Calculates the minimum spanning tree Q parameter (Cartwright & Whitworth 2004) for a projection of the particle set. :argument distfunc: distfunc is the distance function which can be used to select the projection plane. """ if distfunc is None: def distfunc(p,q): return (((p.x-q.x)**2+(p.y-q.y)**2)**0.5).value_in(p.x.unit) N=len(parts) graph=Graph() for p in parts: d=distfunc(p,parts) for i,q in enumerate(parts): if p!=q: graph.add_edge(p,q, d[i] ) all_edges=graph.all_edges() ml=reduce(lambda x,y: x+y[0],all_edges,zero )/len(all_edges) mst=MinimumSpanningTreeFromEdges(all_edges) mlmst=reduce(lambda x,y: x+y[0],mst, zero )/len(mst) # normalize mlmst=mlmst/(N*numpy.pi)**0.5*(N-1) return mlmst/ml
def Qparameter(parts, distfunc=None): """ Calculates the minimum spanning tree Q parameter (Cartwright & Whitworth 2004) for a projection of the particle set. :argument distfunc: distfunc is the distance function which can be used to select the projection plane. """ if distfunc is None: def distfunc(p, q): return (((p.x - q.x)**2 + (p.y - q.y)**2)**0.5).value_in(p.x.unit) N = len(parts) graph = Graph() for p in parts: d = distfunc(p, parts) for i, q in enumerate(parts): if p != q: graph.add_edge(p, q, d[i]) all_edges = graph.all_edges() ml = reduce(lambda x, y: x + y[0], all_edges, zero) / len(all_edges) mst = MinimumSpanningTreeFromEdges(all_edges) mlmst = reduce(lambda x, y: x + y[0], mst, zero) / len(mst) # normalize mlmst = mlmst / (N * numpy.pi)**0.5 * (N - 1) return mlmst / ml
def minimum_spanning_tree_length(particles): """ Calculates the length of the minimum spanning tree (MST) of a set of particles using David Eppstein's Python implemention of Kruskal's algorithm. """ graph = Graph() for particle in particles: others = particles - particle distances = (particle.position - others.position).lengths() for other, distance in zip(others, distances): graph.add_edge(particle, other, distance) return sum([edge[0] for edge in MinimumSpanningTree(graph)], zero)
def connected_components(parts, treshold=None, distfunc=None): if distfunc is None: def distfunc(p, q): return ((p.x - q.x)**2 + (p.y - q.y)**2 + (p.z - q.z)**2)**0.5 print "making graph" graph = Graph() for p in parts: graph.add_node(p) d = distfunc(p, parts) edges = [(d[i], p, q) for i, q in enumerate(parts) if p != q] edges = filter(lambda x: x[0] < treshold, edges) for e in edges: graph.add_edge(e[1], e[2], e[0]) print "done" cc = ConnectedComponents(graph) print "number of edges:", len(graph.all_edges()) print "number of CC:", len(cc) return graph, cc