-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
621 lines (482 loc) · 23.9 KB
/
main.py
File metadata and controls
621 lines (482 loc) · 23.9 KB
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# by Facundo Sosa-Rey, 2021. MIT license
import subprocess
from trackingParameters import getTrackingParams
from extractCenterPoints import extractCenterPoints, checkIfFilesPresent
from trackFibers import tracking, saveFiberStruct
from assignVoxelsToFibers_refactored import assignVoxelsToFibers_Main
from postProcessing import postProcessingOfFibers
from combinePermutationsRefactored import combinePermutations
from outputPropertyMapsRefactored import outputPropertyMap
from preProcessingFunctions import find
from cropTiff_Amitex import cropTiff_Amitex
from tifffile import TiffFile
import tifffile
import json
import pickle
import os
import numpy as np
from fibers import fiberObj
import time
def getCommonPaths(rootPath):
# finds all files in rootPath that have been preprocessed and pre-segmented with INSEGT, but not tracked, postprocessed and and combined
directories=find(rootPath,"SegtParams.json")
unProcessedDirectories = []
for dir in directories:
#check for output directory of last step: cropping
test=find(dir,"croppingStats.json")
if len(test)==0:
unProcessedDirectories.append(dir)
unProcessedDirectories.sort()
return unProcessedDirectories
cmd = "hostname"
# returns output as byte string
hostnameStr = subprocess.check_output(cmd).decode("utf-8").replace("\n","").replace("\r","")
# using decode() function to convert byte string to string
print('Current hostname is :', hostnameStr)
##############################################################################
# Directories for pre segmented data
####################################################################makePlots##
dataPath="./TomographicData/PEEK15/"
# find directories with dataset processed with Insegt, but not tracked
# (will have SegtParams.json file, but not PropertyMaps.vtk, the final output)
directories = getCommonPaths(dataPath)
if directories:
print("\n\n\tFound unprocessed datasets at these locations:\n")
for dir in directories:
print(dir)
else:
print("\n\n\tNo unprocessed datasets at specified location\n")
exclusiveZone=None # will process entire dataset
# example of exclusive zone: processing will only occur in volume defined by these coordinates.
# produces truncated volumes as output tiff files.
# exclusiveZone={
# "xMin":300,
# "xMax":601,
# "yMin":300,
# "yMax":641,
# "zMin":0,
# "zMax":600
# }
##############################################################################
permutationPaths = ["Permutation123/", "Permutation132/", "Permutation321/"]
permutationVecAll = ["123", "132", "321"]
permutationIndices = [0, 1, 2]
for commonPath in directories:
print("\nBatch mode, entering:\n ", commonPath)
for permutationIndex in permutationIndices:
permutationVec = permutationVecAll[permutationIndex]
print("\n\tInitiating processing on permutation:{}".format(permutationVec))
##############################################################################
# extraction of centerpoints with the watershed transform
# for every step, results are writen to disk, so that the steps can be redone
# independently in the development phase.
##############################################################################
doExtraction = checkIfFilesPresent(
os.path.join(commonPath,permutationPaths[permutationIndex]),
'V_voxelMap.tiff',
"watershedExtractionStats.json",
"watershedCenterPoints.pickle",
)
if doExtraction:
ticTotal_extraction = time.perf_counter()
V_voxels, watershedData, xRes, unitTiff, descriptionDict, times_centroids =\
extractCenterPoints(
commonPath,
permutationVec,
plotCentroids =False,
plotInitialAndResults =False,
plotEveryIteration =False,
plotWatershedStepsGlobal =False,
plotWatershedStepsMarkersOnly=False,
plotConvexityDefects =False,
plotExpansion =False, # won't plot if useProbabilityMap==False in trackingParams.json
plotOverlayFrom123 =False,
# manualRange =range(100,105), #used in debugging, process only slice numbers in range
exclusiveZone =exclusiveZone,
parallelHandle =True
)
# write results to disk
saveExtractionData = True
if saveExtractionData:
times_centroids["Entire extractCenterPoints procedure:"] = time.strftime(
"%Hh%Mm%Ss", time.gmtime(time.perf_counter()-ticTotal_extraction))
print("\n\textractCenterPoints():\n\tWriting output to : \n {}".format(
os.path.join(commonPath,permutationPaths[permutationIndex],'V_voxelMap.tiff')))
tifffile.imwrite(
os.path.join(
commonPath,
permutationPaths[permutationIndex],
'V_voxelMap.tiff'
),
V_voxels,
resolution=(xRes, xRes, unitTiff),
description=descriptionDict["descriptionStr"],
compress=True
)
watershedDict = {
"times_centroids": times_centroids,
"volumeDescription": descriptionDict,
"hostname":hostnameStr
}
pathCenterPointStats = os.path.join(
commonPath,
permutationPaths[permutationIndex],
"watershedExtractionStats.json"
)
with open(pathCenterPointStats, "w") as f:
json.dump(watershedDict, f, sort_keys=False, indent=4)
pathCenterPointsData = os.path.join(
commonPath,
permutationPaths[permutationIndex],
"watershedCenterPoints.pickle"
)
with open(pathCenterPointsData, "wb") as f:
pickle.dump(watershedData, f,protocol=pickle.HIGHEST_PROTOCOL)
##############################################################################
# tracking of fibers from centerPoints
##############################################################################
doTracking = checkIfFilesPresent(
os.path.join(commonPath,permutationPaths[permutationIndex]),
"fiberStats.json",
"fiberStruct.pickle"
)
if doTracking:
ticTracking = time.perf_counter()
fiberStruct, V_fibersShape, times_tracking, V_fibers, xRes, unitTiff = tracking(
commonPath,
permutationPaths[permutationIndex],
permutationVec,
exclusiveZone=exclusiveZone,
parallelHandle=True,
verboseHandle=False
)
tocTracking = time.perf_counter()
times_tracking["Entire tracking procedure:"] = time.strftime(
"%Hh%Mm%Ss", time.gmtime(tocTracking-ticTracking))
print("\n\n\ttime for entire tracking procedure: {: >.2f}s \n".format(
tocTracking-ticTracking))
saveTrackingData = True
if saveTrackingData:
fiberStructPickle = { # saved to binary
"fiberStruct": fiberStruct,
"fiberObj_classAttributes": fiberObj.classAttributes,# otherwise class attributes are not pickled
"trackingTimes": times_tracking,
"exclusiveZone": fiberObj.getExclusiveZone(),
"trackedCenterPoints": fiberObj.getTrackedCenterPoints(),
"rejectedCenterPoints": fiberObj.getRejectedCenterPoints()
}
fiberStats = { # saved to human readable .json
"trackingTimes": times_tracking,
"numberOfFiberObj (total)": len(fiberStruct),
"numberOfFiberObj (tracked)": len(fiberObj.classAttributes["listFiberIDs_tracked"]),
"exclusiveZone": fiberObj.getExclusiveZone()
}
saveFiberStruct(
commonPath,
permutationPaths[permutationIndex],
fiberStructPickle,
fiberStats
)
plotTracking = False
if plotTracking:
import cameraConfig
if exclusiveZone is None:
rangeOutline = [0., V_fibersShape[1], 0.,
V_fibersShape[2], 0., V_fibersShape[0]]
else:
rangeOutline = [
exclusiveZone["xMin"],
exclusiveZone["xMax"],
exclusiveZone["yMin"],
exclusiveZone["yMax"],
exclusiveZone["zMin"],
exclusiveZone["zMax"]
]
xMin = exclusiveZone["xMin"]
xMax = exclusiveZone["xMax"]
yMin = exclusiveZone["yMin"]
yMax = exclusiveZone["yMax"]
plottingParams = getTrackingParams(
commonPath, "plottingParams", xRes, unitTiff)
plottingParams["panningPlane"] = True
plottingParams["planeWidgets"] = True
plottingParams["staticCam"] = False
# plottingParams["cameraConfigKey"]="manual_0"
if plottingParams["cameraConfigKey"] == "dynamic":
cameraConfig.createCamViewFromOutline(
rangeOutline, permutationVec, "dynamic")
from visualisationTool import makeVisualisation
filesInDir = [f.path for f in os.scandir(
os.path.join(commonPath,permutationPaths[permutationIndex])) if f.is_file()]
for i, iPath in enumerate(filesInDir):
if ".tiff" in iPath:
if "V_hist.tiff" in iPath:
indexHistTiff = i
if "V_pores.tiff" in iPath:
indexPoresTiff = i
if plottingParams["planeWidgets"]:
with TiffFile(filesInDir[indexHistTiff]) as tif:
if exclusiveZone is None:
V_hist = np.transpose(tif.asarray(), (1, 2, 0))/255
else:
rangeSlice = range(
exclusiveZone["zMin"], exclusiveZone["zMax"])
offset = exclusiveZone["zMin"]
nSlices = len(rangeSlice)
temp = tif.pages[0].asarray()[xMin:xMax, yMin:yMax]
V_hist = np.empty((nSlices, *temp.shape), np.uint8)
for imSlice in rangeSlice:
V_hist[imSlice-offset] = tif.pages[imSlice].asarray()[xMin:xMax,
yMin:yMax]
V_hist = np.transpose(V_hist, (1, 2, 0))
else:
V_hist = None
if plottingParams["plotPorosityMask"]:
with TiffFile(filesInDir[indexPoresTiff]) as tif:
if exclusiveZone is None:
# load entire volume
V_porosity = np.transpose(
tif.asarray(), (1, 2, 0))/255
else:
# load partial volume
temp = tif.pages[0].asarray()[xMin:xMax, yMin:yMax]
V_porosity = np.empty(
(nSlices, *temp.shape), np.uint8)
for imSlice in rangeSlice:
V_porosity[imSlice-offset] = tif.pages[imSlice].asarray()[
xMin:xMax, yMin:yMax]
V_porosity = np.transpose(V_porosity, (1, 2, 0))
else:
V_porosity = None
makeVisualisation(fiberStruct, V_porosity, V_hist,
rangeOutline, plottingParams, widgetLUT="black-white")
##############################################################################
# assign voxels to tracked fibers V_voxelMap
##############################################################################
doAssignment = checkIfFilesPresent(
os.path.join(commonPath,permutationPaths[permutationIndex]),
"V_fiberMap.tiff"
)
if doAssignment:
ticAssign = time.perf_counter()
V_fiberMap,\
fiberStruct,\
xRes, unitTiff,\
descriptionStr,\
times_assign = assignVoxelsToFibers_Main(
commonPath,
permutationPaths[permutationIndex],
# manualRange=range(500,510), #used for debugging only a few slices
makePlots=False,
parallelHandle=True,
verbose=False,
)
saveAssignmentData = True
if saveAssignmentData:
tocAssign = time.perf_counter()
times_assign["Total assignVoxelsToFibers procedure"] = time.strftime(
"%Hh%Mm%Ss", time.gmtime(tocAssign-ticAssign))
fiberStructPickle = { # saved to binary
"fiberStruct": fiberStruct["fiberStruct"],
"fiberObj_classAttributes": fiberStruct["fiberObj_classAttributes"], # otherwise class attributes are not pickled
"trackingTimes": fiberStruct["trackingTimes"],
"assignmentTimes": times_assign,
"exclusiveZone": fiberStruct["exclusiveZone"],
"trackedCenterPoints": fiberStruct["trackedCenterPoints"],
"rejectedCenterPoints": fiberStruct["rejectedCenterPoints"]
}
fiberStats = { # saved to human-readable JSON
"trackingTimes": fiberStruct["trackingTimes"],
"assignmentTimes": times_assign,
"numberOfFiberObj (total)": len(fiberStruct["fiberStruct"]),
"numberOfFiberObj (tracked)": len(fiberStruct["fiberObj_classAttributes"]["listFiberIDs_tracked"]),
"exclusiveZone": fiberStruct["exclusiveZone"],
}
saveFiberStruct(
commonPath,
permutationPaths[permutationIndex],
fiberStructPickle,
fiberStats
)
print("\n\tassignVoxelsToFibers():\n\tWriting tiff file to : \n{}".format(
os.path.join(commonPath,permutationPaths[permutationIndex],'V_fiberMap.tiff')))
tifffile.imwrite(
os.path.join(commonPath,permutationPaths[permutationIndex],'V_fiberMap.tiff'),
V_fiberMap,
resolution=(xRes, xRes, unitTiff),
description=descriptionStr,
compress=True
)
##############################################################################
# Volumetric post-processing of fibers
##############################################################################
makePlotAll = False
doPostProcessing = checkIfFilesPresent(
os.path.join(commonPath,permutationPaths[permutationIndex]),
"V_fiberMap_postProcessed.tiff"
)
if doPostProcessing:
V_fiberMap,\
V_fiberMap_randomized,\
V_fibers_masked,\
xRes, unitTiff,\
descriptionStr,\
times_postProc = postProcessingOfFibers(
commonPath,
permutationPaths[permutationIndex],
SE_radius=4,
useInclinedCylinderSE=True,# cant be done in parallel, will be over-riden inside function
makePlotsIndividual=False,# cant be done in parallel, will be over-riden inside function
makePlotAll=makePlotAll,
parallelHandle=True, # requires large amounts of RAM
postProcessAllFibers=True,
exclusiveFibers=None #list of fibers which will be postprocessed. useful for debugging
)
savePostProcessingData = True
if savePostProcessingData:
print("\n\tpostProcessingOfFibers():\n\tWriting tiff file to : \n{}".format(
os.path.join(commonPath,permutationPaths[permutationIndex],'V_fiberMap_postProcessed.tiff')))
tifffile.imwrite(
os.path.join(
commonPath,
permutationPaths[permutationIndex],
'V_fiberMap_postProcessed.tiff'
),
V_fiberMap,
resolution=(xRes, xRes, unitTiff),
description=descriptionStr,
compress=True
)
if V_fiberMap_randomized is not None:
print("\n\tpostProcessingOfFibers():\n\tWriting tiff file to : \n{}".format(
os.path.join(commonPath,permutationPaths[permutationIndex]+'V_fiberMap_randomized.tiff')))
tifffile.imwrite(
os.path.join(
commonPath,
permutationPaths[permutationIndex],
'V_fiberMap_randomized.tiff'
),
V_fiberMap_randomized,
resolution=(xRes, xRes, unitTiff),
description=descriptionStr,
compress=True
)
if permutationVec == "123":
print("\n\tpostProcessingOfFibers():\n\tWriting tiff file to : \n{}".format(
os.path.join(commonPath,permutationPaths[permutationIndex],'V_fibers_masked.tiff')))
tifffile.imwrite(
os.path.join(
commonPath,
permutationPaths[permutationIndex],
'V_fibers_masked.tiff'
),
V_fibers_masked,
resolution=(xRes, xRes, unitTiff),
description=descriptionStr,
compress=True
)
times_postProc["hostname"]=hostnameStr
with open(os.path.join(commonPath,permutationPaths[permutationIndex],'postProcStats.json'), "w") as f:
json.dump(times_postProc, f, sort_keys=False, indent=4)
if makePlotAll:
from mayavi import mlab
mlab.show()
#########################################################################
# Combination of processed data from all permuted referentials
#########################################################################
doCombination = checkIfFilesPresent(
commonPath,
"fiberStruct_final.pickle",
"V_fiberMapCombined_postProcessed.tiff",
)
if doCombination:
combinePermutations(commonPath, permutationPaths, parallelHandle=True)
doOutputVTK = checkIfFilesPresent(
commonPath,
"PropertyMaps.vtk",
"V_fiberMapCombined_randomized.tiff",
"V_fiberMapCombined_randomizedFloat.tiff"
)
makeVTKfiles=True
randomizeFiberMap=True
if doOutputVTK:
outputPropertyMap(
commonPath,
parallelHandle=True,
randomizeFiberMap=randomizeFiberMap,
makeVTKfiles=makeVTKfiles)
#########################################
###
### make files for Amitex computations
###
makeAmitexOutput =True
keepOnlyDownSampled =False # cropped (not downsampled) files are required for adjustment
doFractionAdjustment=False
if makeAmitexOutput:
pathAmitexFiles=os.path.join(commonPath,"AmitexFiles")
pathAmitexFilesCropped={}
pathAmitexFilesDownSampled={}
if manualCropping is None:
manualCropping={"noManualCropping":None}
for volumeLabel in manualCropping:
pathAmitexFilesCropped[volumeLabel]=os.path.join(pathAmitexFiles,"cropped",volumeLabel)
pathAmitexFilesDownSampled[volumeLabel]=os.path.join(pathAmitexFiles,"downSampled",volumeLabel)
cropForAmitex=False
if os.path.exists(pathAmitexFiles):
if os.path.exists(pathAmitexFilesCropped[volumeLabel]) and \
os.path.exists(pathAmitexFilesDownSampled[volumeLabel]):
croppedFilesMissing = checkIfFilesPresent(
pathAmitexFilesCropped[volumeLabel],
"fiberStruct_AMITEX.pickle",
"V_fiberMapCombined_postProcessed_cropped.tiff",
"V_perim_cropped.tiff",
"V_pores_cropped.tiff"
)
downSampledFilesMissing = checkIfFilesPresent(
pathAmitexFilesDownSampled[volumeLabel],
"fiberStruct_AMITEX.pickle",
"V_fiberMapCombined_postProcessed_downsampled_by_2.tiff",
"V_perim_downsampled_by_2.tiff",
"V_pores_downsampled_by_2.tiff"
)
if croppedFilesMissing or downSampledFilesMissing:
cropForAmitex=True
else:
cropForAmitex=True
else:
cropForAmitex=True
if cropForAmitex:
if {"noManualCropping"} != set(manualCropping.keys()):
if set(manualCropping.keys())=={"xMin","yMin","zMin","xMax","yMax","zMax"}:
# case of a single cropping volume
manualCropping={
"singleVolume":manualCropping
}
print("\n\tcalling cropTiff_Amitex() with manualCropping of:")
for volumeTag,croppingSpecs in manualCropping.items():
print("\nvolumeTag={}".format(volumeTag))
print("\txMin={: >4.0f}\txMax={: >4.0f}".format(croppingSpecs["xMin"],croppingSpecs["xMax"]))
print("\tyMin={: >4.0f}\tyMax={: >4.0f}".format(croppingSpecs["yMin"],croppingSpecs["yMax"]))
if croppingSpecs["zMin"]!="all":
print("\tzMin={: >4.0f}\tzMax={: >4.0f}".format(croppingSpecs["zMin"],croppingSpecs["zMax"]))
else:
print("\tzMin= all\tzMax= all")
else:
print("\n\tcalling cropTiff_Amitex() with manualCropping of: None")
outputDict=cropTiff_Amitex(
manualCropping,
commonPath,
pathAmitexFiles,
makePlots=False,
doDownSampling=True,
keepOnlyDownSampled=keepOnlyDownSampled
)
croppingStatsDict={
"manualCropping":manualCropping,
"commonPath":commonPath,
"hostname":hostnameStr
}
croppingStatsDict.update(outputDict)
with open(os.path.join(pathAmitexFiles,"croppingStats.json"), "w") as f:
json.dump(croppingStatsDict, f, sort_keys=False, indent=4)
print("\n\tDone ")