Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ols_test_main.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1998 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Alan W Black */
34 /* Date : January 1998 */
35 /*-----------------------------------------------------------------------*/
36 /* A program for testing a OLS */
37 /* */
38 /*=======================================================================*/
39 #include <cstdlib>
40 #include <iostream>
41 #include <fstream>
42 #include <cstring>
43 #include "EST_Wagon.h"
44 #include "EST_cutils.h"
45 #include "EST_multistats.h"
46 #include "EST_Token.h"
47 #include "EST_cmd_line.h"
48 
49 static int ols_test_main(int argc, char **argv);
50 static void load_ols_data(EST_FMatrix &X, EST_FMatrix &Y, WDataSet &d);
51 
52 
53 int main(int argc, char **argv)
54 {
55 
56  ols_test_main(argc,argv);
57 
58  exit(0);
59  return 0;
60 }
61 
62 static int ols_test_main(int argc, char **argv)
63 {
64  // Top level function sets up data and creates a tree
65  EST_Option al;
66  EST_StrList files;
67  EST_FMatrix X,Y,coeffs;
68  WDataSet dataset;
69  EST_String outfile;
70 
71  parse_command_line
72  (argc, argv,
73  EST_String("[options]\n")+
74  "ols_test <options>\n"+
75  "program to test OLS on data\n"+
76  "-desc <ifile> Field description file\n"+
77  "-data <ifile> Datafile, one vector per line\n"+
78  "-coeffs <ifile> File containing OLS coefficients\n"+
79  "-predict Predict for each vector returning value\n"+
80  "-o <ofile> File to save output in\n",
81  files, al);
82 
83  siod_init();
84 
85  if (al.present("-desc"))
86  {
87  dataset.load_description(al.val("-desc"),NIL);
88  }
89  else
90  {
91  cerr << argv[0] << ": no description file specified" << endl;
92  exit(-1);
93  }
94 
95  if (coeffs.load(al.val("-coeffs")) != format_ok)
96  {
97  cerr << argv[0] << ": no coefficients file specified" << endl;
98  exit(-1);
99  }
100 
101  if (al.present("-data"))
102  wgn_load_dataset(dataset,al.val("-data"));
103  else
104  {
105  cerr << argv[0] << ": no data file specified" << endl;
106  exit(-1);
107  }
108 
109  if (al.present("-o"))
110  outfile = al.val("-o");
111  else
112  outfile = "-";
113 
114  EST_FMatrix pred;
115  float cor,rmse;
116 
117  load_ols_data(X,Y,dataset);
118  ols_apply(X,coeffs,pred);
119  if (ols_test(Y,pred,cor,rmse))
120  printf(";; RMSE %f Correlation is %f\n",rmse,cor);
121  else
122  printf(";; varation too small RMSE %f but no correlation\n",rmse);
123  if (al.present("-o") || al.present("-predict"))
124  pred.save(outfile);
125 
126  return 0;
127 }
128 
129 static void load_ols_data(EST_FMatrix &X, EST_FMatrix &Y, WDataSet &d)
130 {
131  EST_Litem *p;
132  int n,m;
133 
134  X.resize(d.length(),d.width());
135  Y.resize(d.length(),1);
136 
137  for (n=0,p=d.head(); p != 0; p=p->next(),n++)
138  {
139  Y(n,0) = d(p)->get_flt_val(0);
140  X(n,0) = 1;
141  for (m=1; m < d.width(); m++)
142  X(n,m) = d(p)->get_flt_val(m);
143  }
144 
145 }
EST_write_status save(const EST_String &filename, const EST_String &type=EST_FMatrix::default_file_type)
Save in file (ascii or binary)
Definition: EST_FMatrix.cc:340
EST_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
Definition: EST_FMatrix.cc:513
const int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
void resize(int rows, int cols, int set=1)
resize matrix