-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_w_density.cpp
198 lines (160 loc) · 5.71 KB
/
main_w_density.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
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// File: main_w_density.C
// Programmer: Primoz Skraba
// Description: Example persistence clustering pipeline
// Last modified: Sept. 8, 2009 (Version 0.2)
//----------------------------------------------------------------------
// Copyright (c) 2009 Primoz Skraba. All Rights Reserved.
//-----------------------------------------------------------------------
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//
//-----------------------------------------------------------------------
//----------------------------------------------------------------------
// History:
// Revision 0.1 August 10, 2009
// Initial release
//----------------------------------------------------------------------
//----------------------------------------------------------------------
#include <iostream>
#include <sstream>
#include <cassert>
#include <algorithm>
#include "Vertex.h"
#include "Distance_ANN.h"
#include "Core.h"
#include "Cluster_Basic.h"
#include "Point.h"
#include "Density.h"
using namespace std;
//rename for brevity
typedef Vertex<ANNPoint,Cluster_Info > Point;
// comparison function object for vector indices
template<class V> class Less_Than {
protected:
V& v;
public:
Less_Than (V& v_): v(v_){}
bool operator()(const int a, const int b) const
{return Point::Less_Than()(v[a], v[b]);}
};
// main function
int main(int argc, char *argv[]){
if(argc!=5){
cout<<"Usage:"<<endl<<argv[0]<<" <input filename> <Number of neighbors> <Rips radius> <persistence threshold>"<<endl;
exit(0);
}
int com=1;
vector< Point > point_cloud;
//read in data points
string input_file_name = argv[com++];
ifstream input;
input.open(input_file_name.c_str());
assert(input.good());
int dim = -1;
int nb_points = 0;
string lineData;
while(getline(input, lineData))
{
// read next point's coordinates
double d;
vector<double> row;
stringstream lineStream(lineData);
while (lineStream >> d)
row.push_back(d);
// set up dim if not already done
if (dim < 0) {
dim = row.size();
cout << "Dimension: " << dim << endl;
}
// else check that dimension is preserved
else if (dim != row.size()) {
cerr << "Error: mismatched dimension in "
<< input_file_name << " at line " << (nb_points+1) << endl;
return -1;
}
// create new point and corresponding vertex
ANNPoint p(dim);
p.coord = new double[dim];
for (int i=0; i<dim; i++)
p.coord[i] = row[i];
Point v(p);
v.data.boundary_flag=false;
point_cloud.push_back(v);
nb_points++;
}
input.close();
cout << "Number of input points: " << nb_points << endl;
//create distance structure
Distance_ANN< vector< Point >::iterator > metric_information;
metric_information.initialize(point_cloud.begin(),
point_cloud.end(),
dim);
//compute density
int num_neighb= atoi(argv[com++]);
distance_to_density(point_cloud.begin(),point_cloud.end(),
num_neighb, metric_information);
// sort point cloud and retrieve permutation (for pretty output)
vector<int> perm;
perm.reserve(nb_points);
for(int i=0; i < nb_points; i++)
perm.push_back(i);
std::sort(perm.begin(), perm.end(), Less_Than<vector<Point> >(point_cloud));
// store inverse permutation as array of iterators on initial point cloud
vector< vector<Point>::iterator> pperm;
pperm.reserve(nb_points);
for (int i=0; i<nb_points; i++)
pperm.push_back(point_cloud.begin());
for (int i=0; i<nb_points; i++)
pperm[perm[i]] = (point_cloud.begin() + i);
// operate permutation on initial point cloud
vector<Point> pc;
pc.reserve(nb_points);
for (int i=0; i<nb_points; i++)
pc.push_back(point_cloud[i]);
for (int i=0; i<nb_points; i++)
point_cloud[i] = pc[perm[i]];
//update distance structure --- since it relies on the order of entry
metric_information.initialize(point_cloud.begin(),point_cloud.end(), dim);
//set rips parameter
double r = atof(argv[com++]);
metric_information.mu = r*r;
//create cluster data structure
Cluster< vector< Point >::iterator > output_clusters;
//set threshold
output_clusters.tau = atof(argv[com++]);
// perform clustering
compute_persistence(point_cloud.begin(),point_cloud.end(),
metric_information,output_clusters);
// compress data structure:
// attach each data point to its cluster's root directly
// to speed up output processing
attach_to_clusterheads(point_cloud.begin(),point_cloud.end());
// output clusters (use permutation to preserve original point order)
ofstream out;
out.open("clusters.txt");
output_clusters.output_clusters(out, pperm.begin(), pperm.end());
out.close();
//output barcode
out.open("diagram.txt");
output_clusters.output_intervals(out);
out.close();
//output colored clusters to COFF file (first 3 dimensions are selected)
out.open("clusters_3d.coff");
output_clusters.output_clusters_coff(out,point_cloud.begin(),point_cloud.end());
out.close();
}