TTK
Loading...
Searching...
No Matches
MergeTreeDistanceMatrix.h
Go to the documentation of this file.
1
19
20#pragma once
21
22// ttk common includes
23#include <Debug.h>
24
26#include <FTMTree.h>
27#include <FTMTreeUtils.h>
28#include <MergeTreeBase.h>
29#include <MergeTreeDistance.h>
30#include <PathMappingDistance.h>
31
32namespace ttk {
33
38 class MergeTreeDistanceMatrix : virtual public Debug,
39 virtual public MergeTreeBase {
40 protected:
41 int baseModule_ = 0;
43 int pathMetric_ = 0;
44
45 public:
48 "MergeTreeDistanceMatrix"); // inherited from Debug: prefix will be
49 // printed at the
50 // beginning of every msg
51 }
52 ~MergeTreeDistanceMatrix() override = default;
53
54 void setBaseModule(int m) {
55 baseModule_ = m;
56 }
57
58 void setBranchMetric(int m) {
59 branchMetric_ = m;
60 }
61
62 void setPathMetric(int m) {
63 pathMetric_ = m;
64 }
65
69 template <class dataType>
70 void execute(std::vector<ftm::MergeTree<dataType>> &trees,
71 std::vector<ftm::MergeTree<dataType>> &trees2,
72 std::vector<std::vector<double>> &distanceMatrix) {
73 executePara<dataType>(trees, distanceMatrix);
74 if(trees2.size() != 0) {
75 useDoubleInput_ = true;
76 std::vector<std::vector<double>> distanceMatrix2(
77 trees2.size(), std::vector<double>(trees2.size()));
78 executePara<dataType>(trees2, distanceMatrix2, false);
79 mixDistancesMatrix(distanceMatrix, distanceMatrix2);
80 }
81 }
82
83 template <class dataType>
84 void execute(std::vector<ftm::MergeTree<dataType>> &ftmtrees,
85 std::vector<std::vector<double>> &distanceMatrix) {
86 for(unsigned int i = 0; i < distanceMatrix.size(); ++i) {
87 if(i % std::max(int(distanceMatrix.size() / 10), 1) == 0) {
88 std::stringstream stream;
89 stream << i << " / " << distanceMatrix.size();
90 printMsg(stream.str());
91 }
92
93 BranchMappingDistance branchDist;
94 branchDist.setBaseMetric(branchMetric_);
96 branchDist.setSquared(not distanceSquaredRoot_);
97 PathMappingDistance pathDist;
98 pathDist.setBaseMetric(pathMetric_);
100 pathDist.setSquared(not distanceSquaredRoot_);
101 pathDist.setComputeMapping(true);
102
103 distanceMatrix[i][i] = 0.0;
104 // compareTrees(trees[i],&(ftmtrees[i].tree));
105 for(unsigned int j = i + 1; j < distanceMatrix[0].size(); ++j) {
106 // Execute
107 if(baseModule_ == 0) {
108 distanceMatrix[i][j] = 0;
109 } else if(baseModule_ == 1) {
110 dataType dist = branchDist.editDistance_branch<dataType>(
111 &(ftmtrees[i].tree), &(ftmtrees[j].tree));
112 distanceMatrix[i][j] = static_cast<double>(dist);
113 } else if(baseModule_ == 2) {
114 dataType dist = pathDist.editDistance_path<dataType>(
115 &(ftmtrees[i].tree), &(ftmtrees[j].tree));
116 distanceMatrix[i][j] = static_cast<double>(dist);
117 }
118 // distance matrix is symmetric
119 distanceMatrix[j][i] = distanceMatrix[i][j];
120 } // end for j
121 } // end for i
122 }
123
124 template <class dataType>
125 void executePara(std::vector<ftm::MergeTree<dataType>> &trees,
126 std::vector<std::vector<double>> &distanceMatrix,
127 bool isFirstInput = true) {
128#ifdef TTK_ENABLE_OPENMP
129#pragma omp parallel num_threads(this->threadNumber_)
130 {
131#pragma omp single nowait
132#endif
133 executeParaImpl<dataType>(trees, distanceMatrix, isFirstInput);
134#ifdef TTK_ENABLE_OPENMP
135#pragma omp taskwait
136 } // pragma omp parallel
137#endif
138 }
139
140 template <class dataType>
142 std::vector<std::vector<double>> &distanceMatrix,
143 bool isFirstInput = true) {
144 for(unsigned int i = 0; i < distanceMatrix.size(); ++i) {
145#ifdef TTK_ENABLE_OPENMP
146#pragma omp task firstprivate(i) UNTIED() shared(distanceMatrix, trees)
147 {
148#endif
149 if(i % std::max(int(distanceMatrix.size() / 10), 1) == 0) {
150 std::stringstream stream;
151 stream << i << " / " << distanceMatrix.size();
152 printMsg(stream.str());
153 }
154 distanceMatrix[i][i] = 0.0;
155 for(unsigned int j = i + 1; j < distanceMatrix[0].size(); ++j) {
156 // Execute
157 if(baseModule_ == 0) {
158 MergeTreeDistance mergeTreeDistance;
159 mergeTreeDistance.setAssignmentSolver(assignmentSolverID_);
160 mergeTreeDistance.setEpsilonTree1(epsilonTree1_);
161 mergeTreeDistance.setEpsilonTree2(epsilonTree2_);
162 mergeTreeDistance.setEpsilon2Tree1(epsilon2Tree1_);
163 mergeTreeDistance.setEpsilon2Tree2(epsilon2Tree2_);
164 mergeTreeDistance.setEpsilon3Tree1(epsilon3Tree1_);
165 mergeTreeDistance.setEpsilon3Tree2(epsilon3Tree2_);
167 mergeTreeDistance.setParallelize(parallelize_);
169 mergeTreeDistance.setDebugLevel(std::min(debugLevel_, 2));
170 mergeTreeDistance.setThreadNumber(this->threadNumber_);
171 mergeTreeDistance.setNormalizedWasserstein(
173 mergeTreeDistance.setKeepSubtree(keepSubtree_);
175 mergeTreeDistance.setUseMinMaxPair(useMinMaxPair_);
176 mergeTreeDistance.setSaveTree(true);
177 mergeTreeDistance.setCleanTree(true);
178 mergeTreeDistance.setIsCalled(true);
179 mergeTreeDistance.setPostprocess(false);
181 if(useDoubleInput_) {
182 double const weight
183 = mixDistancesMinMaxPairWeight(isFirstInput);
184 mergeTreeDistance.setMinMaxPairWeight(weight);
185 mergeTreeDistance.setDistanceSquaredRoot(true);
186 }
187 std::vector<std::tuple<ftm::idNode, ftm::idNode>> outputMatching;
188 distanceMatrix[i][j] = mergeTreeDistance.execute<dataType>(
189 trees[i], trees[j], outputMatching);
190 } else {
191 distanceMatrix[i][j] = 0;
192 }
193 // distance matrix is symmetric
194 distanceMatrix[j][i] = distanceMatrix[i][j];
195 } // end for j
196#ifdef TTK_ENABLE_OPENMP
197 } // end task
198#endif
199 } // end for i
200 }
201
202 }; // MergeTreeDistanceMatrix class
203
204} // namespace ttk
virtual int setThreadNumber(const int threadNumber)
Definition BaseClass.h:80
dataType editDistance_branch(ftm::FTMTree_MT *tree1, ftm::FTMTree_MT *tree2)
void setAssignmentSolver(int assignmentSolver)
Minimalist debugging class.
Definition Debug.h:88
int debugLevel_
Definition Debug.h:379
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
virtual int setDebugLevel(const int &debugLevel)
Definition Debug.cpp:147
void setBranchDecomposition(bool useBD)
void setNormalizedWasserstein(bool normalizedWasserstein)
void setDistanceSquaredRoot(bool distanceSquaredRoot)
void setEpsilon3Tree1(double epsilon)
void setEpsilonTree1(double epsilon)
void setAssignmentSolver(int assignmentSolver)
void setEpsilon2Tree1(double epsilon)
void setEpsilonTree2(double epsilon)
void setPersistenceThreshold(double pt)
void setCleanTree(bool clean)
void setEpsilon2Tree2(double epsilon)
void setKeepSubtree(bool keepSubtree)
void setUseMinMaxPair(bool useMinMaxPair)
void setEpsilon3Tree2(double epsilon)
double mixDistancesMinMaxPairWeight(bool isFirstInput)
void setParallelize(bool para)
void setIsPersistenceDiagram(bool isPD)
void mixDistancesMatrix(std::vector< std::vector< dataType > > &distanceMatrix, std::vector< std::vector< dataType > > &distanceMatrix2)
void executePara(std::vector< ftm::MergeTree< dataType > > &trees, std::vector< std::vector< double > > &distanceMatrix, bool isFirstInput=true)
void execute(std::vector< ftm::MergeTree< dataType > > &ftmtrees, std::vector< std::vector< double > > &distanceMatrix)
void executeParaImpl(std::vector< ftm::MergeTree< dataType > > &trees, std::vector< std::vector< double > > &distanceMatrix, bool isFirstInput=true)
void execute(std::vector< ftm::MergeTree< dataType > > &trees, std::vector< ftm::MergeTree< dataType > > &trees2, std::vector< std::vector< double > > &distanceMatrix)
~MergeTreeDistanceMatrix() override=default
void setPostprocess(bool postproc)
void setSaveTree(bool save)
void setMinMaxPairWeight(double weight)
dataType execute(ftm::MergeTree< dataType > &mTree1, ftm::MergeTree< dataType > &mTree2, std::vector< std::tuple< ftm::idNode, ftm::idNode, double > > &outputMatching)
void setAssignmentSolver(int assignmentSolver)
dataType editDistance_path(ftm::FTMTree_MT *tree1, ftm::FTMTree_MT *tree2)
The Topology ToolKit.
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)