-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPairwiseEvaluation.h
290 lines (252 loc) · 9.57 KB
/
PairwiseEvaluation.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifndef PairwiseEvaluation_h
#define PairwiseEvaluation_h
//============================================================================
/**
Template class representing a frame based evaluation of two timed sequences
of musical properties.
@author Johan Pauwels
@date 20100913
*/
//============================================================================
#include "MusOO/TimedLabel.h"
#include <Eigen/Core>
#include <vector>
#include <set>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <algorithm>
template<typename T>
class SimilarityScore;
template <typename T>
class PairwiseEvaluation
{
public:
typedef std::vector<MusOO::TimedLabel<T> > LabelSequence;
/** Default constructor. */
PairwiseEvaluation(const std::string& inScoreSelect);
/** Destructor. */
virtual ~PairwiseEvaluation();
void addSequencePair(const LabelSequence& inRefSequence, const LabelSequence& inTestSequence, double inStartTime, double inEndTime, std::ostream& inVerboseOStream, const double inMinRefDuration = 0., const double inMaxRefDuration = std::numeric_limits<double>::infinity(), const double inDelay = 0.);
const std::vector<T>& getLabels() const;
const Eigen::ArrayXXd::Index getNumOfRefLabels() const;
const Eigen::ArrayXXd::Index getNumOfTestLabels() const;
// Get results of last file
const double getDuration() const;
const double getScore() const;
const Eigen::ArrayXXd& getConfusionMatrix() const;
// Reductions over data set
const double calcTotalDuration() const;
const Eigen::ArrayXXd calcTotalConfusionMatrix() const;
const double calcAverageScore() const;
const double calcWeightedAverageScore() const;
protected:
void printVerboseOutput(std::ostream& inVerboseOStream, const double theStartTime, const double theEndTime, const T& theRefLabel, const T& theTestLabel, const T& theMappedRefLabel, const T& theMappedTestLabel, const double theScore, const double theSegmentLength) const;
SimilarityScore<T>* m_SimilarityScore;
Eigen::ArrayXXd::Index m_NumOfRefLabels;
Eigen::ArrayXXd::Index m_NumOfTestLabels;
std::vector<double> m_Durations;
std::vector<double> m_Scores;
std::vector<Eigen::ArrayXXd> m_ConfusionMatrices;
private:
};
void printConfusionMatrix(std::ostream& inOutputStream, const Eigen::ArrayXXd& inConfusionMatrix,
const std::vector<std::string>& inLabels, const std::string inSeparator = ",",
const std::string inQuote = "\"");
template <typename T>
PairwiseEvaluation<T>::~PairwiseEvaluation()
{
delete m_SimilarityScore;
}
template <typename T>
void PairwiseEvaluation<T>::addSequencePair(const LabelSequence& inRefSequence, const LabelSequence& inTestSequence, double inStartTime, double inEndTime, std::ostream& inVerboseOStream, const double inMinRefDuration /*= 0.*/, const double inMaxRefDuration /*= std::numeric_limits<double>::infinity()*/, const double inDelay /*= 0.*/)
{
m_ConfusionMatrices.push_back(Eigen::ArrayXXd::Zero(m_NumOfRefLabels, m_NumOfTestLabels));
Eigen::ArrayXXd& curConfusionMatrix = m_ConfusionMatrices.back();
m_Scores.push_back(0.);
double& curScore = m_Scores.back();
double theCurTime = inStartTime;
double thePrevTime;
double theSegmentLength;
size_t theRefIndex = 0;
size_t theTestIndex = 0;
//set end time of test and reference sequence
double theRefEndTime = 0.;
double theTestEndTime = 0.;
if (!inRefSequence.empty())
{
theRefEndTime = inRefSequence.back().offset();
}
if (!inTestSequence.empty())
{
theTestEndTime = inTestSequence.back().offset() - inDelay;
}
//test whether end time is set
if (inEndTime <= inStartTime)
{
inEndTime = theRefEndTime;
}
//collect all transition times of both reference and test sequence
std::set<double> theChangeTimes;
for (size_t i = 0; i < inRefSequence.size(); i++)
{
if (inRefSequence[i].onset() >= inStartTime && inRefSequence[i].onset() < inEndTime)
{
theChangeTimes.insert(inRefSequence[i].onset());
}
if (inRefSequence[i].offset() >= inStartTime && inRefSequence[i].offset() < inEndTime)
{
theChangeTimes.insert(inRefSequence[i].offset());
}
}
for (size_t i = 0; i < inTestSequence.size(); i++)
{
if (inTestSequence[i].onset() >= inStartTime && inTestSequence[i].onset() < inEndTime)
{
theChangeTimes.insert(inTestSequence[i].onset()-inDelay);
}
if (inTestSequence[i].offset() >= inStartTime && inTestSequence[i].offset() < inEndTime)
{
theChangeTimes.insert(inTestSequence[i].offset()-inDelay);
}
}
//add test length to change times
theChangeTimes.insert(inStartTime);
theChangeTimes.insert(inEndTime);
//run over all the times of chord change
for (std::set<double>::iterator i = ++theChangeTimes.begin(); i != theChangeTimes.end(); ++i)
{
thePrevTime = theCurTime;
theCurTime = *i;
theSegmentLength = theCurTime - thePrevTime;
//advance chord segments until in the first segment that ends after the current time
//or until in the last chord segment
while (theRefIndex+1 < inRefSequence.size() && inRefSequence[theRefIndex].offset() < theCurTime)
{
theRefIndex++;
}
while (theTestIndex+1 < inTestSequence.size() && inTestSequence[theTestIndex].offset()-inDelay < theCurTime)
{
theTestIndex++;
}
/***********************************/
/* Classification of ended segment */
/***********************************/
T theRefLabel;
double theRefDuration;
//label in reference
if (theCurTime <= theRefEndTime && theCurTime > inRefSequence[theRefIndex].onset())
{
theRefLabel = inRefSequence[theRefIndex].label();
theRefDuration = std::min(inRefSequence[theRefIndex].offset(), inEndTime) - std::max(inRefSequence[theRefIndex].onset(), inStartTime);
}
//no label in reference
else
{
theRefLabel = T::silence();
if (theRefIndex > 0 && inRefSequence[theRefIndex-1].offset() >= inStartTime)
{
theRefDuration = std::min(inRefSequence[theRefIndex].onset(), inEndTime) - inRefSequence[theRefIndex-1].offset();
}
else
{
theRefDuration = std::min(inRefSequence[theRefIndex].onset(), inEndTime) - inStartTime;
}
}
T theTestLabel;
//label in test
if (theCurTime <= theTestEndTime && theCurTime > inTestSequence[theTestIndex].onset()-inDelay)
{
theTestLabel = inTestSequence[theTestIndex].label();
}
//no label in test
else
{
theTestLabel = T::silence();
}
if (theRefDuration >= inMinRefDuration && theRefDuration <= inMaxRefDuration)
{
double theSegmentScore = m_SimilarityScore->score(theRefLabel, theTestLabel);
// NemaEval implementation errors recreation
// if (theCurTime > theTestEndTime || theCurTime <= inTestSequence[theTestIndex].onset()-inDelay || theCurTime <= inRefSequence[theRefIndex].onset())
// {
// theScore = 0.;
// }
if (theSegmentScore >= 0)
{
curConfusionMatrix(m_SimilarityScore->getRefIndex(), m_SimilarityScore->getTestIndex()) += theSegmentLength;
curScore += theSegmentScore * theSegmentLength;
}
/******************/
/* Verbose output */
/******************/
if (inVerboseOStream.good())
{
printVerboseOutput(inVerboseOStream, thePrevTime, theCurTime, theRefLabel, theTestLabel, m_SimilarityScore->getMappedRefLabel(), m_SimilarityScore->getMappedTestLabel(), theSegmentScore, theSegmentLength);
}
}
}
m_Durations.push_back(curConfusionMatrix.sum());
const double& curDuration = m_Durations.back();
if (curDuration > 0.)
{
curScore /= curDuration;
}
}
template <typename T>
const std::vector<T>& PairwiseEvaluation<T>::getLabels() const
{
return m_SimilarityScore->getLabels();
}
template <typename T>
const Eigen::ArrayXXd::Index PairwiseEvaluation<T>::getNumOfRefLabels() const
{
return m_NumOfRefLabels;
}
template <typename T>
const Eigen::ArrayXXd::Index PairwiseEvaluation<T>::getNumOfTestLabels() const
{
return m_NumOfTestLabels;
}
template <typename T>
const double PairwiseEvaluation<T>::getDuration() const
{
return m_Durations.back();
}
template <typename T>
const double PairwiseEvaluation<T>::getScore() const
{
return m_Scores.back();
}
template <typename T>
const Eigen::ArrayXXd& PairwiseEvaluation<T>::getConfusionMatrix() const
{
return m_ConfusionMatrices.back();
}
template <typename T>
const double PairwiseEvaluation<T>::calcTotalDuration() const
{
return std::accumulate(m_Durations.begin(), m_Durations.end(), 0.);
}
template <typename T>
const Eigen::ArrayXXd PairwiseEvaluation<T>::calcTotalConfusionMatrix() const
{
Eigen::ArrayXXd retTotalConfusionMatrix(Eigen::ArrayXXd::Zero(m_NumOfRefLabels, m_NumOfTestLabels));
for (std::vector<Eigen::ArrayXXd>::const_iterator iConfMat = m_ConfusionMatrices.begin(); iConfMat !=m_ConfusionMatrices.end(); ++iConfMat)
{
retTotalConfusionMatrix += *iConfMat;
}
return retTotalConfusionMatrix;
}
template <typename T>
const double PairwiseEvaluation<T>::calcAverageScore() const
{
return std::accumulate(m_Scores.begin(), m_Scores.end(), 0.) / static_cast<double>(m_Scores.size());
}
template <typename T>
const double PairwiseEvaluation<T>::calcWeightedAverageScore() const
{
return std::inner_product(m_Durations.begin(), m_Durations.end(), m_Scores.begin(), 0.) / calcTotalDuration();
}
#endif // #ifndef PairwiseEvaluation_h