TTK
Loading...
Searching...
No Matches
MergeTreeUtils.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <FTMTreeUtils.h>
10#include <stack>
11
12namespace ttk {
13
14 template <class dataType>
16 ftm::idNode tree1Node,
17 ftm::FTMTree_MT *tree2,
18 ftm::idNode tree2Node) {
19 dataType tree1NodeDeath
20 = std::get<1>(tree1->getBirthDeath<dataType>(tree1Node));
21 dataType tree2NodeDeath
22 = std::get<1>(tree2->getBirthDeath<dataType>(tree2Node));
23 return tree1NodeDeath > tree2NodeDeath;
24 }
25
26 // --------------------
27 // Normalized Wasserstein
28 // --------------------
29 template <class dataType>
31 ftm::idNode nodeId,
32 bool getMin = true) {
33 auto nodeIdParent = tree->getParentSafe(nodeId);
34
35 if(tree->notNeedToNormalize(nodeId))
36 return getMin ? 0.0 : 1.0;
37
38 // General case
39 std::tuple<dataType, dataType> birthDeath
40 = tree->getBirthDeath<dataType>(nodeIdParent);
41
42 // Verify inconsistency
43 if(tree->isParentInconsistent<dataType>(nodeId)) {
44 // printErr("inconsistency");
45 // if(tree->getNumberOfNodes() < 1000)
46 tree->printTree();
47 // printPairsFromTree<dataType>(tree, true);
48 tree->printNode2<dataType>(nodeId);
49 tree->printNode2<dataType>(nodeIdParent);
50 }
51
52 return getMin ? std::get<0>(birthDeath) : std::get<1>(birthDeath);
53 }
54
55 template <class dataType>
56 std::tuple<dataType, dataType> getMinMaxLocalT(ftm::FTMTree_MT *tree,
57 ftm::idNode nodeId) {
58 dataType min = getMinMaxLocal<dataType>(tree, nodeId);
59 dataType max = getMinMaxLocal<dataType>(tree, nodeId, false);
60 return std::make_tuple(min, max);
61 }
62
63 template <class dataType>
64 std::tuple<dataType, dataType> getNormalizedBirthDeath(ftm::FTMTree_MT *tree,
65 ftm::idNode nodeId,
66 dataType newMin = 0.0,
67 dataType newMax
68 = 1.0) {
69 auto birthDeath = tree->getBirthDeath<dataType>(nodeId);
70 dataType birth = std::get<0>(birthDeath);
71 dataType death = std::get<1>(birthDeath);
72 dataType shiftMin = getMinMaxLocal<dataType>(tree, nodeId);
73 dataType shiftMax = getMinMaxLocal<dataType>(tree, nodeId, false);
74 if((shiftMax - shiftMin) == 0)
75 return std::make_tuple(0, 0);
76 birth = (newMax - newMin) * (birth - shiftMin)
77 / (shiftMax - shiftMin); // + newMin;
78 death = (newMax - newMin) * (death - shiftMin)
79 / (shiftMax - shiftMin); // + newMin;
80 return std::make_tuple(birth, death);
81 }
82
83 template <class dataType>
84 std::tuple<dataType, dataType> getParametrizedBirthDeath(
85 ftm::FTMTree_MT *tree, ftm::idNode node, bool normalize) {
86 return normalize ? getNormalizedBirthDeath<dataType>(tree, node)
87 : tree->getBirthDeath<dataType>(node);
88 }
89
90 template <class dataType>
92 ftm::idNode nodeId,
93 std::vector<dataType> &scalarsVector,
94 bool getMin = true) {
95 auto nodeIdParent = tree->getParentSafe(nodeId);
96
97 if(tree->notNeedToNormalize(nodeId))
98 return getMin ? 0.0 : 1.0;
99
100 // General case
101 dataType death = scalarsVector[nodeIdParent];
102 dataType birth = scalarsVector[tree->getNode(nodeIdParent)->getOrigin()];
103 if(death < birth) {
104 dataType temp = death;
105 death = birth;
106 birth = temp;
107 }
108
109 return getMin ? birth : death;
110 }
111
112 // --------------------
113 // Testing
114 // --------------------
115 template <class dataType>
116 std::shared_ptr<ftm::FTMTree_MT>
117 makeFakeTree(dataType *nodesScalar,
118 std::vector<SimplexId> &nodes,
119 std::vector<std::tuple<ftm::idNode, ftm::idNode>> &arcs) {
120 // Init Scalars
121 auto scalars = std::make_shared<ftm::Scalars>();
122 scalars->size = nodes.size();
123 scalars->values = (void *)nodesScalar;
124
125 // Init Tree
126 auto params = std::make_shared<ftm::Params>();
127 auto tree
128 = std::make_shared<ftm::FTMTree_MT>(params, scalars, ftm::Join_Split);
129 tree->makeAlloc();
130
131 // Add Nodes
132 for(auto i : nodes)
133 tree->makeNode(i);
134
135 // Add Arcs
136 for(std::tuple<ftm::idNode, ftm::idNode> arc : arcs)
137 tree->makeSuperArc(std::get<0>(arc), std::get<1>(arc)); // (down, Up)
138
139 return tree;
140 }
141
142 template <class dataType>
143 ftm::MergeTree<dataType>
144 makeFakeMergeTree(std::vector<dataType> &scalarsVector,
145 std::vector<std::tuple<ftm::idNode, ftm::idNode>> &arcs) {
147 = ttk::ftm::createEmptyMergeTree<dataType>(scalarsVector.size());
148 ttk::ftm::setTreeScalars<dataType>(mergeTree, scalarsVector);
149 ftm::FTMTree_MT *tree = &(mergeTree.tree);
150
151 // Add Nodes
152 for(unsigned int i = 0; i < scalarsVector.size(); ++i)
153 tree->makeNode(i);
154
155 // Add Arcs
156 for(std::tuple<ftm::idNode, ftm::idNode> arc : arcs)
157 tree->makeSuperArc(std::get<0>(arc), std::get<1>(arc)); // (down, Up)
158
159 return mergeTree;
160 }
161
162} // namespace ttk
bool notNeedToNormalize(idNode nodeId)
std::tuple< dataType, dataType > getBirthDeath(idNode nodeId)
bool isParentInconsistent(ftm::idNode nodeId)
idNode getParentSafe(idNode nodeId)
std::stringstream printNode2(idNode nodeId, bool doPrint=true)
idNode makeNode(SimplexId vertexId, SimplexId linked=nullVertex)
idSuperArc makeSuperArc(idNode downNodeId, idNode upNodeId)
Node * getNode(idNode nodeId)
Definition FTMTree_MT.h:393
std::stringstream printTree(bool doPrint=true)
unsigned int idNode
Node index in vect_nodes_.
The Topology ToolKit.
dataType getMinMaxLocalFromVector(ftm::FTMTree_MT *tree, ftm::idNode nodeId, std::vector< dataType > &scalarsVector, bool getMin=true)
std::shared_ptr< ftm::FTMTree_MT > makeFakeTree(dataType *nodesScalar, std::vector< SimplexId > &nodes, std::vector< std::tuple< ftm::idNode, ftm::idNode > > &arcs)
ftm::MergeTree< dataType > makeFakeMergeTree(std::vector< dataType > &scalarsVector, std::vector< std::tuple< ftm::idNode, ftm::idNode > > &arcs)
dataType getMinMaxLocal(ftm::FTMTree_MT *tree, ftm::idNode nodeId, bool getMin=true)
std::tuple< dataType, dataType > getMinMaxLocalT(ftm::FTMTree_MT *tree, ftm::idNode nodeId)
std::tuple< dataType, dataType > getNormalizedBirthDeath(ftm::FTMTree_MT *tree, ftm::idNode nodeId, dataType newMin=0.0, dataType newMax=1.0)
bool isDeathHigher(ftm::FTMTree_MT *tree1, ftm::idNode tree1Node, ftm::FTMTree_MT *tree2, ftm::idNode tree2Node)
std::tuple< dataType, dataType > getParametrizedBirthDeath(ftm::FTMTree_MT *tree, ftm::idNode node, bool normalize)
coefficient_t normalize(const coefficient_t n, const coefficient_t modulus)
Definition ripserpy.cpp:159
ftm::FTMTree_MT tree
Definition FTMTree_MT.h:903