-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSegmentationEvaluation.cpp
296 lines (262 loc) · 11.2 KB
/
SegmentationEvaluation.cpp
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
291
292
293
294
295
//============================================================================
/**
Implementation file for SegmentationEvaluation.h
@author Johan Pauwels
@date 20131009
*/
//============================================================================
// Includes
#include <numeric>
#include <functional>
#include <stdexcept>
#include "SegmentationEvaluation.h"
SegmentationEvaluation::SegmentationEvaluation(const std::string& inVariant, const std::vector<double>& inTolerances)
: m_Variant(inVariant)
, m_Tolerances(inTolerances)
{
}
void SegmentationEvaluation::reset()
{
m_Durations.clear();
m_NumRefSegments.clear();
m_NumTestSegments.clear();
m_Recalls.clear();
m_Precisions.clear();
m_Fmeasures.clear();
m_MissedBoundaries.clear();
m_SegmentFragmentations.clear();
m_CombinedHammingMeasureWorst.clear();
m_CombinedHammingMeasureHarmonic.clear();
}
void SegmentationEvaluation::addSequencePair(const LabelSequence& inRefSequence, const LabelSequence& inTestSequence)
{
// class Onsetor //onset functor
// {
// void operator()(const MusOO::TimedLabel<std::string>& tl) {tl.onset();}
// };
Eigen::ArrayXd refOnsets(inRefSequence.size());
std::transform(inRefSequence.begin(), inRefSequence.end(), &refOnsets[0], std::mem_fun_ref<const double&, MusOO::TimedLabel<std::string> >(&MusOO::TimedLabel<std::string>::onset));
Eigen::ArrayXd refOffsets(inRefSequence.size());
std::transform(inRefSequence.begin(), inRefSequence.end(), &refOffsets[0], std::mem_fun_ref<const double&, MusOO::TimedLabel<std::string> >(&MusOO::TimedLabel<std::string>::offset));
Eigen::ArrayXd testOnsets(inTestSequence.size());
std::transform(inTestSequence.begin(), inTestSequence.end(), &testOnsets[0], std::mem_fun_ref<const double&, MusOO::TimedLabel<std::string> >(&MusOO::TimedLabel<std::string>::onset));
Eigen::ArrayXd testOffsets(inTestSequence.size());
std::transform(inTestSequence.begin(), inTestSequence.end(), &testOffsets[0], std::mem_fun_ref<const double&, MusOO::TimedLabel<std::string> >(&MusOO::TimedLabel<std::string>::offset));
m_Durations.push_back(refOffsets[refOffsets.size()-1]);
Eigen::ArrayXd refBoundaries;
Eigen::ArrayXd testBoundaries;
if (m_Variant == "Onset")
{
refBoundaries = refOnsets;
testBoundaries = testOnsets;
}
else if (m_Variant == "Offset")
{
refBoundaries = refOffsets;
testBoundaries = testOffsets;
}
else if (m_Variant == "Inner")
{
if (refOnsets.size() > 0)
{
refBoundaries = refOnsets.tail(refOnsets.size()-1);
}
if (testOnsets.size() > 0)
{
testBoundaries = testOnsets.tail(testOnsets.size()-1);
}
}
else if (m_Variant == "Outer")
{
if (refOffsets.size() > 0)
{
refBoundaries.resize(refOnsets.size()+1);
refBoundaries << refOnsets, refOffsets.tail<1>();
}
else
{
refBoundaries = refOnsets;
}
if (testOffsets.size() > 0)
{
testBoundaries.resize(testOnsets.size()+1);
testBoundaries << testOnsets, testOffsets.tail<1>();
}
else
{
testBoundaries = testOnsets;
}
}
else
{
throw std::runtime_error("Unknown segmentation variant '" + m_Variant + "'");
}
m_NumRefSegments.push_back(refOnsets.size());
m_NumTestSegments.push_back(testOnsets.size());
Eigen::ArrayXd recalls;
Eigen::ArrayXd precisions;
segmentationScores(refBoundaries, testBoundaries, recalls, precisions);
m_Recalls.push_back(recalls);
m_Precisions.push_back(precisions);
m_Fmeasures.push_back(2 * precisions * recalls / (precisions + recalls));
m_MissedBoundaries.push_back(missedBoundaries(refOnsets, refOffsets, testOnsets, testOffsets));
m_SegmentFragmentations.push_back(segmentFragmentation(refOnsets, refOffsets, testOnsets, testOffsets));
m_CombinedHammingMeasureWorst.push_back(1. - std::max(m_MissedBoundaries.back(), m_SegmentFragmentations.back()));
m_CombinedHammingMeasureHarmonic.push_back(2./(1./getUnderSegmentation() + 1./getOverSegmentation()));
}
const double SegmentationEvaluation::getDuration() const
{
return m_Durations.back();
}
const Eigen::ArrayXd::Index SegmentationEvaluation::getNumRefSegments() const
{
return m_NumRefSegments.back();
}
const Eigen::ArrayXd::Index SegmentationEvaluation::getNumTestSegments() const
{
return m_NumTestSegments.back();
}
const double SegmentationEvaluation::getUnderSegmentation() const
{
return 1. - m_MissedBoundaries.back();
}
const double SegmentationEvaluation::getOverSegmentation() const
{
return 1. - m_SegmentFragmentations.back();
}
const double SegmentationEvaluation::getCombinedHammingMeasureWorst() const
{
return m_CombinedHammingMeasureWorst.back();
}
const double SegmentationEvaluation::getCombinedHammingMeasureHarmonic() const
{
return m_CombinedHammingMeasureHarmonic.back();
}
const double SegmentationEvaluation::calcTotalDuration() const
{
return std::accumulate(m_Durations.begin(), m_Durations.end(), 0.);
}
const double SegmentationEvaluation::calcAverageNumRefSegments() const
{
return std::accumulate(m_NumRefSegments.begin(), m_NumRefSegments.end(), 0.) / static_cast<double>(m_NumRefSegments.size());
}
const double SegmentationEvaluation::calcAverageNumTestSegments() const
{
return std::accumulate(m_NumTestSegments.begin(), m_NumTestSegments.end(), 0.) / static_cast<double>(m_NumTestSegments.size());
}
const double SegmentationEvaluation::calcAverageSegmentsRatio() const
{
return std::inner_product(m_NumTestSegments.begin(), m_NumTestSegments.end(), m_NumRefSegments.begin(), 0., std::plus<double>(), std::divides<double>()) / static_cast<double>(m_NumTestSegments.size());
}
const double SegmentationEvaluation::calcAverageUnderSegmentation() const
{
return std::accumulate(m_MissedBoundaries.begin(), m_MissedBoundaries.end(), static_cast<double>(m_MissedBoundaries.size()), std::minus<double>()) / m_MissedBoundaries.size();
}
const double SegmentationEvaluation::calcWeightedAverageUnderSegmentation() const
{
double totalDuration = calcTotalDuration();
return std::inner_product(m_Durations.begin(), m_Durations.end(), m_MissedBoundaries.begin(), totalDuration, std::minus<double>(), std::multiplies<double>()) / totalDuration;
}
const double SegmentationEvaluation::calcAverageOverSegmentation() const
{
return std::accumulate(m_SegmentFragmentations.begin(), m_SegmentFragmentations.end(), static_cast<double>(m_SegmentFragmentations.size()), std::minus<double>()) / m_SegmentFragmentations.size();
}
const double SegmentationEvaluation::calcWeightedAverageOverSegmentation() const
{
double totalDuration = calcTotalDuration();
return std::inner_product(m_Durations.begin(), m_Durations.end(), m_SegmentFragmentations.begin(), totalDuration, std::minus<double>(), std::multiplies<double>()) / totalDuration;
}
const double SegmentationEvaluation::calcAverageCombinedHammingMeasureWorst() const
{
return std::accumulate(m_CombinedHammingMeasureWorst.begin(), m_CombinedHammingMeasureWorst.end(), 0.) / m_CombinedHammingMeasureWorst.size();
}
const double SegmentationEvaluation::calcWeightedAverageCombinedHammingMeasureWorst() const
{
return std::inner_product(m_Durations.begin(), m_Durations.end(), m_CombinedHammingMeasureWorst.begin(), 0.) / calcTotalDuration();
}
const double SegmentationEvaluation::calcAverageCombinedHammingMeasureHarmonic() const
{
return std::accumulate(m_CombinedHammingMeasureHarmonic.begin(), m_CombinedHammingMeasureHarmonic.end(), 0.) / m_CombinedHammingMeasureHarmonic.size();
}
const double SegmentationEvaluation::calcWeightedAverageCombinedHammingMeasureHarmonic() const
{
return std::inner_product(m_Durations.begin(), m_Durations.end(), m_CombinedHammingMeasureHarmonic.begin(), 0.) / calcTotalDuration();
}
void SegmentationEvaluation::segmentationScores(const Eigen::ArrayXd& inRefTimeStamps, const Eigen::ArrayXd& inTestTimeStamps, Eigen::ArrayXd& outRecalls, Eigen::ArrayXd& outPrecisions)
{
outRecalls = Eigen::ArrayXd::Zero(m_Tolerances.size());
outPrecisions = Eigen::ArrayXd::Zero(m_Tolerances.size());
if (inRefTimeStamps.size() != 0 && inTestTimeStamps.size() != 0)
{
for (Eigen::ArrayXd::Index iRefTime = 0; iRefTime < inRefTimeStamps.size(); ++iRefTime)
{
double minDist = (inRefTimeStamps(iRefTime)-inTestTimeStamps).abs().minCoeff();
for (size_t iTolerance = 0; iTolerance < m_Tolerances.size(); ++iTolerance)
{
if (minDist < m_Tolerances[iTolerance])
{
++outRecalls[iTolerance];
}
}
}
outRecalls /= inRefTimeStamps.size();
for (Eigen::ArrayXd::Index iTestTime = 0; iTestTime < inTestTimeStamps.size(); ++iTestTime)
{
double minDist = (inTestTimeStamps(iTestTime)-inRefTimeStamps).abs().minCoeff();
for (size_t iTolerance = 0; iTolerance < m_Tolerances.size(); ++iTolerance)
{
if (minDist < m_Tolerances[iTolerance])
{
++outPrecisions[iTolerance];
}
}
}
outPrecisions /= inTestTimeStamps.size();
}
}
const double SegmentationEvaluation::missedBoundaries(const Eigen::ArrayXd& inRefOnsets, const Eigen::ArrayXd& inRefOffsets,
const Eigen::ArrayXd& inTestOnsets, const Eigen::ArrayXd& inTestOffsets)
{
if (inTestOnsets.size() != 0)
{
// directional Hamming distance / reference track duration
double dirHammingDist = 0.;
for (Eigen::ArrayXd::Index iTestLabel = 0; iTestLabel < inTestOnsets.size(); ++iTestLabel)
{
Eigen::ArrayXd refOverlap = inRefOffsets.min(inTestOffsets(iTestLabel)) - inRefOnsets.max(inTestOnsets(iTestLabel));
double maxOverlap = refOverlap.maxCoeff();
if (maxOverlap > 0)
{
dirHammingDist += (refOverlap > 0).select(refOverlap, 0).sum() - maxOverlap;
}
}
return dirHammingDist / inRefOffsets(inRefOffsets.size()-1);
}
else
{
return 1;
}
}
const double SegmentationEvaluation::segmentFragmentation(const Eigen::ArrayXd& inRefOnsets, const Eigen::ArrayXd& inRefOffsets,
const Eigen::ArrayXd& inTestOnsets, const Eigen::ArrayXd& inTestOffsets)
{
if (inRefOnsets.size() != 0)
{
// inverse directional Hamming distance / reference track duration
double invDirHammingDist = 0.;
for (Eigen::ArrayXd::Index iRefLabel = 0; iRefLabel < inRefOnsets.size(); ++iRefLabel)
{
Eigen::ArrayXd testOverlap = inTestOffsets.min(inRefOffsets(iRefLabel)) - inTestOnsets.max(inRefOnsets(iRefLabel));
double maxOverlap = testOverlap.maxCoeff();
if (maxOverlap > 0)
{
invDirHammingDist += (testOverlap > 0).select(testOverlap, 0).sum() - maxOverlap;
}
}
return invDirHammingDist / inRefOffsets(inRefOffsets.size()-1);
}
else
{
return 1;
}
}