TTK
Loading...
Searching...
No Matches
ttkMergeTreeAutoencoderUtils.cpp
Go to the documentation of this file.
3
4#ifdef TTK_ENABLE_TORCH
5namespace ttk {
6 namespace wae {
7 void makeOneOutput(
9 vtkUnstructuredGrid *treeNodes,
10 std::vector<int> &treeNodeCorr,
11 vtkDataSet *treeSegmentation,
14 vtkSmartPointer<vtkDataSet> &vtkOutputSegmentation,
15 unsigned int treeID,
16 std::vector<std::tuple<std::string, std::vector<int>>> &customIntArrays,
17 std::vector<std::tuple<std::string, std::vector<double>>>
18 &customDoubleArrays,
19 bool outputSegmentation,
20 double mixtureCoefficient,
21 bool isPersistenceDiagram,
22 bool convertToDiagram,
23 int debugLevel) {
26
27 ttkMergeTreeVisualization visuMakerBary;
28 visuMakerBary.setShiftMode(-1); // Line
29 visuMakerBary.setVtkOutputNode(vtkOutputNode);
30 if(not isPersistenceDiagram)
31 visuMakerBary.setVtkOutputArc(vtkOutputArc);
32 else {
33 visuMakerBary.setVtkOutputArc(vtkOutputNode);
34 visuMakerBary.setIsPDSadMax(mixtureCoefficient == 0);
35 }
36 visuMakerBary.copyPointData(treeNodes, treeNodeCorr);
37 for(auto &tup : customIntArrays)
38 visuMakerBary.addCustomIntArray(std::get<0>(tup), std::get<1>(tup));
39 for(auto &tup : customDoubleArrays)
40 visuMakerBary.addCustomArray(std::get<0>(tup), std::get<1>(tup));
41 visuMakerBary.setDebugLevel(debugLevel);
42 visuMakerBary.setIsPersistenceDiagram(isPersistenceDiagram);
43 visuMakerBary.setConvertedToDiagram(convertToDiagram);
44 if(treeNodes) {
45 visuMakerBary.setTreesNodes(treeNodes);
46 visuMakerBary.setTreesNodeCorrMesh(treeNodeCorr);
47 }
48 if(outputSegmentation) {
49 vtkOutputSegmentation = vtkSmartPointer<vtkUnstructuredGrid>::New();
50 visuMakerBary.setTreesSegmentation(treeSegmentation);
51 visuMakerBary.setPlanarLayout(false);
52 visuMakerBary.setOutputSegmentation(true);
53 visuMakerBary.setVtkOutputSegmentation(vtkOutputSegmentation);
54 } else {
55 visuMakerBary.setPlanarLayout(true);
56 }
57 visuMakerBary.setISampleOffset(treeID);
58 visuMakerBary.setOutputTreeNodeId(true);
59 visuMakerBary.makeTreesOutput<float>(&(tree.tree));
60 }
61
62 void makeManyOutput(
63 std::vector<ttk::ftm::MergeTree<float> *> &trees,
64 std::vector<vtkUnstructuredGrid *> &treesNodesT,
65 std::vector<std::vector<int>> &treesNodeCorr,
66 std::vector<vtkDataSet *> &treesSegmentationT,
68 std::vector<std::vector<std::tuple<std::string, std::vector<int>>>>
69 &customIntArrays,
70 std::vector<std::vector<std::tuple<std::string, std::vector<double>>>>
71 &customDoubleArrays,
72 double mixtureCoefficient,
73 bool isPersistenceDiagram,
74 bool convertToDiagram,
75 int debugLevel) {
79 if(not isPersistenceDiagram) {
81 }
82 bool outputSegmentation
83 = !treesSegmentationT.empty() and treesSegmentationT[0];
85 if(outputSegmentation) {
87 }
88 int shift = 0;
89 for(unsigned int i = 0; i < trees.size(); ++i) {
90 if(trees[i]->tree.template getMaximumPersistence<float>() == 0) {
91 ++shift;
92 continue;
93 }
94 vtkUnstructuredGrid *treeNodes = nullptr;
95 vtkDataSet *treeSegmentation = nullptr;
96 std::vector<int> treeNodeCorr;
97 if(outputSegmentation) {
98 treeSegmentation = treesSegmentationT[i];
99 }
100 if(i < treesNodesT.size()) {
101 treeNodes = treesNodesT[i];
102 treeNodeCorr = treesNodeCorr[i];
103 }
104 vtkSmartPointer<vtkUnstructuredGrid> vtkOutputNode, vtkOutputArc;
105 vtkSmartPointer<vtkDataSet> vtkOutputSegmentation;
106 makeOneOutput(*(trees[i]), treeNodes, treeNodeCorr, treeSegmentation,
107 vtkOutputNode, vtkOutputArc, vtkOutputSegmentation, i,
108 customIntArrays[i], customDoubleArrays[i],
109 outputSegmentation, mixtureCoefficient,
110 isPersistenceDiagram, convertToDiagram, debugLevel);
111 allNodes->SetBlock(i - shift, vtkOutputNode);
112 if(not isPersistenceDiagram)
113 allArcs->SetBlock(i - shift, vtkOutputArc);
114 if(outputSegmentation)
115 allSegs->SetBlock(i - shift, vtkOutputSegmentation);
116 }
117 if(not isPersistenceDiagram) {
118 output->SetNumberOfBlocks(2);
119 output->SetBlock(0, allNodes);
120 output->SetBlock(1, allArcs);
121 if(outputSegmentation)
122 output->SetBlock(2, allSegs);
123 } else {
124 if(not outputSegmentation) {
125 output->ShallowCopy(allNodes);
126 } else {
127 output->SetNumberOfBlocks(2);
128 output->SetBlock(0, allNodes);
129 output->SetBlock(1, allSegs);
130 }
131 }
132 }
133
134 void makeManyOutput(
135 std::vector<ttk::ftm::MergeTree<float> *> &trees,
136 std::vector<vtkUnstructuredGrid *> &treesNodesT,
137 std::vector<std::vector<int>> &treesNodeCorr,
139 std::vector<std::vector<std::tuple<std::string, std::vector<int>>>>
140 &customIntArrays,
141 std::vector<std::vector<std::tuple<std::string, std::vector<double>>>>
142 &customDoubleArrays,
143 double mixtureCoefficient,
144 bool isPersistenceDiagram,
145 bool convertToDiagram,
146 int debugLevel) {
147 std::vector<vtkDataSet *> treesSegmentationT;
148 makeManyOutput(trees, treesNodesT, treesNodeCorr, treesSegmentationT,
149 output, customIntArrays, customDoubleArrays,
150 mixtureCoefficient, isPersistenceDiagram, convertToDiagram,
151 debugLevel);
152 }
153
154 void makeManyOutput(
155 std::vector<ttk::ftm::MergeTree<float> *> &trees,
157 std::vector<std::vector<std::tuple<std::string, std::vector<int>>>>
158 &customIntArrays,
159 std::vector<std::vector<std::tuple<std::string, std::vector<double>>>>
160 &customDoubleArrays,
161 double mixtureCoefficient,
162 bool isPersistenceDiagram,
163 bool convertToDiagram,
164 int debugLevel) {
165 std::vector<vtkUnstructuredGrid *> treesNodesT;
166 std::vector<std::vector<int>> treesNodeCorr;
167 makeManyOutput(trees, treesNodesT, treesNodeCorr, output, customIntArrays,
168 customDoubleArrays, mixtureCoefficient,
169 isPersistenceDiagram, convertToDiagram, debugLevel);
170 }
171
172 void makeManyOutput(std::vector<ttk::ftm::MergeTree<float> *> &trees,
174 double mixtureCoefficient,
175 bool isPersistenceDiagram,
176 bool convertToDiagram,
177 int debugLevel) {
178 std::vector<std::vector<std::tuple<std::string, std::vector<int>>>>
179 customIntArrays(trees.size());
180 std::vector<std::vector<std::tuple<std::string, std::vector<double>>>>
181 customDoubleArrays(trees.size());
182 makeManyOutput(trees, output, customIntArrays, customDoubleArrays,
183 mixtureCoefficient, isPersistenceDiagram, convertToDiagram,
184 debugLevel);
185 }
186
187 void computeTrackingInformation(
188 std::vector<mtu::TorchMergeTree<float>> &origins,
189 std::vector<mtu::TorchMergeTree<float>> &originsPrime,
190 std::vector<std::vector<ttk::ftm::idNode>> &originsMatchingVectorT,
191 std::vector<std::vector<ttk::ftm::idNode>> &invOriginsMatchingVectorT,
192 bool isPersistenceDiagram,
193 std::vector<std::vector<ttk::ftm::idNode>> &originsMatchingVector,
194 std::vector<std::vector<double>> &originsPersPercent,
195 std::vector<std::vector<double>> &originsPersDiff,
196 std::vector<double> &originPersPercent,
197 std::vector<double> &originPersDiff,
198 std::vector<int> &originPersistenceOrder) {
199 unsigned int originsMatchingSize = originsMatchingVectorT.size();
200 originsMatchingVector.resize(originsMatchingSize);
201 originsPersPercent.resize(originsMatchingSize);
202 originsPersDiff.resize(originsMatchingSize);
203 for(unsigned int l = 0; l < originsMatchingSize; ++l) {
204 auto &tree2 = (l == 0 ? originsPrime[0] : originsPrime[l]);
205 originsMatchingVector[l] = invOriginsMatchingVectorT[l];
206 if(l != 0) {
207 for(unsigned int i = 0; i < originsMatchingVector[l].size(); ++i)
208 if(originsMatchingVector[l][i]
209 < originsMatchingVector[l - 1].size())
210 originsMatchingVector[l][i]
211 = originsMatchingVector[l - 1][originsMatchingVector[l][i]];
212 }
213 originsPersPercent[l].resize(tree2.mTree.tree.getNumberOfNodes());
214 originsPersDiff[l].resize(tree2.mTree.tree.getNumberOfNodes());
215 for(unsigned int i = 0; i < originsMatchingVector[l].size(); ++i) {
216 if(originsMatchingVector[l][i]
217 >= origins[0].mTree.tree.getNumberOfNodes())
218 continue;
219 auto pers = origins[0].mTree.tree.template getNodePersistence<float>(
220 originsMatchingVector[l][i]);
221 auto treePers
222 = tree2.mTree.tree.template getNodePersistence<float>(i);
223 originsPersPercent[l][i] = treePers * 100 / pers;
224 originsPersDiff[l][i] = treePers - pers;
225 }
226 }
227
228 originPersPercent.resize(origins[0].mTree.tree.getNumberOfNodes());
229 originPersDiff.resize(origins[0].mTree.tree.getNumberOfNodes());
230 std::vector<ttk::ftm::idNode> originMatchingVector;
231 for(unsigned int l = 0; l < originsMatchingSize; ++l) {
232 std::vector<ttk::ftm::idNode> &originMatchingVectorT
233 = originsMatchingVectorT[l];
234 if(l == 0) {
235 originMatchingVector = originMatchingVectorT;
236 } else {
237 for(unsigned int i = 0; i < originMatchingVector.size(); ++i)
238 if(originMatchingVector[i] < originMatchingVectorT.size())
239 originMatchingVector[i]
240 = originMatchingVectorT[originMatchingVector[i]];
241 }
242 }
243 unsigned int l2 = originsMatchingSize - 1;
244 for(unsigned int i = 0; i < originMatchingVector.size(); ++i) {
245 if(originMatchingVector[i] < originsPersDiff[l2].size()) {
246 originPersPercent[i]
247 = originsPersPercent[l2][originMatchingVector[i]];
248 originPersDiff[i] = originsPersDiff[l2][originMatchingVector[i]];
249 }
250 }
251
252 originPersistenceOrder.resize(
253 origins[0].mTree.tree.getNumberOfNodes(), -1);
254 std::vector<std::tuple<ttk::ftm::idNode, ttk::ftm::idNode, float>>
255 pairsBary;
256 bool useBD = isPersistenceDiagram;
257 origins[0].mTree.tree.template getPersistencePairsFromTree<float>(
258 pairsBary, useBD);
259 for(unsigned int j = 0; j < pairsBary.size(); ++j) {
260 int index = pairsBary.size() - 1 - j;
261 originPersistenceOrder[std::get<0>(pairsBary[j])] = index;
262 originPersistenceOrder[std::get<1>(pairsBary[j])] = index;
263 }
264 }
265
266 void computeCustomArrays(
267 std::vector<std::vector<mtu::TorchMergeTree<float>>> &recs,
268 std::vector<std::vector<double>> &persCorrelationMatrix,
269 std::vector<std::vector<std::vector<ttk::ftm::idNode>>>
270 &invDataMatchingVectorT,
271 std::vector<std::vector<ttk::ftm::idNode>> &invReconstMatchingVectorT,
272 std::vector<std::vector<ttk::ftm::idNode>> &originsMatchingVector,
273 std::vector<std::vector<ttk::ftm::idNode>> &originsMatchingVectorT,
274 std::vector<std::vector<double>> &originsPersPercent,
275 std::vector<std::vector<double>> &originsPersDiff,
276 std::vector<int> &originPersistenceOrder,
277 unsigned int l,
278 unsigned int lShift,
279 std::vector<std::vector<std::tuple<std::string, std::vector<int>>>>
280 &customIntArrays,
281 std::vector<std::vector<std::tuple<std::string, std::vector<double>>>>
282 &customDoubleArrays) {
283 unsigned int originsMatchingSize = originsMatchingVectorT.size();
284 unsigned int dataMatchingSize = invDataMatchingVectorT.size();
285 unsigned int lShifted = l + lShift;
286 std::vector<std::vector<ttk::ftm::idNode>> matchingVectors(recs.size());
287 std::vector<std::vector<double>> dataPersPercent, dataPersDiff;
288 std::vector<std::vector<int>> dataOriginPersOrder;
289 std::vector<std::vector<std::vector<double>>> dataCorrelation;
290 if(lShifted < dataMatchingSize) {
291 for(unsigned int i = 0; i < recs.size(); ++i) {
292 matchingVectors[i] = invDataMatchingVectorT[lShifted][i];
293 if(lShifted != 0) {
294 for(unsigned int j = 0; j < matchingVectors[i].size(); ++j)
295 if(matchingVectors[i][j]
296 < originsMatchingVector[lShifted - 1].size())
297 matchingVectors[i][j]
298 = originsMatchingVector[lShifted - 1][matchingVectors[i][j]];
299 }
300 }
301 }
302 if(lShifted == 0 or lShifted == dataMatchingSize - 1) {
303 dataPersPercent.resize(recs.size());
304 dataPersDiff.resize(recs.size());
305 for(unsigned int i = 0; i < recs.size(); ++i) {
306 dataPersPercent[i].resize(recs[i][l].mTree.tree.getNumberOfNodes());
307 dataPersDiff[i].resize(recs[i][l].mTree.tree.getNumberOfNodes());
308 std::vector<ttk::ftm::idNode> matchingVector;
309 if(lShifted == 0) {
310 matchingVector = matchingVectors[i];
311 for(unsigned int l2 = 0; l2 < originsMatchingSize; ++l2) {
312 std::vector<ttk::ftm::idNode> &originMatchingVector
313 = originsMatchingVectorT[l2];
314 for(unsigned int j = 0; j < matchingVector.size(); ++j)
315 if(matchingVector[j] < originMatchingVector.size())
316 matchingVector[j] = originMatchingVector[matchingVector[j]];
317 }
318 } else {
319 matchingVector = invDataMatchingVectorT[lShifted][i];
320 }
321 unsigned int l2 = originsMatchingSize - 1;
322 for(unsigned int j = 0; j < matchingVector.size(); ++j) {
323 if(matchingVector[j] < originsPersDiff[l2].size()) {
324 dataPersDiff[i][j] = originsPersDiff[l2][matchingVector[j]];
325 dataPersPercent[i][j] = originsPersPercent[l2][matchingVector[j]];
326 }
327 }
328 }
329
330 if(lShifted == 0) {
331 dataCorrelation.resize(recs.size());
332 for(unsigned int i = 0; i < recs.size(); ++i) {
333 dataCorrelation[i].resize(persCorrelationMatrix[0].size());
334 for(unsigned int j = 0; j < persCorrelationMatrix[0].size(); ++j) {
335 dataCorrelation[i][j].resize(
336 recs[i][l].mTree.tree.getNumberOfNodes());
337 for(unsigned int k = 0; k < matchingVectors[i].size(); ++k) {
338 if(matchingVectors[i][k] < persCorrelationMatrix.size())
339 dataCorrelation[i][j][k]
340 = persCorrelationMatrix[matchingVectors[i][k]][j];
341 }
342 }
343 }
344 }
345 }
346
347 if(lShifted == 0 or lShifted == dataMatchingSize - 1
348 or l == recs[0].size() - 1) {
349 dataOriginPersOrder.resize(recs.size());
350 for(unsigned int i = 0; i < recs.size(); ++i) {
351 std::vector<ttk::ftm::idNode> &matchingVector = matchingVectors[i];
352 if(l == recs[0].size() - 1) {
353 matchingVector = invReconstMatchingVectorT[i];
354 std::vector<ttk::ftm::idNode> &matchingVectorT
355 = invDataMatchingVectorT[0][i];
356 for(unsigned int j = 0; j < matchingVector.size(); ++j)
357 if(matchingVector[j] < matchingVectorT.size())
358 matchingVector[j] = matchingVectorT[matchingVector[j]];
359 }
360 dataOriginPersOrder[i].resize(
361 recs[i][l].mTree.tree.getNumberOfNodes());
362 for(unsigned int j = 0; j < matchingVector.size(); ++j) {
363 if(matchingVector[j] < originPersistenceOrder.size())
364 dataOriginPersOrder[i][j]
365 = originPersistenceOrder[matchingVector[j]];
366 else
367 dataOriginPersOrder[i][j] = -1;
368 }
369 }
370 }
371
372 for(unsigned int i = 0; i < recs.size(); ++i) {
373 if(lShifted < dataMatchingSize) {
374 std::vector<int> customArrayMatching;
375 for(auto &e : matchingVectors[i])
376 customArrayMatching.emplace_back(e);
377 std::string name{"OriginTrueNodeId"};
378 customIntArrays[i].emplace_back(
379 std::make_tuple(name, customArrayMatching));
380 if(lShifted == 0 or lShifted == dataMatchingSize - 1) {
381 std::string name2{"OriginPersPercent"};
382 customDoubleArrays[i].emplace_back(
383 std::make_tuple(name2, dataPersPercent[i]));
384 std::string name3{"OriginPersDiff"};
385 customDoubleArrays[i].emplace_back(
386 std::make_tuple(name3, dataPersDiff[i]));
387 }
388 if(lShifted == 0) {
389 for(unsigned int j = 0; j < dataCorrelation[i].size(); ++j) {
390 std::string name2 = ttk::axa::getTableCorrelationPersName(
391 dataCorrelation[i].size(), j);
392 customDoubleArrays[i].emplace_back(
393 std::make_tuple(name2, dataCorrelation[i][j]));
394 }
395 }
396 }
397 if(lShifted == 0 or lShifted == dataMatchingSize - 1
398 or l == recs[0].size() - 1) {
399 std::string name4{"OriginPersOrder"};
400 customIntArrays[i].emplace_back(
401 std::make_tuple(name4, dataOriginPersOrder[i]));
402 }
403 }
404 }
405 } // namespace wae
406} // namespace ttk
407#endif
void setTreesSegmentation(std::vector< vtkDataSet * > &segmentation)
void copyPointData(vtkUnstructuredGrid *treeNodes, std::vector< int > &nodeCorrT)
void setVtkOutputSegmentation(vtkDataSet *vtkSegmentation)
void setTreesNodeCorrMesh(std::vector< std::vector< int > > &nodeCorrMesh)
void makeTreesOutput(FTMTree_MT *tree1)
void setConvertedToDiagram(bool converted)
void setVtkOutputArc(vtkUnstructuredGrid *vtkArc)
void setVtkOutputNode(vtkUnstructuredGrid *vtkNode)
void setTreesNodes(std::vector< vtkUnstructuredGrid * > &nodes)
virtual int setDebugLevel(const int &debugLevel)
Definition Debug.cpp:147
std::string getTableCorrelationPersName(int noAxes, int axeNum)
The Topology ToolKit.
ftm::FTMTree_MT tree
Definition FTMTree_MT.h:903