-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcaFinal.py
132 lines (112 loc) · 4.1 KB
/
pcaFinal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#Invariant tree-analysis with the PCA method
from math import sqrt
from ini.trakem2.display import Display
from jarray import array
import sys
sys.path.append("/Users/berthola/Desktop/Fiji Scripts")
from matrixoperator import Matrix
from javax.media.j3d import Transform3D
from javax.vecmath import Point3d
#from org.jfree.data.statistics import HistogramDataset
m = [0.9509217490842972, 0.24926674965164644, -0.18334097915241027, -51626.05759575438,
-0.2722540786706435, 0.9555797748775074, -0.11289380183274611, -28182.499757448437,
0.14705626054561627, 0.15726850086128405, 0.9765454801857332, -12950.550433910958,
0.0, 0.0, 0.0, 1.0]
wt = Transform3D(m)
def correct(pa):
global wt
p = Point3d(pa[0], pa[1], pa[2])
wt.transform(p)
return [p.x, p.y, p.z]
def getNodeCoordinates(tree):
""" Returns a map of Node instances vs. their X,Y,Z world coordinates. """
root = tree.getRoot()
if root is None:
return {}
calibration = tree.getLayerSet().getCalibration()
affine = tree.getAffineTransform()
coords = []
for nd in root.getSubtreeNodes():
fp = array([nd.getX(), nd.getY()], 'f')
affine.transform(fp, 0, fp, 0, 1)
x = fp[0] * calibration.pixelWidth
y = fp[1] * calibration.pixelHeight
z = nd.getLayer().getZ() * calibration.pixelWidth # a TrakEM2 oddity
# data may be a radius or a java.awt.geom.Area
coords.append( correct([x, y, z]) )
#print coords[len(coords)-1]
return coords
#--------PCA-------------
def pca(ID, biniter):
tree = Display.getFront().getLayerSet().findById(ID)
#Calculate the center of mass
center = Matrix( [[0,0,0]] )
coords = Matrix(getNodeCoordinates(tree))
m=coords.getRowDimension()
for i in range(0, coords.getRowDimension()):
center += Matrix([coords.getRow( i )])
center /= float(m)
#print center
"""
x, y, z = 0, 0, 0
nc = getNodeCoordinates(tree)
for c in nc:
x += c[0]
y += c[1]
z += c[2]
print "center:", x/len(nc), y/len(nc), z/len(nc)
"""
#Define covvariance matrix
cova = Matrix([[0,0,0],[0,0,0],[0,0,0]])
diff= Matrix ( [[0,0,0]])
for i in range(0, m):
diff = Matrix([coords.getRow( i )]) - center
cova += diff.transpose() * diff
cova /= float(m)
#Evaluedecomposition
evaluedecomp = cova.eig()
evalues = evaluedecomp.getRealEigenvalues()
evectors = evaluedecomp.getV() # is a Matrix instance
#Find maximum Eigenvector for maximal Eigenvalue
maxevaluepos = -1
maxevalue = 0
for i in range(0, len(evalues)):
if evalues[i] > maxevalue or maxevaluepos==-1:
maxevalue=evalues[i]
maxevaluepos=i
maxevector=evectors.getColumn(maxevaluepos) #HAVE TO GET VALUES OF COLUMN of evector matrix i NOT ROW
#--------------------TEST if Eigenvaluedecomposition is correct-----------
#vectormatrix=Matrix([evectors.getColumn(maxevaluepos)])
#test=cova * vectormatrix.transpose()
#test2=vectormatrix*maxevalue
#print test
#print test2
#-------------------------------------------------------------------------
#Define a vector over the point cloud and count points in defined interval
#Normalize vector
length=sqrt(maxevector[0]**2 + maxevector[1]**2 + maxevector[2]**2)
normvector = map(lambda x: x / length, maxevector)
normvectormatrix = Matrix([normvector])
pca=[]
m=coords.getRowDimension()
for i in range(0, m):
pcastore = (Matrix([coords.getRow( i )]) - center ) * normvectormatrix.transpose()
pca.append(pcastore.getRow(0))
#Count number of nodes which fall in defined interval of pca projection -> has to be fixed globally when to compare PCAs!
counter = 0
histovector = []
pca=[ x[0] for x in pca] #get it back in array form (get rid of list in list)
binleft = -12500 #binleft = min(pca)
iterations = biniter
length = int(25000/iterations + 0.5) #int((max(pca)-min(pca))/iterations + 0.5)
binright = binleft + length
for i in range(0,iterations):
for i in range(0, len(pca)):
if pca[i] <= binright and pca[i] >= binleft:
counter += 1
histovector.append(counter)
counter=0
binleft+=length
binright+=length
#print "The histogram vector is:", histovector
return histovector