-
Notifications
You must be signed in to change notification settings - Fork 0
/
profilingFixedCoordinates.py
179 lines (148 loc) · 5.04 KB
/
profilingFixedCoordinates.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#Project all nodes on to the 3 main axis and calculate the sum of the distances of all points to that axis in the bin
from math import sqrt
from ini.trakem2.display import Display
from jarray import array, zeros
import sys
#sys.path.append(System.getProperty("user.home") + "/Desktop/Fiji Scripts")
sys.path.append("/Users/berthola/Desktop/Fiji Scripts")
from matrixoperator import Matrix
from javax.media.j3d import Transform3D
from javax.vecmath import Point3d
from ij.io import FileSaver
from java.util import HashSet
from ini.trakem2.vector import VectorString3D, VectorString, Editions
from org.jfree.data.statistics import HistogramDataset
#-------------Transformation-------------------------------------
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]
#-----------Get Coordinates of tree------------------------------------
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
#-----------Get Dendritic Profiles-------------------------------------
# Included additional string to count the distances of all nodes to the axis in that bin
def getDendriticProfiles(ID, biniter):
tree = Display.getFront().getLayerSet().findById(ID)
coords = Matrix(getNodeCoordinates(tree))
#Define axis vectors and origin
xnorm = Matrix([[1,0,0]])
ynorm = Matrix([[0,1,0]])
znorm = Matrix([[0,0,1]])
center= Matrix([[0,0,0]])
#Project nodes onto axis
dpx=[]
dpy=[]
dpz=[]
m=coords.getRowDimension()
for i in range(0, m):
xstore = Matrix([coords.getRow( i )]) * xnorm.transpose()
ystore = Matrix([coords.getRow( i )]) * ynorm.transpose()
zstore = Matrix([coords.getRow( i )]) * znorm.transpose()
dpx.append(xstore.getRow(0))
dpy.append(ystore.getRow(0))
dpz.append(zstore.getRow(0))
#Count number of nodes which fall in defined interval of pca projection
xhistovector = []
yhistovector = []
zhistovector = []
#get it back in array form (get rid of list in list of Jarray)
dpx=[ x[0] for x in dpx]
dpy=[ x[0] for x in dpy]
dpz=[ x[0] for x in dpz]
#Initialize interval size and fix iterations to a specific number
xbinleft = -34600.0
ybinleft = -24200.0
zbinleft = 0
#--------PARAMETER FOR MACHINE LEARNING-----
iterations = biniter
#-------------------------------------------
xlength = int((35600.0+34600.0)/iterations + 0.5)
ylength = int((21300.0+24200.0)/iterations + 0.5)
zlength = int((22500)/iterations + 0.5)
xbinright = xbinleft + xlength
ybinright = ybinleft + ylength
zbinright = zbinleft + zlength
#Count elements in bins
xaxis=[]
#Count lengths in bins
xdist=[]
ydist=[]
zdist=[]
for i in range(0,iterations):
counter=0
xdiff=0
for i in range(0, len(dpx)):
if dpx[i] <= xbinright and dpx[i] >= xbinleft:
counter += 1
xdiff += sqrt(dpy[i]**2 + dpz[i]**2) #sqrt(dpy[i]**2) xdist is yvalue
if counter != 0:
xdiff /= counter
xdist.append(xdiff)
xhistovector.append(counter)
xaxis.append(xbinleft)
xbinleft += xlength
xbinright += xlength
yaxis=[]
for i in range(0,iterations):
counter=0
ydiff = 0
for i in range(0, len(dpy)):
if dpy[i] <= ybinright and dpy[i] >= ybinleft:
counter += 1
ydiff += sqrt(dpx[i]**2 + dpz[i]**2)
if counter != 0:
ydiff /= counter
ydist.append(ydiff)
yhistovector.append(counter)
yaxis.append(ybinleft)
ybinleft += ylength
ybinright += ylength
zaxis=[]
for i in range(0,iterations):
counter=0
zdiff=0
for i in range(0, len(dpz)):
if dpz[i] <= zbinright and dpz[i] >= zbinleft:
counter += 1
zdiff += sqrt(dpx[i]**2 + dpy[i]**2)
if counter != 0:
zdiff /= counter
zdist.append(zdiff)
zhistovector.append(counter)
zaxis.append(zbinleft)
zbinleft += zlength
zbinright += zlength
#print xdist
#print ydist
#print zdist
"""
#Normalize
for i in range(0, iterations):
xhistovector[i]/=float(xlength)
yhistovector[i]/=float(ylength)
zhistovector[i]/=float(zlength)
"""
return xhistovector , yhistovector, zhistovector, xdist, ydist, zdist