Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkMVConstrainedCostFunctionDecoratorTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include <iostream>
14 #include "mitkTestingMacros.h"
15 
18 #include "mitkLinearModel.h"
19 
20 class TestCostFunction : public mitk::MVModelFitCostFunction
21 {
22 public:
23 
24  typedef TestCostFunction Self;
28 
29  itkNewMacro(Self);
30 
32 
33  mutable unsigned int m_calls;
34 
35 protected:
36 
37  MeasureType CalcMeasure(const ParametersType &parameters, const SignalType& signal) const override
38  {
39  MeasureType result = signal;
40  result[0] = parameters[0];
41  result[1] = parameters[1];
42  ++m_calls;
43  return result;
44  };
45 
46  TestCostFunction()
47  {
48  m_calls = 0;
49  }
50 
51  ~TestCostFunction() override{}
52 };
53 
54 int mitkMVConstrainedCostFunctionDecoratorTest(int /*argc*/, char*[] /*argv[]*/)
55 {
56  MITK_TEST_BEGIN("mitkMVConstrainedCostFunctionDecoratorTest")
57 
60  TestCostFunction::Pointer innerCF = TestCostFunction::New();
62  decorator->SetModel(model);
63  innerCF->SetModel(model);
64 
66  signal.Fill(0.0);
67  decorator->SetSample(signal);
68  innerCF->SetSample(signal);
69 
71  grid[0] = 0;
72  grid[1] = 1;
73  grid[2] = 2;
74  grid[3] = 3;
75  grid[4] = 4;
76 
77  model->SetTimeGrid(grid);
78 
80  p1[0] = 0;
81  p1[1] = 50;
83  p2[0] = 10;
84  p2[1] = 50;
86  p3[0] = 100;
87  p3[1] = 50;
89  p4[0] = 2;
90  p4[1] = 50;
91 
92  checker->SetLowerBarrier(0,1,4);
93  checker->SetUpperBarrier(0,100);
94 
95  double defaultMaxPenalty = 1e15;
96 
98  //Tests
99 
100  //check freshly created checker;
101  MITK_TEST_CONDITION_REQUIRED(decorator->GetWrappedCostFunction() == nullptr,
102  "Testing GetWrappedCostFunction for new decorator.");
103  MITK_TEST_CONDITION_REQUIRED(decorator->GetConstraintChecker() == nullptr,
104  "Testing GetWrappedCostFunction for new decorator.");
105  MITK_TEST_CONDITION_REQUIRED(decorator->GetFailureThreshold() == defaultMaxPenalty,
106  "Testing GetWrappedCostFunction for new decorator.");
107 
108  MITK_TEST_FOR_EXCEPTION(mitk::Exception, decorator->GetValue(p1));
109  decorator->SetWrappedCostFunction(innerCF);
110  MITK_TEST_FOR_EXCEPTION(mitk::Exception, decorator->GetValue(p1));
111  decorator->SetWrappedCostFunction(nullptr);
112  decorator->SetConstraintChecker(checker);
113  MITK_TEST_FOR_EXCEPTION(mitk::Exception, decorator->GetValue(p1));
114  decorator->SetWrappedCostFunction(innerCF);
115 
116 
117  mitk::MVModelFitCostFunction::MeasureType measure = decorator->GetValue(p1);
118  MITK_TEST_CONDITION_REQUIRED(measure[0] == defaultMaxPenalty, "Testing measure 1 with parameters p1.");
119  MITK_TEST_CONDITION_REQUIRED(measure[1] == defaultMaxPenalty, "Testing measure 2 with parameters p1.");
120  MITK_TEST_CONDITION_REQUIRED(measure[2] == defaultMaxPenalty, "Testing measure 3 with parameters p1.");
121  MITK_TEST_CONDITION_REQUIRED(measure[3] == defaultMaxPenalty, "Testing measure 3 with parameters p1.");
122  MITK_TEST_CONDITION_REQUIRED(measure[4] == defaultMaxPenalty, "Testing measure 3 with parameters p1.");
123  MITK_TEST_CONDITION_REQUIRED(innerCF->m_calls == 0, "Checking calls with parameters p1.");
124 
125  measure = decorator->GetValue(p2);
126  MITK_TEST_CONDITION_REQUIRED(measure[0] == 10, "Testing measure 1 with parameters p2.");
127  MITK_TEST_CONDITION_REQUIRED(measure[1] == 50, "Testing measure 2 with parameters p2.");
128  MITK_TEST_CONDITION_REQUIRED(measure[2] == 70, "Testing measure 3 with parameters p2.");
129  MITK_TEST_CONDITION_REQUIRED(innerCF->m_calls == 1, "Checking calls with parameters p2.");
130 
131  measure = decorator->GetValue(p3);
132  MITK_TEST_CONDITION_REQUIRED(measure[0] == defaultMaxPenalty, "Testing measure 1 with parameters p3.");
133  MITK_TEST_CONDITION_REQUIRED(measure[1] == defaultMaxPenalty, "Testing measure 2 with parameters p3.");
134  MITK_TEST_CONDITION_REQUIRED(measure[2] == defaultMaxPenalty, "Testing measure 3 with parameters p3.");
135  MITK_TEST_CONDITION_REQUIRED(innerCF->m_calls == 1, "Checking calls with parameters p3.");
136 
137  decorator->SetActivateFailureThreshold(false);
138  measure = decorator->GetValue(p3);
139  MITK_TEST_CONDITION_REQUIRED(measure[0] == 100+defaultMaxPenalty, "Testing measure 1 with parameters p3 (deactiveated threshold).");
140  MITK_TEST_CONDITION_REQUIRED(measure[1] == 50+defaultMaxPenalty, "Testing measure 2 with parameters p3 (deactiveated threshold).");
141  MITK_TEST_CONDITION_REQUIRED(measure[2] == 250+defaultMaxPenalty, "Testing measure 3 with parameters p3 (deactiveated threshold).");
142  MITK_TEST_CONDITION_REQUIRED(innerCF->m_calls ==2, "Checking calls with parameters p3 (deactiveated threshold).");
143  decorator->SetActivateFailureThreshold(true);
144 
145  measure = decorator->GetValue(p4);
146  MITK_TEST_CONDITION_REQUIRED(measure[0] == 2+(-1*log(1/4.)), "Testing measure 1 with parameters p4.");
147  MITK_TEST_CONDITION_REQUIRED(measure[1] == 50+(-1*log(1/4.)), "Testing measure 2 with parameters p4.");
148  MITK_TEST_CONDITION_REQUIRED(measure[2] == 54+(-1*log(1/4.)), "Testing measure 3 with parameters p4.");
149  MITK_TEST_CONDITION_REQUIRED(innerCF->m_calls == 3, "Checking calls with parameters p4.");
150 
151 
152  MITK_TEST_END()
153 }
itk::SmartPointer< const Self > ConstPointer
ModelFitCostFunctionInterface::SignalType SignalType
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
int mitkMVConstrainedCostFunctionDecoratorTest(int, char *[])
itk::Array< double > TimeGridType
Definition: mitkModelBase.h:62
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define MITK_TEST_FOR_EXCEPTION(EXCEPTIONCLASS, STATEMENT)
Simplified version of MITK_TEST_FOR_EXCEPTION_BEGIN / END for a single statement. ...
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
static Pointer New()
itk::MultipleValuedCostFunction Superclass
virtual MeasureType CalcMeasure(const ParametersType &parameters, const SignalType &signal) const =0
and MITK_TEST_END()