-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistentHomology.cpp
More file actions
499 lines (408 loc) · 11.8 KB
/
Copy pathPersistentHomology.cpp
File metadata and controls
499 lines (408 loc) · 11.8 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include "Eigen/Dense"
using namespace Eigen;
using namespace std;
MatrixXd load_csv (const string& path);
MatrixXd rowSelection(MatrixXd mat, VectorXi select_rows);
MatrixXd colSelection(MatrixXd mat, VectorXi select_cols);
VectorXi IndexSelection(VectorXi select_index);
VectorXd sort_decending(VectorXd vec);
MatrixXd Neighbors(MatrixXd Points);
MatrixXd addRow(MatrixXd mat, RowVectorXd vec);
VectorXi addElement(VectorXi vec, int element);
MatrixXd rmRow(MatrixXd mat, int row);
MatrixXd rmDupRow(MatrixXd mat);
MatrixXd rmDupRows(MatrixXd mat);
bool MatrixContainRowVector(MatrixXd mat, RowVectorXd vec);
bool VectorContainElement(VectorXi vec, int element);
VectorXi extract_keys(map<int,VectorXi> mymap);
VectorXi extract_keys_double(map<int,double> mymap);
template<typename T> void vecDupRM(vector<T> vec);
VectorXi Intersect(VectorXi x, VectorXi y);
VectorXi Union(VectorXi x, VectorXi y);
MatrixXd UnionMatrix(MatrixXd A, MatrixXd B);
bool ManhattanDist(MatrixXd PointSet, MatrixXd n_Pos);
void PersistentHomologyAnalysis(MatrixXd mat);
// Main Function
int main(int argc, char *argv[])
{
MatrixXd A = load_csv(argv[1]);
//MatrixXd B = MatrixXd::Random(4,7);
//TestMatrix(A,B);
PersistentHomologyAnalysis(A);
return 0;
}
// Functions
MatrixXd load_csv (const string& path)
{
ifstream indata;
indata.open(path);
string line;
vector<double> values;
uint rows = 0;
while (getline(indata, line))
{
stringstream lineStream(line);
string cell;
while (getline(lineStream, cell, '\t'))
{
values.push_back(stod(cell));
}
++rows;
}
MatrixXd Mat = Map<Matrix<double,Dynamic,Dynamic,RowMajor> >(values.data(), rows, values.size()/rows);
indata.close();
return Mat;
}
MatrixXd rowSelection(MatrixXd mat, VectorXi select_rows)
{
MatrixXd mat_sel(select_rows.sum(), mat.cols());
int rownew = 0;
for (int i = 0; i < mat.rows(); ++i)
{
if (select_rows[i])
{
mat_sel.row(rownew) = mat.row(i);
rownew++;
}
}
return mat_sel;
}
MatrixXd colSelection(MatrixXd mat, VectorXi select_cols)
{
MatrixXd mat_sel(mat.rows(),select_cols.sum());
int colnew = 0;
for (int i = 0; i < mat.cols(); ++i)
{
if (select_cols[i])
{
mat_sel.col(colnew) = mat.col(i);
colnew++;
}
}
return mat_sel;
}
VectorXi IndexSelection(VectorXi select_index)
{
VectorXi idx_sel(select_index.sum());
int item = 0;
for (int i = 0; i < select_index.size(); ++i)
{
if (select_index[i])
{
idx_sel(item) = i;
item++;
}
}
return idx_sel;
}
VectorXd sort_decending(VectorXd vec)
{
ArrayXd sorted = vec.array();
std::sort(sorted.begin(), sorted.end(), std::greater<double>());
VectorXd result = sorted;
return result;
}
MatrixXd Neighbors(MatrixXd Points)
{
MatrixXd NeighborPoints;
RowVectorXd Point;
RowVectorXd Neighbor;
for(int i = 0; i < Points.rows(); ++i)
{
Point = Points.row(i);
for(int j = 0; j < Point.size(); ++j)
{
Neighbor = Point;
Neighbor[j] = Point[j] + 1;
NeighborPoints.conservativeResize(NeighborPoints.rows() + 1, Points.cols());
NeighborPoints.row(NeighborPoints.rows()-1) = Neighbor;
Neighbor = Point;
Neighbor[j] = Neighbor[j] - 1;
NeighborPoints.conservativeResize(NeighborPoints.rows() + 1, Points.cols());
NeighborPoints.row(NeighborPoints.rows()-1) = Neighbor;
}
}
MatrixXd Result = rmDupRows(NeighborPoints);
return Result;
}
MatrixXd addRow(MatrixXd mat, RowVectorXd vec)
{
mat.conservativeResize(mat.rows() + 1, mat.cols());
mat.row(mat.rows()-1) = vec;
return mat;
}
VectorXi addElement(VectorXi vec, int element)
{
vector<int> tmp(&vec[0], vec.data() + vec.size());
tmp.push_back(element);
Map<VectorXi> result(tmp.data(), tmp.size());
return result;
}
MatrixXd rmRow(MatrixXd mat, int row)
{
int numRows = mat.rows() - 1;
int numCols = mat.cols();
if( row < numRows )
{
mat.block(row,0,numRows-row,numCols) = mat.block(row+1,0,numRows-row,numCols);
}
mat.conservativeResize(numRows,numCols);
return mat;
}
MatrixXd rmDupRow(MatrixXd mat)
{
for(int i = 0; i < mat.rows(); ++i)
{
for(int j = i + 1; j < mat.rows(); ++j)
{
if(mat.row(i) == mat.row(j))
{
mat = rmRow(mat,j);
break;
}
}
}
return mat;
}
MatrixXd rmDupRows(MatrixXd mat)
{
MatrixXd Update = mat;
while(true)
{
int Before = Update.rows();
Update = rmDupRow(Update);
int After = Update.rows();
if(Before == After)
{
break;
}
}
return Update;
}
bool MatrixContainRowVector(MatrixXd mat, RowVectorXd vec)
{
int contain = false;
for(int i = 0; i < mat.rows(); ++i)
{
if(mat.row(i) == vec)
{
contain = true;
}
}
return contain;
}
bool VectorContainElement(VectorXi vec, int element)
{
int contain = false;
for(int i = 0; i < vec.size(); ++i)
{
if(vec[i] == element)
{
contain = true;
}
}
return contain;
}
VectorXi extract_keys(map<int,VectorXi> mymap)
{
vector<int> v;
for (map<int,VectorXi>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
v.push_back(it->first);
}
Map<VectorXi> result(v.data(), v.size());
return result;
}
VectorXi extract_keys_double(map<int,double> mymap)
{
vector<int> v;
for (map<int,double>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
v.push_back(it->first);
}
Map<VectorXi> result(v.data(), v.size());
return result;
}
vector<int> vecDupRM(vector<int> vec)
{
std::sort(vec.begin(),vec.end());
vec.erase(std::unique(vec.begin(),vec.end()),vec.end());
return(vec);
}
VectorXi Intersect(VectorXi x, VectorXi y)
{
vector<int> result;
for(int i = 0; i<x.size(); ++i)
{
for(int j = 0; j<y.size(); ++j)
{
if(x[i] == y[j])
{
result.push_back(x[i]);
}
}
}
result = vecDupRM(result);
Map<VectorXi> Intersection(result.data(), result.size());
return Intersection;
}
VectorXi Union(VectorXi x, VectorXi y)
{
vector<int> result;
for(int i = 0; i<x.size(); ++i)
{
for(int j = 0; j<y.size(); ++j)
{
result.push_back(x[i]);
result.push_back(y[j]);
}
}
result = vecDupRM(result);
Map<VectorXi> UnionSet(result.data(), result.size());
return UnionSet;
}
MatrixXd UnionMatrix(MatrixXd A, MatrixXd B)
{
MatrixXd C(A.rows()+B.rows(), A.cols());
C << A, B;
MatrixXd D = rmDupRows(C);
return D;
}
bool ManhattanDist(MatrixXd PointSet, MatrixXd n_Pos)
{
for(int i=0; i<=PointSet.rows(); ++i)
{
PointSet.row(i) = PointSet.row(i) - n_Pos;
}
MatrixXd ABS = PointSet.cwiseAbs();
double Minimum = ABS.rowwise().sum().minCoeff();
if(Minimum <= 1)
{
return true;
}
else
{
return false;
}
}
void PersistentHomologyAnalysis(MatrixXd mat)
{
// Read density landscape file
MatrixXd Dimension = mat.block(0,0,mat.rows(),mat.cols()-1);
VectorXd Density = mat.block(0,mat.cols()-1,mat.rows(),1);
int dim = Dimension.cols();
cout << "Data dimension is = " << dim << endl;
double Maximum = Density.maxCoeff();
double Minimum = Density.minCoeff();
cout << "Maximum Density = " << Maximum << endl;
cout << "Minimum Density = " << Minimum << endl;
map<int,VectorXi> PointIndexCluster;
map<int,double> BarcodeBirth;
map<int,double> BarcodeDeath;
map<int,VectorXi> Peaks;
VectorXi IndexNotes;
VectorXi BeforeUpdate;
VectorXi AfterUpdate;
MatrixXd Point;
VectorXi Keys;
VectorXi I;
VectorXi J;
VectorXi Intersection;
VectorXi BarcodeBirthKey;
VectorXi BarcodeDeathKey;
VectorXd NewRow;
VectorXi select_rows_1st = (mat.col(mat.cols()-1).array() == Maximum).cast<int>();
MatrixXd HighestLevelSet = rowSelection(Dimension, select_rows_1st);
VectorXi Index = IndexSelection(select_rows_1st);
cout << "HighestLevelSet: " << HighestLevelSet << endl;
cout << "highest Index: " << Index << endl << endl;
// Find the highest point in the density landscape
for(int i = 0; i < Index.size(); ++i)
{
Point = HighestLevelSet(i,all);
PointIndexCluster[i] = Index;
Peaks[i] = Index;
BarcodeBirth[i] = Maximum;
}
// Superlevel set filtration
VectorXd Den = sort_decending(Density);
for(int i = 1; i < Den.size(); ++i)
{
VectorXi select_rows_2nd = (mat.col(mat.cols()-1).array() == Den[i]).cast<int>();
MatrixXd LevelSet = rowSelection(Dimension, select_rows_2nd);
VectorXi Index = IndexSelection(select_rows_2nd);
cout << "Density: " << Den[i] << endl;
for(int n = 0; n < LevelSet.rows(); ++n)
{
MatrixXd n_pos = LevelSet.row(n);
VectorXi n_index(1);
n_index << Index(n);
IndexNotes = extract_keys(PointIndexCluster);
int knowncluster = IndexNotes(IndexNotes.size()-1);
for(int key = 0; key < IndexNotes.size(); ++key)
{
VectorXi idx = PointIndexCluster[key];
MatrixXd PointPosition = Dimension(idx,all);
if( MatrixContainRowVector(Neighbors(PointPosition),n_pos) )
{
PointIndexCluster[key] = addElement(PointIndexCluster[key],Index[n]);
}
else
{
PointIndexCluster[knowncluster + 1] = n_index;
Peaks[knowncluster + 1] = n_index;
BarcodeBirth[knowncluster + 1] = Den[i];
}
}
}
BeforeUpdate = extract_keys(PointIndexCluster);
Keys = BeforeUpdate;
if(Keys.size() > 1)
{
for(int x = 0; x < Keys.size(); ++x)
{
for(int y = x + 1; y < Keys.size(); ++y)
{
I = PointIndexCluster[x];
J = PointIndexCluster[y];
Intersection = Intersect(I,J);
if(Intersection.size() > 0)
{
BarcodeDeath[y] = Den[i];
PointIndexCluster[x] = Union(I,J);
PointIndexCluster.erase(y);
}
}
}
}
}
BarcodeBirthKey = extract_keys_double(BarcodeBirth);
BarcodeDeathKey = extract_keys_double(BarcodeDeath);
cout << BarcodeBirthKey << "\nend" << endl;
cout << BarcodeDeathKey << "\nend" << endl << endl;
for(int i = 0; i<BarcodeBirthKey.size(); ++i)
{
int key = BarcodeBirthKey[i];
cout << VectorContainElement(BarcodeDeathKey,key) << endl;
if(not VectorContainElement(BarcodeDeathKey,key) )
{
BarcodeDeath[key] = Minimum;
cout << BarcodeDeath[key] << endl;
}
}
ofstream output;
output.open ("PersistentDiagram.txt");
output << "BarcodeBirth\tBarcodeDeath\tBarcodeLength" << endl;
for(int i = 0; i<BarcodeBirthKey.size(); ++i)
{
int key = BarcodeBirthKey[i];
output << BarcodeBirth[key] << "\t" << BarcodeDeath[key] << "\t" << BarcodeBirth[key] - BarcodeDeath[key]<<endl;
}
output.close();
cout << "Persistent Diagram Calculating Finished." << endl;
}