Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkROIBasedParameterFitImageGenerator.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 "itkChangeInformationImageFilter.h"
14 
16 #include "mitkImageAccessByItk.h"
17 #include "mitkImageCast.h"
18 
19 void
21 onFitProgressEvent(::itk::Object* caller, const ::itk::EventObject& /*eventObject*/)
22 {
23  this->InvokeEvent(::itk::ProgressEvent());
24 
25  itk::ProcessObject* process = dynamic_cast<itk::ProcessObject*>(caller);
26 
27  if (process)
28  {
29  this->m_Progress = process->GetProgress();
30  }
31 };
32 
33 template <typename TPixel, unsigned int VDim>
34 void
36  double value)
37 {
38  typedef itk::Image<TPixel, VDim> MaskType;
39  typedef itk::Image<ScalarType, VDim> ParameterImageType;
40 
41  typedef itk::ChangeInformationImageFilter< ParameterImageType > OutputImageInformationFilterType;
42  typename OutputImageInformationFilterType::Pointer copyGeoInfoFilter =
43  OutputImageInformationFilterType::New();
44 
45  typename ParameterImageType::Pointer paramImg = ParameterImageType::New();
46 
47  copyGeoInfoFilter->ChangeDirectionOn();
48  copyGeoInfoFilter->SetOutputDirection(image->GetDirection());
49  copyGeoInfoFilter->ChangeOriginOn();
50  copyGeoInfoFilter->SetOutputOrigin(image->GetOrigin());
51  copyGeoInfoFilter->ChangeSpacingOn();
52  copyGeoInfoFilter->SetOutputSpacing(image->GetSpacing());
53  copyGeoInfoFilter->SetInput(paramImg);
54  copyGeoInfoFilter->Update();
55 
56  paramImg = copyGeoInfoFilter->GetOutput();
57  paramImg->SetRegions(image->GetLargestPossibleRegion());
58  paramImg->Allocate();
59  paramImg->FillBuffer(0.0);
60 
61  typedef itk::ImageRegionConstIterator<MaskType> MaskIteratorType;
62  typedef itk::ImageRegionIterator<ParameterImageType> ImageIteratorType;
63 
64  MaskIteratorType maskItr(image, image->GetLargestPossibleRegion());
65  ImageIteratorType imgItr(paramImg, image->GetLargestPossibleRegion());
66  maskItr.GoToBegin();
67  imgItr.GoToBegin();
68  while (!maskItr.IsAtEnd())
69  {
70  if (maskItr.Get() > 0.0)
71  {
72  imgItr.Set(value);
73  }
74 
75  ++maskItr;
76  ++imgItr;
77  }
78 
79  m_TempResultImage = Image::New();
80  mitk::CastToMitkImage(paramImg, m_TempResultImage);
81 }
82 
83 bool
85 {
86  bool result = Superclass::HasOutdatedResult();
87 
88  if (m_ModelParameterizer.IsNotNull())
89  {
90  if (m_ModelParameterizer->GetMTime() > this->m_GenerationTimeStamp)
91  {
92  result = true;
93  }
94  }
95 
96  if (m_Mask.IsNotNull())
97  {
98  if (m_Mask->GetMTime() > this->m_GenerationTimeStamp)
99  {
100  result = true;
101  }
102  }
103 
104  if (m_FitFunctor.IsNotNull())
105  {
106  if (m_FitFunctor->GetMTime() > this->m_GenerationTimeStamp)
107  {
108  result = true;
109  }
110  }
111 
112  return result;
113 };
114 
115 void
117 {
118  Superclass::CheckValidInputs();
119 
120  if (m_Mask.IsNull())
121  {
122  mitkThrow() << "Cannot generate fitted parameter images. Input mask is not set.";
123  }
124 
125  if (m_Signal.Size() != m_TimeGrid.Size())
126  {
127  mitkThrow() << "Cannot generate fitted parameter images. Signal and TimeGrid do not match.";
128  }
129 
130 };
131 
133  parameterImages, ParameterImageMapType& derivedParameterImages,
134  ParameterImageMapType& criterionImages, ParameterImageMapType& evaluationParameterImages)
135 {
136  this->m_Progress = 0;
137 
138  //fit the signal
140  index.Fill(0);
141  this->m_ModelParameterizer->SetDefaultTimeGrid(m_TimeGrid);
142  ParameterizerType::ModelBasePointer parameterizedModel =
143  m_ModelParameterizer->GenerateParameterizedModel(index);
144  ParameterizerType::ParametersType initialParameters =
145  m_ModelParameterizer->GetInitialParameterization(index);
146 
148 
149  for (SignalType::const_iterator pos = m_Signal.begin(); pos != m_Signal.end(); ++pos)
150  {
151  inputValues.push_back(*pos);
152  }
153 
154  ModelFitFunctorBase::OutputPixelArrayType fitResult = m_FitFunctor->Compute(inputValues,
155  parameterizedModel, initialParameters);
156 
157  //generate the results maps
158  ParameterImageMapType tempResultMap;
159  ParameterImageMapType tempDerivedResultMap;
160  ParameterImageMapType tempEvaluationResultMap;
161  ParameterImageMapType tempCriterionResultMap;
162 
163  ModelFitFunctorBase::ParameterNamesType paramNames = parameterizedModel->GetParameterNames();
164  ModelFitFunctorBase::ParameterNamesType derivedParamNames =
165  parameterizedModel->GetDerivedParameterNames();
166  ModelFitFunctorBase::ParameterNamesType criterionNames = this->m_FitFunctor->GetCriterionNames();
167  ModelFitFunctorBase::ParameterNamesType evaluationParamNames =
168  this->m_FitFunctor->GetEvaluationParameterNames();
169  ModelFitFunctorBase::ParameterNamesType debugParamNames = this->m_FitFunctor->GetDebugParameterNames();
170 
171  if (fitResult.size() != (paramNames.size() + derivedParamNames.size() + criterionNames.size() +
172  evaluationParamNames.size() + debugParamNames.size()))
173  {
174  mitkThrow() <<
175  "Error while generating fitted parameter images. Fit functor output size does not match expected parameter number. Output size: "
176  << fitResult.size();
177  }
178 
179  for (ModelFitFunctorBase::ParameterNamesType::size_type j = 0; j < paramNames.size(); ++j)
180  {
181  ModelFitFunctorBase::OutputPixelArrayType::value_type value = fitResult[j];
183  tempResultMap.insert(std::make_pair(paramNames[j], m_TempResultImage));
184  }
185 
186  ModelFitFunctorBase::ParameterNamesType::size_type offset = paramNames.size();
187  for (ModelFitFunctorBase::ParameterNamesType::size_type j = 0; j < derivedParamNames.size(); ++j)
188  {
189  ModelFitFunctorBase::OutputPixelArrayType::value_type value = fitResult[j + offset];
191  tempDerivedResultMap.insert(std::make_pair(derivedParamNames[j], m_TempResultImage));
192  }
193 
194  offset += derivedParamNames.size();
195  for (ModelFitFunctorBase::ParameterNamesType::size_type j = 0; j < criterionNames.size(); ++j)
196  {
197  ModelFitFunctorBase::OutputPixelArrayType::value_type value = fitResult[j + offset];
199  tempCriterionResultMap.insert(std::make_pair(criterionNames[j], m_TempResultImage));
200  }
201 
202  offset += criterionNames.size();
203  for (ModelFitFunctorBase::ParameterNamesType::size_type j = 0; j < evaluationParamNames.size(); ++j)
204  {
205  ModelFitFunctorBase::OutputPixelArrayType::value_type value = fitResult[j + offset];
207  tempEvaluationResultMap.insert(std::make_pair(evaluationParamNames[j], m_TempResultImage));
208  }
209 
210  offset += evaluationParamNames.size();
211  for (ModelFitFunctorBase::ParameterNamesType::size_type j = 0; j < debugParamNames.size(); ++j)
212  { //add debug params (if they are generated to the evaluation result map
213  ModelFitFunctorBase::OutputPixelArrayType::value_type value = fitResult[j + offset];
215  tempEvaluationResultMap.insert(std::make_pair(debugParamNames[j], m_TempResultImage));
216  }
217 
218  parameterImages = tempResultMap;
219  derivedParameterImages = tempDerivedResultMap;
220  criterionImages = tempCriterionResultMap;
221  evaluationParameterImages = tempEvaluationResultMap;
222 
223 };
224 
225 double
227 {
228  return m_Progress;
229 };
230 
233 {
234  ParameterizerType::ModelBasePointer parameterizedModel =
235  m_ModelParameterizer->GenerateParameterizedModel();
236 
237  return parameterizedModel->GetParameterNames();
238 }
239 
242 {
243  ParameterizerType::ModelBasePointer parameterizedModel =
244  m_ModelParameterizer->GenerateParameterizedModel();
245 
246  return parameterizedModel->GetDerivedParameterNames();
247 }
248 
251 {
252  return this->m_FitFunctor->GetCriterionNames();
253 }
254 
257 {
258  auto evals = this->m_FitFunctor->GetEvaluationParameterNames();
259  auto debugs = this->m_FitFunctor->GetDebugParameterNames();
260 
261  evals.insert(evals.end(), debugs.begin(), debugs.end());
262 
263  return evals;
264 }
void onFitProgressEvent(::itk::Object *caller, const ::itk::EventObject &eventObject)
std::vector< ParameterImagePixelType > InputPixelArrayType
ModelBaseType::ParameterNamesType ParameterNamesType
ModelBase::ParameterNamesType ParameterNamesType
#define AccessByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a MITK image by an ITK image with one or more parameters.
static Vector3D offset
#define mitkThrow()
void DoFitAndGetResults(ParameterImageMapType &parameterImages, ParameterImageMapType &derivedParameterImages, ParameterImageMapType &criterionImages, ParameterImageMapType &evaluationParameterImages) override
std::vector< ParameterImagePixelType > OutputPixelArrayType
std::map< ParameterNameType, Image::Pointer > ParameterImageMapType
mitk::Image::Pointer image
static Pointer New()
void DoImageGeneration(itk::Image< TPixel, VDim > *image, double value)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:74
ModelBaseType::ParametersType ParametersType