Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkModelFitInfoTest.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 #include "mitkProperties.h"
17 
18 #include "mitkModelFitInfo.h"
19 #include "mitkModelFitConstants.h"
20 #include "mitkModelFitException.h"
21 
23 {
25  node->SetName("Param1");
26  auto testImage = mitk::Image::New();
27  node->SetData(testImage);
28  testImage->SetProperty("modelfit.testEmpty", mitk::StringProperty::New(""));
29  testImage->SetProperty("modelfit.testValid", mitk::StringProperty::New("test"));
31 
32  testImage->SetProperty(mitk::ModelFitConstants::FIT_UID_PROPERTY_NAME().c_str(), mitk::StringProperty::New("Fit1"));
33  testImage->SetProperty(mitk::ModelFitConstants::FIT_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("MyName"));
35  testImage->SetProperty(mitk::ModelFitConstants::FIT_INPUT_IMAGEUID_PROPERTY_NAME().c_str(), mitk::StringProperty::New("input UID"));
36 
37  testImage->SetProperty(mitk::ModelFitConstants::MODEL_TYPE_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModels"));
38  testImage->SetProperty(mitk::ModelFitConstants::MODEL_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModel_1"));
40  testImage->SetProperty(mitk::ModelFitConstants::MODEL_FUNCTION_CLASS_PROPERTY_NAME().c_str(), mitk::StringProperty::New("ModelClass"));
41  testImage->SetProperty(mitk::ModelFitConstants::MODEL_X_PROPERTY_NAME().c_str(), mitk::StringProperty::New("myX"));
42 
47 
48  return node;
49 }
50 
52 {
54 
55  //create input node
57  inputNode->SetName("Input");
59  inputNode->SetData(image);
60  mitk::NodeUIDType inputUID = mitk::EnsureModelFitUID(inputNode);
61 
62  storage->Add(inputNode);
63 
64  mitk::DataStorage::SetOfObjects::Pointer parents = mitk::DataStorage::SetOfObjects::New();
65  parents->push_back(inputNode);
66 
67  //create first result for Fit1
70  node1->SetData(paramImage);
71  node1->SetName("Param1");
73  paramImage->SetProperty(mitk::ModelFitConstants::FIT_UID_PROPERTY_NAME().c_str(), mitk::StringProperty::New("Fit1"));
74  paramImage->SetProperty(mitk::ModelFitConstants::FIT_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("MyName1"));
76  paramImage->SetProperty(mitk::ModelFitConstants::FIT_INPUT_IMAGEUID_PROPERTY_NAME().c_str(), mitk::StringProperty::New(inputUID.c_str()));
77 
78  paramImage->SetProperty(mitk::ModelFitConstants::MODEL_TYPE_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModels"));
79  paramImage->SetProperty(mitk::ModelFitConstants::MODEL_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModel_1"));
81  paramImage->SetProperty(mitk::ModelFitConstants::MODEL_FUNCTION_CLASS_PROPERTY_NAME().c_str(), mitk::StringProperty::New("ModelClass"));
82  paramImage->SetProperty(mitk::ModelFitConstants::MODEL_X_PROPERTY_NAME().c_str(), mitk::StringProperty::New("myX"));
83 
87  paramImage->SetProperty(mitk::ModelFitConstants::YAXIS_UNIT_PROPERTY_NAME().c_str(), mitk::StringProperty::New("kg"));
88 
89  paramImage->SetProperty(mitk::ModelFitConstants::PARAMETER_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("Param1"));
92 
93  storage->Add(node1,parents);
94 
95  //create second result for Fit1
97  node2->SetName("Param2");
98  mitk::Image::Pointer paramImage2 = mitk::Image::New();
99  node2->SetData(paramImage2);
101  paramImage2->SetProperty(mitk::ModelFitConstants::FIT_UID_PROPERTY_NAME().c_str(), mitk::StringProperty::New("Fit1"));
102  paramImage2->SetProperty(mitk::ModelFitConstants::FIT_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("MyName1"));
104  paramImage2->SetProperty(mitk::ModelFitConstants::FIT_INPUT_IMAGEUID_PROPERTY_NAME().c_str(), mitk::StringProperty::New(inputUID.c_str()));
105 
106  paramImage2->SetProperty(mitk::ModelFitConstants::MODEL_TYPE_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModels"));
107  paramImage2->SetProperty(mitk::ModelFitConstants::MODEL_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModel_1"));
109  paramImage2->SetProperty(mitk::ModelFitConstants::MODEL_FUNCTION_CLASS_PROPERTY_NAME().c_str(), mitk::StringProperty::New("ModelClass"));
110  paramImage2->SetProperty(mitk::ModelFitConstants::MODEL_X_PROPERTY_NAME().c_str(), mitk::StringProperty::New("myX"));
111 
113  paramImage2->SetProperty(mitk::ModelFitConstants::XAXIS_UNIT_PROPERTY_NAME().c_str(), mitk::StringProperty::New("h"));
115  paramImage2->SetProperty(mitk::ModelFitConstants::YAXIS_UNIT_PROPERTY_NAME().c_str(), mitk::StringProperty::New("kg"));
116 
117  paramImage2->SetProperty(mitk::ModelFitConstants::PARAMETER_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("Param2"));
120 
121  storage->Add(node2, parents);
122 
123  //create result for Fit 2
125  node3->SetName("Param_Other");
126  mitk::Image::Pointer paramImage3 = mitk::Image::New();
127  node3->SetData(paramImage3);
129  paramImage3->SetProperty(mitk::ModelFitConstants::FIT_UID_PROPERTY_NAME().c_str(), mitk::StringProperty::New("Fit2"));
130  paramImage3->SetProperty(mitk::ModelFitConstants::FIT_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("MyName2"));
132  paramImage3->SetProperty(mitk::ModelFitConstants::FIT_INPUT_IMAGEUID_PROPERTY_NAME().c_str(), mitk::StringProperty::New(inputUID.c_str()));
133 
134  paramImage3->SetProperty(mitk::ModelFitConstants::MODEL_TYPE_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModels"));
135  paramImage3->SetProperty(mitk::ModelFitConstants::MODEL_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("TestModel_2"));
137  paramImage3->SetProperty(mitk::ModelFitConstants::MODEL_FUNCTION_CLASS_PROPERTY_NAME().c_str(), mitk::StringProperty::New("ModelClass_B"));
138 
139  paramImage3->SetProperty(mitk::ModelFitConstants::PARAMETER_NAME_PROPERTY_NAME().c_str(), mitk::StringProperty::New("Param_Other"));
141 
142  storage->Add(node3, parents);
143 
144  return storage;
145 }
146 
147 int mitkModelFitInfoTest(int /*argc*/, char*[] /*argv[]*/)
148 {
149  MITK_TEST_BEGIN("mitkModelFitTest")
150 
151  mitk::modelFit::Parameter::Pointer p = mitk::modelFit::Parameter::New();
152  p->name = "p";
153  mitk::modelFit::ModelFitInfo::Pointer fit = mitk::modelFit::ModelFitInfo::New();
154  fit->AddParameter(p);
155  MITK_TEST_CONDITION_REQUIRED(fit->GetParameters().size() == 1,
156  "Testing if AddParameter successfully adds a parameter.");
157 
158  mitk::modelFit::Parameter::ConstPointer resultParam = fit->GetParameter("test",
160  MITK_TEST_CONDITION_REQUIRED(resultParam.IsNull(),
161  "Testing if GetParameter returns NULL for wrong parameter.");
162 
163  resultParam = fit->GetParameter("p", mitk::modelFit::Parameter::ParameterType);
164  MITK_TEST_CONDITION_REQUIRED(resultParam == p,
165  "Testing if GetParameter returns the correct parameter.");
166 
168  resultParam = fit->GetParameter("p", mitk::modelFit::Parameter::CriterionType);
169  MITK_TEST_CONDITION_REQUIRED(resultParam == p,
170  "Testing if GetParameter returns the correct parameter with a " <<
171  "non-default type.");
172 
173  fit->DeleteParameter("test", mitk::modelFit::Parameter::CriterionType);
174  MITK_TEST_CONDITION_REQUIRED(fit->GetParameters().size() == 1,
175  "Testing if DeleteParameter fails for wrong parameter.");
176 
177  fit->DeleteParameter("p", mitk::modelFit::Parameter::CriterionType);
178  MITK_TEST_CONDITION_REQUIRED(fit->GetParameters().size() == 0,
179  "Testing if DeleteParameter successfully removes a parameter.");
180 
183 
185  mitk::modelFit::GetMandatoryProperty(node.GetPointer(), "modelfit.testInvalid"));
187  mitk::modelFit::GetMandatoryProperty(node.GetPointer(), "modelfit.testEmpty"));
188 
189  MITK_TEST_CONDITION_REQUIRED(mitk::modelFit::GetMandatoryProperty(node.GetPointer(), "modelfit.testValid")
190  == "test",
191  "Testing if GetMandatoryProperty returns the correct value.");
192 
194 
196  "Testing if CreateFitInfoFromNode returns NULL for invalid node.");
197 
199  "Testing if CreateFitInfoFromNode returns NULL for node with " <<
200  "missing properties.");
201 
202  mitk::DataNode::Pointer testNode = storage->GetNamedNode("Param1");
203  mitk::modelFit::ModelFitInfo::Pointer resultFit = mitk::modelFit::CreateFitInfoFromNode("Fit1", storage);
204  MITK_TEST_CONDITION_REQUIRED(resultFit.IsNotNull(),
205  "Testing if CreateFitInfoFromNode returns a valid model fit info.");
207  resultFit->uid == "Fit1" &&
208  resultFit->fitName == "MyName1" &&
209  resultFit->modelType == "TestModels" &&
210  resultFit->modelName == "TestModel_1" &&
211  resultFit->function == ""&&
212  resultFit->functionClassID == "ModelClass" &&
213  resultFit->x == "myX" &&
214  resultFit->xAxisName == mitk::ModelFitConstants::XAXIS_NAME_VALUE_DEFAULT() &&
215  resultFit->xAxisUnit == "h" &&
216  resultFit->yAxisName == mitk::ModelFitConstants::YAXIS_NAME_VALUE_DEFAULT() &&
217  resultFit->yAxisUnit == "kg" &&
218  resultFit->GetParameters().size() == 2,
219  "Testing if CreateFitInfoFromNode creates a fit with correct attributes.");
220 
221  mitk::modelFit::Parameter::ConstPointer param = resultFit->GetParameter("Param1",mitk::modelFit::Parameter::ParameterType);
222  MITK_TEST_CONDITION_REQUIRED(param.IsNotNull(),
223  "Testing if param 1 exists.");
224 
225  MITK_TEST_CONDITION_REQUIRED(param->name == "Param1" && param->unit == "b" && param->image == testNode->GetData(),
226  "Testing if param 1 is configured correctly.");
227 
228  mitk::modelFit::Parameter::ConstPointer param2 = resultFit->GetParameter("Param2",mitk::modelFit::Parameter::DerivedType);
229  MITK_TEST_CONDITION_REQUIRED(param2.IsNotNull(),
230  "Testing if param 2 exists.");
231 
232  testNode = storage->GetNamedNode("Param2");
233  MITK_TEST_CONDITION_REQUIRED(param2->name == "Param2" && param2->unit == "a" && param2->image == testNode->GetData(),
234  "Testing if param 2 is configured correctly.");
235 
236  MITK_TEST_CONDITION_REQUIRED(mitk::modelFit::GetNodesOfFit(resultFit->uid,storage)->Size() == 2,
237  "Testing if GetNodesOfFit works correctly for Fit1");
238 
239  MITK_TEST_CONDITION_REQUIRED(mitk::modelFit::GetNodesOfFit("Fit2",storage)->Size() == 1,
240  "Testing if GetNodesOfFit works correctly for Fit1");
241 
242  MITK_TEST_CONDITION_REQUIRED(mitk::modelFit::GetNodesOfFit("unkown_fit",storage)->Size() == 0,
243  "Testing if GetNodesOfFit works correctly for unkown fits.");
244 
245  MITK_TEST_CONDITION_REQUIRED(mitk::modelFit::GetNodesOfFit("unkown_fit",nullptr).IsNull(),
246  "Testing if GetNodesOfFit works correctly for illegal calls.");
247 
248  testNode = storage->GetNamedNode("Input");
250 
251  MITK_TEST_CONDITION_REQUIRED(uidSet.size() == 2 &&
252  uidSet.find("Fit1")!=uidSet.end() &&
253  uidSet.find("Fit2")!=uidSet.end(),
254  "Testing if GetFitUIDsOfNode works correctly.");
255 
256  uidSet = mitk::modelFit::GetFitUIDsOfNode(nullptr,storage);
257 
258  MITK_TEST_CONDITION_REQUIRED(uidSet.size() == 0,
259  "Testing if GetFitUIDsOfNode works correctly with invalid node.");
260 
261  uidSet = mitk::modelFit::GetFitUIDsOfNode(testNode,nullptr);
262 
263  MITK_TEST_CONDITION_REQUIRED(uidSet.size() == 0,
264  "Testing if GetFitUIDsOfNode works correctly with invalid storage.");
265 
266  MITK_TEST_END()
267 }
MITKMODELFIT_EXPORT NodeUIDSetType GetFitUIDsOfNode(const mitk::DataNode *node, const mitk::DataStorage *storage)
mitk::DataNode::Pointer generateModelFitTestNode()
static const std::string FIT_TYPE_PROPERTY_NAME()
MITKMODELFIT_EXPORT DataStorage::SetOfObjects::ConstPointer GetNodesOfFit(const ModelFitInfo::UIDType &fitUID, const mitk::DataStorage *storage)
int mitkModelFitInfoTest(int, char *[])
static const std::string MODEL_TYPE_PROPERTY_NAME()
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
static const std::string YAXIS_NAME_PROPERTY_NAME()
static const std::string PARAMETER_UNIT_PROPERTY_NAME()
static const std::string MODEL_NAME_PROPERTY_NAME()
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
static const std::string XAXIS_NAME_VALUE_DEFAULT()
static const std::string YAXIS_NAME_VALUE_DEFAULT()
#define MITK_TEST_FOR_EXCEPTION(EXCEPTIONCLASS, STATEMENT)
Simplified version of MITK_TEST_FOR_EXCEPTION_BEGIN / END for a single statement. ...
static const std::string FIT_UID_PROPERTY_NAME()
mitk::StandaloneDataStorage::Pointer generateModelFitTestStorage()
static const std::string MODEL_FUNCTION_PROPERTY_NAME()
static Pointer New()
static const std::string PARAMETER_TYPE_VALUE_DERIVED_PARAMETER()
static const std::string XAXIS_NAME_PROPERTY_NAME()
static const std::string FIT_INPUT_IMAGEUID_PROPERTY_NAME()
NodeUIDType MITKMODELFIT_EXPORT EnsureModelFitUID(mitk::BaseData *data)
MITKMODELFIT_EXPORT ModelFitInfo::Pointer CreateFitInfoFromNode(const ModelFitInfo::UIDType &uid, const mitk::DataStorage *storage)
Creates a new ModelFitInfo instance from the nodes in the passed storage. The fit will be identified ...
mitk::Image::Pointer image
static const std::string MODEL_X_PROPERTY_NAME()
static Pointer New()
std::string NodeUIDType
Definition: mitkUIDHelper.h:26
static const std::string MODEL_FUNCTION_CLASS_PROPERTY_NAME()
static const std::string PARAMETER_TYPE_VALUE_PARAMETER()
static const std::string YAXIS_UNIT_PROPERTY_NAME()
Class-specific exception for modelfits.
static const std::string FIT_TYPE_VALUE_PIXELBASED()
std::set< ModelFitInfo::UIDType > NodeUIDSetType
static const std::string FIT_NAME_PROPERTY_NAME()
static const std::string XAXIS_UNIT_PROPERTY_NAME()
and MITK_TEST_END()
MITKMODELFIT_EXPORT const std::string GetMandatoryProperty(const mitk::DataNode *node, const std::string &prop)
Reads the string property with the given name from the data of the given node and returns its value...
static const std::string PARAMETER_NAME_PROPERTY_NAME()
static const std::string PARAMETER_TYPE_PROPERTY_NAME()
static Pointer New()