viterbi.cc

Go to the documentation of this file.
00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00044 #include "fastlib/fastlib.h"
00045 #include "support.h"
00046 #include "discreteHMM.h"
00047 #include "gaussianHMM.h"
00048 #include "mixgaussHMM.h"
00049 #include "mixtureDST.h"
00050 
00051 using namespace hmm_support;
00052 
00053 success_t viterbi_discrete();
00054 success_t viterbi_gaussian();
00055 success_t viterbi_mixture();
00056 void usage();
00057 
00058 const fx_entry_doc hmm_viterbi_main_entries[] = {
00059   {"type", FX_REQUIRED, FX_STR, NULL,
00060    "  HMM type : discrete | gaussian | mixture.\n"},
00061   {"profile", FX_REQUIRED, FX_STR, NULL,
00062    "  A file containing HMM profile.\n"},
00063   {"seqfile", FX_PARAM, FX_STR, NULL,
00064    "  Output file for the data sequences.\n"},
00065   {"statefile", FX_PARAM, FX_STR, NULL,
00066    "  Output file for the most probable state sequences.\n"},
00067   FX_ENTRY_DOC_DONE
00068 };
00069 
00070 const fx_submodule_doc hmm_viterbi_main_submodules[] = {
00071   FX_SUBMODULE_DOC_DONE
00072 };
00073 
00074 const fx_module_doc hmm_viterbi_main_doc = {
00075   hmm_viterbi_main_entries, hmm_viterbi_main_submodules,
00076   "This is a program computing the most probable state sequences \n"
00077   "of data sequences from HMM models.\n"
00078 };
00079 
00080 int main(int argc, char* argv[]) {
00081   fx_init(argc, argv, &hmm_viterbi_main_doc);
00082   success_t s = SUCCESS_PASS;
00083   if (fx_param_exists(NULL,"type")) {
00084     const char* type = fx_param_str_req(NULL, "type");
00085     if (strcmp(type, "discrete")==0)
00086       s = viterbi_discrete();
00087     else if (strcmp(type, "gaussian")==0)
00088       s = viterbi_gaussian();
00089     else if (strcmp(type, "mixture")==0)
00090       s = viterbi_mixture();
00091     else {
00092       printf("Unrecognized type: must be: discrete | gaussian | mixture !!!\n");
00093       s = SUCCESS_FAIL;
00094     }
00095   }
00096   else {
00097     printf("Unrecognized type: must be: discrete | gaussian | mixture  !!!\n");
00098     s = SUCCESS_FAIL;
00099   }
00100   if (!PASSED(s)) usage();
00101   fx_done(NULL);
00102 }
00103 
00104 void usage() {
00105   printf("\n"
00106          "Usage:\n"
00107          "  viterbi --type=={discrete|gaussian|mixture} OPTIONS\n"
00108          "[OPTIONS]\n"
00109          "  --profile=file   : file contains HMM profile\n"
00110          "  --seqfile=file   : file contains input sequences\n"
00111          "  --statefile=file : output file for state sequences\n"
00112          );
00113 }
00114 
00115 success_t viterbi_mixture() {
00116   if (!fx_param_exists(NULL, "profile")) {
00117     printf("--profile must be defined.\n");
00118     return SUCCESS_FAIL;
00119   }
00120   const char* profile = fx_param_str_req(NULL, "profile");
00121   const char* seqin = fx_param_str(NULL, "seqfile", "seq.mix.out");
00122   const char* stateout = fx_param_str(NULL, "statefile", "state.viterbi.mix.out");
00123 
00124   MixtureofGaussianHMM hmm;
00125   hmm.InitFromFile(profile);
00126 
00127   ArrayList<Matrix> seqs;
00128   load_matrix_list(seqin, &seqs);
00129 
00130   TextWriter w_state;
00131   if (!PASSED(w_state.Open(stateout))) {
00132     NONFATAL("Couldn't open '%s' for writing.", stateout);
00133     return SUCCESS_FAIL;
00134   }
00135 
00136   for (int i = 0; i < seqs.size(); i++) {
00137     Vector states;
00138     char s[100];
00139 
00140     hmm.ComputeViterbiStateSequence(seqs[i], &states);
00141     
00142     sprintf(s, "%% viterbi state sequence %d", i);
00143     print_vector(w_state, states, s, "%.0f,");
00144   }
00145   return SUCCESS_PASS;
00146 }
00147 
00148 success_t viterbi_gaussian() {
00149   if (!fx_param_exists(NULL, "profile")) {
00150     printf("--profile must be defined.\n");
00151     return SUCCESS_FAIL;
00152   }
00153   const char* profile = fx_param_str_req(NULL, "profile");
00154   const char* seqin = fx_param_str(NULL, "seqfile", "seq.gauss.out");
00155   const char* stateout = fx_param_str(NULL, "statefile", "state.viterbi.gauss.out");
00156   GaussianHMM hmm;
00157   hmm.InitFromFile(profile);
00158 
00159   ArrayList<Matrix> seqs;
00160   load_matrix_list(seqin, &seqs);
00161 
00162   TextWriter w_state;
00163   if (!PASSED(w_state.Open(stateout))) {
00164     NONFATAL("Couldn't open '%s' for writing.", stateout);
00165     return SUCCESS_FAIL;
00166   }
00167 
00168   for (int i = 0; i < seqs.size(); i++) {
00169     Vector states;
00170     char s[100];
00171     hmm.ComputeViterbiStateSequence(seqs[i], &states);
00172     
00173     sprintf(s, "%% viterbi state sequence %d", i);
00174     print_vector(w_state, states, s, "%.0f,");
00175   }
00176   return SUCCESS_PASS;
00177 }
00178 
00179 success_t viterbi_discrete() {
00180   if (!fx_param_exists(NULL, "profile")) {
00181     printf("--profile must be defined.\n");
00182     return SUCCESS_FAIL;
00183   }
00184   const char* profile = fx_param_str_req(NULL, "profile");
00185   const char* seqin = fx_param_str(NULL, "seqfile", "seq.out");
00186   const char* stateout = fx_param_str(NULL, "statefile", "state.viterbi.out");
00187 
00188   DiscreteHMM hmm;
00189 
00190   hmm.InitFromFile(profile);
00191 
00192   ArrayList<Vector> seqs;
00193   load_vector_list(seqin, &seqs);
00194 
00195   TextWriter w_state;
00196   if (!PASSED(w_state.Open(stateout))) {
00197     NONFATAL("Couldn't open '%s' for writing.", stateout);
00198     return SUCCESS_FAIL;
00199   }
00200 
00201   for (int i = 0; i < seqs.size(); i++) {
00202     Vector states;
00203     char s[100];
00204     
00205     hmm.ComputeViterbiStateSequence(seqs[i], &states);
00206     
00207     sprintf(s, "%% viterbi state sequence %d", i);
00208     print_vector(w_state, states, s, "%.0f,");
00209   }
00210   return SUCCESS_PASS;
00211 }
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3