1 import scipy
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
21 """
22 distMat is a distance matrix. distMat[i,j] == distance from i to j
23 """
24 return distMat[ind1,ind2]
25
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
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
50 return a
51
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
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
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