-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
116 lines (96 loc) · 3.97 KB
/
main.cc
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
#include <fstream>
#include <iostream>
#include "hmm.h"
void showUsage(std::string programName)
{
std::cerr << "Usage: " << programName
<< " path_to_model path_to_data " << std::endl;
}
void printPredictionEstimation(size_t stateInd,
const HMM::Data::PredictionEstimation& estimation,
const HMM::Data::Model& model)
{
std::cout << "State " << model.stateIndexToName[stateInd]
<< " => "
<< "True Positives=" << estimation.truePositives << ", "
<< "False Positives=" << estimation.falsePositives << ", "
<< "True Negatives=" << estimation.trueNegatives << ", "
<< "False Negatives=" << estimation.falseNegatives << ", "
<< "f-measure=" << estimation.fMeasure << '\n';
}
int main(int argc, char* argv[])
{
// section: check arguments and prepare input streams
if (argc < 3) {
showUsage(argv[0]);
return -1;
}
std::ifstream modelSource(argv[1]);
std::ifstream dataSource(argv[2]);
if (! modelSource.good()) {
std::cerr << "ERROR: Failed to open model file properly." << std::endl;
return -1;
}
if (! dataSource.good()) {
std::cerr << "ERROR: Failed to open data file properly." << std::endl;
return -1;
}
// enable exceptions to signal errors later while reading model and data
std::ios_base::iostate ioExcept = (std::ifstream::failbit |
std::ifstream::badbit |
std::ifstream::eofbit);
modelSource.exceptions(ioExcept);
dataSource.exceptions(ioExcept);
// section: read model and data
HMM::Data::Model model;
HMM::Data::ExperimentData data;
try
{
model.ReadModel(modelSource);
} catch(std::exception& e) {
std::cerr << "ERROR: fatal problem while reading model. Details: '" << e.what()
<< "'" << std::endl;
return -1;
} catch (...) {
std::cerr << "ERROR: unknown exception while reading model" << std::endl;
return -1;
}
try
{
data.ReadExperimentData(model, dataSource);
} catch(std::exception& e) {
std::cerr << "ERROR: fatal problem while reading experiment data. Details: '" << e.what()
<< "'" << std::endl;
return -1;
} catch (...) {
std::cerr << "ERROR: unknown exception while reading experiment data " << std::endl;
return -1;
}
// secton: run and estimate viterbi predictions
std::vector<size_t> mostProbableSeq =
HMM::Algorithms::FindMostProbableStateSequence(model, data);
std::vector<std::vector<size_t> > confusionMatrix =
HMM::Estimation::CombineConfusionMatrix(data, mostProbableSeq, model);
std::vector<HMM::Data::PredictionEstimation> estimations =
HMM::Estimation::GetStatePredictionEstimations(confusionMatrix);
std::cout << "Viterbi algorithm state prediction estimations:\n";
// skip first and last states (begin and end)
for (size_t i = 1; i + 1 < estimations.size(); ++i) {
printPredictionEstimation(i, estimations[i], model);
}
std::cout << "\n";
// section: run and estimate forward-backward predictions
std::vector<std::vector<std::pair<double, double> > > forwardBackwardProb =
HMM::Algorithms::CalcForwardBackwardProbabiliies(model, data);
std::vector<size_t> mostProbableStates =
HMM::Estimation::GetMostProbableStates(forwardBackwardProb);
confusionMatrix = HMM::Estimation::CombineConfusionMatrix(data, mostProbableStates, model);
estimations = HMM::Estimation::GetStatePredictionEstimations(confusionMatrix);
std::cout << "Forward-backward algorithm state prediction estimations:\n";
// skip first and last states (begin and end)
for (size_t i = 1; i + 1 < estimations.size(); ++i) {
printPredictionEstimation(i, estimations[i], model);
}
std::cout << "\n";
return 0;
}