Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkLevenbergMarquardtModelFitFunctorTest.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 
16 #include "itkImage.h"
17 #include "itkImageRegionIterator.h"
18 
20 
21 #include "mitkLinearModel.h"
22 
23 int mitkLevenbergMarquardtModelFitFunctorTest(int /*argc*/, char*[] /*argv[]*/)
24 {
25  // always start with this!
26  MITK_TEST_BEGIN("LevenbergMarquardtModelFitFunctor")
27 
28  //Prepare test artifacts and helper
29 
31 
32  typedef std::vector<double> ValueArrayType;
33 
34  ValueArrayType sample1(10);
35  ValueArrayType sample2(10);
36 
37  for (int i = 0; i < 10; ++i)
38  {
39  grid[i] = i;
40  sample1[i] = 5 * i;
41  sample2[i] = 2 * i + 10;
42  }
43 
45  model->SetTimeGrid(grid);
46 
48  initParams.SetSize(2);
49  initParams.Fill(0.0);
50 
51 
54 
55  //Test functor for sample1
56 
57  MITK_TEST_FOR_EXCEPTION(::itk::ExceptionObject, testFunctor->GetNumberOfOutputs(nullptr));
58 
59  CPPUNIT_ASSERT_MESSAGE("Check number of outputs with model set.", 4 == testFunctor->GetNumberOfOutputs(model));
60 
61  ValueArrayType output = testFunctor->Compute(sample1, model, initParams);
62 
63  CPPUNIT_ASSERT_MESSAGE("Check number of values in functor output.", 4 == output.size());
64 
65  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(5, output[0], 1e-6, true) == true,
66  "Check fitted parameter 1 (slope) for sample 1.");
67  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(0, output[1], 1e-6, true) == true,
68  "Check fitted parameter 2 (offset) for sample 1.");
69  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(0, output[2], 1e-6, true) == true,
70  "Check derived parameter 1 (x-intercept) for sample 1.");
71 
72  //Test functor for sample2
73  output = testFunctor->Compute(sample2, model, initParams);
74 
75  CPPUNIT_ASSERT_MESSAGE("Check number of values in functor output.", 4 == output.size());
76 
77  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(2, output[0], 1e-6, true) == true,
78  "Check fitted parameter 1 (slope) for sample 2.");
79  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(10, output[1], 1e-6, true) == true,
80  "Check fitted parameter 2 (offset) for sample 2.")
81  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(-5, output[2], 1e-6, true) == true,
82  "Check derived parameter 1 (x-intercept) for sample 2.");
83 
85 }
ModelTraitsInterface::ParametersType ParametersType
Definition: mitkModelBase.h:59
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
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. ...
static Pointer New()
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
int mitkLevenbergMarquardtModelFitFunctorTest(int, char *[])
and MITK_TEST_END()