-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_pairs.cpp
256 lines (225 loc) · 7.92 KB
/
compute_pairs.cpp
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
/* compute_pairs.cpp
Copyright 2017-2018 Takeki Sudo and Kazushi Ahara.
This file is part of CubicalRipser_4dim.
CubicalRipser: C++ system for computation of Cubical persistence pairs
Copyright 2017-2018 Takeki Sudo and Kazushi Ahara.
CubicalRipser is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
CubicalRipser is deeply depending on 'Ripser', software for Vietoris-Rips
persitence pairs by Ulrich Bauer, 2015-2016. We appreciate Ulrich very much.
We rearrange his codes of Ripser and add some new ideas for optimization on it
and modify it for calculation of a Cubical filtration.
This part of CubicalRiper is a calculator of cubical persistence pairs for
4 dimensional voxel data. The input data format conforms to that of DIPHA or of PERSEUS.
See more descriptions in README.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <algorithm>
#include <queue>
#include "dense_cubical_grids.h"
#include "birthday_index.h"
#include "columns_to_reduce.h"
#include "simplex_coboundary_enumerator.h"
#include "write_pairs.h"
#include "union_find.h"
#include "compute_pairs.h"
using namespace std;
ComputePairs::ComputePairs(DenseCubicalGrids* _dcg, ColumnsToReduce* _ctr, vector<WritePairs> &_wp, const bool _print){
dcg = _dcg;
ctr = _ctr;
dim = _ctr -> dim;
wp = &_wp;
print = _print;
ax = _dcg -> ax;
ay = _dcg -> ay;
az = _dcg -> az;
aw = _dcg -> aw;
}
void ComputePairs::compute_pairs_main(){
if(print == true){
cout << "persistence intervals in dim " << dim << ":" << endl;
}
vector<BirthdayIndex> coface_entries;
SimplexCoboundaryEnumerator cofaces;
unordered_map<int, priority_queue<BirthdayIndex, vector<BirthdayIndex>, BirthdayIndexComparator>> recorded_wc;
pivot_column_index = hash_map<int, int>();
auto ctl_size = ctr -> columns_to_reduce.size();
pivot_column_index.reserve(ctl_size);
recorded_wc.reserve(ctl_size);
for(int i = 0; i < ctl_size; ++i){
auto column_to_reduce = ctr -> columns_to_reduce[i];
priority_queue<BirthdayIndex, vector<BirthdayIndex>, BirthdayIndexComparator> working_coboundary;
double birth = column_to_reduce.getBirthday();
int j = i;
BirthdayIndex pivot(0, -1, 0);
bool might_be_apparent_pair = true;
bool goto_found_persistence_pair = false;
do {
auto simplex = ctr -> columns_to_reduce[j];
coface_entries.clear();
cofaces.setSimplexCoboundaryEnumerator(simplex, dcg);
while (cofaces.hasNextCoface() && !goto_found_persistence_pair) {
BirthdayIndex coface = cofaces.getNextCoface();
coface_entries.push_back(coface);
if (might_be_apparent_pair && (simplex.getBirthday() == coface.getBirthday())) {
if (pivot_column_index.find(coface.getIndex()) == pivot_column_index.end()) {
pivot.copyBirthdayIndex(coface);
goto_found_persistence_pair = true;// goto (B)
} else {
might_be_apparent_pair = false;// goto(A)
}
}
}
if (!goto_found_persistence_pair) {// (A) I haven't had a pivot
auto findWc = recorded_wc.find(j);
if(findWc != recorded_wc.end()){// if the pivot is old,
auto wc = findWc->second;
while (!wc.empty()){// we push the data of the old pivot's wc
auto e = wc.top();
working_coboundary.push(e);
wc.pop();
}
} else {
for (auto e : coface_entries) {// making wc here
working_coboundary.push(e);
}
}
pivot = get_pivot(working_coboundary);// getting a pivot from wc
if (pivot.getIndex() != -1) {//When I have a pivot, ...
auto pair = pivot_column_index.find(pivot.getIndex());
if (pair != pivot_column_index.end()) { // if the pivot already exists, go on the loop
j = pair->second;
continue;
} else {// if the pivot is new,
// I record this wc into recorded_wc, and
recorded_wc.insert(make_pair(i, working_coboundary));
// I output PP as WritePairs
double death = pivot.getBirthday();
outputPP(dim,birth,death);
pivot_column_index.insert(make_pair(pivot.getIndex(), i));
break;
}
} else {// if wc is empty, I output a PP as [birth,)
outputPP(-1, birth, dcg->threshold);
break;
}
} else {// (B) I have a pivot and output PP as WritePairs
double death = pivot.getBirthday();
outputPP(dim,birth,death);
pivot_column_index.insert(make_pair(pivot.getIndex(), i));
break;
}
} while (true);
}
}
void ComputePairs::outputPP(int _dim, double _birth, double _death){
if(_birth != _death){
if(_death != dcg -> threshold){
if(print == true){
cout << "[" <<_birth << "," << _death << ")" << endl;
}
wp -> push_back(WritePairs(_dim, _birth, _death));
} else {
if(print == true){
cout << "[" << _birth << ", )" << endl;
}
wp -> push_back(WritePairs(-1, _birth, dcg -> threshold));
}
}
}
BirthdayIndex ComputePairs::pop_pivot(priority_queue<BirthdayIndex, vector<BirthdayIndex>, BirthdayIndexComparator>& column){
if (column.empty()) {
return BirthdayIndex(0, -1, 0);
} else {
auto pivot = column.top();
column.pop();
while (!column.empty() && column.top().index == pivot.getIndex()) {
column.pop();
if (column.empty())
return BirthdayIndex(0, -1, 0);
else {
pivot = column.top();
column.pop();
}
}
return pivot;
}
}
BirthdayIndex ComputePairs::get_pivot(priority_queue<BirthdayIndex, vector<BirthdayIndex>, BirthdayIndexComparator>&
column) {
BirthdayIndex result = pop_pivot(column);
if (result.getIndex() != -1) {
column.push(result);
}
return result;
}
void ComputePairs::assemble_columns_to_reduce() {
++dim;
ctr -> dim = dim;
if (dim == 1) {
ctr -> columns_to_reduce.clear();
for(int w = 1; w <= aw; ++w){
for(int z = 1; z <= az; ++z){
for (int y = 1; y <= ay; ++y) {
for (int x = 1; x <= ax; ++x) {
for (int m = 0; m < 4; ++m) { // the number of type
double index = x | (y << 7) | (z << 14) | (w << 21) | (m << 28);
if (pivot_column_index.find(index) == pivot_column_index.end()) {
double birthday = dcg -> getBirthday(index, 1);
if (birthday != dcg -> threshold) {
ctr->columns_to_reduce.push_back(BirthdayIndex(birthday, index, 1));
}
}
}
}
}
}
}
} else if(dim == 2){
ctr -> columns_to_reduce.clear();
for(int w = 1; w <= aw; ++w){
for(int z = 1; z <= az; ++z){
for (int y = 1; y <= ay; ++y) {
for (int x = 1; x <= ax; ++x) {
for (int m = 0; m < 6; ++m) { // the number of type
double index = x | (y << 7) | (z << 14) | (w << 21) | (m << 28);
if (pivot_column_index.find(index) == pivot_column_index.end()) {
double birthday = dcg -> getBirthday(index, 2);
if (birthday != dcg -> threshold) {
ctr->columns_to_reduce.push_back(BirthdayIndex(birthday, index, 2));
}
}
}
}
}
}
}
} else if(dim == 3){
ctr -> columns_to_reduce.clear();
for(int w = 1; w <= aw; ++w){
for(int z = 1; z <= az; ++z){
for (int y = 1; y <= ay; ++y) {
for (int x = 1; x <= ax; ++x) {
for (int m = 0; m < 4; ++m) { // the number of type
double index = x | (y << 7) | (z << 14) | (w << 21) | (m << 28);
if (pivot_column_index.find(index) == pivot_column_index.end()) {
double birthday = dcg -> getBirthday(index, 3);
if (birthday != dcg -> threshold) {
ctr -> columns_to_reduce.push_back(BirthdayIndex(birthday, index, 3));
}
}
}
}
}
}
}
}
sort(ctr -> columns_to_reduce.begin(), ctr -> columns_to_reduce.end(), BirthdayIndexComparator());
}