#!/usr/bin/python ########################################################################### # # MakePartition - Determining partition at threshold thresh # # MakePartition.run(treeF,leftF,rightF,heightF,nver,threshold) # # import sys; sys.path.append(r'D:\Vlado\work\R\Netana\ClusRel') # # import MakePartition # MakePartition.run('obTree.clu','obLeft.clu','obRight.clu','obHeight.vec',193,2.0) # # reload(MakePartition) # MakePartition.run('obTree.clu','obLeft.clu','obRight.clu','obHeight.vec',193,2.0) # # Vladimir Batagelj, 30. May 2007 # ########################################################################### import string, os def setPart(v,L): global height, left, right, thresh, Lmax, part if height[v] > thresh : part[v] = 0 if left[v] > 0 : setPart(left[v],L) if right[v] > 0 : Lmax = Lmax + 1 setPart(right[v],Lmax) else: part[v] = L if left[v] > 0 : setPart(left[v],L) if right[v] > 0 : setPart(right[v],L) def run(treeF,leftF,rightF,heightF,nver,threshold): global height, left, right, thresh, Lmax, part thresh = threshold workdir = 'd:\\vlado\\work\\R\\Netana\\ClusRel\\' fa = open(workdir+treeF, 'r') v = 0; line = fa.readline(); tree = [0]*(2*nver) for line in fa.readlines() : v = v + 1; tree[v] = eval(line) leng = v; fa.close() ls = open(workdir+leftF, 'r') v = 0; line = ls.readline(); left = [0]*(2*nver) for line in ls.readlines() : v = v + 1; left[v] = eval(line) ls.close() rs = open(workdir+rightF, 'r') v = 0; line = rs.readline(); right = [0]*(2*nver) for line in rs.readlines() : v = v + 1; right[v] = eval(line) rs.close() hi = open(workdir+heightF, 'r') v = 0; line = hi.readline(); height = [0.0]*(2*nver) for line in hi.readlines() : v = v + 1; height[v] = eval(line) hi.close() pa = open(workdir+'partition.clu', 'w') Lmax = 0; part = [0]*(2*nver) for t in range(1,leng+1): if tree[t] == 0: Lmax = Lmax + 1 setPart(t,Lmax) print "\n",Lmax," clusters in partition\n" pa.write('*vertices '+str(nver)+'\n') for t in range(1,nver+1): pa.write(str(part[t])+'\n') pa.close() print "end MakePartition\n"