Medical Imaging Interaction Toolkit  2018.4.99-c4b6bb11
Medical Imaging Interaction Toolkit
mitkModelFitFunctorBase.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 
17 Compute(const InputPixelArrayType& value, const ModelBase* model,
18  const ModelBase::ParametersType& initialParameters) const
19 {
20  if (!model)
21  {
22  itkExceptionMacro("Cannot compute fit. Passed model is not defined.");
23  }
24 
25  if (model->GetNumberOfParameters() != initialParameters.Size())
26  {
27  itkExceptionMacro("Cannot compute fit. Parameter count of passed model and passed initial parameters differ. Model parameter count: "
28  << model->GetNumberOfParameters() << "; Initial parameters: " << initialParameters);
29  }
30 
31  SignalType sample(value.size());
32 
33  for (SignalType::SizeValueType i = 0; i < sample.Size(); ++i)
34  {
35  sample[i] = value [i];
36  }
37 
38  DebugParameterMapType debugParams;
39  ParameterNamesType debugNames;
40  if (this->m_DebugParameterMaps)
41  {
42  debugNames = this->GetDebugParameterNames();
43  }
44 
45  ParametersType fittedParameters = DoModelFit(sample, model, initialParameters, debugParams);
46 
47  OutputPixelArrayType derivedParameters = this->GetDerivedParameters(model, fittedParameters);
48 
49  OutputPixelArrayType criteria = this->GetCriteria(model, fittedParameters, sample);
50 
51  OutputPixelArrayType evaluationParameters = this->GetEvaluationParameters(model, fittedParameters,
52  sample);
53 
54  if (criteria.size() != this->GetCriterionNames().size())
55  {
56  itkExceptionMacro("ModelFitInfo implementation seems to be inconsitent. Number of criterion values is not equal to number of criterion names.");
57  }
58 
59  OutputPixelArrayType result(fittedParameters.Size() + derivedParameters.size() + criteria.size() +
60  evaluationParameters.size() + debugNames.size());
61 
62  for (ParametersType::SizeValueType i = 0; i < fittedParameters.Size(); ++i)
63  {
64  result[i] = fittedParameters[i];
65  }
66 
67  OutputPixelArrayType::size_type offset = fittedParameters.Size();
68 
69  for (OutputPixelArrayType::size_type j = 0; j < derivedParameters.size(); ++j)
70  {
71  result[offset + j] = derivedParameters[j];
72  }
73 
74  offset += derivedParameters.size();
75  for (OutputPixelArrayType::size_type j = 0; j < criteria.size(); ++j)
76  {
77  result[offset + j] = criteria[j];
78  }
79 
80  offset += criteria.size();
81  for (OutputPixelArrayType::size_type j = 0; j < evaluationParameters.size(); ++j)
82  {
83  result[offset + j] = evaluationParameters[j];
84  }
85 
86  offset += evaluationParameters.size();
87  for (OutputPixelArrayType::size_type j = 0; j < debugNames.size(); ++j)
88  {
89  DebugParameterMapType::const_iterator pos = debugParams.find(debugNames[j]);
90  if (pos == debugParams.end())
91  {
92  itkExceptionMacro("ModelFitInfo implementation seems to be inconsitent. Debug parameter defined by functor is not in its returned debug map. Invalid debug parameter name: "<<debugNames[j]);
93  }
94  else
95  {
96  result[offset + j] = pos->second;
97  }
98  }
99 
100  return result;
101 };
102 
103 unsigned int
105 {
106  if (!model)
107  {
108  itkExceptionMacro("Cannot get number of outputs. Model is not defined.");
109  }
110 
111  return model->GetNumberOfParameters() + model->GetNumberOfDerivedParameters() +
112  this->GetCriterionNames().size() + m_CostFunctionMap.size()+ this->GetDebugParameterNames().size();
113 };
114 
115 void
117 {
118  m_Mutex.Lock();
119 
120  m_CostFunctionMap.clear();
121 
122  m_Mutex.Unlock();
123 };
124 
125 void
127  SVModelFitCostFunction* evaluationCostFunction)
128 {
129  m_Mutex.Lock();
130 
131  SVModelFitCostFunction::Pointer costFunctPtr = evaluationCostFunction;
132 
133  m_CostFunctionMap.insert(std::make_pair(parameterName, costFunctPtr));
134 
135  m_Mutex.Unlock();
136 };
137 
140 {
141  m_Mutex.Lock();
142 
143  ParameterNamesType result;
144 
145  for (CostFunctionMapType::const_iterator pos = m_CostFunctionMap.begin();
146  pos != m_CostFunctionMap.end(); ++pos)
147  {
148  result.push_back(pos->first);
149  }
150 
151  m_Mutex.Unlock();
152 
153  return result;
154 };
155 
158 const
159 {
160  const SVModelFitCostFunction* result = nullptr;
161 
162  m_Mutex.Lock();
163 
164  CostFunctionMapType::const_iterator pos = m_CostFunctionMap.find(parameterName);
165 
166  if (pos != m_CostFunctionMap.end())
167  {
168  result = (pos->second).GetPointer();
169  }
170 
171  m_Mutex.Unlock();
172 
173  return result;
174 };
175 
178 {
179  ParameterNamesType result;
180 
181  if (this->m_DebugParameterMaps)
182  {
183  result = this->DefineDebugParameterNames();
184  }
185 
186  return result;
187 };
188 
190 ModelFitFunctorBase() : m_DebugParameterMaps(false)
191 {};
192 
195 
198  const ParametersType& parameters) const
199 {
200  ModelBase::DerivedParameterMapType derivedParameterMap = model->GetDerivedParameters(parameters);
201  OutputPixelArrayType result(derivedParameterMap.size());
202 
203  unsigned int i = 0;
204 
205  for (ModelBase::DerivedParameterMapType::const_iterator pos = derivedParameterMap.begin();
206  pos != derivedParameterMap.end(); ++pos, ++i)
207  {
208  result[i] = pos->second;
209  }
210 
211  return result;
212 };
213 
216  const ParametersType& parameters, const SignalType& sample) const
217 {
218  m_Mutex.Lock();
219 
220  OutputPixelArrayType result(m_CostFunctionMap.size());
221 
222  unsigned int i = 0;
223 
224  for (CostFunctionMapType::const_iterator pos = m_CostFunctionMap.begin();
225  pos != m_CostFunctionMap.end(); ++pos, ++i)
226  {
227  //break constness to configure evaluation cost functions. This operatoin is guarded be the mutex
228  //after costFct->GetValue() the cost function may change its state again and is irrelevant for the
229  //current call of GetEvaluationParameters
230  SVModelFitCostFunction* costFct = const_cast<SVModelFitCostFunction*>(pos->second.GetPointer());
231 
232  costFct->SetModel(model);
233  costFct->SetSample(sample);
234 
235  result[i] = costFct->GetValue(parameters);
236  }
237 
238  m_Mutex.Unlock();
239 
240  return result;
241 };
Base class for (dynamic) models. A model can be used to calculate its signal given the discrete time ...
Definition: mitkModelBase.h:47
ModelTraitsInterface::ParametersType ParametersType
Definition: mitkModelBase.h:59
void SetSample(const SignalType &sampleSet) override
virtual ParameterNamesType GetCriterionNames() const =0
MeasureType GetValue(const ParametersType &parameter) const override
std::vector< ParameterImagePixelType > InputPixelArrayType
std::map< std::string, ParameterImagePixelType > DebugParameterMapType
DerivedParameterMapType GetDerivedParameters(const ParametersType &parameters) const
virtual OutputPixelArrayType GetCriteria(const ModelBase *model, const ParametersType &parameters, const SignalType &sample) const =0
const SVModelFitCostFunction * GetEvaluationParameterCostFunction(const std::string &parameterName) const
ModelBase::ParameterNamesType ParameterNamesType
virtual void SetModel(const ModelBase *_arg)
OutputPixelArrayType Compute(const InputPixelArrayType &value, const ModelBase *model, const ModelBase::ParametersType &initialParameters) const
virtual ParametersType DoModelFit(const SignalType &value, const ModelBase *model, const ModelBase::ParametersType &initialParameters, DebugParameterMapType &debugParameters) const =0
OutputPixelArrayType GetDerivedParameters(const ModelBase *model, const ParametersType &parameters) const
static Vector3D offset
virtual ParametersSizeType GetNumberOfParameters() const =0
ModelFitCostFunctionInterface::SignalType SignalType
ParameterNamesType GetDebugParameterNames() const
ModelBase::ParametersType ParametersType
std::map< ParameterNameType, DerivedParameterValueType > DerivedParameterMapType
Definition: mitkModelBase.h:75
OutputPixelArrayType GetEvaluationParameters(const ModelBase *model, const ParametersType &parameters, const SignalType &sample) const
DerivedParametersSizeType GetNumberOfDerivedParameters() const override
std::vector< ParameterImagePixelType > OutputPixelArrayType
virtual ParameterNamesType DefineDebugParameterNames() const =0
ParameterNamesType GetEvaluationParameterNames() const
unsigned int GetNumberOfOutputs(const ModelBase *model) const
void RegisterEvaluationParameter(const std::string &parameterName, SVModelFitCostFunction *evaluationCostFunction)