Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r79]: src / agf_preprocess.cc Maximize Restore History

Download this file

agf_preprocess.cc    451 lines (400 with data), 13.8 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//Copyright (C) 2007 Peter Mills. All rights reserved.
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <gsl/gsl_linalg.h>
#include "randomize.h"
#include "full_util.h"
#include "agf_lib.h"
#define WC_DEFAULT 20.
using namespace std;
using namespace libagf;
using namespace libpetey;
int main(int argc, char *argv[]) {
char *vecfile=NULL; //training data
char *clsfile=NULL; //class data
char *outbase;
char *outvec=NULL; //output file
char *outcls=NULL;
char *testvec=NULL;
char *testcls=NULL;
FILE *fs;
FILE *diagfs; //print diagnostics to this file stream
nel_ta ntrain; //number of training data points
nel_ta ntrain2;
dim_ta nvar; //number of variables
nel_ta nres; //number of results
dim_ta nvar2; //number of variables in result
real_a **train; //training data vectors
real_a *all; //train[0] to this for deletion
cls_ta *cls;
char *command=NULL;
int exit_value;
real_a *std, *ave;
agf_command_opts opt_args;
exit_value=0;
exit_value=agf_parse_command_opts(argc, argv, "a:nc:d:f:S:RFPCz", &opt_args);
if (exit_value==FATAL_COMMAND_OPTION_PARSE_ERROR) return exit_value;
//parse the command line arguments:
if ((argc < 2 || (argc<3 && opt_args.fflag)) && opt_args.asciiflag==0) {
printf("\n");
printf("Syntax: agf_preprocess [-A] [-a normfile] [-d ndiv] [-f frac] [-F]\n");
printf(" [-H] [-n] [-R] [-S nsv] [input output [test]]\n");
printf(" [i1 [i2 [i3...]]]\n");
printf("\n");
printf("arguments:\n");
printf(" input base name for binary input files\n");
printf(" output base name for binary output files:\n");
printf(" .vec for feature data\n");
printf(" .cls for class data\n");
printf(" .std for transformation/normalization matrix\n");
printf(" test base name for test data\n");
printf(" iI Ith selection/partition term\n");
printf("\n");
printf("options (in order of execution):\n");
printf(" -A operate on ASCII files from stdin and stdout\n");
printf(" -F select features\n");
printf(" -n normalize with standard deviations\n");
printf(" -S nsv singular value decomposition (SVD); keep nsv singular values\n");
printf(" -a normfile input/output transformation matrix\n");
printf(" -z randomly permute data\n");
printf(" -R data separation works by random selection rather than\n");
printf(" permutation (output files are not of definite size)\n");
printf(" -d ndiv number of separate output files\n");
printf(" -f frac separate into test and training (over-rides -d)\n");
printf(" -P calculate cross-correlation matrix (not compatible with -d or -f)\n");
printf(" -H strip dimension header from output files\n");
printf("\n");
printf("syntax for class re-mapping/partitioning:\n\n");
printf(" agf_preprocess [....] cls1a cls1b cls1c ... %c cls2a cls2b cls2c ... \n", PARTITION_SYMBOL);
printf(" %c cls3a cls3b cls3c ...\n", PARTITION_SYMBOL);
printf("\n");
printf("where clsIJ is the Jth class number in the Ith partition\n");
printf("\n");
return INSUFFICIENT_COMMAND_ARGS;
}
diagfs=stderr;
ran_init();
if (opt_args.asciiflag==0) {
vecfile=new char[strlen(argv[0])+5];
if (opt_args.Cflag) {
sprintf(vecfile, "%s", argv[0]);
} else {
sprintf(vecfile, "%s.vec", argv[0]);
}
}
if (opt_args.svd>0 || opt_args.normflag || opt_args.selectflag || opt_args.normfile!=NULL) {
if (opt_args.normfile==NULL) {
if (opt_args.asciiflag) {
fprintf(stderr, "agf_preprocess: please specify normalization file with -n\n");
exit(INSUFFICIENT_COMMAND_ARGS);
} else {
opt_args.normfile=new char[strlen(argv[1])+5];
sprintf(opt_args.normfile, "%s.std", argv[1]);
}
}
if (opt_args.asciiflag) {
command=new char [strlen(opt_args.normfile)+50+4*argc];
sprintf(command, "agf_precondition -A -a %s", opt_args.normfile);
} else {
command=new char [strlen(opt_args.normfile)+50+4*argc+strlen(argv[0])];
sprintf(command, "%s%s%s -O -a %s %s", AGF_COMMAND_PREFIX,
AGF_LTRAN_COM, AGF_OPT_VER, opt_args.normfile, vecfile);
}
if (opt_args.normflag) strcat(command, " -n");
if (opt_args.svd!=0) {
sprintf(command+strlen(command), " -S %d", opt_args.svd);
}
if (opt_args.selectflag) {
int i0=2;
strcat(command, " -F");
if (opt_args.asciiflag) {
i0=0;
} else if (opt_args.fflag) {
i0=3;
}
for (int i=i0; i<argc; i++) {
sprintf(command+strlen(command), " %s", argv[i]);
}
}
printf("%s\n", command);
fs=popen(command, "r");
//this is a fucking mess....
} else {
if (opt_args.asciiflag) {
fs=stdin;
} else {
fs=fopen(vecfile, "r");
}
}
if (fs==NULL) {
fprintf(stderr, "agf_preprocess: Unable to open coordinate data for reading\n");
exit(UNABLE_TO_OPEN_FILE_FOR_READING);
}
if (opt_args.asciiflag) {
ntrain=read_lvq(fs, train, cls, nvar, opt_args.Cflag*2);
if (command!=NULL) pclose(fs); else fclose(fs);
} else {
int32_t nt1, nv1;
outbase=argv[1];
train=read_matrix<real_a, int32_t>(fs, nt1, nv1);
if (nt1 == -1) {
fprintf(stderr, "agf_preprocess: Error reading coordinate data\n");
exit(FILE_READ_ERROR);
}
ntrain=nt1;
nvar=nv1;
if (command!=NULL) pclose(fs); else fclose(fs);
outvec=new char[strlen(outbase)+5];
if (opt_args.Cflag) {
sprintf(outvec, "%s", outbase);
} else {
sprintf(outvec, "%s.vec", outbase);
}
if (opt_args.Cflag==0) {
clsfile=new char[strlen(argv[0])+5];
sprintf(clsfile, "%s.cls", argv[0]);
}
argv+=2;
argc-=2;
if (opt_args.fflag) {
testvec=new char[strlen(argv[0])+5];
sprintf(testvec, "%s.vec", argv[0]);
testcls=new char[strlen(argv[0])+5];
sprintf(testcls, "%s.cls", argv[0]);
argv++;
argc--;
}
cls=NULL;
if (opt_args.Cflag==0) {
outcls=new char[strlen(outbase)+5];
sprintf(outcls, "%s.cls", outbase);
cls=read_clsfile(clsfile, ntrain2);
if (ntrain2 == -1) {
fprintf(stderr, "agf_preprocess: Error reading file: %s\n", clsfile);
exit(FILE_READ_ERROR);
}
if (cls==NULL) {
fprintf(stderr, "agf_preprocess: Unable to open file for reading: %s\n", clsfile);
exit(UNABLE_TO_OPEN_FILE_FOR_READING);
}
fprintf(diagfs, "%d class labels found in file: %s\n", ntrain2, clsfile);
if (ntrain2!=ntrain) {
fprintf(stderr, "agf_preprocess: Sample count mismatch\n");
exit(SAMPLE_COUNT_MISMATCH);
}
}
}
nres=ntrain;
if (opt_args.Pflag) {
real_a **cov;
real_a diff1, diff2;
cov=zero_matrix<real_a, int32_t>(nvar, nvar);
ave=new real_a[nvar];
std=new real_a[nvar];
for (dim_ta i=0; i<nvar; i++) {
ave[i]=0;
for (nel_ta j=0; j<ntrain; j++) ave[i]+=train[j][i];
ave[i]/=ntrain;
}
for (dim_ta i=0; i<nvar; i++) {
for (dim_ta j=0; j<nvar; j++) {
for (nel_ta k=0; k<ntrain; k++) {
diff1=train[k][i]-ave[i];
diff2=train[k][j]-ave[j];
cov[i][j]+=diff1*diff2;
}
}
}
for (dim_ta i=0; i<nvar; i++) {
for (dim_ta j=0; j<nvar; j++) {
cov[i][j]/=(ntrain-1);
printf("%12.6g ", cov[i][j]);
}
printf("\n");
}
delete_matrix(train);
train=cov;
nres=nvar;
delete [] std;
delete [] ave;
}
if (argc>0 && opt_args.selectflag==0 && opt_args.Cflag==0) {
cls_ta *clsmap;
cls_ta ncls=1;
cls_ta ncls1=1;
int err;
for (cls_ta i=0; i<ntrain; i++) if (cls[i]>=ncls) ncls=cls[i]+1;
err=parse_partition(argc, argv, ncls, clsmap);
apply_partition(cls, ntrain, clsmap);
for (cls_ta i=0; i<ncls; i++) if (clsmap[i]>=ncls) ncls1=clsmap[i]+1;
ncls=ncls1;
}
all=train[0];
//randomly permute the data:
if (opt_args.zflag) {
randomize_vec(train, nvar, nres, cls);
}
//write the results to a file:
if (opt_args.fflag && opt_args.Pflag!=1) {
//randomize not only the division, but the numbers:
if (opt_args.Rflag) {
FILE *trainvecfs;
FILE *trainclsfs;
FILE *testvecfs;
FILE *testclsfs;
trainvecfs=fopen(outvec, "w");
testvecfs=fopen(testvec, "w");
if (opt_args.Hflag==0) {
fwrite(&nvar, sizeof(nvar), 1, trainvecfs);
}
if (opt_args.Cflag==0) {
trainclsfs=fopen(outcls, "w");
testclsfs=fopen(testcls, "w");
}
if (opt_args.Hflag==0) fwrite(&nvar, sizeof(nvar), 1, testvecfs);
for (nel_ta i=0; i<nres; i++) {
if (ranu() < opt_args.ftest) {
fwrite(train[i], sizeof(real_a), nvar, testvecfs);
if (opt_args.Cflag==0) fwrite(cls+i, sizeof(cls_ta), 1, testclsfs);
} else {
fwrite(train[i], sizeof(real_a), nvar, trainvecfs);
if (opt_args.Cflag==0) fwrite(cls+i, sizeof(cls_ta), 1, trainclsfs);
}
}
fclose(trainvecfs);
fclose(testvecfs);
if (opt_args.Cflag==0) {
fclose(trainclsfs);
fclose(testclsfs);
}
} else {
size_t l1, l2;
//l1=size_t(nres*(1-opt_args.ftest)); //don't do this...
l2=size_t(nres*opt_args.ftest);
l1=nres-l2;
fs=fopen(outvec, "w");
if (opt_args.Hflag==0) {
fwrite(&nvar, sizeof(nvar), 1, fs);
}
for (nel_ta i=0; i<l1; i++) {
fwrite(train[i], sizeof(real_a), nvar, fs);
}
fclose(fs);
if (opt_args.Cflag==0) {
fs=fopen(outcls, "w");
fwrite(cls, sizeof(cls_ta), l1, fs);
fclose(fs);
}
fs=fopen(testvec, "w");
if (opt_args.Hflag==0) {
fwrite(&nvar, sizeof(nvar), 1, fs);
}
for (nel_ta i=l1; i<nres; i++) {
fwrite(train[i], sizeof(real_a), nvar, fs);
}
fclose(fs);
if (opt_args.Cflag==0) {
fs=fopen(testcls, "w");
fwrite(cls+l1, sizeof(cls_ta), l2, fs);
fclose(fs);
}
}
delete [] testvec;
delete [] testcls;
} else if (opt_args.div>0) {
char *outfile;
int rann;
outfile=new char[strlen(outbase)+8];
if (opt_args.Rflag) {
FILE **outvfs;
FILE **outcfs;
outvfs=new FILE * [opt_args.div];
outcfs=new FILE * [opt_args.div];
for (int32_t i=0; i<opt_args.div; i++) {
sprintf(outfile, "%s-%2.2d.vec", outbase, i);
outvfs[i]=fopen(outfile, "w");
if (opt_args.Hflag==0) {
fwrite(&nvar, sizeof(nel_ta), 1, outvfs[i]);
}
if (opt_args.Cflag==0) {
sprintf(outfile, "%s-%2.2d.cls", outbase, i);
outcfs[i]=fopen(outfile, "w");
}
}
// clind=sort_classes(vec, nvar, cls, n, ncl);
for (nel_ta i=0; i<nres; i++) {
rann=ranu()*opt_args.div;
fwrite(train[i], sizeof(float), nvar, outvfs[rann]);
if (opt_args.Cflag==0) {
fwrite(cls+i, sizeof(cls_ta), 1, outcfs[rann]);
}
}
for (int32_t i=0; i<opt_args.div; i++) {
fclose(outvfs[i]);
if (opt_args.Cflag==0) fclose(outcfs[i]);
}
} else {
nel_ta l1, l2;
l1=0;
for (int32_t i=0; i<opt_args.div; i++) {
sprintf(outfile, "%s-%2.2d.vec", outbase, i);
fs=fopen(outfile, "w");
if (opt_args.Hflag==0) fwrite(&nvar, sizeof(nel_ta), 1, fs);
l2=(i+1)*nres/opt_args.div;
for (nel_ta j=l1; j<l2; j++) {
fwrite(train[j], sizeof(real_a), nvar, fs);
}
fclose(fs);
if (opt_args.Cflag==0) {
sprintf(outfile, "%s-%2.2d.cls", outbase, i);
fs=fopen(outfile, "w");
fwrite(cls+l1, sizeof(cls_ta), l2-l1, fs);
fclose(fs);
}
l1=l2;
}
}
delete [] outfile;
} else {
if (opt_args.asciiflag) {
if (opt_args.Hflag==0) printf("%d\n", nvar);
for (nel_ta i=0; i<nres; i++) {
for (dim_ta j=0; j<nvar; j++) printf("%g ", train[i][j]);
if (opt_args.Cflag==0) printf("%d", cls[i]);
}
} else {
fs=fopen(outvec, "w");
if (fs==NULL) {
fprintf(stderr, "Unable to open file for writing: %s\n", outvec);
return UNABLE_TO_OPEN_FILE_FOR_WRITING;
}
fwrite(&nvar, sizeof(nvar), 1, fs);
for (nel_ta i=0; i<nres; i++) {
fwrite(train[i], sizeof(real_a), nvar, fs);
}
fclose(fs);
if (opt_args.Cflag==0) {
fs=fopen(outcls, "w");
if (fs==NULL) {
fprintf(stderr, "Unable to open file for writing: %s\n", outvec);
return UNABLE_TO_OPEN_FILE_FOR_WRITING;
}
fwrite(cls, sizeof(real_a), nres, fs);
fclose(fs);
}
}
}
//clean up:
delete [] all;
delete [] train;
if (opt_args.Cflag==0) {
delete [] cls;
}
if (clsfile!=NULL) delete [] clsfile;
if (outcls!=NULL) delete [] outcls;
if (vecfile!=NULL) delete [] vecfile;
if (outvec!=NULL) delete [] outvec;
if (command!=NULL) delete [] command;
return exit_value;
}