TTK
Loading...
Searching...
No Matches
PersistenceDiagramDictionaryDecoding.cpp
Go to the documentation of this file.
4#include <cmath>
5
7 std::vector<ttk::DiagramType> &dictDiagrams,
8 std::vector<std::vector<double>> &vectorWeights,
9 std::vector<ttk::DiagramType> &Barycenters) const {
10
11 Timer tm{};
12
13 std::vector<std::vector<std::vector<MatchingType>>> AllMatchingsAtoms(
14 Barycenters.size());
15
16 for(size_t i = 0; i < Barycenters.size(); ++i) {
17 auto &barycenter = Barycenters[i];
18 auto &weight = vectorWeights[i];
19 auto &matchings = AllMatchingsAtoms[i];
21 dictDiagrams, weight, barycenter, matchings, *this, ProgBarycenter);
22 }
23
24 this->printMsg(
25 "Computed barycenters", 1.0, tm.getElapsedTime(), this->threadNumber_);
26}
27
29 std::vector<ttk::DiagramType> &atoms,
30 const std::vector<std::vector<double>> &vectorWeights,
31 std::vector<std::array<double, 3>> &coords,
32 std::vector<std::array<double, 3>> &trueCoords,
33 std::vector<double> &xVector,
34 std::vector<double> &yVector,
35 std::vector<double> &zVector,
36 const double spacing,
37 const size_t nAtoms) const {
38
39 if(nAtoms == 2) {
41 std::array<size_t, 2> nInputs{nAtoms, 0};
42 MatrixCalculator.setDos(true, true, true);
43 MatrixCalculator.setThreadNumber(2);
44 const auto distMatrix = MatrixCalculator.execute(atoms, nInputs);
45 coords[0][0] = 0.;
46 trueCoords[0][0] = 0.;
47 coords[0][1] = 0.;
48 trueCoords[0][0] = 0.;
49 coords[1][0] = spacing * distMatrix[0][1];
50 trueCoords[1][0] = distMatrix[0][1];
51 trueCoords[1][1] = 0.;
52
53 } else if(nAtoms == 3) {
55 std::array<size_t, 2> nInputs{nAtoms, 0};
56 MatrixCalculator.setDos(true, true, true);
57 MatrixCalculator.setThreadNumber(3);
58 std::vector<std::vector<double>> distMatrix
59 = MatrixCalculator.execute(atoms, nInputs);
60 coords[0][0] = 0.;
61 trueCoords[0][0] = 0.;
62 coords[0][1] = 0.;
63 trueCoords[0][1] = 0.;
64 coords[1][0] = spacing * distMatrix[0][1];
65 trueCoords[1][0] = distMatrix[0][1];
66 coords[1][1] = 0.;
67 trueCoords[1][1] = 0.;
68 double distOpposed = distMatrix[2][1];
69 double firstDist = distMatrix[0][1];
70 double distAdja = distMatrix[0][2];
71 double alpha = std::acos(
72 (distOpposed * distOpposed - firstDist * firstDist - distAdja * distAdja)
73 / (-2. * firstDist * distAdja));
74 coords[2][0] = spacing * distAdja * std::cos(alpha);
75 trueCoords[2][0] = distAdja * std::cos(alpha);
76 coords[2][1] = spacing * distAdja * std::sin(alpha);
77 trueCoords[2][1] = distAdja * std::sin(alpha);
78
79 } else if(nAtoms == 4) {
80 switch(this->ProjMet) {
83 std::array<size_t, 2> nInputs{nAtoms, 0};
84 MatrixCalculator.setDos(true, true, true);
85 MatrixCalculator.setThreadNumber(3);
86 std::vector<std::vector<double>> distMatrix
87 = MatrixCalculator.execute(atoms, nInputs);
88 coords[0][0] = 0.;
89 trueCoords[0][0] = 0.;
90 coords[0][1] = 0.;
91 trueCoords[0][1] = 0.;
92 coords[1][0] = spacing * distMatrix[0][1];
93 trueCoords[1][0] = distMatrix[0][1];
94 coords[1][1] = 0.;
95 trueCoords[1][1] = 0.;
96 double distOpposed = distMatrix[2][1];
97 double firstDist = distMatrix[0][1];
98 double distAdja = distMatrix[0][2];
99 double alpha = std::acos((distOpposed * distOpposed
100 - firstDist * firstDist - distAdja * distAdja)
101 / (-2. * firstDist * distAdja));
102 coords[2][0] = spacing * distAdja * std::cos(alpha);
103 trueCoords[2][0] = distAdja * std::cos(alpha);
104 coords[2][1] = spacing * distAdja * std::sin(alpha);
105 trueCoords[2][1] = distAdja * std::sin(alpha);
106 double firstHeight = distMatrix[3][0];
107 double secondHeight = distMatrix[3][1];
108 double thirdHeight = distMatrix[3][2];
109 trueCoords[3][0] = (pow(firstHeight, 2) - pow(secondHeight, 2)
110 + pow(trueCoords[1][0], 2))
111 / (2 * trueCoords[1][0]);
112 trueCoords[3][1]
113 = (pow(firstHeight, 2) - pow(thirdHeight, 2)
114 + pow(trueCoords[2][0], 2) + pow(trueCoords[2][1], 2)
115 - 2 * trueCoords[3][0] * trueCoords[2][0])
116 / (2 * trueCoords[2][1]);
117 trueCoords[3][2]
118 = std::sqrt(pow(firstHeight, 2) - pow(trueCoords[3][0], 2)
119 - pow(trueCoords[3][1], 2));
120
121 break;
122 }
123
124 case BACKEND::MDS: {
125 ttk::DimensionReduction DimProjector;
126 DimProjector.setIsInputDistanceMatrix(true);
127 DimProjector.setInputNumberOfComponents(3);
129 std::array<size_t, 2> nInputs{nAtoms, 0};
130 MatrixCalculator.setDos(true, true, true);
131 MatrixCalculator.setThreadNumber(4);
132 std::vector<std::vector<double>> distMatrix
133 = MatrixCalculator.execute(atoms, nInputs);
134 int nRow = distMatrix.size();
135 std::vector<double> matrixForProjector;
136 for(int i = 0; i < nRow; ++i) {
137 for(int j = 0; j < nRow; ++j) {
138 matrixForProjector.push_back(distMatrix[j][i]);
139 }
140 }
141 std::vector<std::vector<double>> coordsAtom;
142 DimProjector.execute(coordsAtom, matrixForProjector, nRow, nRow);
143
144 for(size_t i = 0; i < 3; ++i) {
145 for(size_t j = 0; j < nAtoms; ++j) {
146 if(i == 0) {
147 trueCoords[j][0] = coordsAtom[0][j];
148 } else if(i == 1) {
149 trueCoords[j][1] = coordsAtom[1][j];
150 } else {
151 trueCoords[j][2] = coordsAtom[2][j];
152 }
153 }
154 }
155
156 break;
157 }
158 }
159 } else {
160 switch(this->ProjMet) {
161 case BACKEND::DICTIONARY: {
162
163 std::vector<ttk::DiagramType> dictDiagrams;
164 std::vector<ttk::DiagramType> intermediateAtoms;
165 std::vector<double> lossTab;
166 std::vector<std::vector<double>> allLosses(nAtoms);
167 const int seed = 0;
168 const int m = 3;
169 std::vector<std::vector<double>> tempWeights(nAtoms);
170 for(size_t i = 0; i < tempWeights.size(); ++i) {
171 std::vector<double> weights(m, 1. / (m * 1.));
172 tempWeights[i] = std::move(weights);
173 }
174 ttk::PersistenceDiagramDictionary DictionaryEncoder;
175 DictionaryEncoder.setUseDimReduct(false);
176 DictionaryEncoder.setUseProgApproach(true);
177 DictionaryEncoder.execute(atoms, atoms, dictDiagrams, tempWeights, seed,
178 m, lossTab, allLosses, 0.);
179 std::vector<std::array<double, 3>> tempCoords(3);
180 std::vector<std::array<double, 3>> tempTrueCoords(3);
182 std::array<size_t, 2> nInputs{3, 0};
183 MatrixCalculator.setDos(true, true, true);
184 MatrixCalculator.setThreadNumber(3);
185 std::vector<std::vector<double>> distMatrix
186 = MatrixCalculator.execute(dictDiagrams, nInputs);
187 tempCoords[0][0] = 0.;
188 tempTrueCoords[0][0] = 0.;
189 tempCoords[0][1] = 0.;
190 tempTrueCoords[0][1] = 0.;
191 tempCoords[1][0] = spacing * distMatrix[0][1];
192 tempTrueCoords[1][0] = distMatrix[0][1];
193 tempCoords[1][1] = 0.;
194 tempTrueCoords[0][1] = 0.;
195 double distOpposed = distMatrix[2][1];
196 double firstDist = distMatrix[0][1];
197 double distAdja = distMatrix[0][2];
198 double alpha = std::acos((distOpposed * distOpposed
199 - firstDist * firstDist - distAdja * distAdja)
200 / (-2. * firstDist * distAdja));
201 tempCoords[2][0] = spacing * distAdja * std::cos(alpha);
202 tempTrueCoords[2][0] = distAdja * std::cos(alpha);
203 tempCoords[2][1] = spacing * distAdja * std::sin(alpha);
204 tempTrueCoords[2][1] = distAdja * std::sin(alpha);
205 for(int i = 0; i < 2; ++i) {
206 for(size_t j = 0; j < nAtoms; ++j) {
207 double lamb = 0.;
208 for(int iAtom = 0; iAtom < 3; ++iAtom) {
209 if(i == 0) {
210 lamb += tempWeights[j][iAtom] * tempTrueCoords[iAtom][0];
211 trueCoords[j][0] = lamb;
212 } else {
213 lamb += tempWeights[j][iAtom] * tempTrueCoords[iAtom][1];
214 trueCoords[j][1] = lamb;
215 }
216 }
217 }
218 }
219
220 break;
221 }
222
223 case BACKEND::MDS: {
224 ttk::DimensionReduction DimProjector;
225 DimProjector.setIsInputDistanceMatrix(true);
227 std::array<size_t, 2> nInputs{nAtoms, 0};
228 MatrixCalculator.setDos(true, true, true);
229 MatrixCalculator.setThreadNumber(3);
230 std::vector<std::vector<double>> distMatrix
231 = MatrixCalculator.execute(atoms, nInputs);
232 int nRow = distMatrix.size();
233 std::vector<double> matrixForProjector;
234 for(int i = 0; i < nRow; ++i) {
235 for(int j = 0; j < nRow; ++j) {
236 matrixForProjector.push_back(distMatrix[j][i]);
237 }
238 }
239 std::vector<std::vector<double>> coordsAtom;
240 DimProjector.execute(coordsAtom, matrixForProjector, nRow, nRow);
241
242 for(size_t i = 0; i < 2; ++i) {
243 for(size_t j = 0; j < nAtoms; ++j) {
244 if(i == 0) {
245 trueCoords[j][0] = coordsAtom[0][j];
246 } else {
247 trueCoords[j][1] = coordsAtom[1][j];
248 }
249 }
250 }
251
252 break;
253 }
254 }
255 }
256 size_t nDiags = vectorWeights.size();
257
258 for(int i = 0; i < 3; ++i) {
259 for(size_t j = 0; j < nDiags; ++j) {
260 double temp = 0.;
261 for(size_t iAtom = 0; iAtom < nAtoms; ++iAtom) {
262 if(i == 0) {
263 temp += vectorWeights[j][iAtom] * trueCoords[iAtom][0];
264 } else if(i == 1) {
265 temp += vectorWeights[j][iAtom] * trueCoords[iAtom][1];
266 } else {
267 temp += vectorWeights[j][iAtom] * trueCoords[iAtom][2];
268 }
269 }
270 if(i == 0) {
271 xVector[j] = temp;
272 } else if(i == 1) {
273 yVector[j] = temp;
274 } else {
275 zVector[j] = temp;
276 }
277 }
278 }
279}
virtual int setThreadNumber(const int threadNumber)
Definition BaseClass.h:80
TTK VTK-filter that apply dimension reduction algorithms on input.
void setIsInputDistanceMatrix(const bool data)
int execute(std::vector< std::vector< double > > &outputEmbedding, const std::vector< double > &inputMatrix, const int nRows, const int nColumns, int *insertionTimeForTopoMap=nullptr) const
void setInputNumberOfComponents(const int numberOfComponents)
void computeAtomsCoordinates(std::vector< ttk::DiagramType > &atoms, const std::vector< std::vector< double > > &vectorWeights, std::vector< std::array< double, 3 > > &coords, std::vector< std::array< double, 3 > > &trueCoords, std::vector< double > &xVector, std::vector< double > &yVector, std::vector< double > &zVector, const double spacing, const size_t nAtoms) const
void execute(std::vector< DiagramType > &dictDiagrams, std::vector< std::vector< double > > &vectorWeights, std::vector< DiagramType > &Barycenters) const
void execute(std::vector< ttk::DiagramType > &intermediateDiagrams, const std::vector< ttk::DiagramType > &intermediateAtoms, std::vector< ttk::DiagramType > &dictDiagrams, std::vector< std::vector< double > > &vectorWeights, const int seed, const int numAtom, std::vector< double > &lossTab, std::vector< std::vector< double > > &allLosses, double percent)
std::vector< std::vector< double > > execute(const std::vector< DiagramType > &intermediateDiagrams, const std::array< size_t, 2 > &nInputs) const
void setDos(const bool min, const bool sad, const bool max)
void computeWeightedBarycenter(std::vector< DiagramType > &intermediateDiagrams, std::vector< double > &weights, DiagramType &barycenter, std::vector< std::vector< MatchingType > > &matchings, const ttk::Debug &dbg, const bool ProgBarycenter)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/| (_) |"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)