-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcombinePermutationsRefactored.py
589 lines (417 loc) · 21.6 KB
/
combinePermutationsRefactored.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# by Facundo Sosa-Rey, 2021. MIT license
import multiprocessing
from tifffile.tifffile import imwrite
from extractCenterPoints import getTiffProperties
from fibers import fiberObj
from trackingFunctions import fiberPloting
from trackingParameters import getTrackingParams
from combineFunctions import compactifySlice,findCollisions,compactify
from postProcessing import collisionDetectionWrapper
from tifffile import TiffFile,imwrite
import pickle
from joblib import Parallel, delayed
import os
import time
import numpy as np
def combinePermutations(
commonPath,
permutationPaths,
makePlot=False,
parallelHandle=True
):
permutationIndex=0
permutationPath=permutationPaths[permutationIndex]
print("\n\tcombinePermutation() called on dataset: \n{}".format(commonPath))
print("\t\treading from disk")
tic=time.perf_counter()
###################################################################
### check if compactification has already been performed
filesInDir = [f.path for f in os.scandir(commonPath) if f.is_file()]
indexFiberMapTiffCompactified =fiberStrucCompactifiedPickle =None
indexFiberMapTiffCompactified132=indexFiberMapTiffCompactified321=None
for i,iPath in enumerate(filesInDir):
if "V_fiberMapCompactified.tiff" in iPath:
indexFiberMapTiffCompactified=i
if "fiberStruct_compactified.pickle" in iPath:
fiberStrucCompactifiedPickle=i
if "V_fiberMap132_Compactified.tiff" in iPath:
indexFiberMapTiffCompactified132=i
if "V_fiberMap321_Compactified.tiff" in iPath:
indexFiberMapTiffCompactified321=i
if indexFiberMapTiffCompactified is None:
#first time through
print("\tCompactifying data...")
V_fiberMapCompactified,\
fiberStruct_compactified,\
V_fiberMap132,\
V_fiberMap321,\
fibers132,\
fibers321,\
xRes,\
unitTiff,\
descriptionStr,\
exclusiveZone=compactify(commonPath,permutationPaths,parallelHandle=parallelHandle)
print(f"\tWriting compactified fiberMap to disk at\n {commonPath}V_fiberMapCompactified.tiff")
imwrite(
os.path.join(commonPath,'V_fiberMapCompactified.tiff'),
V_fiberMapCompactified,
resolution=(xRes,xRes,unitTiff),
description=descriptionStr,
compress=True
)
imwrite(
os.path.join(commonPath,'V_fiberMap132_Compactified.tiff'),
V_fiberMap132,
resolution=(xRes,xRes,unitTiff),
description=descriptionStr,
compress=True
)
imwrite(
os.path.join(commonPath,'V_fiberMap321_Compactified.tiff'),
V_fiberMap321,
resolution=(xRes,xRes,unitTiff),
description=descriptionStr,
compress=True
)
fiberStructPickle={
"fiberStructCompactified" :fiberStruct_compactified,
"fiberObj_classAttributes":fiberObj.classAttributes, #otherwise class attributes are not pickled
"fibers132" :fibers132, # these have the new IDs (compactified)
"fibers321" :fibers321,
"exclusiveZone" :exclusiveZone
}
with open(os.path.join(commonPath,"fiberStruct_compactified.pickle"),"wb") as f:
pickle.dump(fiberStructPickle,f,protocol=pickle.HIGHEST_PROTOCOL)
else:
print("\tFound previously compactified data, loading...")
with TiffFile(filesInDir[indexFiberMapTiffCompactified]) as tif:
xRes,unitTiff,descriptionStr=getTiffProperties(tif,getDescription=True)
V_fiberMapCompactified =tif.asarray()
#load fiberMap from other permutations, to check for collisions.
#these fiberMaps have the compactified (dense, not spare) fiberIDs
with TiffFile(filesInDir[indexFiberMapTiffCompactified132]) as tif:
V_fiberMap132 =tif.asarray()
with TiffFile(filesInDir[indexFiberMapTiffCompactified321]) as tif:
V_fiberMap321 =tif.asarray()
with open(filesInDir[fiberStrucCompactifiedPickle], "rb") as f:
fiberStruct_compactified_all = pickle.load(f)
fiberStruct_compactified=fiberStruct_compactified_all["fiberStructCompactified"]
fibers132=fiberStruct_compactified_all["fibers132"]
fibers321=fiberStruct_compactified_all["fibers321"]
# loading class attributes from 123 allows checking for collisions at filling(),
# with any of the fibers from 123
fiberObj.classAttributes=fiberStruct_compactified_all["fiberObj_classAttributes"]
exclusiveZone=fiberStruct_compactified_all["exclusiveZone"]
del fiberStruct_compactified_all
### permutation123
filesInDir = [f.path for f in os.scandir(os.path.join(commonPath,permutationPath)) if f.is_file()]
watershedFound=False
if makePlot:
print("\tLoading V_hist for plotting purposes")
indexHistTiff123 =None
for i,iPath in enumerate(filesInDir):
if "V_hist.tiff" in iPath:
indexHistTiff123=i
if indexHistTiff123 is None :
raise FileNotFoundError(f"missing files in {os.path.join(commonPath,permutationPath)}")
with TiffFile(filesInDir[indexHistTiff123]) as tif:
V_hist=tif.asarray()/255
else:
V_hist=None
params=getTrackingParams(commonPath,"secondPass",xRes,unitTiff)
smartStitchingMinFibLength =params["smartStitchingMinFibLength"]
smartStitchingMaxDistance =params["smartStitchingMaxDistance"]
smartStitchingMaxLateralDist =params["smartStitchingMaxLateralDist"]
smartStitchingAlignAngle =params["smartStitchingAlignAngle"]
smartStitchingBackTrackingLimit =params["smartStitchingBackTrackingLimit"]
processingMinFiberLength =params["processingMinFiberLength"]
tagAngleTooSteep =params["tagAngleTooSteep"]
maxSteepnessAngle =params["maxSteepnessAngle"]
# include123 =params["include123"]
doSecondPass =params["doSecondPass"]
doLastPass =params["doLastPass"]
verboseHandle =params["verboseHandle"]
preventSelfStitch =params["preventSelfStitch_123_123"]
toc=time.perf_counter()
print("\t\treading from disk complete in {: >6.2f} s".format(toc-tic))
####################################################################################
### check for collisions between fibers in 132 and 321
####################################################################################
# by design, there can't be any collisions from V_fiberMap123-> no centroid allowed in regions masked by fiber detections in 123
maxAll132,maxAll321,V_collisions=findCollisions(V_fiberMap132,V_fiberMap321)
#########################################################
### create new fiberObj from colliding voxels
minCountCombination=100
angleCombineDEG=30. #max angle difference to allow combination of fibers from different permutations
offset_nextAvailableID=1
doCombineFibers=True
fiberStruct_combined={}
if doCombineFibers:
for fiberID132,dictF in maxAll132.items():
fiberID321=dictF["fiberID"]
counts=dictF["counts"]
if fiberID132==5594:
print()
# Only combine fibers if minimal number of pixels are interfering
if counts>minCountCombination:
fibObj321=fibers321[fiberID321]
oriVec321=fibObj321.orientationVec/np.linalg.norm(fibObj321.orientationVec)
oriVec132=fibers132[fiberID132].orientationVec/np.linalg.norm(fibers132[fiberID132].orientationVec)
angle=np.degrees(np.arccos(np.dot(oriVec132,oriVec321)))
if angle>90:
angle=180-angle
if angle<angleCombineDEG:
# keep uncombined Obj for plotting purposes
newFibObj=fibers132[fiberID132].copy()
nextID= len(fibers132)+offset_nextAvailableID
offset_nextAvailableID+=1
fibers132[nextID]=newFibObj
fibers132[fiberID132].combine(fibObj321)
fibers132[fiberID132].setColor("combined")
fiberStruct_combined[fiberID132]=fibers132[fiberID132]
if fiberID321 in fiberStruct_compactified.keys():
fiberStruct_compactified.pop(fiberID321)
#reassign voxels
V_fiberMapCompactified[V_fiberMapCompactified==fiberID321]=fiberID132
for fiberID321,dictF in maxAll321.items():
fiberID132=dictF["fiberID"]
counts=dictF["counts"]
if counts>minCountCombination:
fibObj321=fibers321[fiberID321]
oriVec321=fibObj321.orientationVec/np.linalg.norm(fibObj321.orientationVec)
oriVec132=fibers132[fiberID132].orientationVec/np.linalg.norm(fibers132[fiberID132].orientationVec)
angle=np.degrees(np.arccos(np.dot(oriVec132,oriVec321)))
if angle>90:
angle=180-angle
if angle<angleCombineDEG:
if fiberID132 in fiberStruct_combined.keys() and \
fiberID321 not in fiberStruct_combined[fiberID132].combinedWith:
fiberStruct_combined[fiberID132].combine(fibObj321)
fiberStruct_combined[fiberID132].setColor("combined")
# in the event of a second combination to the same fiberObj in 132, no need to delete again
if fiberID321 in fiberStruct_compactified.keys():
fiberStruct_compactified.pop(fiberID321)
#reassign voxels
V_fiberMapCompactified[V_fiberMapCompactified==fiberID321]=fiberID132
elif fiberID132 not in fiberStruct_combined.keys():
# edge case where the fiber132 had a more numerous match, which failed the combination conditions,
# but another fiber321 still passes combination requirements with it
# keep uncombined Obj for plotting purposes
newFibObj=fibers132[fiberID132].copy()
nextID= len(fibers132)+offset_nextAvailableID
offset_nextAvailableID+=1
fibers132[nextID]=newFibObj
fibers132[fiberID132].combine(fibObj321)
fibers132[fiberID132].setColor("combined")
fiberStruct_combined[fiberID132]=fibers132[fiberID132]
if fiberID321 in fiberStruct_compactified.keys():
fiberStruct_compactified.pop(fiberID321)
#reassign voxels
V_fiberMapCompactified[V_fiberMapCompactified==fiberID321]=fiberID132
##################################################################################
# Smart fiberStitching
##################################################################################
fiberObj.classAttributes["collisionDistance"] =params["collisionDistance"]
fiberObj.classAttributes["maxTrimPoints"] =params["maxTrimPoints"]
#reinitialise for second postProcessing step
fiberObj.classAttributes["interpolatedCenters"]={}
if doSecondPass:
print("\tSmart fiber stitching started")
fiberStructExtended,times_stitching=fiberObj.smartStitching(
fiberStruct_compactified,
smartStitchingMinFibLength,
smartStitchingMaxDistance,
smartStitchingMaxLateralDist,
smartStitchingAlignAngle,
smartStitchingBackTrackingLimit,
processingMinFiberLength,
tagAngleTooSteep,
maxSteepnessAngle,
verboseHandle=verboseHandle,
checkIfInSegt=False,
createNewPoints=False,
stitchingType="smart_transposed",
preventSelfStitch=preventSelfStitch
)
if doLastPass:
fiberStruct_lastPass={fiberID:fib for \
fiberID,fib in fiberStruct_compactified.items()
if not fib.addedTo and fib.suffix!=0.123}
fiberStructExtended2,times_stitching=fiberObj.smartStitching(
fiberStruct_lastPass,
smartStitchingMinFibLength,
smartStitchingMaxDistance,
smartStitchingMaxLateralDist,
smartStitchingAlignAngle,
smartStitchingBackTrackingLimit,
processingMinFiberLength,
tagAngleTooSteep,
maxSteepnessAngle,
verboseHandle=verboseHandle,
checkIfInSegt=False,
createNewPoints=False,
stitchingType="smart_lastPass",
preventSelfStitch=preventSelfStitch # Only fibers from different permutations are allowed to be stitched at this stage
)
for fib in fiberStructExtended2.values():
if not fib.addedTo:
fiberID_toKeep=fib.fiberID
marker_toKeep =int(fiberID_toKeep)
for fiberID_toReplace in fib.extendedBy:
marker_toReplace=int(fiberID_toReplace)
#no need to do this one in parallel, as typically very few fibers are concerned
V_fiberMapCompactified[V_fiberMapCompactified==marker_toReplace]=marker_toKeep
# add initial segments as new fiberObj, so that they can be plotted seperately (different color)
for fibID,fib in fiberStructExtended.items():
fiberStruct_compactified[fibID]=fib
# Reassign markers in V_fiberMapCombined with the new stitched fibers
print("\tReassign markers in V_fiberMapCombined with the new stitched fibers")
currentMarkers=np.unique(V_fiberMapCompactified)
#by default, each marker points to itself
compactifyIDs_LUT={m:m for m in currentMarkers}
for fib in fiberStructExtended.values():
if not fib.addedTo:
fiberID_toKeep=fib.fiberID
marker_toKeep =int(fiberID_toKeep)
for fiberID_toReplace in fib.extendedBy:
marker_toReplace=int(fiberID_toReplace)
compactifyIDs_LUT[marker_toReplace]=marker_toKeep
# V_fiberMapCompactified[V_fiberMapCompactified==marker_toReplace]=marker_toKeep
if parallelHandle:
num_cores=min(int(multiprocessing.cpu_count())-2,48)
else:
num_cores=1
results = Parallel(n_jobs=num_cores)\
(delayed(compactifySlice)\
(
V_fiberMapCompactified[iSlice],
compactifyIDs_LUT
)for iSlice in range(V_fiberMapCompactified.shape[0]) )
for iSlice,resTuple in enumerate(results):
V_fiberMapCompactified[iSlice]=resTuple
print("\tReassign markers finished in V_fiberMapCombined")
########################################################################
###
### PostProcessing: gap filling due to stitching
###
########################################################################
postProcessQueue=[]
for fiberID,interpolationChains in fiberObj.classAttributes["interpolatedCenters"].items():
#fiberObj that were added to another at smartStitching wont be processed (only starting fiberObj will)
if not fiberStruct_compactified[int(fiberID)].addedTo:
oriVec=fiberStruct_compactified[int(fiberID)].orientationVec
oriVec/=np.linalg.norm(oriVec)
#if there is more than one interpolation chain, keep the longest to create Structuring Element
if len(interpolationChains)>1:
# listLengths=[len(chain) for chain in interpolationChains]
# pos=listLengths.index(max(listLengths))
pos=interpolationChains.index(max(interpolationChains))
else:
pos=0
angle=np.degrees(np.arccos(np.dot(oriVec,[0.,0.,1.])))
# costly to evaluate, used in debugging
# numPixels=np.count_nonzero(V_fiberMapCompactified==int(fiberID))
# print('Post-Processqueue, adding: fiberID: {} ,angle: {: >8.4f}, numPixels: {: >8.0f}, length: {: >8.4f}'.format(int(fiberID),angle,numPixels,interpolationChains[pos]))
print('Post-Processqueue, adding: fiberID: {} ,angle: {: >8.4f}, length: {: >8.4f}'.format(int(fiberID),angle,interpolationChains[pos]))
postProcessQueue.append(
(
fiberID,
(interpolationChains[pos], oriVec, angle)
)
)
oriVecAll={}
for fiberID,fib in fiberStruct_compactified.items():
if "oriVec_normalized" in fib.__dir__():
oriVecAll[int(fiberID)]=fib.oriVec_normalized
else:
fib.oriVec_normalized=fib.orientationVec/np.linalg.norm(fib.orientationVec)
oriVecAll[int(fiberID)]=fib.oriVec_normalized
collisionsDict,V_fiberMapCompactified=collisionDetectionWrapper(
postProcessQueue,
minCountCombination,
angleCombineDEG,
oriVecAll,
fiberStruct_compactified,
V_fiberMapCompactified,
fiberStruct_combined,
V_hist,
makePlotsIndividual=False,#only implemented for parallelHandle==False, will be overriden internally otherwise
makePlotAll=False,
parallelHandle=parallelHandle
)
print(f"Writing to disk: {commonPath}V_fiberMapCombined_postProcessed.tiff")
imwrite(
os.path.join(commonPath,'V_fiberMapCombined_postProcessed.tiff'),
V_fiberMapCompactified,
resolution=(xRes,xRes,unitTiff),
description=descriptionStr,
compress=True
)
fiberStructPickle={ #saved to binary
"fiberStruct" :fiberStruct_compactified,
"fiberObj_classAttributes":fiberObj.classAttributes, #otherwise class attributes are not pickled,
"exclusiveZone" :exclusiveZone
}
with open(os.path.join(commonPath,"fiberStruct_final.pickle"),"wb") as f:
pickle.dump(fiberStructPickle,f,protocol=pickle.HIGHEST_PROTOCOL)
if makePlot:
import cameraConfig
from mayavi import mlab
from visualisationTool import makeLegend
rangeOutline=[0.,V_hist.shape[1],0.,V_hist.shape[2],0.,V_hist.shape[0] ]
cameraConfigKey="dynamic"
cameraConfig.createCamViewFromOutline(rangeOutline,"132",cameraConfigKey)
engine=mlab.get_engine()
mlab.figure(figure="Combined Permutations",size=(1200,1050),bgcolor=(0.9,0.9,0.9))
srcHist=mlab.pipeline.scalar_field(np.transpose(V_hist,(1,2,0)))
ipw=mlab.pipeline.image_plane_widget(srcHist,
plane_orientation='z_axes',
slice_index=0,
)
ipw.module_manager.scalar_lut_manager.lut_mode = "black-white"
mlab.outline()
axes = mlab.axes(color=(0., 0., 0.), nb_labels=10)
axes.title_text_property.color = (0., 0., 0.)
axes.title_text_property.font_family = 'times'
axes.label_text_property.color = (0., 0., 0.)
axes.label_text_property.font_family = 'times'
axes.axes.font_factor=0.65
color132 =(0.7,0.4,0.1)
color321 =(0.2,0.7,0.4)
colorCollisions =(1.,0.1,0.1)
colorCombination =(0.1,0.,0.95)
V_fiberMap132_mask=np.zeros(V_fiberMap132.shape,np.uint8)
V_fiberMap132_mask[V_fiberMap132>=0]=255
V_fiberMap321_mask=np.zeros(V_fiberMap321.shape,np.uint8)
V_fiberMap321_mask[V_fiberMap321>=0]=255
srcFiberMap132=mlab.pipeline.scalar_field(np.transpose(V_fiberMap132_mask,(1,2,0)))
mlab.pipeline.iso_surface(srcFiberMap132,contours=[255], opacity=0.2, color=color132 )
srcFiberMap321=mlab.pipeline.scalar_field(np.transpose(V_fiberMap321_mask,(1,2,0)))
mlab.pipeline.iso_surface(srcFiberMap321,contours=[255], opacity=0.2, color=color321 )
srcCollisions=mlab.pipeline.scalar_field(np.transpose(V_collisions,(1,2,0)))
mlab.pipeline.iso_surface(srcCollisions,contours=[255], opacity=0.8, color=colorCollisions)
if fiberStruct_compactified:
fib0=list(fiberStruct_compactified.keys())[0]
numFibersTracked=len(fiberStruct_compactified[fib0].classAttributes["listFiberIDs_tracked"])
else:
numFibersTracked=0
params={
"drawJaggedLines" :True,
"drawCenterLines" :False,
"drawEllipsoids" :False,
"addText" :True,
"fiberDiameter" :10.
}
for fibID,fib in fiberStruct_compactified.items():
if fib.colorLabel!="basic_123":
fiberPloting(fib,fibID,len(fiberStruct_compactified),
numFibersTracked,
engine,
params,
scale=2.
)
V_fibersShape=V_fiberMap132_mask.shape
rangeOutline=[0.,V_fibersShape[0],0.,V_fibersShape[1],0.,V_fibersShape[2] ]
makeLegend(rangeOutline)
mlab.show(stop=False)
print("\tDone")