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

Download this file

agf_io.cc    556 lines (460 with data), 13.3 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//Copyright (C) 2007 Peter Mills. All rights reserved.
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "error_codes.h"
#include "read_ascii_all.h"
#include "full_util.h"
#include "linked.h"
#include "agf_util.h"
#include "agf_io.h"
using namespace std;
using namespace libpetey;
namespace libagf {
//#define VERBOSE 1
#define CHECK_ERROR 0
#define STATS_HEADER "dim average std. dev.\n"
//-returns null pointer on failure
//-dimension is -1 if this is a bad value
//-sample number is -1 if there is an allocation failure
real_a ** read_vecfile(const char *filename, nel_ta &m, dim_ta &n) {
FILE *fs;
real_a **data;
nel_ta n1; //dimensions have to be same type for matrix routines
fs=fopen(filename, "r");
if (fs == NULL) {
m=0; n=0;
return NULL;
}
//just piggy-back off the matrix routines:
data=read_matrix<real_a, nel_ta>(fs, m, n1);
n=n1;
fclose(fs);
return data;
}
cls_ta * read_clsfile(const char *filename, nel_ta &n) {
FILE *fs;
cls_ta *data;
fs=fopen(filename, "r");
if (fs == NULL) {
n=0;
return NULL;
}
fseek(fs, 0, SEEK_END);
n=ftell(fs);
if (n % sizeof(real_a) != 0) { //check for consistency
n=-1;
fclose(fs);
return NULL;
}
n=n/sizeof(real_a);
fseek(fs, 0, SEEK_SET);
data=new cls_ta [n];
fread(data, sizeof(cls_ta), n, fs);
fclose(fs);
return data;
}
real_a * read_datfile(const char *filename, nel_ta &n) {
FILE *fs;
real_a *data;
fs=fopen(filename, "r");
if (fs == NULL) {
n=0;
return NULL;
}
fseek(fs, 0, SEEK_END);
n=ftell(fs);
if (n % sizeof(real_a) != 0) { //check for consistency
n=-1;
fclose(fs);
return NULL;
}
n=n/sizeof(real_a);
fseek(fs, 0, SEEK_SET);
data=new real_a [n];
if (data==NULL) {
n=-1;
return data;
}
fread(data, sizeof(real_a), n, fs);
fclose(fs);
return data;
}
#define MAXLL 200
int read_stats(const char *filename, real_a *ave, real_a *std, dim_ta ndim) {
FILE *fs;
dim_ta ivar;
char header[MAXLL];
fs=NULL;
fs=fopen(filename, "r");
if (fs==NULL) return UNABLE_TO_OPEN_FILE_FOR_READING;
fgets(header, MAXLL, fs);
for (dim_ta i=0; i<ndim; i++) {
fscanf(fs, "%d %g %g", &ivar, &ave[i], &std[i]);
}
fclose(fs);
return 0;
}
real_a ** read_stats2(const char *filename, real_a *&ave, dim_ta &m, dim_ta &n) {
FILE *fs;
real_a **mat;
dim_ta ivar;
char *header=NULL;
char **line=NULL;
long nline;
int ncon;
ave=NULL;
fs=NULL;
fs=fopen(filename, "r");
if (fs==NULL) return NULL;
header=fget_line(fs, 1);
if (strcmp(header, STATS_HEADER)==0) {
line=read_ascii_all(fs, &nline, 1);
if (line==NULL || nline<=0) goto fail;
n=nline;
ave=new real_a[n];
mat=allocate_matrix<real_a, nel_ta>(n, n);
for (dim_ta i=0; i<n; i++) {
if (strlen(line[i])==0) {
n=i+1;
break;
}
ncon=sscanf(line[i], "%d %g %g", &ivar, ave+i, mat[i]+i);
if (ncon!=3) {
goto fail;
}
delete [] line[i];
}
m=n;
delete [] line;
} else if (header==NULL) {
goto fail;
} else {
fseek(fs, 0, SEEK_SET);
mat=read_matrix<real_a, nel_ta>(fs, m, n);
if (mat==NULL || m<=0 || n<=0) goto fail;
ave=new real_a[m];
//printf("read_stats2: constant term:\n");
n--;
for (dim_ta i=0; i<m; i++) {
ave[i]=mat[i][n];
printf("%g\n", ave[i]);
}
//printf("read_stats2: transformation matrix:\n");
//print_matrix(stdout, mat, m, n);
}
delete [] header;
fclose(fs);
return mat;
fail: //clean up in event of read failure and set error indicators
fprintf(stderr, "read_stats2: an error occurred reading data file, %s\n", filename);
if (line!=NULL) {
for (long i=0; i<nline; i++) delete [] line[i];
delete [] line;
}
if (ave!=NULL) delete [] ave;
if (mat!=NULL) delete_matrix(mat);
if (fs!=NULL) fclose(fs);
n=-1;
m=-1;
return NULL;
}
int print_stats(FILE *fs, real_a *ave, real_a *std, dim_ta ndim) {
dim_ta ivar;
fprintf(fs, STATS_HEADER);
for (dim_ta i=0; i<ndim; i++) {
fprintf(fs, "%3d %10.6g %10.6g\n", i, ave[i], std[i]);
}
return 0;
}
int agf_read_train(const char *fbase, real_a **&train, cls_ta *&cls, nel_ta &n, dim_ta &nvar) {
char *vecfile=NULL;
char *classfile=NULL;
nel_ta n1;
int err=0; //return code
train=NULL;
cls=NULL;
vecfile=new char[strlen(fbase)+5];
sprintf(vecfile, "%s.vec", fbase);
classfile=new char[strlen(fbase)+5];
sprintf(classfile, "%s.cls", fbase);
//read in the training data:
train=read_vecfile(vecfile, n, nvar);
if (nvar <= 0 || n <= 0) {
fprintf(stderr, "Error reading file: %s\n", vecfile);
err=FILE_READ_ERROR;
goto fail;
}
if (train==NULL) {
fprintf(stderr, "Unable to open file, %s, for reading.\n", vecfile);
err=UNABLE_TO_OPEN_FILE_FOR_READING;
goto fail;
}
cls=read_clsfile(classfile, n1);
if (n1 <= 0) {
fprintf(stderr, "Error reading file: %s\n", classfile);
err=FILE_READ_ERROR;
goto fail;
}
if (cls == NULL) {
fprintf(stderr, "Unable to open file, %s, for reading.\n", classfile);
err=UNABLE_TO_OPEN_FILE_FOR_READING;
goto fail;
}
if (n1!=n) {
fprintf(stderr, "Sample count mismatch: %d in %s, %d in %s.\n", n, vecfile, n1, classfile);
err=SAMPLE_COUNT_MISMATCH;
goto fail;
}
fail:
delete [] classfile;
delete [] vecfile;
return err;
}
int agf_read_borders(const char *fbase, real_a **&brd, real_a **&grd, nel_ta &n, dim_ta &nvar) {
char *brdfile;
char *grdfile;
nel_ta n1;
dim_ta nvar1;
int err=0;
brd=NULL;
grd=NULL;
brdfile=new char[strlen(fbase)+5];
strcpy(brdfile, fbase);
strcat(brdfile, ".brd");
grdfile=new char[strlen(fbase)+5];
strcpy(grdfile, fbase);
strcat(grdfile, ".bgd");
//read in the decision surface data:
brd=read_vecfile(brdfile, n, nvar);
if (nvar <= 0 || nvar <= 0) {
fprintf(stderr, "Error reading file: %s\n", brdfile);
err=FILE_READ_ERROR;
goto fail;
}
if (brd == NULL) {
fprintf(stderr, "Unable to open input file: %s\n", brdfile);
err=UNABLE_TO_OPEN_FILE_FOR_READING;
goto fail;
}
grd=read_vecfile(grdfile, n1, nvar1);
if (nvar1 <= 0 || n1 <= 0) {
fprintf(stderr, "Error reading file: %s\n", grdfile);
err=FILE_READ_ERROR;
goto fail;
}
if (grd == NULL) {
fprintf(stderr, "Unable to open input file: %s\n", grdfile);
err=UNABLE_TO_OPEN_FILE_FOR_READING;
goto fail;
}
if (nvar1 != nvar) {
fprintf(stderr, "Error: dimensions of border and gradient vectors do not agree:\n");
fprintf(stderr, " %s: D=%d vs. %s: D=%d\n", brdfile, nvar, grdfile, nvar1);
err=DIMENSION_MISMATCH;
goto fail;
}
if (n1 != n) {
fprintf(stderr, "Error: number of samples in border and gradient files do not agree:\n");
fprintf(stderr, " %d in %s vs. %d in %s\n", n, brdfile, n1, grdfile);
err=SAMPLE_COUNT_MISMATCH;
goto fail;
}
fail:
delete [] grdfile;
delete [] brdfile;
return err;
}
//increasingly I'm finding this bit really brain-dead:
real_a **agf_get_features(const char *fbase, agf_command_opts *opt_args, dim_ta &nvar, nel_ta &n, flag_a sufflag)
{
real_a **train;
char *vecfile;
vecfile=new char[strlen(fbase)+5];
if (sufflag) {
sprintf(vecfile, "%s", fbase);
} else {
sprintf(vecfile, "%s.vec", fbase);
}
if (opt_args->normflag || opt_args->svd>0 || opt_args->normfile != NULL) {
//if the user wants some pre-processing we farm it out to "agf_precondition"
FILE *fs;
linked_list<real_a> data;
real_a *data2;
char *command;
real_a val=0;
int check;
long ntot=0;
nel_ta nvar1;
//if (opt_args.normfile == NULL) {
// opt_args.normfile=new char[strlen(argv[3])+5];
// sprintf(opt_args.normfile, "%s.std", argv[3]);
//}
command=new char[strlen(opt_args->normfile)+strlen(fbase)+50];
sprintf(command, "%s%s%s -a %s", AGF_COMMAND_PREFIX,
AGF_LTRAN_COM, AGF_OPT_VER, opt_args->normfile);
if (opt_args->normflag) strcat(command, " -n");
if (opt_args->svd>0) {
sprintf(command+strlen(command), " -S %d", opt_args->svd);
}
sprintf(command+strlen(command), " %s", vecfile);
fprintf(stderr, "%s\n", command);
fs=popen(command, "r");
train=read_matrix<real_a, nel_ta>(fs, n, nvar1);
nvar=nvar1;
pclose(fs);
delete [] command;
} else {
//read in the training data:
train=read_vecfile(vecfile, n, nvar);
}
if (nvar <= 0 || n <= 0) {
fprintf(stderr, "Error reading file: %s\n", vecfile);
exit(FILE_READ_ERROR);
}
if (train==NULL) {
fprintf(stderr, "Unable to open file, %s, for reading.\n", vecfile);
exit(UNABLE_TO_OPEN_FILE_FOR_READING);
}
delete [] vecfile;
return train;
}
//reads ASCII files in the same format as used by Kohonen's LVQ package
//returns number of lines read in
//if there is an error, returns a negative number
//flag: 1st bit=no header; 2nd bit=no class data; 3rd bit: omit class data
nel_ta read_lvq(FILE *fs, real_a **&train, cls_ta *&cls, dim_ta &nvar, int flags) {
char **line; //all the lines in the file
int nread; //for counting the number of characters read in a line
int pos; //string pointer
int err; //counts number of items read
long n; //number of lines
real_a dum; //for counting number of features
nel_ta count; //number of lines actually read
int hflag, cflag, oflag; //no header, omit class data, no class data
int hflag2; //not hflag
linked_list<real_a> first_line; //first line data
//separate out each of the flags:
hflag=flags & 1; //no header
cflag=(flags & 2) >> 1; //there is no class data
oflag=(flags & 4) >> 2; //omit class data
printf("hflag=%d; cflag=%d; oflag=%d\n", hflag, cflag, oflag);
train=NULL;
cls=NULL;
if (hflag) hflag2=0; else hflag2=1;
line=read_ascii_all(fs, &n, 1);
//won't use explicit error messages since we are reading a text file:
//users can examine it for themselves
//however we will return the line number where the error occurred...
if (line==NULL || n<=1) return -1;
if (hflag) {
pos=0;
nvar=0;
while (sscanf(line[0]+pos, "%g%n", &dum, &nread)==1) {
first_line.add(dum);
pos+=nread;
nvar++;
}
if (cflag==0) nvar--;
//printf("read_lvq: found %d features\n", nvar);
} else {
err=sscanf(line[0], "%d", &nvar);
if (err!=1) return -1;
n--;
}
train=new real_a*[n];
train[0]=new real_a[n*nvar];
if (cflag==0 && oflag==0) {
cls=new cls_ta[n];
}
//so we don't have to re-read the first line:
if (hflag) {
for (dim_ta i=0; i<nvar; i++) {
first_line.pop(dum);
train[0][i]=dum;
}
if (cflag==0 && oflag==0) {
first_line.pop(dum);
cls[0]=(cls_ta) dum;
}
}
count=hflag;
for (nel_ta i=hflag; i<n; i++) {
train[i]=train[0]+i*nvar;
pos=0;
for (dim_ta j=0; j<nvar; j++) {
err=sscanf(line[i+hflag2]+pos, "%g%n", train[i]+j, &nread);
if (err != 1) {
if (j>0) count=-count-1-hflag; //not an error if blank line
break;
}
pos+=nread;
}
if (err != 1) break;
if (cflag || oflag) {
count++;
continue;
}
err=sscanf(line[i+hflag2]+pos, "%d", cls+i);
if (err != 1) {
count=-count-1-hflag;
break;
}
count++;
}
return count;
}
nel_ta read_lvq(const char *fname, real_a **&train, cls_ta *&cls, dim_ta &nvar, int flags) {
nel_ta result;
FILE *fs;
fs=fopen(fname, "r");
if (fs==NULL) return -1;
result=read_lvq(fs, train, cls, nvar, flags);
fclose(fs);
return result;
}
//pretty stupid, but kind of fun to write:
cls_ta * read_lvq_classes(FILE *fs, nel_ta &n, int hflag) {
int c1;
char c2;
char cold;
int tranloc; //last position where there is a transition
//between space and number
int count; //position in line
linked_list<cls_ta> data;
cls_ta val;
long n1;
cls_ta *cls;
if (hflag==0) {
//get header, don't care what's in it...
do {
c1=fgetc(fs);
if (c1==EOF) break;
c2=(char) c1;
} while (c2!='\n');
}
while (feof(fs)==0) {
c1=fgetc(fs);
if (c1==EOF) break;
cold=(char) c1;
count=1;
if (isdigit(cold)) tranloc=0; else tranloc=-1;
do {
c1=fgetc(fs);
if (c1==EOF) break;
c2=(char) c1;
if (isspace(cold) and isdigit(c1)) tranloc=count;
count++;
} while (c1!='\n');
if (tranloc<0) break;
fseek(fs, tranloc-count, SEEK_CUR);
fscanf(fs, "%d", &val);
data.add(val);
}
cls=data.make_array(n1);
n=n1;
return cls;
}
} //end namespace libagf