loglik.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  */
00043 #include "fastlib/fastlib.h"
00044 #include "support.h"
00045 #include "discreteHMM.h"
00046 #include "gaussianHMM.h"
00047 #include "mixgaussHMM.h"
00048 #include "mixtureDST.h"
00049 
00050 using namespace hmm_support;
00051 
00052 success_t loglik_discrete();
00053 success_t loglik_gaussian();
00054 success_t loglik_mixture();
00055 void usage();
00056 
00057 const fx_entry_doc hmm_loglik_main_entries[] = {
00058   {"type", FX_REQUIRED, FX_STR, NULL,
00059    "  HMM type : discrete | gaussian | mixture.\n"},
00060   {"profile", FX_REQUIRED, FX_STR, NULL,
00061    "  A file containing HMM profile.\n"},
00062   {"seqfile", FX_PARAM, FX_STR, NULL,
00063    "  Output file for the data sequences.\n"},
00064   {"logfile", FX_PARAM, FX_STR, NULL,
00065    "  Output file for the computed log-likelihood of the sequences.\n"},
00066   FX_ENTRY_DOC_DONE
00067 };
00068 
00069 const fx_submodule_doc hmm_loglik_main_submodules[] = {
00070   FX_SUBMODULE_DOC_DONE
00071 };
00072 
00073 const fx_module_doc hmm_loglik_main_doc = {
00074   hmm_loglik_main_entries, hmm_loglik_main_submodules,
00075   "This is a program computing log-likelihood of data sequences \n"
00076   "from HMM models.\n"
00077 };
00078 
00079 int main(int argc, char* argv[]) {
00080   fx_init(argc, argv, &hmm_loglik_main_doc);
00081   success_t s = SUCCESS_PASS;
00082   if (fx_param_exists(NULL,"type")) {
00083     const char* type = fx_param_str_req(NULL, "type");
00084     if (strcmp(type, "discrete")==0)
00085       s = loglik_discrete();
00086     else if (strcmp(type, "gaussian")==0) 
00087       s = loglik_gaussian();
00088     else if (strcmp(type, "mixture")==0) 
00089       s = loglik_mixture();
00090     else {
00091       printf("Unrecognized type: must be: discrete | gaussian | mixture !!!\n");
00092       s = SUCCESS_FAIL;
00093     }
00094   }
00095   else {
00096     printf("Unrecognized type: must be: discrete | gaussian | mixture  !!!\n");
00097     s = SUCCESS_FAIL;
00098   }
00099   if (!PASSED(s)) usage();
00100   fx_done(NULL);
00101 }
00102 
00103 void usage() {
00104   printf("\n"
00105          "Usage:\n"
00106          "  loglik --type=={discrete|gaussian|mixture} OPTIONS\n"
00107          "[OPTIONS]\n"
00108          "  --profile==file   : file contains HMM profile\n"
00109          "  --seqfile==file   : file contains input sequences\n"
00110          "  --logfile==file   : output file for log-likelihood of the sequences\n"
00111          );
00112 }
00113 
00114 success_t loglik_mixture() {
00115   if (!fx_param_exists(NULL, "profile")) {
00116     printf("--profile must be defined.\n");
00117     return SUCCESS_FAIL;
00118   }
00119   const char* profile = fx_param_str_req(NULL, "profile");
00120   const char* seqin = fx_param_str(NULL, "seqfile", "seq.mix.out");
00121   const char* logout = fx_param_str(NULL, "logfile", "log.mix.out");
00122 
00123   MixtureofGaussianHMM hmm;
00124   hmm.InitFromFile(profile);
00125 
00126   ArrayList<Matrix> seqs;
00127   load_matrix_list(seqin, &seqs);
00128 
00129   TextWriter w_log;
00130   if (!PASSED(w_log.Open(logout))) {
00131     NONFATAL("Couldn't open '%s' for writing.", logout);
00132     return SUCCESS_FAIL;
00133   }
00134 
00135   ArrayList<double> list_loglik;
00136   hmm.ComputeLogLikelihood(seqs, &list_loglik);
00137 
00138   for (int i = 0; i < seqs.size(); i++)
00139     w_log.Printf("%f\n", list_loglik[i]);
00140   
00141   return SUCCESS_PASS;
00142 }
00143 
00144 success_t loglik_gaussian() {
00145   if (!fx_param_exists(NULL, "profile")) {
00146     printf("--profile must be defined.\n");
00147     return SUCCESS_FAIL;
00148   }
00149   const char* profile = fx_param_str_req(NULL, "profile");
00150   const char* seqin = fx_param_str(NULL, "seqfile", "seq.gauss.out");
00151   const char* logout = fx_param_str(NULL, "logfile", "log.gauss.out");
00152 
00153   GaussianHMM hmm;
00154   hmm.InitFromFile(profile);
00155 
00156   ArrayList<Matrix> seqs;
00157   load_matrix_list(seqin, &seqs);
00158 
00159   TextWriter w_log;
00160   if (!PASSED(w_log.Open(logout))) {
00161     NONFATAL("Couldn't open '%s' for writing.", logout);
00162     return SUCCESS_FAIL;
00163   }
00164 
00165   ArrayList<double> list_loglik;
00166   hmm.ComputeLogLikelihood(seqs, &list_loglik);
00167 
00168   for (int i = 0; i < seqs.size(); i++)
00169     w_log.Printf("%f\n", list_loglik[i]);
00170   
00171   return SUCCESS_PASS;
00172 }
00173 
00174 success_t loglik_discrete() {
00175   if (!fx_param_exists(NULL, "profile")) {
00176     printf("--profile must be defined.\n");
00177     return SUCCESS_FAIL;
00178   }
00179   const char* profile = fx_param_str_req(NULL, "profile");
00180   const char* seqin = fx_param_str(NULL, "seqfile", "seq.out");
00181   const char* logout = fx_param_str(NULL, "logfile", "log.out");
00182 
00183   DiscreteHMM hmm;
00184   hmm.InitFromFile(profile);
00185 
00186   ArrayList<Vector> seqs;
00187   load_vector_list(seqin, &seqs);
00188 
00189   TextWriter w_log;
00190   if (!PASSED(w_log.Open(logout))) {
00191     NONFATAL("Couldn't open '%s' for writing.", logout);
00192     return SUCCESS_FAIL;
00193   }
00194 
00195   ArrayList<double> list_loglik;
00196   hmm.ComputeLogLikelihood(seqs, &list_loglik);
00197 
00198   for (int i = 0; i < seqs.size(); i++)
00199     w_log.Printf("%f\n", list_loglik[i]);
00200   return SUCCESS_PASS;
00201 }
00202 
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3