Package SloppyCell :: Package Vandermonde :: Module clusterScripts
[hide private]

Source Code for Module SloppyCell.Vandermonde.clusterScripts

 1  import scipy 
 2   
 3  # see VdmPairwise for definition of r2PC12 and dtMatPC12 
 4  # 1. make cluster instance 
 5  # PC12cluster = cluster.HierarchicalClustering(range(48), lambda x,y: getDist(x,y,r2PC12/dtMatPC12)) 
 6  # 2. set desired linake method (see cluster.py for different options) 
 7  # PC12cluster.setLinkageMethod('single') 
 8  # 3. getlevel returns the clusters of items with requested distance of one another. 
 9  #    You must run this before you can ask for the topology 
10  # PC12cluster.getlevel(0.1) 
11  # 4. get topology (formatted as a nested tuple 
12  # PC12topo = PC12cluster.topo() 
13  # 5. convert topo into a permutation list 
14  # PC12permList = FlattenIt(PC12topo,r2PC12/dtMatPC12) 
15  # 6. get permutation matrix from permutation list 
16  # PC12permMat = makePermutationMatrix(permList) 
17  # 7. permute your jacobian 
18  # PC12permJac = scipy.dot(jacPC12,PC12permMat) 
19   
20 -def getDist(ind1,ind2,distMat):
21 """ 22 distMat is a distance matrix. distMat[i,j] == distance from i to j 23 """ 24 return distMat[ind1,ind2]
25
26 -def FlattenIt(x, distMat):
27 """ 28 Takes topo output from hierarchical clustering and orders the list at the bottom 29 level to correspond to the optimal permutation. 30 """ 31 if isinstance(x, int): 32 return [x] 33 a = FlattenIt(x[0], distMat) 34 b = FlattenIt(x[1], distMat) 35 # print a, b 36 d00 = distMat[a[0],b[0]] 37 d01 = distMat[a[0],b[-1]] 38 d10 = distMat[a[-1],b[0]] 39 d11 = distMat[a[-1],b[-1]] 40 dMin = min(d00, d01, d10, d11) 41 if d00 == dMin: 42 a.reverse() 43 elif d01 == dMin: 44 a.reverse() 45 b.reverse() 46 elif d11 == dMin: 47 b.reverse() 48 a.extend(b) 49 # print a 50 return a
51
52 -def makePermutationMatrix(permList):
53 """ 54 Takes a list defining the permutation and makes the appropriate matrix. 55 """ 56 permList = scipy.array(permList) 57 n = len(permList) 58 if 0 not in permList: 59 permList = permList - 1 60 permMat = scipy.zeros((n,n),'d') 61 for ii, jj in enumerate(permList): 62 permMat[ii,jj] = 1. 63 return scipy.transpose(permMat)
64 65
66 -def normColumns(mat1):
67 """ 68 Normalizes each column of a matrix. So far just for display purposes. 69 """ 70 n = len(mat1[0]) 71 mat2 = mat1.copy() 72 for ii in range(n): 73 mat2[:,ii] = mat2[:,ii]/scipy.sqrt(scipy.dot(mat2[:,ii],mat2[:,ii])) 74 return mat2
75
76 -def colorAlignColumns(mat1):
77 """ 78 Multiplies columns by +/- 1 to make them have positive dot product with neighboring 79 columns. So far just for display purposes. 80 """ 81 n = len(mat1[0]) 82 mat2 = mat1.copy() 83 for ii in range(n-1): 84 if scipy.dot(mat2[:,ii],mat2[:,ii+1]) < 0.: 85 mat2[:,ii+1] = -1.*mat2[:,ii+1] 86 return mat2
87