/
machine_.py
495 lines (392 loc) · 12.9 KB
/
machine_.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
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 ij.io import FileSaver
from profilingFixedCoordinates import getDendriticProfiles
from profilingFixedCoordinates import getNodeCoordinates
from java.util import ArrayList
from java.text import DecimalFormat
from pcaFinal import pca
from scholl import sphereCount
##IMPORTANT: ID2 ALWAYS HAS TO BE THE TREE FORM THE RIGHT HANDSIDE OF LARVA
#------------Get distances-----------------
def profileDistances(ID, biniter):
histo=getDendriticProfiles(ID, biniter)
xhisto=histo[0]
yhisto=histo[1]
zhisto=histo[2]
xdistance=[]
ydistance=[]
zdistance=[]
tree = Display.getFront().getLayerSet().findById(ID)
coords = Matrix(getNodeCoordinates(tree))
m=coords.getRowDimension()
for i in range(0, m):
xdist = coords.get(i, 1 )
if xdist > 0 :
xdistance.append(xdist)
else:
xdistance.append((-1)*xdist)
for i in range(0, m):
ydist = coords.get(i, 0 )
if ydist > 0 :
ydistance.append(ydist)
else:
ydistance.append((-1)*ydist)
for i in range(0, m):
zdist = sqrt(coords.get(i, 0)**2 + coords.get(i,1)**2)
zdistance.append(zdist)
return xdistance, ydistance, zdistance
#--------Compute feature 1 -> Sum of distances-----------------
def sumDist(id1, biniter):
distance=profileDistances(id1, biniter)
xdistance=distance[0]
ydistance=distance[1]
zdistance=distance[2]
tree = Display.getFront().getLayerSet().findById(id1)
coords = Matrix(getNodeCoordinates(tree))
m=coords.getRowDimension()
xdist=[]
ydist=[]
zdist=[]
#Initialize interval size and fix iterations to a specific coordinate position
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
sumdist=0.0
for i in range(0, iterations):
for i in range(0, len(xdistance)):
if xdistance[i] <= xbinright and xdistance[i] >= xbinleft:
sumdist += xdistance[i]
xdist.append(sumdist)
sumdist=0.0
xbinleft += xlength
xbinright += xlength
for i in range(0, iterations):
for i in range(0, len(ydistance)):
if ydistance[i] <= ybinright and ydistance[i] >= ybinleft:
sumdist += ydistance[i]
ydist.append(sumdist)
sumdist=0.0
ybinleft += ylength
ybinright += ylength
for i in range(0, iterations):
for i in range(0, len(zdistance)):
if zdistance[i] <= zbinright and zdistance[i] >= zbinleft:
sumdist += zdistance[i]
zdist.append(sumdist)
sumdist=0.0
zbinleft += zlength
zbinright += zlength
sumdistance=[]
sumdistance= [xdist, ydist, zdist]
return sumdistance
# Own function to reverse elements of a list
def reversing(array):
liste=array
reverse=[]
runs=len(liste)
for l in range(0, runs):
element = liste[runs-l-1]
reverse.append(element)
return reverse
#Correct version of feature 1: compute sum of distances in a bin
def feature1(histo1, histo2, biniter):
diff1=histo1
diff2=histo2
"""
tree1 = Display.getFront().getLayerSet().findById(id1)
coords1 = Matrix(getNodeCoordinates(tree1))
m1=coords1.getRowDimension()
tree2 = Display.getFront().getLayerSet().findById(id2)
coords2 = Matrix(getNodeCoordinates(tree2))
m2=coords2.getRowDimension()
"""
xdiff1=diff1[3]
ydiff1=diff1[4]
zdiff1=diff1[5]
xdiff2=diff2[3]
#xdiff2.reverse() #####
xdiff2=reversing(xdiff2)
ydiff2=diff2[4]
zdiff2=diff2[5]
xdiff=0
ydiff=0
zdiff=0
euclidean=[]
for i in range(0, len(xdiff1)):
xdiff += abs(xdiff1[i]-xdiff2[i]) #Normalize differences with number of nodes! REMOVED
ydiff += abs(ydiff1[i]-ydiff2[i])
zdiff += abs(zdiff1[i]-zdiff2[i])
"""
#NORMALIZE BY NUMBER OF NODES-----NEW-------
xdiff=xdiff/m
ydiff=ydiff/m
zdiff=zdiff/m
#---------------------------
"""
summeandist=[xdiff, ydiff, zdiff]
return summeandist
#----------Compute second feature -> Sum of Euclidean distances bincount-----------
def feature2(histo1, histo2, biniter):
#histo1=getDendriticProfiles(id1, biniter)
#histo2=getDendriticProfiles(id2, biniter)
xhisto1=histo1[0]
yhisto1=histo1[1]
zhisto1=histo1[2]
xhisto2=histo2[0]
#xhisto2.reverse() #####
xhisto2=reversing(xhisto2)
yhisto2=histo2[1]
zhisto2=histo2[2]
xdiff=0
ydiff=0
zdiff=0
euclidean=[]
for i in range(0, len(histo1[0])):
xdiff += (xhisto1[i]-xhisto2[i])**2
ydiff += (yhisto1[i]-yhisto2[i])**2
zdiff += (zhisto1[i]-zhisto2[i])**2
euclidean=[sqrt(xdiff), sqrt(ydiff), sqrt(zdiff)]
return euclidean
#---------Compute third feature -> Standart deviation of node-count histogram----
def stdev(histo1, biniter):
#histo1=getDendriticProfiles(id1, biniter)
xhisto=histo1[0]
yhisto=histo1[1]
zhisto=histo1[2]
standard=[]
n, mean, std = len(xhisto), 0, 0
for a in xhisto:
mean = mean + a
mean = mean / float(n)
for a in xhisto:
std = std + (a - mean)**2
std = sqrt(std / float(n-1))
standard.append(std)
n, mean, std = len(yhisto), 0, 0
for a in yhisto:
mean = mean + a
mean = mean / float(n)
for a in yhisto:
std = std + (a - mean)**2
std = sqrt(std / float(n-1))
standard.append(std)
n, mean, std = len(zhisto), 0, 0
for a in zhisto:
mean = mean + a
mean = mean / float(n)
for a in zhisto:
std = std + (a - mean)**2
std = sqrt(std / float(n-1))
standard.append(std)
return standard
#------Compute Standartdeviation sum diff of histograms------------
def feature3(histo1, histo2, biniter):
histo1=stdev(histo1, biniter)
histo2=stdev(histo2, biniter)
xhisto1=histo1[0]
yhisto1=histo1[1]
zhisto1=histo1[2]
xhisto2=histo2[0]
yhisto2=histo2[1]
zhisto2=histo2[2]
euclidean=[]
xdiff = (xhisto1-xhisto2)**2
ydiff = (yhisto1-yhisto2)**2
zdiff = (zhisto1-zhisto2)**2
euclidean=[sqrt(xdiff), sqrt(ydiff), sqrt(zdiff)]
return euclidean
#----Compute a simple difference of both histograms
def feature4(histo1, histo2, biniter):
#isto1=getDendriticProfiles(id1, biniter)
#histo2=getDendriticProfiles(id2, biniter)
xhisto1=histo1[0]
yhisto1=histo1[1]
zhisto1=histo1[2]
#xhisto2=histo2[0]
xhisto2=reversing(histo2[0])
yhisto2=histo2[1]
zhisto2=histo2[2]
xdiff=0
ydiff=0
zdiff=0
histodiff=[]
x=[]
y=[]
z=[]
for i in range(0, biniter):
xdiff = abs(xhisto1[i]-xhisto2[i])
x.append(xdiff)
ydiff = abs(yhisto1[i]-yhisto2[i])
y.append(ydiff)
zdiff = abs(zhisto1[i]-zhisto2[i])
z.append(zdiff)
histodiff = x + y + z
return histodiff
#-------Input the raw HistogramVector as training Vector
def rawHistogramVector(id1, id2, biniter):
if not id1 in iddict:
iddict[id1]=getDendriticProfiles(id1, biniter)
if not id2 in iddict:
iddict[id2]=getDendriticProfiles(id2, biniter)
histo1=iddict[id1]
histo2=iddict[id2]
xhisto1=histo1[0]
yhisto1=histo1[1]
zhisto1=histo1[2]
xhisto2=histo2[0]
xhisto2=reversing(xhisto2)
yhisto2=histo2[1]
zhisto2=histo2[2]
trainingvector = []
trainingvector = xhisto1 + yhisto1 + zhisto1 + xhisto2 + yhisto2 + zhisto2
return trainingvector
#--------Features combined--------------------
#Save all the histograms in iddict to increase overall performance
global iddict
iddict={}
#---------------------------------------------
def featureVector(id1, id2, biniter):
if not id1 in iddict:
iddict[id1]=getDendriticProfiles(id1, biniter)
if not id2 in iddict:
iddict[id2]=getDendriticProfiles(id2, biniter)
histo1=iddict[id1]
histo2=iddict[id2]
trainingvector =feature1(histo1,histo2, biniter) + feature2(histo1, histo2, biniter) + feature3(histo1, histo2, biniter) + feature4(histo1, histo2, biniter)
return trainingvector
#print featureVector(72481, 99481, 50)
#-----------------sum of all features for a given pair-----------
def featureMeasure(idpair, biniter):
""" input a pair of 2 possible matches as follows: [id1, id2]. Return sum of featureVector"""
trainingvector = featureVector(idpair[0], idpair[1], biniter)
sumMeasure=0
for s in trainingvector:
sumMeasure+=s
return sumMeasure
#------------raw Data------------------------
def rawFeatureVector(id2, id1, biniter):
if not id1 in iddict:
iddict[id1]=getDendriticProfiles(id1, biniter)
if not id2 in iddict:
iddict[id2]=getDendriticProfiles(id2, biniter)
histo1=iddict[id1]
histo2=iddict[id2]
xhisto1=histo1[0]
yhisto1=histo1[1]
zhisto1=histo1[2]
xhisto2=histo2[0]
yhisto2=histo2[1]
zhisto2=histo2[2]
#Calculation of sum of distances in bin is now included in
#getDendriticProfiles function to reduce runtime
xsumDist1 = histo1[3]
ysumDist1 = histo1[4]
zsumDist1 = histo1[5]
xsumDist2 = histo2[3]
ysumDist2 = histo2[4]
zsumDist2 = histo2[5]
rawVector = xhisto1 + xhisto2 + yhisto1 + yhisto2 + zhisto1 + zhisto2 + xsumDist1 +xsumDist2 + ysumDist1 + ysumDist2 + zsumDist1 + zsumDist2
return rawVector
#-------Special dictionary for sphereCount and PCA------------------
pdict={}
sdict={}
#---------------Combinatin of PCA and Scholl-like vector------------
def pcaSphereVector(id1, id2, biniter):
if not id1 in (pdict and sdict):
pdict[id1]=pca(id1, biniter)
sdict[id1]=sphereCount(id1, biniter)
if not id2 in (pdict and sdict):
pdict[id2]=pca(id2, biniter)
sdict[id2]=sphereCount(id2, biniter)
sphere1 = sdict[id1]
sphere2 = sdict[id2]
s1 = sphere1[0]
s2 = sphere2[0]
pca1 = pdict[id1]
pca2 = reversing(pdict[id2])
pcadiff=[]
spherediff=[]
#for i in range(0, len(pca1)):
# pdiff = abs(pca1[i]) - abs(pca2[i])
# pcadiff.append(pdiff)
# sdiff = abs(s1[i]) - abs(s2[i])
# spherediff.append(sdiff)
trainingvector=[]
trainingvector = sphere1[0] + sphere2[0] + pca1 + pca2 #+feature4(id1, id2, biniter)
return trainingvector
#-----------------Machine Learning-------------------------------------------
from weka.core import DenseInstance, Instances, Attribute
from weka.classifiers import AbstractClassifier
from hr.irb.fastRandomForest import FastRandomForest
def createClassifier(numTrees, numFeatures, seeds):
rf = FastRandomForest()
rf.setNumTrees(numTrees)
rf.setNumFeatures(numFeatures)
rf.setSeed(seeds) #initially 123
return rf
def createAttributes(featureVector):
numFeatures = len(featureVector)
attributes = [Attribute(str(i) + " numeric") for i in range(numFeatures)]
attributes.append(Attribute("class", ArrayList(["true", "false"])))
instances = Instances("tests", ArrayList(attributes), 1)
instances.setClassIndex(len(attributes) -1)
return ArrayList(attributes)
def createTrainingInstances(matchingExamples, mismatchingExamples):
""" Expects the matchingExamples to be a list of feature lists,
i.e. the feature vector is a list. """
numFeatures = len(matchingExamples[0])
attributes = [Attribute(str(i) + " numeric") for i in range(numFeatures)]
attributes.append(Attribute("class", ArrayList(["true", "false"])))
trainingData = Instances("matches", ArrayList(attributes), len(matchingExamples) + len(mismatchingExamples))
trainingData.setClassIndex(len(attributes) -1) # the last index
for f in matchingExamples:
trainingData.add(DenseInstance(1.0, f + [1])) # 1 is True
for f in mismatchingExamples:
trainingData.add(DenseInstance(1.0, f + [0])) # 0 is False
return trainingData
def trainClassifier(classifier, matchingExamples, mismatchingExamples):
ti = createTrainingInstances(matchingExamples, mismatchingExamples)
classifier.buildClassifier(ti)
df = DecimalFormat("0.0000")
return df.format(classifier.measureOutOfBagError()) #print
"""
def trainClassifier(classifier, matchingExamples, mismatchingExamples):
try:
ti = createTrainingInstances(matchingExamples, mismatchingExamples)
classifier.buildClassifier(ti)
df = DecimalFormat("0.0000")
print "out of bag error:", df.format(classifier.measureOutOfBagError()) #print
except:
import sys, traceback
print 'error:', traceback.print_exc(file=sys.stdout) # sys.exc_info()
"""
def classify(classifier, matches):
""" Expects one vector numFeatures length """
""" returns a list of [result, distributionforinstance match]"""
attributes = createAttributes(matches[0])
instances = Instances("tests", attributes, 1)
instances.setClassIndex(len(attributes) -1)
distribution=[] ###
for match in matches:
instances.add(DenseInstance(1.0, match + [0]))
for i in range(len(matches)):
result=classifier.classifyInstance(instances.instance(i))
dist=(classifier.distributionForInstance(instances.instance(i)))
results=[result, dist[1]]
return results