-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutputPropertyMapsRefactored.py
250 lines (175 loc) · 8.63 KB
/
outputPropertyMapsRefactored.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
# by Facundo Sosa-Rey, 2021. MIT license
import json
import os
import numpy as np
import time
import pickle
import multiprocessing
import tifffile
from tifffile import TiffFile
from fibers import fiberObj
from postProcessing import randomizeVoxels,makePropertyMap
from extractCenterPoints import getTiffProperties
from vtk import vtkStructuredPoints,vtkStructuredPointsWriter,VTK_FLOAT
from vtk.util import numpy_support
def outputPropertyMap(
commonPath,
parallelHandle=True,
randomizeFiberMap=False,
croppedFiles=False,
makeVTKfiles=True,
forceReprocessing=False,
doDownSampling=True,
downSamplingFactor=2, # to reduce PropertyMaps.vtk file size
):
print("\noutputPropertyMap() called on dataset:\n{}".format(commonPath))
if croppedFiles:
postProcessedFileName="V_fiberMapCombined_postProcessed_cropped.tiff"
fiberStructPath=commonPath.split("CroppedResult")[0]
else:
postProcessedFileName="V_fiberMapCombined_postProcessed.tiff"
fiberStructPath=commonPath
with TiffFile(os.path.join(commonPath,postProcessedFileName)) as tif:
print("\tloading: \n{}".format(os.path.join(commonPath,"/V_fiberMapCombined_postProcessed.tiff")))
xRes,unitTiff,descriptionStr=getTiffProperties(tif,getDescription=True)
if unitTiff=="INCH":
pixelSize_micron=xRes[1]/xRes[0]*0.0254*1e6
elif unitTiff=="CENTIMETER":
pixelSize_micron=xRes[1]/xRes[0]*0.01*1e6
else:
raise ValueError("other units values not implemented in getTiffProperties")
V_fiberMap=tif.asarray()
if doDownSampling:
pixelSize_micronDownSampled=pixelSize_micron*downSamplingFactor # to have a correct scaleBar
_z=np.array([val*downSamplingFactor for val in range(int(V_fiberMap.shape[0]/downSamplingFactor))])
_x=np.array([val*downSamplingFactor for val in range(int(V_fiberMap.shape[1]/downSamplingFactor))])
_y=np.array([val*downSamplingFactor for val in range(int(V_fiberMap.shape[2]/downSamplingFactor))])
_zz,_xx,_yy=np.meshgrid(_z,_x,_y,indexing='ij')
V_fiberMap=V_fiberMap[_zz,_xx,_yy]
else:
pixelSize_micronDownSampled=pixelSize_micron
with open(os.path.join(fiberStructPath,"fiberStruct_final.pickle"), "rb") as f:
fiberStruct_all = pickle.load(f)
fiberStruct=fiberStruct_all["fiberStruct"]
exclusiveZone=fiberStruct_all["exclusiveZone"]
print("\t loading from disk complete")
####################################################################################################
### make vtk with multi-field data
if forceReprocessing:
if "processedFibers" in fiberStruct_all.keys():
del fiberStruct_all["processedFibers"]
if "processedFibers"in fiberStruct_all.keys():
print("\tload processed fibers from a previous run")
processedFibers=fiberStruct_all["processedFibers"]
else:
if forceReprocessing:
print("forceReprocessing set to True, running fiber processing again")
else:
print("\tfirst run, re-process all fibers, some of them need their orientationVec updated after combinations")
processedFibers={}
for fiberID,fib in fiberStruct.items():
fib.processPointCloudToFiberObj(0.,False,None,sort=False,doTrimming=False)
if "oriVec_normalized" not in fib.__dir__():
oriVec=fib.orientationVec/np.linalg.norm(fib.orientationVec)
else:
oriVec=fib.oriVec_normalized
angle=np.degrees(np.arccos(np.dot(oriVec,[0,0,1])))
processedFibers[fiberID]={
"length_inMicrons":fib.totalLength*pixelSize_micron,
"angle" :angle
}
fiberStructPickle={ #saved to binary
"fiberStruct" :fiberStruct,
"fiberObj_classAttributes" :fiberStruct_all["fiberObj_classAttributes"], #otherwise class attributes are not pickled
"processedFibers" :processedFibers,
"exclusiveZone" :exclusiveZone
}
with open(os.path.join(commonPath,"fiberStruct_final.pickle"),"wb") as f:
pickle.dump(fiberStructPickle,f,protocol=pickle.HIGHEST_PROTOCOL)
if makeVTKfiles:
print("\tcreating property map for lengths")
marker_to_lengthLUT={-1:np.nan}
marker_to_angleLUT ={-1:np.nan}
for fiberID,fibData in processedFibers.items():
marker_to_lengthLUT[fiberID]=fibData["length_inMicrons"]
marker_to_angleLUT [fiberID]=fibData["angle"]
V_length =makePropertyMap(V_fiberMap,marker_to_lengthLUT,parallelHandle)
for proc in multiprocessing.active_children():
# Manual termination of processes to avoid strange infinite hanging at 0% CPU for the next step, for large datasets
# print(f"\tforced termination of process {proc.name}")
proc.terminate()
proc.join()
print("\n\tcreating property map for angles")
V_angleTheta=makePropertyMap(V_fiberMap,marker_to_angleLUT, parallelHandle)
for proc in multiprocessing.active_children():
# Manual termination of processes to avoid strange infinite hanging at 0% CPU for the next step, for large datasets
# print(f"\tforced termination of process {proc.name}")
proc.terminate()
proc.join()
print("\n\tsaving propertyMaps to disk")
structPoints = vtkStructuredPoints()
structPoints.SetDimensions(
V_fiberMap.shape[2],
V_fiberMap.shape[1],
V_fiberMap.shape[0]
)
structPoints.SetOrigin(0, 0, 0)
structPoints.SetSpacing(
pixelSize_micronDownSampled/1000,
pixelSize_micronDownSampled/1000,
pixelSize_micronDownSampled/1000
)
fiberIDs_VTK = numpy_support.numpy_to_vtk(num_array=V_fiberMap.ravel(), array_type=VTK_FLOAT)
fiberIDs_VTK.SetName('FiberID')
lengthVTK = numpy_support.numpy_to_vtk(num_array=V_length.ravel(), array_type=VTK_FLOAT)
lengthVTK.SetName('Length (microns)')
deviationVTK = numpy_support.numpy_to_vtk(num_array=V_angleTheta.ravel(), array_type=VTK_FLOAT)
deviationVTK.SetName('Deviation (degrees)')
structPoints.GetPointData().SetScalars(lengthVTK)
structPoints.GetPointData().AddArray(deviationVTK)
structPoints.GetPointData().AddArray(fiberIDs_VTK)
filename = os.path.join(commonPath,"PropertyMaps.vtk")
writer = vtkStructuredPointsWriter()
writer.SetFileName(filename)
writer.SetInputData(structPoints)
writer.SetFileTypeToBinary()
writer.Write()
if randomizeFiberMap:
print("\n\trandom shuffling of markers for rendering purposes started")
listMarkers=np.unique(V_fiberMap)
listMarkers=[val for val in listMarkers if val>=0]# tracked fibers have markers starting at 0
ticRandomize=time.perf_counter()
V_fiberMap_randomized=randomizeVoxels(V_fiberMap,listMarkers,parallelHandle)
tocRandomize=time.perf_counter()
print("random shuffling in: {}".format(time.strftime("%Hh%Mm%Ss", time.gmtime(tocRandomize-ticRandomize))))
print("saving to disk...")
# Conversion to floats makes a different rendering in Paraview
# visualization in Paraview is different if data type is
# float rather than int (handling of the np.nan is different)
V_fiberMap_randomizedFloat=np.array(V_fiberMap_randomized,np.float32)
V_fiberMap_randomizedFloat[V_fiberMap_randomized==-1]=np.nan
filename='V_fiberMapCombined_randomized.tiff'
if doDownSampling:
descriptionDict=json.loads(descriptionStr.replace("None","[]"))
descriptionDict["downSamplingFactor"]=downSamplingFactor
descriptionStr=json.dumps(descriptionDict)
xRes=(xRes[0],xRes[1]*downSamplingFactor) # so scaling is consistent with original size
tifffile.imwrite(
os.path.join(commonPath,filename),
V_fiberMap_randomized,
resolution=(xRes,xRes,unitTiff),
description=descriptionStr,
compress=True
)
# if doDownSampling:
# filename='V_fiberMapCombined_randomizedFloat_downSampled.tiff'
# else:
filename='V_fiberMapCombined_randomizedFloat.tiff'
tifffile.imwrite(
os.path.join(commonPath,filename),
V_fiberMap_randomizedFloat,
resolution=(xRes,xRes,unitTiff),
description=descriptionStr,
compress=True
)
print("\toutputPropertyMap done\n\n")