def clustercost(data,objectDict,baseline = 0.001): # data is a tuple of two dictionaries: core cluster data first, then larger and more permissive clusters\ # this func needs to return a unified list of possible clusters using both dictionaries in the style of the chain finder function # quick and dirty: smallClusters = [] bigClusters = [] #cores for i in data[0].values(): corePO = map(lambda x: objectDict.get(x),i) coreHull = cluster_util.convex_hull(corePO) try: density = len(i)/cluster_util.area(coreHull) except: density = 0 if density>=1: print "groups are too dense, things may be weird." cost = len(i)*(1-density) + baseline corecluster=cluster_util.GroupBundle(i,cost) smallClusters.append(corecluster) #fringes for i in data[1].values(): fringePO = map(lambda x: objectDict.get(x),i) fringeHull = cluster_util.convex_hull(fringePO) try: density = len(i)/cluster_util.area(fringeHull) except: density = 0 if density>=1: print "groups are too dense, things may be weird." cost = len(i)*(1-density) + baseline bigcluster = cluster_util.GroupBundle(i,cost) bigClusters.append(bigcluster) return (smallClusters,bigClusters)
def clustercost(data, objectDict, baseline=0.001): # data is a tuple of two dictionaries: core cluster data first, then larger and more permissive clusters\ # this func needs to return a unified list of possible clusters using both dictionaries in the style of the chain finder function # quick and dirty: smallClusters = [] bigClusters = [] #cores for i in data[0].values(): corePO = map(lambda x: objectDict.get(x), i) coreHull = cluster_util.convex_hull(corePO) try: density = len(i) / cluster_util.area(coreHull) except: density = 0 if density >= 1: print "groups are too dense, things may be weird." cost = len(i) * (1 - density) + baseline corecluster = cluster_util.GroupBundle(i, cost) smallClusters.append(corecluster) #fringes for i in data[1].values(): fringePO = map(lambda x: objectDict.get(x), i) fringeHull = cluster_util.convex_hull(fringePO) try: density = len(i) / cluster_util.area(fringeHull) except: density = 0 if density >= 1: print "groups are too dense, things may be weird." cost = len(i) * (1 - density) + baseline bigcluster = cluster_util.GroupBundle(i, cost) bigClusters.append(bigcluster) return (smallClusters, bigClusters)
def clusterCostWorker(data,objectDict,maxDiscountRatio,strong=False): #maxDiscountRatio is the maximum amount of the groups cost that can be deducted #for membership in a group. Higher values will make groups more favorable compared # to lines. output = [] for i in data: clusterMembers = map(lambda x: objectDict.get(x),i) hullAroundMembers = cluster_util.convex_hull(clusterMembers) areaOfMembers = sum(cluster_util.bb_area(objectDict.get(p).representation.rect) for p in i) try: density = areaOfMembers/cluster_util.area(hullAroundMembers) except: density = 0 if density>=1: density = 1 cost = len(i)*(1-density**2)*maxDiscountRatio + len(i)*(1-maxDiscountRatio) # corecluster=cluster_util.GroupBundle(i,cost,hullAroundMembers) corecluster=landmark.GroupRectangleRepresentation([objectDict.get(j) for j in i],None,cost) corecluster.density = density corecluster.isStrong = strong output.append(corecluster) return output