-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.cpp
executable file
·204 lines (168 loc) · 4.88 KB
/
knn.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
// DO HCLUST BY DECREASING K AND CHOOSING INCLUSION DIAGRAM...
// what's the null class label? -1 ????
/* 20170517 verified that program hardly uses CPU when idle */
#include <cmath>
#include <float.h>
#include "misc.h"
#include "glut.h"
#include "pick.h"
#include "global.h"
#include "clust_knn.h"
using namespace myglut;
size_t N, NRow, NCol; // number of bands, rows, col's
int bi[3]; // active band indices
vector <FILE*> filehandles;
vector <GLUT2d *> GLUT2d_windows;
vector <GLUT3d *> GLUT3d_windows;
vector <clust_knn*> knn_clusterings;
void quit(){
int i;
for0(i, filehandles.size()) if(filehandles[i]) fclose(filehandles[i]);
for0(i, float_buffers.size()) free(float_buffers[i]);
exit(0);
}
void idle(){
printf("idle\n");
myglut2d_img->refresh();
myglut2d->refresh();
if(number_of_classes == 0){
myglut3d->runclust();
if(!myglut2d->glbusy){
myglut2d->recalc_classes();
myglut2d->draw_classes();
}
}
}
void scaleband(SA<float> * buf){
float dat, MAX, MIN;
long int i, n;
MAX = FLT_MIN;
MIN = FLT_MAX;
n = buf->size();
for0(i, n){
dat = buf->at(i);
if(!(isinf(dat) || isnan(dat))){
if(dat < MIN) MIN = dat;
if(dat > MAX) MAX = dat;
}
// else (*isBad)[i] = 1;
}
printf(" MIN %e MAX %e\n", MIN, MAX);
for0(i, buf->size()){
dat = buf->at(i);
buf->at(i) = (dat - MIN) / (MAX - MIN);
dat = buf->at(i);
}
}
int main(int argc, char *argv[]){
//srand(37);
// default parameters
const char *args[5] = {"kgc.exe\0", "data/rgb.bin\0", "33333\0", "230\0", "2\0"};
if(argc < 5){
str msg("kgc2010 [input binary file] [n_desired] [knn_use] [rand_iter_max] # [optional arg: perform scaling to [0,1]]");
cout << "Error: " << msg << endl;
argv = (char **)(void **)args;
}
str fn(argv[1]);
if(!exists(fn)) err("input file not found");
str hfn(hdr_fn(fn));
hread(hfn, NRow, NCol, N);
printf("NRow %d NCol %d NBand %d\n", (int)NRow, (int)NCol, (int)N);
/* make output folder, if it doesn't already exist */
int a = system("mkdir -p output");
NDESIRED = atoi(argv[2]);
KNN_MAX = KNN_USE = atoi(argv[3]);
printf("KNN_MAX %d KNN_USE %d\n", KNN_MAX, KNN_USE);
RAND_ITER_MAX = atoi(argv[4]);
bool use_scaling = argc > 5;
int i;
int n = NRow * NCol;
float * dd = bread(fn, NRow, NCol, N); // buffer data
SA< SA< float > * > fbufs(N); // reshaped data
for0(i, N){
fbufs[i] = (SA<float> *) new SA<float>(NRow, NCol);
float_buffers.push_back(fbufs[i]);
}
i_coord = new SA<int>(NRow * NCol);
j_coord = new SA<int>(NRow * NCol);
int kk;
for0(kk, N){
SA<float> * db = fbufs[kk];
for0(i, n) (*db)[i] = dd[(kk * n) + i]; // buffer data band
if(use_scaling) scaleband(fbufs[kk]); // scale band to [0, 1]
}
// data conditioning: in absence of de-duplication: substitute NAN in one band, or zero in all bands, with small random number..
for0(kk, n){
bool all_zero = true;
for0(i, N){
float d = (* (fbufs[i]) )[kk];
if(isnan(d) || isinf(d)){
float r = (crappy_rand() / RAND_MAX) / 111111.;
(* (fbufs[i]) )[kk] = r;
}
else{
if( d != 0.) all_zero = false;
}
}
if(all_zero){
for0(i, N){
float r = (crappy_rand() / RAND_MAX) / 111111.;
(* (fbufs[i]) )[kk] = r;
}
}
}
free(dd);
if(false){
int kk;
// write data in ascii format
FILE * afile = fopen("./ascii.txt", "wb");
for0(kk, NRow * NCol){
for0(i, N) fprintf(afile, "%7.7e,", fbufs[i]->at(kk));
fprintf(afile, "\n");
}
fclose(afile);
}
//set default band vis.: 1, 2, 3
for0(i, 3) bi[i] = i;
glutInit(&argc,argv);
// image display window
GLUT2d img( NRow, NCol, "image");
GLUT2d_windows.push_back(&img);
img.setPos(0, 0);
img.setRGB(fbufs[0], fbufs[1], fbufs[2], 0, 1, 2);
myglut2d_img = &img;
img.glbusy = img.isClassification = false;
img.mark();
// classification display window
GLUT2d clasi(NRow, NCol, "class");
GLUT2d_windows.push_back(&clasi);
clasi.setRightOf(&img);
clasi.setRGB(fbufs[0], fbufs[1], fbufs[2], 0, 1, 2);
myglut2d = &clasi;
clasi.isClassification = true;
clasi.glbusy = false;
clasi.mark();
number_of_classes = 0;
laststate = beforelaststate = -1;
// init scatter plot
GLUT3d scatter(NCol * 2, NCol * 2 + 1, N);
scatter.setRGB(fbufs[0], fbufs[1], fbufs[2], 0, 1, 2);
GLUT3d_windows.push_back(&scatter);
scatter.setBelow(&img);
scatter.mark();
clust_knn myKNNclust(NRow, NCol);
// myglut::
myclust_knn = &myKNNclust;
// init clustering
// KMax = KNN_USE;
float ratio = floor(((float)n) / ((float)NDESIRED));
printf("floor(n / NDESIRED) = %f\n", ratio);
myKNNclust.init(&scatter, &clasi, &float_buffers, ratio, false);
myKNNclust.set_Rand_Iter_Max(RAND_ITER_MAX);
knn_clusterings.push_back(&myKNNclust);
scatter.set_clust(&myKNNclust);
clasi.set_clust(&myKNNclust);
glutMainLoop();
quit();
return 0;
}