Edinburgh Speech Tools  2.1-release
ols_test_main.cc
Go to the documentation of this file.
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 using namespace std;
50 
51 static int ols_test_main(int argc, char **argv);
52 static void load_ols_data(EST_FMatrix &X, EST_FMatrix &Y, WDataSet &d);
53 
54 
55 int main(int argc, char **argv)
56 {
57 
58  ols_test_main(argc,argv);
59 
60  exit(0);
61  return 0;
62 }
63 
64 static int ols_test_main(int argc, char **argv)
65 {
66  // Top level function sets up data and creates a tree
67  EST_Option al;
68  EST_StrList files;
69  EST_FMatrix X,Y,coeffs;
70  WDataSet dataset;
72 
74  (argc, argv,
75  EST_String("[options]\n")+
76  "ols_test <options>\n"+
77  "program to test OLS on data\n"+
78  "-desc <ifile> Field description file\n"+
79  "-data <ifile> Datafile, one vector per line\n"+
80  "-coeffs <ifile> File containing OLS coefficients\n"+
81  "-predict Predict for each vector returning value\n"+
82  "-o <ofile> File to save output in\n",
83  files, al);
84 
85  siod_init();
86 
87  if (al.present("-desc"))
88  {
89  dataset.load_description(al.val("-desc"),NIL);
90  }
91  else
92  {
93  cerr << argv[0] << ": no description file specified" << endl;
94  exit(-1);
95  }
96 
97  if (coeffs.load(al.val("-coeffs")) != format_ok)
98  {
99  cerr << argv[0] << ": no coefficients file specified" << endl;
100  exit(-1);
101  }
102 
103  if (al.present("-data"))
104  wgn_load_dataset(dataset,al.val("-data"));
105  else
106  {
107  cerr << argv[0] << ": no data file specified" << endl;
108  exit(-1);
109  }
110 
111  if (al.present("-o"))
112  outfile = al.val("-o");
113  else
114  outfile = "-";
115 
116  EST_FMatrix pred;
117  float cor,rmse;
118 
119  load_ols_data(X,Y,dataset);
120  ols_apply(X,coeffs,pred);
121  if (ols_test(Y,pred,cor,rmse))
122  printf(";; RMSE %f Correlation is %f\n",rmse,cor);
123  else
124  printf(";; varation too small RMSE %f but no correlation\n",rmse);
125  if (al.present("-o") || al.present("-predict"))
126  pred.save(outfile);
127 
128  return 0;
129 }
130 
131 static void load_ols_data(EST_FMatrix &X, EST_FMatrix &Y, WDataSet &d)
132 {
133  EST_Litem *p;
134  int n,m;
135 
136  X.resize(d.length(),d.width());
137  Y.resize(d.length(),1);
138 
139  for (n=0,p=d.head(); p != 0; p=p->next(),n++)
140  {
141  Y(n,0) = d(p)->get_flt_val(0);
142  X(n,0) = 1;
143  for (m=1; m < d.width(); m++)
144  X(n,m) = d(p)->get_flt_val(m);
145  }
146 
147 }
int ols_test(const EST_FMatrix &real, const EST_FMatrix &predicted, float &correlation, float &rmse)
Definition: EST_ols.cc:288
int width(void) const
Definition: EST_Wagon.h:94
#define NIL
Definition: siod_defs.h:92
int main(int argc, char **argv)
EST_UItem * next()
Definition: EST_UList.h:55
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:341
int ols_apply(const EST_FMatrix &samples, const EST_FMatrix &coeffs, EST_FMatrix &res)
Definition: EST_ols.cc:185
EST_String outfile
EST_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
Definition: EST_FMatrix.cc:523
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
int length() const
Definition: EST_UList.cc:57
#define X
#define format_ok
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
void wgn_load_dataset(WDataSet &ds, EST_String fname)
Definition: wagon.cc:118
EST_UItem * head() const
Definition: EST_UList.h:97
int siod_init(int heap_size=DEFAULT_HEAP_SIZE)
Definition: siod.cc:58
void resize(int rows, int cols, int set=1)
resize matrix
EST_String
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)
Definition: cmd_line.cc:101
void load_description(const EST_String &descfname, LISP ignores)
Definition: wagon_aux.cc:181