CVB++ 14.0
classification_test_result.hpp
1#pragma once
2
3#include "../_cexports/c_polimago.h"
4
5#include "../global.hpp"
6#include "../string.hpp"
7#include "test_result_base.hpp"
8
9#include <vector>
10#include <memory>
11
12namespace Cvb
13{
14CVB_BEGIN_INLINE_NS
15
17namespace Polimago
18{
20namespace Testing
21{
22
24
27{
28private:
29 ClassificationTestResult (ReleaseObjectGuard&& guard)
30 : TestResultBase (std::move (guard))
31 {
32 auto numClasses = NumClasses ();
33 for (decltype(numClasses) i = 0; i < numClasses; ++i)
34 {
35 const int classPredClassMaxLen = 256;
36 Char lbl[classPredClassMaxLen];
37 CVB_CALL_CAPI_CHECKED(PMGetTestResultClassLabelTyped(Handle(), i, lbl));
38 classes_.push_back (lbl);
39 }
40
41 auto numExamples = NumExamples ();
42 for (decltype(numExamples) i = 0; i < numExamples; ++i)
43 {
44 auto confidence = CVB_CALL_CAPI(PMGetTestResultConfidence(Handle(), i));
45 exampleConfidences_.push_back (confidence);
46 }
47
48 for (decltype(numExamples) i = 0; i < numExamples; ++i)
49 {
50 std::vector<double> confidenceDistribution;
51 for (decltype(numClasses) j = 0; j < numClasses; ++j)
52 {
53 auto confidenceDistributionValue = CVB_CALL_CAPI(PMGetTestResultConfidenceDistribution(Handle(), i, j));
54 confidenceDistribution.push_back (confidenceDistributionValue);
55 }
56 exampleConfidenceDistributions_.push_back (confidenceDistribution);
57 }
58
59 for (decltype(numExamples) i = 0; i < numExamples; ++i)
60 {
61 auto index = CVB_CALL_CAPI(PMGetTestResultTrueClassIndex(Handle(), i));
62 trueClassIndices_.push_back (index);
63 }
64
65 for (decltype(numExamples) i = 0; i < numExamples; ++i)
66 {
67 auto index = CVB_CALL_CAPI(PMGetTestResultPredictedClass(Handle(), i));
68 predictedClassIndices_.push_back (index);
69 }
70 }
71
72 std::string ObjectName () const override
73 {
74 return thisObjectName_;
75 }
76
77public:
79
85 : ClassificationTestResult (ReleaseObjectGuard(LoadInternal (fileName)))
86 {
87 if (NumClasses() == 0)
88 {
89 throw std::runtime_error ("Loading the ClassificationTestResult failed");
90 }
91 }
92
95
97 ClassificationTestResult& operator=(ClassificationTestResult&&) noexcept = default;
98
99 virtual ~ClassificationTestResult () {};
100
102
109 static std::unique_ptr<ClassificationTestResult> FromHandle (ReleaseObjectGuard&& guard)
110 {
111 if (!guard.Handle ())
112 {
113 throw std::invalid_argument ("invalid classification test result native handle");
114 }
116 }
117
119
125 {
127 }
128
130
134 int NumClasses() const
135 {
136 return static_cast<int>(CVB_CALL_CAPI (PMGetTestResultNumClasses(Handle())));
137 }
138
140
144 int OutputDimension() const
145 {
146 return static_cast<int>(CVB_CALL_CAPI (PMGetTestResultOutputDimension(Handle())));
147 }
148
150
154 int NumErrors() const
155 {
156 return static_cast<int>(CVB_CALL_CAPI (PMGetTestResultNumErrors(Handle())));
157 }
158
160
164 double ErrorRate() const
165 {
166 return static_cast<int>(CVB_CALL_CAPI (PMGetTestResultErrorRate(Handle())));
167 }
168
170
175 {
176 return classes_;
177 }
178
180
185 {
186 return exampleConfidences_;
187 }
188
190
195 {
196 return exampleConfidenceDistributions_;
197 }
198
200
205 {
206 return trueClassIndices_;
207 }
208
210
215 {
216 return predictedClassIndices_;
217 }
218
219private:
220 std::vector<String> classes_;
221 std::vector<double> exampleConfidences_;
222 std::vector<std::vector<double>> exampleConfidenceDistributions_;
223 std::vector<int> trueClassIndices_;
224 std::vector<int> predictedClassIndices_;
225 const std::string thisObjectName_ = "Polimago Classification Test Resul";
226};
227
230
231
232} /* namespace Testing */
233
236
237} /* namespace Polimago */
238CVB_END_INLINE_NS
239} /* namespace Cvb */
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
Classification test result object.
Definition: classification_test_result.hpp:27
int NumClasses() const
Number of classes in the sample database on which the test result was calculated.
Definition: classification_test_result.hpp:134
std::vector< double > ExampleConfidences() const
Confidences in the classification decision for each sample.
Definition: classification_test_result.hpp:184
static std::unique_ptr< ClassificationTestResult > Load(const String &fileName)
Load a saved test result from a file.
Definition: classification_test_result.hpp:124
int NumErrors() const
Total number of errors made during classification testing.
Definition: classification_test_result.hpp:154
double ErrorRate() const
Total number of errors divided by the total number of examples.
Definition: classification_test_result.hpp:164
int OutputDimension() const
For tests with usage OneVersusAll the output dimension will equal the number of classes in the traini...
Definition: classification_test_result.hpp:144
std::vector< std::vector< double > > ExampleConfidenceDistributions() const
Confidence distributions for each example.
Definition: classification_test_result.hpp:194
std::vector< String > Classes() const
Class labels available in this test result.
Definition: classification_test_result.hpp:174
std::vector< int > TrueClassIndices() const
The real class index for each example.
Definition: classification_test_result.hpp:204
ClassificationTestResult(ClassificationTestResult &&) noexcept=default
Move constructor.
std::vector< int > PredictedClassIndices() const
Prediction results for all indices.
Definition: classification_test_result.hpp:214
ClassificationTestResult(const String &fileName)
Load a saved classification test result from a file.
Definition: classification_test_result.hpp:84
static std::unique_ptr< ClassificationTestResult > FromHandle(ReleaseObjectGuard &&guard)
Creates test result from a classic API handle.
Definition: classification_test_result.hpp:109
Base class for all Polimago Test Results.
Definition: test_result_base.hpp:26
int NumExamples() const
Number of examples that contributed to this test result.
Definition: test_result_base.hpp:67
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
char Char
Character type for wide characters or unicode characters.
Definition: string.hpp:59