def numarray_min(inputarray): ''' faster than numarray.nd_image.min ''' f = numarray.ravel(inputarray) i = numarray.argmin(f) return float(f[i])
def ChooseVertex(self): """ Chooses a vertex from the list according to criterion : Selection Criterion : Choose the node that causes the least number of edges to be added in step 2b, breaking ties by choosing the nodes that induces the cluster with the smallest weight Implementation in Graph.ChooseVertex() The WEIGHT of a node V is the nmber of values V can take (BVertex.nvalues) The WEIGHT of a CLUSTER is the product of the weights of its constituent nodes Only works with graphs composed of BVertex instances """ vertices = self.all_v # for each vertex, check how many edges will be added edgestoadd = [0 for v in vertices] clusterweight = [1 for v in vertices] for v,i in zip(vertices, range(len(vertices))): cluster = [a.name for a in v.adjacent_v] cluster.append(v.name) clusterleft = copy.copy(cluster) # calculate clusterweight for c in cluster: clusterweight[i] *= self.v[c].nvalues for v1 in cluster: clusterleft.pop(0) for v2 in clusterleft: if not v1 in [a.name for a in self.v[v2].adjacent_v]: edgestoadd[i] += 1 # keep only the smallest ones, the index minedges = min(edgestoadd) mini = [vertices[i] for e, i in zip(edgestoadd, \ range(len(edgestoadd))) if e == minedges] # from this list, pick the one that has the smallest clusterweight = nvalues # this only works with BVertex instances v = mini[na.argmin([clusterweight[vertices.index(v)] for v in mini])] return v