Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
mitkModelTestFixture.h
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 #ifndef MITKMODELTESTFIXTURE_H
14 #define MITKMODELTESTFIXTURE_H
15 
16 
17 #include "mitkTestingMacros.h"
18 #include <mitkTestingConfig.h>
19 #include "mitkTestFixture.h"
20 #include <itksys/SystemTools.hxx>
21 #include <mitkModelBase.h>
22 #include "mitkModelFitException.h"
23 
24 #include <nlohmann/json.hpp>
25 #include <iostream>
26 #include <fstream>
27 #include "mitkVector.h"
28 
30 
31 namespace mitk
32 {
34  {
35  public:
36  static json ParseJSONFile(const std::string path)
37  {
38  std::string pathToFile = GetTestDataFilePath(path);
39  std::ifstream file(pathToFile);
40  json json_obj;
41  if (!file.is_open())
42  {
43  MITK_ERROR << "Could not open \"" << pathToFile << "\"!";
44  }
45  try
46  {
47  json_obj = nlohmann::json::parse(file);
48  }
49  catch (const json::exception& e)
50  {
51  MITK_ERROR << "Could not parse JSON file!";
52  }
53  return json_obj;
54  }
55 
56  static ModelBase::ParametersType ParseTestParameters(const json modelValues_json_obj)
57  {
58  ModelBase::ParametersType testparameters;
59  testparameters.SetSize(modelValues_json_obj["modelParameterValues"].size());
60  for (unsigned long i = 0; i < modelValues_json_obj["modelParameterValues"].size(); ++i)
61  {
62  testparameters[i] = modelValues_json_obj["modelParameterValues"][i];
63  }
64  return testparameters;
65  }
66 
67  static ModelBase::StaticParameterMapType ParseStaticParameters(const json profile_json_obj, const json modelValues_json_obj)
68  {
69  ModelBase::StaticParameterMapType staticParameterMap;
70  for (unsigned long i = 0; i < modelValues_json_obj["staticParameterValues"].size(); ++i)
71  {
72  ModelBase::StaticParameterValuesType staticParameterValues;
73  ModelBase::ParameterNameType staticParameterName;
74  staticParameterName = profile_json_obj["staticParameterNames"][i];
75  for (unsigned int j = 0; j < modelValues_json_obj["staticParameterValues"][i].size(); ++j)
76  {
77  staticParameterValues.push_back(modelValues_json_obj["staticParameterValues"][i][j]);
78  }
79  staticParameterMap.insert(std::make_pair(staticParameterName, staticParameterValues));
80  }
81  return staticParameterMap;
82  }
83 
84  static void SetStaticParametersForTest(mitk::ModelBase::Pointer testmodel, const json profile_json_obj, const json modelValues_json_obj)
85  {
86  mitk::ModelBase::StaticParameterMapType staticParameterMap;
87  staticParameterMap = ParseStaticParameters(profile_json_obj, modelValues_json_obj);
88  testmodel->SetStaticParameters(staticParameterMap);
89  }
90 
91  static void CompareModelAndReferenceProfile(const mitk::ModelBase::Pointer testmodel, const json profile_json_obj)
92  {
93  CPPUNIT_ASSERT_MESSAGE("Checking number of parameters in model.", testmodel->GetNumberOfParameters() == profile_json_obj["numberOfParameters"]);
94  for (unsigned long i = 0; i < profile_json_obj["numberOfParameters"]; i++)
95  {
96  CPPUNIT_ASSERT_MESSAGE("Checking parameter names.", testmodel->GetParameterNames()[i] == profile_json_obj["parameterNames"][i]);
97  CPPUNIT_ASSERT_MESSAGE("Checking parameter scales.", testmodel->GetParameterScales()[testmodel->GetParameterNames()[i]] == profile_json_obj["parameterScales"][i]);
98  CPPUNIT_ASSERT_MESSAGE("Checking parameter units.", testmodel->GetParameterUnits()[testmodel->GetParameterNames()[i]] == profile_json_obj["parameterUnits"][i]);
99  }
100  CPPUNIT_ASSERT_MESSAGE("Checking number of derived parameters in model.", testmodel->GetNumberOfDerivedParameters() == profile_json_obj["numberOfDerivedParameters"]);
101  for (unsigned long i = 0; i < profile_json_obj["numberOfDerivedParameters"]; i++)
102  {
103  CPPUNIT_ASSERT_MESSAGE("Checking derived parameter names.", testmodel->GetDerivedParameterNames()[i] == profile_json_obj["derivedParameterNames"][i]);
104  CPPUNIT_ASSERT_MESSAGE("Checking derived parameter scales.", testmodel->GetDerivedParameterScales()[testmodel->GetDerivedParameterNames()[i]] == profile_json_obj["derivedParameterScales"][i]);
105  CPPUNIT_ASSERT_MESSAGE("Checking derived parameter units.", testmodel->GetDerivedParameterUnits()[testmodel->GetDerivedParameterNames()[i]] == profile_json_obj["derivedParameterUnits"][i]);
106  }
107  CPPUNIT_ASSERT_MESSAGE("Checking number of static parameters in model.", testmodel->GetNumberOfStaticParameters() == profile_json_obj["numberOfStaticParameters"]);
108  for (unsigned long i = 0; i < profile_json_obj["numberOfStaticParameters"]; i++)
109  {
110  CPPUNIT_ASSERT_MESSAGE("Checking static parameter names.", testmodel->GetStaticParameterNames()[i] == profile_json_obj["staticParameterNames"][i]);
111  CPPUNIT_ASSERT_MESSAGE("Checking static parameter units.", testmodel->GetStaticParameterUnits()[testmodel->GetStaticParameterNames()[i]] == profile_json_obj["staticParameterUnits"][i]);
112  }
113  CPPUNIT_ASSERT_MESSAGE("Checking function string.", testmodel->GetFunctionString() == profile_json_obj["functionString"]);
114  CPPUNIT_ASSERT_MESSAGE("Checking class ID.", testmodel->GetClassID() == profile_json_obj["classID"]);
115  CPPUNIT_ASSERT_MESSAGE("Checking model display name.", testmodel->GetModelDisplayName() == profile_json_obj["modelDisplayName"]);
116  CPPUNIT_ASSERT_MESSAGE("Checking model type.", testmodel->GetModelType() == profile_json_obj["modelType"]);
117  CPPUNIT_ASSERT_MESSAGE("Checking x name.", testmodel->GetXName() == profile_json_obj["xName"]);
118  CPPUNIT_ASSERT_MESSAGE("Checking x axis name.", testmodel->GetXAxisName() == profile_json_obj["xAxisName"]);
119  CPPUNIT_ASSERT_MESSAGE("Checking x axis unit.", testmodel->GetXAxisUnit() == profile_json_obj["xAxisUnit"]);
120  CPPUNIT_ASSERT_MESSAGE("Checking y axis name.", testmodel->GetYAxisName() == profile_json_obj["yAxisName"]);
121  CPPUNIT_ASSERT_MESSAGE("Checking y axis unit.", testmodel->GetYAxisUnit() == profile_json_obj["yAxisUnit"]);
122  }
123 
124  static void CompareModelAndReferenceSignal(mitk::ModelBase::Pointer testmodel, const json modelValues_json_obj, const json profile_json_obj)
125  {
126 
127  for (unsigned int j = 0; j < modelValues_json_obj["modelValues"].size(); j++)
128  {
129  json modelValues_json_obj_current = modelValues_json_obj["modelValues"][j];
130 
131  SetStaticParametersForTest(testmodel, profile_json_obj, modelValues_json_obj_current);
132 
133  // Set time grid
135  timeGrid.SetSize(modelValues_json_obj_current["timeGrid"].size());
136  for (unsigned long i = 0; i < modelValues_json_obj_current["timeGrid"].size(); ++i)
137  {
138  timeGrid[i] = modelValues_json_obj_current["timeGrid"][i];
139  }
140  testmodel->SetTimeGrid(timeGrid);
141 
142  // Parse test parameters
143  mitk::ModelBase::ParametersType testparameters;
144  testparameters = ParseTestParameters(modelValues_json_obj_current);
145 
146  //Generate signal
148  signal = testmodel->GetSignal(testparameters);
149 
150 
151  //ComputeModelfunction is called within GetSignal(), therefore no explicit testing of ComputeModelFunction()
152  // The reference signal has been calculated in a matlab script.
153  std::stringstream ss;
154  ss << "Checking signal for model parameter set " << j << ".";
155  std::string message = ss.str();
156  for (unsigned long i = 0; i < signal.size(); i++)
157  {
158  CPPUNIT_ASSERT_MESSAGE(message, mitk::Equal(signal[i], modelValues_json_obj_current["signal"][i], 1e-6, true) == true);
159  }
160  }
161  }
162 
163  static void CompareModelAndReferenceDerivedParameters(const mitk::ModelBase::Pointer testmodel, json modelValues_json_obj)
164  {
165  for (unsigned int j = 0; j < modelValues_json_obj["modelValues"].size(); j++)
166  {
167  json modelValues_json_obj_current = modelValues_json_obj["modelValues"][j];
168 
169  // Set time grid
171  timeGrid.SetSize(modelValues_json_obj_current["timeGrid"].size());
172  for (unsigned long i = 0; i < modelValues_json_obj_current["timeGrid"].size(); ++i)
173  {
174  timeGrid[i] = modelValues_json_obj_current["timeGrid"][i];
175  }
176  testmodel->SetTimeGrid(timeGrid);
177  mitk::ModelBase::ParametersType testparameters;
178  testparameters = mitk::mitkModelTestFixture::ParseTestParameters(modelValues_json_obj_current);
179  mitk::ModelBase::DerivedParameterMapType derivedParameterMap;
180  derivedParameterMap = testmodel->GetDerivedParameters(testparameters);
181  std::stringstream ss;
182  ss << "Checking derived parameters for model parameter set " << j << ".";
183  std::string message = ss.str();
184  for (unsigned long i = 0; i < modelValues_json_obj_current["derivedParameterValues"].size(); i++)
185  {
186  CPPUNIT_ASSERT_MESSAGE(message, mitk::Equal(derivedParameterMap[testmodel->GetDerivedParameterNames()[i]], modelValues_json_obj_current["derivedParameterValues"][i], 1e-6, true) == true);
187  }
188  }
189  }
190  };
191 }
192 #endif // MITKMODELTESTFIXTURE_H
mitk::TestFixture::GetTestDataFilePath
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
Definition: mitkTestFixture.h:107
mitk::mitkModelTestFixture
Definition: mitkModelTestFixture.h:33
mitk::ModelBase::ModelResultType
ModelTraitsInterface::ModelResultType ModelResultType
Definition: mitkModelBase.h:55
mitk::ModelBase::StaticParameterValuesType
std::vector< StaticParameterValueType > StaticParameterValuesType
Definition: mitkModelBase.h:71
mitk::mitkModelTestFixture::SetStaticParametersForTest
static void SetStaticParametersForTest(mitk::ModelBase::Pointer testmodel, const json profile_json_obj, const json modelValues_json_obj)
Definition: mitkModelTestFixture.h:84
MITK_ERROR
#define MITK_ERROR
Definition: mitkLog.h:211
mitk::ModelBase::ParameterNameType
ModelTraitsInterface::ParameterNameType ParameterNameType
Definition: mitkModelBase.h:63
mitk::ModelBase::ParametersType
ModelTraitsInterface::ParametersType ParametersType
Definition: mitkModelBase.h:59
mitk::Equal
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.
itk::SmartPointer< Self >
mitk::ModelBase::DerivedParameterMapType
std::map< ParameterNameType, DerivedParameterValueType > DerivedParameterMapType
Definition: mitkModelBase.h:75
mitk::mitkModelTestFixture::ParseTestParameters
static ModelBase::ParametersType ParseTestParameters(const json modelValues_json_obj)
Definition: mitkModelTestFixture.h:56
mitk::mitkModelTestFixture::ParseJSONFile
static json ParseJSONFile(const std::string path)
Definition: mitkModelTestFixture.h:36
mitkModelBase.h
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitkTestingMacros.h
mitk::mitkModelTestFixture::CompareModelAndReferenceProfile
static void CompareModelAndReferenceProfile(const mitk::ModelBase::Pointer testmodel, const json profile_json_obj)
Definition: mitkModelTestFixture.h:91
json
nlohmann::json json
Definition: mitkModelTestFixture.h:29
mitk::mitkModelTestFixture::ParseStaticParameters
static ModelBase::StaticParameterMapType ParseStaticParameters(const json profile_json_obj, const json modelValues_json_obj)
Definition: mitkModelTestFixture.h:67
mitk::TestFixture
Test fixture for parameterized tests.
Definition: mitkTestFixture.h:86
mitkTestingConfig.h
mitkModelFitException.h
mitkTestFixture.h
mitk::ModelBase::TimeGridType
itk::Array< double > TimeGridType
Definition: mitkModelBase.h:62
mitkVector.h
mitk::mitkModelTestFixture::CompareModelAndReferenceSignal
static void CompareModelAndReferenceSignal(mitk::ModelBase::Pointer testmodel, const json modelValues_json_obj, const json profile_json_obj)
Definition: mitkModelTestFixture.h:124
mitk::ModelBase::StaticParameterMapType
std::map< ParameterNameType, StaticParameterValuesType > StaticParameterMapType
Definition: mitkModelBase.h:72
mitk::mitkModelTestFixture::CompareModelAndReferenceDerivedParameters
static void CompareModelAndReferenceDerivedParameters(const mitk::ModelBase::Pointer testmodel, json modelValues_json_obj)
Definition: mitkModelTestFixture.h:163