[r80]: src / multi_borders.cc  Maximize  Restore  History

Download this file

217 lines (188 with data), 8.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
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/timeb.h>
#include <unistd.h>
#include "peteys_tmpl_lib.h"
#include "parse_command_opts.h"
#include "agf_lib.h"
using namespace std;
using namespace libpetey;
using namespace libagf;
int main (int argc, char **argv) {
multi_parse_param param;
cls_ta cls[200]; //awful lot of fixed-length arrays...
cls_ta ncls;
char opt0[200];
int32_t nsv; //number of singular values to keep
char *command=NULL; //command for performing the normalization
char *normfile=NULL; //normalization file
agf_command_opts opt_args;
void *opt[30];
int flag[30];
int testflag; //test run (simply prints out the commands)
char *tempbase=NULL; //base name of normalized training data
int err;
char **argv1;
int argc1;
//we want to parse out the options that only apply at the top level
//while keeping the ones to pass to class_borders
//--the former include options for normalization or pre-processing:
opt[3]=&nsv;
argc=parse_command_opts(argc, argv, "a0nS", "%s%%%d", opt, flag, 3);
if (argc<0) {
fprintf(stderr, "multi_borders: error parsing command line\n");
exit(FATAL_COMMAND_OPTION_PARSE_ERROR);
}
//parse out the rest of the options so we can figure out what the
//main arguments are:
argc1=argc;
err=agf_parse_command_opts(argc1, argv, "h:i:I:k:l:r:s:t:v:V:W:uK", &opt_args);
//argc1=parse_command_opts(argc, argv, "hiIklrstvVWu", "%d%d%d%d%g%g%d%g%g%g%g%", opt+4, flag+4, 1);
if (err==FATAL_COMMAND_OPTION_PARSE_ERROR) {
fprintf(stderr, "multi_borders: error parsing command line\n");
exit(err);
}
//argc=argc1;
//printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
if (argc1<4) {
FILE *helpfs=stdout;
fprintf(helpfs, "\nsyntax: multi_borders [-n] [-a normfile] [-S nsv] [trainopt]\n");
fprintf(helpfs, " control train border out\n");
fprintf(helpfs, "\nwhere:\n");
fprintf(helpfs, " control input control file\n");
fprintf(helpfs, " train base filename for binary training data:\n");
fprintf(helpfs, " .vec for vectors;\n");
fprintf(helpfs, " .cls for classes.\n");
fprintf(helpfs, " .std for transformation/normalization matrix\n");
fprintf(helpfs, " (unless -a specified)\n");
fprintf(helpfs, " border base name for files containing border data\n");
fprintf(helpfs, " (file names are recorded in output control file)\n");
fprintf(helpfs, " out output control file to feed to classify_m\n\n");
fprintf(helpfs, " * commands to train the model are written to stdout\n");
fprintf(helpfs, "\noptions:\n");
fprintf(helpfs, " trainopt are a series of options to pass to class_borders\n");
fprintf(helpfs, " (-h, -k, -i, -I, -r, -s, -t, -v, -V, -W --for an\n");
fprintf(helpfs, " explanation of each, run class_borders with no arguments)\n");
fprintf(helpfs, " -n normalize training data with standard deviations\n");
fprintf(helpfs, " -S nsv perform SVD, use nsv singular values\n");
fprintf(helpfs, " -u store borders data in un-normalized coordinates\n");
fprintf(helpfs, " -a normfile normalize/use normfile for normalization data\n");
fprintf(helpfs, " (default is <train>.std\n");
//fprintf(helpfs, " -0 trial run only: print out commands but do not execute them\n");
fprintf(helpfs, "\n");
fprintf(helpfs, "The syntax of the control file is as follows:\n\n");
fprintf(helpfs, " <branch> ::= <model> \"{\" <branch_list> \"}\" | <CLASS>\n");
fprintf(helpfs, " <model> ::= <OPTIONS> | <partition_list>\n");
fprintf(helpfs, " <branch_list> ::= <branch> | <branch_list> <branch>\n");
fprintf(helpfs, " <partition_list> ::= <partition> | <partition_list> <partition>\n");
fprintf(helpfs, " <partition> ::= <OPTIONS> <class_list> \" %c \" <class_list> \";\"\n", PARTITION_SYMBOL);
fprintf(helpfs, " <class_list> ::= <CLASS> | <class_list> <CLASS>\n\n");
fprintf(helpfs, " <OPTIONS> ::= \"\"\"\"\"\" | \"\"\".\"\"\" | \"\"\" \"\"\"\n");
fprintf(helpfs, " | \"\"\"[\"-v\" <vmin>] [\"-V\" <vmax>] [\"-W\" <W>]\n");
fprintf(helpfs, " [\"-s\" <n>] [\"-t\" <tol>] ...\"\"\"\n");
fprintf(helpfs, " <CLASS> ::= 0 | 1 | 2 | 3 ... | <ncls-1>\n\n");
fprintf(helpfs, "where:\n");
fprintf(helpfs, " <OPTIONS> are a double-quoted list of options to pass to class borders\n");
fprintf(helpfs, " - a null string (\"\") means take options from the command line\n");
fprintf(helpfs, " - a period in quotes (\".\") means use the previous option list\n");
fprintf(helpfs, " on the same level or lower\n");
fprintf(helpfs, " - to make the options list empty, use a whitespace character\n");
fprintf(helpfs, " (i.e., \" \")\n");
fprintf(helpfs, " <CLASS> class number from zero (0) to the number of classes less one\n");
fprintf(helpfs, " <partition_list> describes a non-hierarchical multi-borders model\n");
fprintf(helpfs, "\n");
exit(0);
}
if (flag[0]) {
normfile=new char[strlen((char *) opt[0])+1];
strcpy(normfile, (char *) opt[0]);
}
//write statements to stdout:
param.commandfs=stdout;
if (flag[0] || flag[2] || flag[3]) {
timeb date;
pid_t pid;
int64_t session_id; //for naming temporary files
//stick transformed training data in a temporary file:
ftime(&date);
pid=getpid();
session_id=pid+date.time*100000; //how big do PID's get??
tempbase=new char [strlen(argv[2])+20];
sprintf(tempbase, "%s.%ld", argv[2], session_id);
if (normfile==NULL) {
normfile=new char [strlen(argv[2])+5];
sprintf(normfile, "%s.std", argv[2]);
}
command=new char [strlen(argv[1])+strlen(normfile)+strlen(tempbase)+100];
sprintf(command, "%s%s%s -a %s", AGF_COMMAND_PREFIX, AGF_LTRAN_COM,
AGF_OPT_VER, normfile);
if (flag[2]) {
sprintf(command+strlen(command), " -n");
}
if (flag[3]) {
sprintf(command+strlen(command), " -S %d", nsv);
}
sprintf(command+strlen(command), " %s.vec %s.vec", argv[1], tempbase);
fprintf(param.commandfs, "%s\n", command);
if (opt_args.uflag) {
param.train=new char [strlen(argv[1])+1];
sprintf(param.train, "%s", argv[1]);
} else {
param.train=new char [strlen(tempbase)+1];
sprintf(param.train, "%s", tempbase);
fprintf(param.commandfs, "cp %s.cls %s.cls\n", argv[1], tempbase);
}
} else {
param.train=new char [strlen(argv[1])+1];
sprintf(param.train, "%s", argv[1]);
}
multi_train_begin(&param, argc-argc1-1, argv-argc+argc1+1);
//input control file:
param.infs=fopen(argv[0], "r");
if (param.infs==NULL) {
fprintf(stderr, "multi_borders: unable to open file %s for reading\n",
argv[0]);
exit(UNABLE_TO_OPEN_FILE_FOR_READING);
}
//output control file (last argument):
param.outfs=fopen(argv[3], "w");
if (param.outfs==NULL) {
fprintf(stderr, "multi_borders: unable to open file %s for writing\n",
argv[3]);
exit(UNABLE_TO_OPEN_FILE_FOR_WRITING);
}
//use this command for training
if (opt_args.uflag && normfile!=NULL) {
//if we want to un-normalize the data, we have to pass the option up to
//class borders through the command name:
param.commandname=new char[strlen(AGF_COMMAND_PREFIX)+strlen(normfile)+
strlen(AGF_BINARY_CLASSIFIER)+strlen(AGF_OPT_VER)+8];
sprintf(param.commandname, "%s%s%s -u -a %s", AGF_COMMAND_PREFIX,
AGF_BINARY_CLASSIFIER, AGF_OPT_VER, normfile);
} else {
param.commandname=new char[strlen(AGF_COMMAND_PREFIX)+
strlen(AGF_BINARY_CLASSIFIER)+strlen(AGF_OPT_VER)+1];
sprintf(param.commandname, "%s%s%s", AGF_COMMAND_PREFIX,
AGF_BINARY_CLASSIFIER, AGF_OPT_VER);
}
//parse the control file:
ncls=parse_multi_train(&param, argv[2], cls);
fclose(param.infs);
fclose(param.outfs);
if (normfile!=NULL && opt_args.Kflag==0) {
fprintf(param.commandfs, "rm %s.vec\n", tempbase);
if (opt_args.uflag==0) {
fprintf(param.commandfs, "rm %s.cls\n", tempbase);
}
delete [] normfile;
}
multi_train_end(&param);
if (command!=NULL) delete [] command;
if (tempbase!=NULL) delete [] tempbase;
delete [] param.train;
delete [] param.commandname;
//delete [] param.optstack;
}