-
Notifications
You must be signed in to change notification settings - Fork 54
/
max_meaningful_clustering.cpp
374 lines (319 loc) · 15.2 KB
/
max_meaningful_clustering.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
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
#include "max_meaningful_clustering.h"
MaxMeaningfulClustering::MaxMeaningfulClustering(unsigned char method, unsigned char metric)
{
}
void MaxMeaningfulClustering::operator()(t_float *data, unsigned int num, int dim, unsigned char method, unsigned char metric, vector< vector<int> > *meaningful_clusters)
{
t_float *Z = (t_float*)malloc(((num-1)*4) * sizeof(t_float)); // we need 4 floats foreach sample merge.
linkage_vector(data, (int)num, dim, Z, method, metric);
vector<HCluster> merge_info;
build_merge_info(Z, data, (int)num, dim, false, &merge_info, meaningful_clusters);
free(Z);
merge_info.clear();
}
void MaxMeaningfulClustering::operator()(t_float *data, unsigned int num, unsigned char method, vector< vector<int> > *meaningful_clusters)
{
t_float *Z = (t_float*)malloc(((num-1)*4) * sizeof(t_float)); // we need 4 floats foreach sample merge.
linkage(data, (int)num, Z, method); //TODO think if complete linkage is the correct
vector<HCluster> merge_info;
build_merge_info(Z, (int)num, &merge_info, meaningful_clusters);
free(Z);
merge_info.clear();
}
void MaxMeaningfulClustering::build_merge_info(t_float *Z, t_float *X, int N, int dim, bool use_full_merge_rule, vector<HCluster> *merge_info, vector< vector<int> > *meaningful_clusters)
{
// walk the whole dendogram
for (int i=0; i<(N-1)*4; i=i+4)
{
HCluster cluster;
cluster.num_elem = Z[i+3]; //number of elements
int node1 = Z[i];
int node2 = Z[i+1];
float dist = Z[i+2];
if (node1<N)
{
vector<float> point;
for (int n=0; n<dim; n++)
point.push_back(X[node1*dim+n]);
cluster.points.push_back(point);
cluster.elements.push_back((int)node1);
}
else
{
for (int i=0; i<merge_info->at(node1-N).points.size(); i++)
{
cluster.points.push_back(merge_info->at(node1-N).points[i]);
cluster.elements.push_back(merge_info->at(node1-N).elements[i]);
}
//update the extended volume of node1 using the dist where this cluster merge with another
merge_info->at(node1-N).dist_ext = dist;
}
if (node2<N)
{
vector<float> point;
for (int n=0; n<dim; n++)
point.push_back(X[node2*dim+n]);
cluster.points.push_back(point);
cluster.elements.push_back((int)node2);
}
else
{
for (int i=0; i<merge_info->at(node2-N).points.size(); i++)
{
cluster.points.push_back(merge_info->at(node2-N).points[i]);
cluster.elements.push_back(merge_info->at(node2-N).elements[i]);
}
//update the extended volume of node2 using the dist where this cluster merge with another
merge_info->at(node2-N).dist_ext = dist;
}
Minibox mb;
for (int i=0; i<cluster.points.size(); i++)
{
mb.check_in(&cluster.points.at(i));
}
cluster.dist = dist;
cluster.volume = mb.volume();
if (cluster.volume >= 1)
cluster.volume = 0.999999;
if (cluster.volume == 0)
cluster.volume = 0.001; //TODO is this the minimum we can get?
cluster.volume_ext=1;
if (node1>=N)
{
merge_info->at(node1-N).volume_ext = cluster.volume;
}
if (node2>=N)
{
merge_info->at(node2-N).volume_ext = cluster.volume;
}
cluster.node1 = node1;
cluster.node2 = node2;
merge_info->push_back(cluster);
}
for (int i=0; i<merge_info->size(); i++)
{
merge_info->at(i).nfa = nfa(merge_info->at(i).volume, merge_info->at(i).volume_ext, merge_info->at(i).num_elem, N);
int node1 = merge_info->at(i).node1;
int node2 = merge_info->at(i).node2;
{
if ((node1<N)&&(node2<N))
{
//els dos nodes son single samples (nfa=1) per tant aquest cluster es maxim
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
} else {
if ((node1>=N)&&(node2>=N))
{
//els dos nodes son "sets" per tant hem d'avaluar el merging condition
if ( ( (use_full_merge_rule) && ((merge_info->at(i).nfa < merge_info->at(node1-N).nfa + merge_info->at(node2-N).nfa) && (merge_info->at(i).nfa<min(merge_info->at(node1-N).min_nfa_in_branch,merge_info->at(node2-N).min_nfa_in_branch))) ) || ( (!use_full_merge_rule) && ((merge_info->at(i).nfa<min(merge_info->at(node1-N).min_nfa_in_branch,merge_info->at(node2-N).min_nfa_in_branch))) ) )
{
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
for (int k =0; k<merge_info->at(node1-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node1-N).max_in_branch.at(k)).max_meaningful = false;
for (int k =0; k<merge_info->at(node2-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node2-N).max_in_branch.at(k)).max_meaningful = false;
} else {
merge_info->at(i).max_meaningful = false;
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node1-N).max_in_branch.begin(),merge_info->at(node1-N).max_in_branch.end());
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node2-N).max_in_branch.begin(),merge_info->at(node2-N).max_in_branch.end());
if (merge_info->at(i).nfa<min(merge_info->at(node1-N).min_nfa_in_branch,merge_info->at(node2-N).min_nfa_in_branch))
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
else
merge_info->at(i).min_nfa_in_branch = min(merge_info->at(node1-N).min_nfa_in_branch,merge_info->at(node2-N).min_nfa_in_branch);
}
} else {
//un dels nodes es un "set" i l'altre es un single sample, s'avalua el merging condition pero amb compte
if (node1>=N)
{
if ((merge_info->at(i).nfa < merge_info->at(node1-N).nfa + 1) && (merge_info->at(i).nfa<merge_info->at(node1-N).min_nfa_in_branch))
{
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
for (int k =0; k<merge_info->at(node1-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node1-N).max_in_branch.at(k)).max_meaningful = false;
} else {
merge_info->at(i).max_meaningful = false;
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node1-N).max_in_branch.begin(),merge_info->at(node1-N).max_in_branch.end());
merge_info->at(i).min_nfa_in_branch = min(merge_info->at(i).nfa,merge_info->at(node1-N).min_nfa_in_branch);
}
} else {
if ((merge_info->at(i).nfa < merge_info->at(node2-N).nfa + 1) && (merge_info->at(i).nfa<merge_info->at(node2-N).min_nfa_in_branch))
{
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
for (int k =0; k<merge_info->at(node2-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node2-N).max_in_branch.at(k)).max_meaningful = false;
} else {
merge_info->at(i).max_meaningful = false;
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node2-N).max_in_branch.begin(),merge_info->at(node2-N).max_in_branch.end());
merge_info->at(i).min_nfa_in_branch = min(merge_info->at(i).nfa,merge_info->at(node2-N).min_nfa_in_branch);
}
}
}
}
}
}
for (int i=0; i<merge_info->size(); i++)
{
if (merge_info->at(i).max_meaningful)
{
vector<int> cluster;
for (int k=0; k<merge_info->at(i).elements.size();k++)
cluster.push_back(merge_info->at(i).elements.at(k));
meaningful_clusters->push_back(cluster);
}
}
}
void MaxMeaningfulClustering::build_merge_info(t_float *Z, int N, vector<HCluster> *merge_info, vector< vector<int> > *meaningful_clusters)
{
// walk the whole dendogram
for (int i=0; i<(N-1)*4; i=i+4)
{
HCluster cluster;
cluster.num_elem = Z[i+3]; //number of elements
int node1 = Z[i];
int node2 = Z[i+1];
float dist = Z[i+2];
if (dist != dist) //this is to avoid NaN values
dist=0;
//fprintf(stderr," merging %d %d\n",node1,node2);
if (node1<N)
{
cluster.elements.push_back((int)node1);
}
else
{
for (int i=0; i<merge_info->at(node1-N).elements.size(); i++)
{
cluster.elements.push_back(merge_info->at(node1-N).elements[i]);
}
}
if (node2<N)
{
cluster.elements.push_back((int)node2);
}
else
{
for (int i=0; i<merge_info->at(node2-N).elements.size(); i++)
{
cluster.elements.push_back(merge_info->at(node2-N).elements[i]);
}
}
cluster.dist = dist;
if (cluster.dist >= 1)
cluster.dist = 0.999999;
if (cluster.dist == 0)
cluster.dist = 1.e-25; //TODO is this the minimum we can get?
cluster.dist_ext = 1;
if (node1>=N)
{
merge_info->at(node1-N).dist_ext = cluster.dist;
}
if (node2>=N)
{
merge_info->at(node2-N).dist_ext = cluster.dist;
}
cluster.node1 = node1;
cluster.node2 = node2;
merge_info->push_back(cluster);
}
//print all merge info
//cout << "---------------------------------------------------------" << endl;
//cout << "-- MERGE INFO ---- Evidence Accumulation " << endl;
//cout << "---------------------------------------------------------" << endl;
for (int i=0; i<merge_info->size(); i++)
{
merge_info->at(i).nfa = nfa(merge_info->at(i).dist, merge_info->at(i).dist_ext, merge_info->at(i).num_elem, N);
int node1 = merge_info->at(i).node1;
int node2 = merge_info->at(i).node2;
{
if ((node1<N)&&(node2<N))
{
//els dos nodes son single samples (nfa=1) per tant aquest cluster es maxim
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
//fprintf(stderr,"%d = (%d,%d) els dos nodes son single samples (nfa=1) per tant aquest merge_info->at(i) es maxim min_nfa_in_branch = %d \n",i,node1-N,node2-N,merge_info->at(i).min_nfa_in_branch);
} else {
if ((node1>=N)&&(node2>=N))
{
//els dos nodes son "sets" per tant hem d'avaluar el merging condition
if ((merge_info->at(i).nfa < merge_info->at(node1-N).nfa + merge_info->at(node2-N).nfa) && (merge_info->at(i).nfa<min(merge_info->at(node1-N).min_nfa_in_branch,merge_info->at(node2-N).min_nfa_in_branch)))
{
//fprintf(stderr,"%d = (%d,%d) MAX because merging condition 1 (%d < %d + %d ) && (%d<min(%d,%d)) \n",i,node1-N,node2-N,merge_info->at(i).nfa,merge_info->at(node1-N).nfa, merge_info->at(node2-N).nfa, merge_info->at(i).nfa, merge_info->at(node1-N).nfa,merge_info->at(node2-N).nfa);
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
for (int k =0; k<merge_info->at(node1-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node1-N).max_in_branch.at(k)).max_meaningful = false;
for (int k =0; k<merge_info->at(node2-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node2-N).max_in_branch.at(k)).max_meaningful = false;
//fprintf(stderr," min_nfa_in_branch = %d \n",merge_info->at(i).min_nfa_in_branch);
} else {
merge_info->at(i).max_meaningful = false;
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node1-N).max_in_branch.begin(),merge_info->at(node1-N).max_in_branch.end());
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node2-N).max_in_branch.begin(),merge_info->at(node2-N).max_in_branch.end());
if (merge_info->at(i).nfa<min(merge_info->at(node1-N).min_nfa_in_branch,merge_info->at(node2-N).min_nfa_in_branch))
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
else
merge_info->at(i).min_nfa_in_branch = min(merge_info->at(node1-N).min_nfa_in_branch,merge_info->at(node2-N).min_nfa_in_branch);
//fprintf(stderr,"%d = (%d,%d) NONmax min_nfa_in_branch = %d \n",i,node1-N,node2-N,merge_info->at(i).min_nfa_in_branch);
}
} else {
//un dels nodes es un "set" i l'altre es un single sample, s'avalua el merging condition pero amb compte
if (node1>=N)
{
if ((merge_info->at(i).nfa < merge_info->at(node1-N).nfa + 1) && (merge_info->at(i).nfa<merge_info->at(node1-N).min_nfa_in_branch))
{
//fprintf(stderr,"%d = (%d,%d) MAX because merging condition 2 (%d < %d + 1 ) && (%d<%d) \n",i,node1-N,node2-N,merge_info->at(i).nfa,merge_info->at(node1-N).nfa, merge_info->at(i).nfa, merge_info->at(node1-N).min_nfa_in_branch);
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
for (int k =0; k<merge_info->at(node1-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node1-N).max_in_branch.at(k)).max_meaningful = false;
} else {
merge_info->at(i).max_meaningful = false;
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node1-N).max_in_branch.begin(),merge_info->at(node1-N).max_in_branch.end());
merge_info->at(i).min_nfa_in_branch = min(merge_info->at(i).nfa,merge_info->at(node1-N).min_nfa_in_branch);
//fprintf(stderr,"%d = (%d,%d) NONmax2 min_nfa_in_branch = %d \n",i,node1-N,node2-N,merge_info->at(i).min_nfa_in_branch);
}
} else {
if ((merge_info->at(i).nfa < merge_info->at(node2-N).nfa + 1) && (merge_info->at(i).nfa<merge_info->at(node2-N).min_nfa_in_branch))
{
//fprintf(stderr,"%d = (%d,%d) MAX because merging condition 3 (%d < %d + 1 ) && (%d<%d) \n ",i,node1-N,node2-N,merge_info->at(i).nfa,merge_info->at(node2-N).nfa, merge_info->at(i).nfa, merge_info->at(node2-N).min_nfa_in_branch);
merge_info->at(i).max_meaningful = true;
merge_info->at(i).max_in_branch.push_back(i);
merge_info->at(i).min_nfa_in_branch = merge_info->at(i).nfa;
for (int k =0; k<merge_info->at(node2-N).max_in_branch.size(); k++)
merge_info->at(merge_info->at(node2-N).max_in_branch.at(k)).max_meaningful = false;
} else {
merge_info->at(i).max_meaningful = false;
merge_info->at(i).max_in_branch.insert(merge_info->at(i).max_in_branch.end(),merge_info->at(node2-N).max_in_branch.begin(),merge_info->at(node2-N).max_in_branch.end());
merge_info->at(i).min_nfa_in_branch = min(merge_info->at(i).nfa,merge_info->at(node2-N).min_nfa_in_branch);
//fprintf(stderr,"%d = (%d,%d) NONmax3 min_nfa_in_branch = %d \n",i,node1-N,node2-N,merge_info->at(i).min_nfa_in_branch);
}
}
}
}
}
}
for (int i=0; i<merge_info->size(); i++)
{
if (merge_info->at(i).max_meaningful)
{
vector<int> cluster;
for (int k=0; k<merge_info->at(i).elements.size();k++)
cluster.push_back(merge_info->at(i).elements.at(k));
meaningful_clusters->push_back(cluster);
}
}
}
int MaxMeaningfulClustering::nfa(float sigma, float sigma2, int k, int N)
{
return -1*(int)NFA( N, k, (double) sigma, 0); //this uses an approximation for the nfa calculations (faster)
}