-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayoutquality.cpp
293 lines (239 loc) · 6.86 KB
/
layoutquality.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
#include <math.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <time.h>
#include <iostream>
#include <sys/time.h>
#include <ctime>
#include <vector>
#include "vptree.h"
#include "vec.h"
using namespace std;
char *g_dmat_filename = NULL; // for ground truth
char *g_csv_filename = NULL; // for testing accuracy
char *g_true_nn_filename = NULL; // for ground truth
char *g_test_nn_filename = NULL;
int k_nearest = 5;
typedef struct _DMATPAIR {
int key;
float value;
} DMATPAIR;
void usage() {
printf("layoutquality [options]\n");
printf("\td - input truncated distance matrix (vec format)\n");
printf("\ti - input coordinates (csv format)\n");
printf("\tn - input true nn file\n");
printf("\tt - input test nn file\n");
printf("\tk - k nearest neighbors to check (if they exist in the distance matrix)\n");
}
/* catch buggy input params */
void check_args() {
if( g_dmat_filename == NULL && g_true_nn_filename == NULL ) {
fprintf(stderr,"Need input distance matrix or true nn file, use -d or -n\n");
exit(0);
}
if( g_csv_filename == NULL && g_test_nn_filename == NULL ) {
fprintf(stderr,"Need input coordinates or test nn file , use -i or -t\n");
exit(0);
}
}
/* parse user input */
void proc_command_args( int argc, char **argv) {
int i = 0;
char *argument = NULL;
while( i < argc ) {
if( ( argv[i][0] == '-' ) && (strlen( argv[i] ) > 1 ) ){
if( argv[i][2] != '\0')
argument = &(argv[i][2]);
else
argument = argv[i+1];
if( argv[i][1] == '?' ) {
usage(); exit(0);
} else if( argv[i][1] == 'd' ) {
g_dmat_filename = argument;
} else if( argv[i][1] == 'i' ) {
g_csv_filename = argument;
} else if( argv[i][1] == 'n' ) {
g_true_nn_filename = argument;
} else if( argv[i][1] == 't' ) {
g_test_nn_filename = argument;
} else if( argv[i][1] == 'k' ) {
k_nearest = atoi(argument);
}
}
i++;
}
check_args();
}
int count_nns(const char* filename) {
char line[65536];
int line_num = 0;
FILE* fp = NULL;
fp = fopen( filename, "r" );
while( fgets( line, 65535, fp ) != NULL ) {
line_num++;
}
fclose(fp);
return line_num;
}
int* load_nns( int N, int k, const char* filename ) {
char line[65536];
char item[256];
int line_num = 0;
int i,j,kk;
FILE* fp = NULL;
fp = fopen( filename, "r" );
int* nns = (int*) calloc( N*k, sizeof(int) );
while( fgets( line, 65535, fp ) != NULL ) {
i = 0;
j=0;
kk = line_num*k;
while( line[i] != '\0' && line[i] != '\n' ) {
if( line[i] == ',') {
item[j]='\0';
nns[kk++]=atoi(item);
j=0;
}
else {
item[j++]=line[i];
}
i++;
}
item[j]='\0';
nns[kk]=atoi(item);
line_num++;
}
fclose(fp);
return nns;
}
float* load_coordinates( int N, const char* filename ) {
char line[65536];
char item[256];
int line_num = 0;
int i,j;
FILE* fp = NULL;
fp = fopen( filename, "r" );
float* coordinates = (float*) malloc( N*2*sizeof(float) );
while( fgets( line, 65535, fp ) != NULL ) {
i = 0;
j=0;
while( line[i] != '\0' && line[i] != '\n' ) {
if( line[i] == ',') {
item[j]='\0';
coordinates[line_num*2]=atof(item);
j=0;
}
item[j++]=line[i];
i++;
}
item[j]='\0';
coordinates[line_num*2+1]=atof(item);
line_num++;
}
fclose(fp);
return coordinates;
}
int compare_dp(gpointer a, gpointer b) {
DMATPAIR* x = (DMATPAIR*)a;
DMATPAIR* y = (DMATPAIR*)b;
return (x->value < y->value)?-1:1;
}
inline int intmin( int a, int b) {return (a<b)?a:b;}
inline int intmax( int a, int b) {return (a>b)?a:b;}
int main (int argc, char* argv[]) {
int N = 0;
proc_command_args(argc,argv);
bool b_use_dmat = (g_dmat_filename != NULL);
bool b_use_coords = (g_csv_filename != NULL);
int* ground_truth_nn = NULL;
int* test_set_nn = NULL;
// load ground truth nns
if( b_use_dmat ) {
DMAT* dmat = load_dmat( g_dmat_filename );
N = dmat->N;
ground_truth_nn = (int*)calloc(dmat->N*k_nearest,sizeof(int));
for( int i = 0; i < dmat->N; i++ ) {
GHashTable* row = g_array_index(dmat->rows,GHashTable*,i);
GArray* pairs = g_array_sized_new( FALSE, FALSE, sizeof(DMATPAIR), g_hash_table_size(row));
GList* indices = g_hash_table_get_keys(row);
while(indices != NULL) {
if( *((int*)(indices->data)) != i ) {
DMATPAIR dmp = { *((int*)(indices->data)) ,*((float*)g_hash_table_lookup(row,(int*)(indices->data)))};
g_array_append_val(pairs,dmp);
}
indices = indices->next;
}
g_array_sort(pairs,(GCompareFunc)compare_dp);
for( int j = 0; j < k_nearest && j < pairs->len; j++ )
ground_truth_nn[i*k_nearest+j] = g_array_index(pairs,DMATPAIR,j).key;
g_array_free(pairs,TRUE);
}
}
else {
N = count_nns(g_true_nn_filename);
ground_truth_nn = load_nns(N,k_nearest,g_true_nn_filename);
}
// load test set nns
if( b_use_coords ) {
float* coords = load_coordinates( N, g_csv_filename );
// load the data into a structure
VpTree<DataPoint, euclidean_distance>* tree = new VpTree<DataPoint, euclidean_distance>();
vector<DataPoint> obj_X(N, DataPoint(2, -1, coords));
for(int n = 0; n < N; n++) obj_X[n] = DataPoint(2, n, coords + (n * 2));
test_set_nn = new int[k_nearest*N];
// Loop over all points to find nearest neighbors in coords
// cerr << "Building tree...";
tree->create(obj_X);
// cerr << "done." << endl;
// cerr << "Computing coordinate knn...";
vector<DataPoint> indices;
vector<float> distances;
for(int n = 0; n < N; n++) {
// Find nearest neighbors
indices.clear();
distances.clear();
tree->search(obj_X[n], k_nearest+1, &indices, &distances);
int found_self = 0;
int ids_idx = 0;
for( int kk = 0; kk < k_nearest+1; kk++) {
if( indices[kk].index() == n )
found_self++;
else {
test_set_nn[n*k_nearest+ids_idx] = indices[kk].index();
ids_idx++;
}
if( !found_self && kk == k_nearest-1)
break;
}
}
}
else {
test_set_nn = load_nns(N,k_nearest,g_test_nn_filename);
}
// cerr << "done." << endl;
// calculate the size of the intersection of the coordinate nearest neighbors
double* intersection_length = (double*) calloc( N, sizeof(double) );
// output the result
for( int i = 0; i < N; i++ ) {
for( int j = 0; j < k_nearest; j++ ) {
for( int k = 0; k < k_nearest; k++ ) {
// if( i == 0)
// cout << "" << test_set_nn[i*k_nearest+j] << "==" << ground_truth_nn[i*k_nearest+k] << endl;
if( test_set_nn[i*k_nearest+j] == ground_truth_nn[i*k_nearest+k] ) {
intersection_length[i]+=1.0;
break;
}
}
}
intersection_length[i] /= k_nearest;
}
double mean_intersection_length = 0.0;
for( int i = 0; i < N; i++ ) {
mean_intersection_length += (intersection_length[i]-mean_intersection_length)/((double)(i+1));
}
mean_intersection_length -= ((double)k_nearest)/((double)N - 1.0);
cout << "" << k_nearest << "," << mean_intersection_length << endl;
return 0;
}