-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPersistenceComputer.cpp
More file actions
219 lines (184 loc) · 7.52 KB
/
PersistenceComputer.cpp
File metadata and controls
219 lines (184 loc) · 7.52 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
/**************************************************************************
* Copyright 2017 Rutgers University and CUNY Queens College.
* Code by Pengxiang Wu based on the implementations of Hubert Wagner and Chao Chen.
*
* License: GPLv3. (https://www.gnu.org/licenses/gpl.html)
*
* The SOFTWARE PACKAGE provided in this page is provided "as is", without any guarantee
* made as to its suitability or fitness for any particular use. It may contain bugs, so use of
* this tool is at your own risk. We take no responsibility for any damage of any sort that may
* unintentionally be caused through its use.
*
* If you have any questions, please contact Pengxiang Wu (pxiangwu@gmail.com)
* The class is wrapped by Fan Wang
**************************************************************************/
#include <cmath>
#include <map>
#include <set>
#include <list>
#include <cassert>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <deque>
#include <ctime>
#include <blitz/array.h>
#include <blitz/tinyvec-et.h>
#include "PersistenceIO.h"
#include "Debugging.h"
#include "Filtration/CubicalFiltration.h"
#include "InputRunner.h"
#include "PersistentPair.h"
#include "PersistenceCalculator.h"
#include "PersistenceCalcRunner.h"
#include "Globals.h"
#include "ParseCommandLine.h"
#include "PersistenceComputer.h"
using namespace std;
using namespace cv;
double Persistence_Computer::run() {
time_t startTime, endTime;
if (debug_enabled) debugStart(debug_path);
time(&startTime);
runPersistenceHomology(file_info, final_red_list_grand, final_boundary_list_grand, pers_V, pers_BD);
time(&endTime);
double ellapsed1 = difftime(endTime, startTime);
if (debug_enabled) debugEnd();
return ellapsed1;
}
void Persistence_Computer::source_from_file(const string& input_file, const string& output_file) {
Globals::inputFileName = input_file;
file_info.source_from_file(input_file, output_file);
output_name = file_info.output_path;
}
void Persistence_Computer::source_from_mat(const string& output_file, const Mat& t) {
file_info.source_from_mat(output_file, t);
output_name = file_info.output_path;
}
void Persistence_Computer::source_from_mat_from_int(const string& output_file, vector<int>& t, const int height, const int width) {
const int size = t.size();
assert(size == height * width);
Mat res(height, width, CV_32SC1, &t.begin()[0]);
file_info.source_from_mat(output_file, res);
output_name = file_info.output_path;
}
void Persistence_Computer::source_from_mat_from_double(const string& output_file, vector<double>& t, const int height, const int width) {
const int size = t.size();
assert(size == height * width);
Mat res(height, width, CV_64FC1, &t.begin()[0]);
file_info.source_from_mat(output_file, res);
output_name = file_info.output_path;
}
void Persistence_Computer::set_pers_thd(double t) {
Globals::reduction_threshold = t;
Globals::use_optimal_alg = true;
}
void Persistence_Computer::set_algorithm(int t) {
if (t >= 2 || t < 0) {
cout << "0 for A* search; 1 for exhaustive search ..."; exit(1);
}
Globals::which_alg = t; Globals::use_optimal_alg = true;
}
void Persistence_Computer::set_output_file(const string& t) { file_info.output_path = t; }
void Persistence_Computer::set_max_dim(int t) { Globals::max_dim = t; }
void Persistence_Computer::set_num_threads(int t) { Globals::num_threads = t; }
void Persistence_Computer::set_verbose(bool t) { file_info.verbose = t; }
void Persistence_Computer::set_debug(bool t, const string& debug_path_) { debug_enabled = t; debug_path = debug_path_; }
void Persistence_Computer::write_bnd(const vector<vector<vector<vector<int>>>>& t) {
string output_bnd_file = output_name + ".bnd";
BinaryPersistentPairsSaver<1>::write_BNDorRED(t, output_bnd_file.c_str());
}
void Persistence_Computer::write_red(const vector<vector<vector<vector<int>>>>& t) {
string output_red_file = output_name + ".red";
BinaryPersistentPairsSaver<1>::write_BNDorRED(t, output_red_file.c_str());
}
void Persistence_Computer::write_pers_V(const vector<vector<vector<int>>>& pers_V) {
string output_persV_file = output_name + ".pers";
BinaryPersistentPairsSaver<1>::write_pers_V(pers_V, output_persV_file.c_str());
}
void Persistence_Computer::write_pers_BD(const vector<vector<vector<double>>>& pers_BD) {
string output_persBD_file = output_name + ".pers.txt";
BinaryPersistentPairsSaver<1>::write_pers_BD(pers_BD, output_persBD_file.c_str());
}
void Persistence_Computer::write_pers_BD(const vector<vector<double>>& pers_BD) {
string output_persBD_file = output_name + ".pers.txt";
BinaryPersistentPairsSaver<1>::write_pers_BD(pers_BD, output_persBD_file.c_str());
}
void Persistence_Computer::return_bnd(vector<vector<vector<vector<int>>>>& t) { t = final_boundary_list_grand; };
void Persistence_Computer::return_red(vector<vector<vector<vector<int>>>>& t) { t = final_red_list_grand; };
void Persistence_Computer::return_pers_V(vector<vector<vector<int>>>& t) { t = pers_V; };
void Persistence_Computer::return_pers_BD(vector<vector<vector<double>>>& t) { t = pers_BD; };
void Persistence_Computer::write_output() {
write_bnd(final_boundary_list_grand);
write_red(final_red_list_grand);
write_pers_V(pers_V);
write_pers_BD(pers_BD);
}
void Persistence_Computer::clear() {
final_red_list_grand.clear();
final_boundary_list_grand.clear();
pers_V.clear();
pers_BD.clear();
}
void Persistence_Computer::debugStart(const string& debug_path) {
string logFile = debug_path + "/" + "log.txt";
string errorFile = debug_path + "/" + "error.txt";
DebuggerClass::init(false, logFile, errorFile);
int max_pers_pts = Globals::BIG_INT; // the maximal number of persistence pairs allowed
fstream filestr;
filestr.open(logFile.c_str(), fstream::out | fstream::trunc);
filestr << "################################################" << endl;
filestr << "Start computing persistence" << endl;
filestr << "Input file = \'" << Globals::inputFileName << "\'" << endl;
filestr << "Maximal number of persistence points allowed = " << max_pers_pts << endl;
filestr.close();
}
void Persistence_Computer::debugEnd() {
DebuggerClass::finish();
}
#ifdef command_line
int main(int argc, const char* argv[])
{
time_t startTime, endTime;
parseCommandLine(argc, argv);
vector<vector<vector<vector<int>>>> final_red_list_grand;
vector<vector<vector<vector<int>>>> final_boundary_list_grand;
vector<vector<vector<int>>> pers_V;
vector<vector<vector<double>>> pers_BD;
InputFileInfo input_file_info;
input_file_info.source_from_file(Globals::inputFileName);
Persistence_Computer::debugStart("."); // debug settings
time(&startTime);
// Run persistence homology algorithm
runPersistenceHomology(input_file_info, final_red_list_grand, final_boundary_list_grand, pers_V, pers_BD);
time(&endTime);
double ellapsed1 = difftime(endTime, startTime);
cout << "All calculations computed in " << setprecision(3) << ellapsed1 / 60.0 << " Min" << endl;
Persistence_Computer::debugEnd();
return 0;
}
#endif
//void main() {
// //std::string file_name = "D:/Data/cremi_new/c/gen_00046.png";
// std::string file_name = "D:/Data/ChaoData_proc/sublvl/000B_sub.dat";
// //cv::Mat img = cv::imread(file_name, 0);
//
// vector<vector<vector<vector<int>>>> final_red_list_grand;
// vector<vector<vector<vector<int>>>> final_boundary_list_grand;
// vector<vector<vector<int>>> pers_V;
// vector<vector<vector<double>>> pers_BD;
//
// Persistence_Computer pc;
// //pc.source_from_mat(file_name, img);
// pc.source_from_file(file_name);
// pc.run();
// pc.write_output();
//
// pc.return_bnd(final_boundary_list_grand);
// pc.return_pers_V(pers_V);
//
//
// pc.clear();
// system("pause");
//}