Medical Imaging Interaction Toolkit  2018.4.99-4c24e3cb
Medical Imaging Interaction Toolkit
mitkModelFitInfo.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 
14 #include <mitkUIDGenerator.h>
15 #include "mitkDataNode.h"
16 #include "mitkDataStorage.h"
17 #include "mitkModelFitInfo.h"
19 #include "mitkModelFitException.h"
20 
22 {
23  if (p.IsNull())
24  {
25  mitkThrow() << "Given parameter must not be NULL";
26  }
27 
28  if (GetParameter(p->name, p->type).IsNull())
29  {
30  MITK_DEBUG << "Adding parameter '" << p->name << "with type " << p->type
31  << "' to modelFit '" << uid << "'.";
32 
33  LockType lock(mutex);
34  parameterList.push_back(p);
35  }
36  else
37  {
38  MITK_DEBUG << "Parameter '" << p->name << "' of modelFit '" << uid
39  << "' already exists. Aborting.";
40  }
41 }
42 
43 mitk::modelFit::Parameter::ConstPointer
45  const Parameter::Type& type) const
46 {
47  for (ConstIterType iter = parameterList.begin(); iter != parameterList.end(); ++iter)
48  {
49  Parameter::ConstPointer p = static_cast<Parameter::ConstPointer>(*iter);
50 
51  if (p->name == name && p->type == type)
52  {
53  return p;
54  }
55  }
56 
57  return nullptr;
58 }
59 
60 
62 const
63 {
64  return this->parameterList;
65 };
66 
67 void mitk::modelFit::ModelFitInfo::DeleteParameter(const std::string& name,
68  const Parameter::Type& type)
69 {
70  for (IterType iter = parameterList.begin(); iter != parameterList.end(); ++iter)
71  {
72  Parameter::ConstPointer p = static_cast<Parameter::ConstPointer>(*iter);
73 
74  if (p->name == name && p->type == type)
75  {
76  MITK_DEBUG << "Deleting parameter '" << name << " with type " << type
77  << "' from modelFit '" << uid << "'.";
78 
79  LockType lock(mutex);
80  parameterList.erase(iter);
81  return;
82  }
83  }
84 }
85 
86 
88  const std::string& prop)
89 {
90  std::string result;
91 
92  if (!node || !node->GetData() ||
93  !node->GetData()->GetPropertyList()->GetStringProperty(prop.c_str(), result) || result.empty())
94  {
96  << " is lacking the required "
97  << "property '" << prop
98  << "' or contains an empty string.";
99  }
100 
101  return result;
102 }
103 
105  const std::string& prop)
106 {
107  std::string result;
108 
109  if (!data || !data->GetPropertyList()->GetStringProperty(prop.c_str(), result) || result.empty())
110  {
111  mitkThrowException(mitk::modelFit::ModelFitException) << "Data is lacking the required "
112  << "property '" << prop
113  << "' or contains an empty string.";
114  }
115 
116  return result;
117 }
118 
119 mitk::modelFit::ModelFitInfo::Pointer
121  const mitk::DataStorage* storage)
122 {
123  if (!storage)
124  {
125  return nullptr;
126  }
127 
128  mitk::DataStorage::SetOfObjects::ConstPointer nodes = GetNodesOfFit(uid, storage);
129 
130  if (nodes.IsNull() || nodes->empty())
131  {
132  return nullptr;
133  }
134 
135  mitk::DataNode::ConstPointer node = nodes->GetElement(
136  0).GetPointer(); //take one of the nodes as template
137 
138  if (!node->GetData())
139  {
140  return nullptr;
141  }
142 
143  ModelFitInfo::Pointer fit = ModelFitInfo::New();
144  fit->uid = uid;
145 
146  // Mandatory properties
147  try
148  {
152  }
153  catch (const ModelFitException& e)
154  {
155  MITK_ERROR << e.what();
156  return nullptr;
157  }
158 
159  // Either a function string or a function class must exist
160  if (!node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::MODEL_FUNCTION_PROPERTY_NAME().c_str(),
161  fit->function))
162  {
163  fit->function = "";
164  }
165 
166  try
167  {
168  fit->functionClassID =
170  }
171  catch (const ModelFitException&)
172  {
173  if (fit->function.empty())
174  {
175  MITK_ERROR << "The properties '"
178  << "' are both empty or missing. One of these is required.";
179  return nullptr;
180  }
181  }
182 
183  node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::FIT_NAME_PROPERTY_NAME().c_str(), fit->fitName);
184  node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::MODEL_X_PROPERTY_NAME().c_str(), fit->x);
185  node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::XAXIS_NAME_PROPERTY_NAME().c_str(), fit->xAxisName);
186  node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::XAXIS_UNIT_PROPERTY_NAME().c_str(), fit->xAxisUnit);
187  node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::YAXIS_NAME_PROPERTY_NAME().c_str(), fit->yAxisName);
188  node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::YAXIS_UNIT_PROPERTY_NAME().c_str(), fit->yAxisUnit);
189 
190  // Parameter
191  for (DataStorage::SetOfObjects::ConstIterator pos = nodes->Begin(); pos != nodes->End(); ++pos)
192  {
193  modelFit::Parameter::Pointer param = ExtractParameterFromData(pos->Value()->GetData());
194 
195  if (param.IsNotNull())
196  {
197  fit->AddParameter(param);
198  }
199  }
200 
201  // Static parameters
202  mitk::ScalarListLookupTableProperty::ConstPointer varProp = dynamic_cast<const mitk::ScalarListLookupTableProperty*>(node->GetData()->GetProperty(mitk::ModelFitConstants::FIT_STATIC_PARAMETERS_PROPERTY_NAME().c_str()).GetPointer());
203 
204  if (varProp.IsNotNull())
205  {
206  const mitk::ScalarListLookupTable lut = varProp->GetValue();
208 
209  for (mitk::ScalarListLookupTable::LookupTableType::const_iterator mapIter =
210  varMap.begin(); mapIter != varMap.end(); ++mapIter)
211  {
212  fit->staticParamMap.Add(mapIter->first, mapIter->second);
213  }
214  }
215 
216  //fit input and ROI
217  try
218  {
219  fit->inputUID = GetMandatoryProperty(node,
221  }
222  catch (const ModelFitException& e)
223  {
224  MITK_ERROR << e.what();
225  return nullptr;
226  }
227 
228  if (storage)
229  {
230  mitk::DataNode::Pointer inputNode = GetNodeByModelFitUID(storage, fit->inputUID);
231 
232  if (inputNode.IsNull())
233  {
234  MITK_ERROR << "Cannot create valid model fit info. input node cannot be found.";
235  return nullptr;
236  }
237 
238  mitk::Image::Pointer inputImage = dynamic_cast<mitk::Image*>(inputNode->GetData());
239 
240  if (inputImage.IsNull())
241  {
242  MITK_ERROR << "Cannot create valid model fit info. input node does not contain an image.";
243  return nullptr;
244  }
245 
246  fit->inputImage = inputImage;
247  }
248 
249  node->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::FIT_INPUT_ROIUID_PROPERTY_NAME().c_str(),
250  fit->roiUID);
251 
252  mitk::ScalarListLookupTableProperty::ConstPointer inputDataProp = dynamic_cast<const mitk::ScalarListLookupTableProperty*>(node->GetData()->GetProperty(mitk::ModelFitConstants::FIT_INPUT_DATA_PROPERTY_NAME().c_str()).GetPointer());
253  if (inputDataProp.IsNotNull())
254  {
255  fit->inputData = inputDataProp->GetValue();
256  }
257 
258  return fit;
259 }
260 
261 mitk::modelFit::ModelFitInfo::Pointer
263 mitk::BaseData* inputImage, const std::string& fitType, const std::string& fitName,
264  const NodeUIDType roiUID)
265 {
266  if (!usedParameterizer)
267  {
268  return nullptr;
269  }
270 
271  UIDGenerator generator("FitUID_");
272  std::string uid = generator.GetUID();
273 
274  ModelFitInfo::Pointer fit = ModelFitInfo::New();
275  fit->uid = uid;
276  fit->fitType = fitType;
277  fit->fitName = fitName;
278  fit->inputImage = dynamic_cast<Image*>(inputImage);
279  fit->inputUID = EnsureModelFitUID(inputImage);
280 
281  if (fit->inputImage.IsNull())
282  {
283  mitkThrow() << "Cannot generate model fit info. Input node does not contain an image.";
284  }
285 
286  fit->modelType = usedParameterizer->GetModelType();
287  fit->modelName = usedParameterizer->GetModelDisplayName();
288 
289  fit->function = usedParameterizer->GetFunctionString();
290  fit->x = usedParameterizer->GetXName();
291  fit->functionClassID = usedParameterizer->GetClassID();
292 
293  fit->xAxisName = usedParameterizer->GetXAxisName();
294  fit->xAxisUnit = usedParameterizer->GetXAxisUnit();
295  fit->yAxisName = usedParameterizer->GetYAxisName();
296  fit->yAxisUnit = usedParameterizer->GetYAxisUnit();
297 
298  // Parameter
299  ModelTraitsInterface::ParameterNamesType paramNames = usedParameterizer->GetParameterNames();
300  ModelTraitsInterface::ParamterScaleMapType paramScales = usedParameterizer->GetParameterScales();
301  ModelTraitsInterface::ParamterUnitMapType paramUnits = usedParameterizer->GetParameterUnits();
302 
303  for (ModelTraitsInterface::ParameterNamesType::iterator pos = paramNames.begin();
304  pos != paramNames.end(); ++pos)
305  {
306  modelFit::Parameter::Pointer param = modelFit::Parameter::New();
307  param->name = *pos;
308  param->type = Parameter::ParameterType;
309 
310  if (paramScales.find(*pos) == paramScales.end())
311  {
312  mitkThrow() <<
313  "Cannot generate model fit info. Model traits invalid (scales do not include parameter). Parameter name: "
314  << *pos;
315  }
316 
317  if (paramUnits.find(*pos) == paramUnits.end())
318  {
319  mitkThrow() <<
320  "Cannot generate model fit info. Model traits invalid (units do not include parameter). Parameter name: "
321  << *pos;
322  }
323 
324  param->scale = paramScales[*pos];
325  param->unit = paramUnits[*pos];
326  fit->AddParameter(param);
327  }
328 
329  //derived parameter
331  usedParameterizer->GetDerivedParameterNames();
333  usedParameterizer->GetDerivedParameterScales();
335  usedParameterizer->GetDerivedParameterUnits();
336 
337  for (ModelTraitsInterface::ParameterNamesType::iterator pos = derivedNames.begin();
338  pos != derivedNames.end(); ++pos)
339  {
340  modelFit::Parameter::Pointer param = modelFit::Parameter::New();
341  param->name = *pos;
342  param->type = Parameter::DerivedType;
343 
344  if (derivedScales.find(*pos) == derivedScales.end())
345  {
346  mitkThrow() <<
347  "Cannot generate model fit info. Model traits invalid (scales do not include parameter). Parameter name: "
348  << *pos;
349  }
350 
351  if (derivedUnits.find(*pos) == derivedUnits.end())
352  {
353  mitkThrow() <<
354  "Cannot generate model fit info. Model traits invalid (units do not include parameter). Parameter name: "
355  << *pos;
356  }
357 
358  param->scale = derivedScales[*pos];
359  param->unit = derivedUnits[*pos];
360  fit->AddParameter(param);
361  }
362 
363  // Static parameters (but transfer only the global ones)
365  usedParameterizer->GetGlobalStaticParameters();
366 
367  for (ModelParameterizerBase::StaticParameterMapType::const_iterator pos = staticParamMap.begin();
368  pos != staticParamMap.end(); ++pos)
369  {
370  fit->staticParamMap.Add(pos->first, pos->second);
371  }
372 
373 
374  fit->roiUID = roiUID;
375 
376  return fit;
377 }
378 
379 mitk::modelFit::ModelFitInfo::Pointer
382 const std::string& fitName, const NodeUIDType roiUID)
383 {
384  mitk::modelFit::ModelFitInfo::Pointer info = CreateFitInfoFromModelParameterizer(usedParameterizer,
385  inputImage, fitType, fitName, roiUID);
386 
387  info->inputData = inputData;
388 
389  return info;
390 }
391 
392 mitk::DataStorage::SetOfObjects::ConstPointer
394 {
395  if (!storage)
396  {
397  return nullptr;
398  }
399 
402  return storage->GetSubset(predicate);
403 };
404 
405 
408 {
410 
411  if (node && storage)
412  {
415  mitk::DataStorage::SetOfObjects::ConstPointer nodes = storage->GetDerivations(node, predicate,
416  false);
417 
418  for (mitk::DataStorage::SetOfObjects::ConstIterator pos = nodes->Begin(); pos != nodes->End();
419  ++pos)
420  {
422  pos->Value()->GetData()->GetPropertyList()->GetStringProperty(mitk::ModelFitConstants::FIT_UID_PROPERTY_NAME().c_str(), uid);
423  result.insert(uid);
424  }
425 
426  }
427 
428  return result;
429 };
MITKMODELFIT_EXPORT NodeUIDSetType GetFitUIDsOfNode(const mitk::DataNode *node, const mitk::DataStorage *storage)
void DeleteParameter(const std::string &name, const Parameter::Type &type)
Searches for the parameter with the given name and type in the fit&#39;s parameter list and deletes it if...
virtual StaticParameterMapType GetGlobalStaticParameters() const =0
virtual DerivedParamterScaleMapType GetDerivedParameterScales() const =0
static const std::string FIT_TYPE_PROPERTY_NAME()
Data management class that handles &#39;was created by&#39; relations.
MITKMODELFIT_EXPORT DataStorage::SetOfObjects::ConstPointer GetNodesOfFit(const ModelFitInfo::UIDType &fitUID, const mitk::DataStorage *storage)
virtual ModellClassIDType GetClassID() const =0
virtual SetOfObjects::ConstPointer GetDerivations(const DataNode *node, const NodePredicateBase *condition=nullptr, bool onlyDirectDerivations=true) const =0
returns a set of derived objects for a given node.
Generated unique IDs.
Base of all data objects.
Definition: mitkBaseData.h:37
std::map< ParameterNameType, double > DerivedParamterScaleMapType
virtual ParamterUnitMapType GetParameterUnits() const =0
static const std::string MODEL_TYPE_PROPERTY_NAME()
#define MITK_ERROR
Definition: mitkLogMacros.h:20
DataNode::Pointer MITKMODELFIT_EXPORT GetNodeByModelFitUID(const mitk::DataStorage *storage, const NodeUIDType &uid)
virtual std::string GetXAxisUnit() const =0
static const std::string YAXIS_NAME_PROPERTY_NAME()
std::map< ParameterNameType, double > ParamterScaleMapType
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
Parameter::ConstPointer GetParameter(const std::string &name, const Parameter::Type &type) const
Searches for the parameter with the given name and type in the fit&#39;s parameter list and returns it...
static const std::string MODEL_NAME_PROPERTY_NAME()
mitk::Image::Pointer inputImage
MITKMODELFIT_EXPORT Parameter::Pointer ExtractParameterFromData(const mitk::BaseData *data)
Data class for modelfit properties that store a map of lists (e.g. static parameters).
StaticParameterMap staticParamMap
static void info(const char *fmt,...)
Definition: svm.cpp:86
static const std::string FIT_UID_PROPERTY_NAME()
MITKMODELFIT_EXPORT ModelFitInfo::Pointer CreateFitInfoFromModelParameterizer(const ModelParameterizerBase *usedParameterizer, mitk::BaseData *inputImage, const std::string &fitType, const std::string &fitName="", const NodeUIDType roiUID="")
static const std::string MODEL_FUNCTION_PROPERTY_NAME()
void AddParameter(Parameter::Pointer p)
Adds the given parameter to this fit&#39;s parameter list if it doesn&#39;t exist already.
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
std::vector< ParameterNameType > ParameterNamesType
ParameterNamesType DerivedParameterNamesType
static const std::string FIT_INPUT_DATA_PROPERTY_NAME()
virtual ParameterNamesType GetParameterNames() const =0
static const std::string XAXIS_NAME_PROPERTY_NAME()
#define mitkThrow()
SetOfObjects::ConstPointer GetSubset(const NodePredicateBase *condition) const
returns a set of data objects that meet the given condition(s)
virtual ParamterScaleMapType GetParameterScales() const =0
Image class for storing images.
Definition: mitkImage.h:72
static const std::string FIT_INPUT_IMAGEUID_PROPERTY_NAME()
NodeUIDType MITKMODELFIT_EXPORT EnsureModelFitUID(mitk::BaseData *data)
static Pointer New(const char *_arg)
virtual std::string GetYAxisName() const =0
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 ...
virtual std::string GetXName() const =0
static const std::string MODEL_X_PROPERTY_NAME()
const LookupTableType & GetLookupTable() const
Returns the map of lists.
std::string NodeUIDType
Definition: mitkUIDHelper.h:26
mitk::PropertyList::Pointer GetPropertyList() const
Get the data&#39;s property list.
static const std::string MODEL_FUNCTION_CLASS_PROPERTY_NAME()
virtual std::string GetXAxisName() const =0
virtual FunctionStringType GetFunctionString() const =0
#define mitkThrowException(classname)
virtual std::string GetModelDisplayName() const =0
static const std::string FIT_INPUT_ROIUID_PROPERTY_NAME()
static const std::string YAXIS_UNIT_PROPERTY_NAME()
Class-specific exception for modelfits.
ScalarListLookupTable inputData
static const std::string FIT_STATIC_PARAMETERS_PROPERTY_NAME()
std::set< ModelFitInfo::UIDType > NodeUIDSetType
ParamListType::const_iterator ConstIterType
static const std::string FIT_NAME_PROPERTY_NAME()
const ParamListType & GetParameters() const
static const std::string XAXIS_UNIT_PROPERTY_NAME()
ModelBaseType::StaticParameterMapType StaticParameterMapType
virtual std::string GetModelType() const =0
std::vector< Parameter::Pointer > ParamListType
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...
std::map< ParameterNameType, std::string > ParamterUnitMapType
std::map< KeyType, ValueType > LookupTableType
virtual std::string GetYAxisUnit() const =0
static Pointer New()
Class for nodes of the DataTree.
Definition: mitkDataNode.h:64
bool GetName(std::string &nodeName, const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="name") const
Convenience access method for accessing the name of an object (instance of StringProperty with proper...
Definition: mitkDataNode.h:376
std::map< ParameterNameType, std::string > DerivedParamterUnitMapType
virtual DerivedParameterNamesType GetDerivedParameterNames() const =0
virtual DerivedParamterUnitMapType GetDerivedParameterUnits() const =0