Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLRDensityEstimation.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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
21 
22 #include <itkImageRegionIterator.h>
23 
25 
26 #include <mitkIOUtil.h>
27 #include "itkImageFileWriter.h"
28 
29 static void EnsureDataImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target);
30 //static std::size_t CountElementsInCollection(mitk::DataCollection::Pointer collection, std::string maskName);
31 
32 void
34 {
35  m_Collection = data;
36 }
37 
40 {
41  return m_Collection;
42 }
43 
44 void
46 {
47  m_TestMask = name;
48 }
49 
50 std::string
52 {
53  return m_TestMask;
54 }
55 
56 void
58 {
59  m_TrainMask = name;
60 }
61 
62 std::string
64 {
65  return m_TrainMask;
66 }
67 
68 void
70 {
71  m_WeightName = name;
72 }
73 
74 std::string
76 {
77  return m_WeightName;
78 }
79 
80 void
82 {
83  typedef itk::Image<unsigned char, 3> MaskImageType;
84  typedef itk::Image<double , 3> FeatureImageType;
85 
87  EnsureDataImageInCollection(m_Collection, m_TrainMask, m_WeightName);
88 
90  DataCollectionSingleImageIterator<unsigned char, 3> train(m_Collection, m_TrainMask);
92  DataCollectionSingleImageIterator<double, 3> weight (m_Collection, m_WeightName);
93  std::vector<DataCollectionSingleImageIterator<double, 3> > featureList;
94  for (unsigned int i = 0; i < m_Modalities.size(); ++i)
95  {
96  DataCollectionSingleImageIterator<double, 3> iter(m_Collection, m_Modalities[i]);
97  featureList.push_back(iter);
98  }
99 
100  // Do for each image...
101  // char buchstabe = 'A';
102  while (!train.IsAtEnd())
103  {
104  itk::ImageRegionIterator<MaskImageType> trainIter(train.GetImage(), train.GetImage()->GetLargestPossibleRegion() );
105  itk::ImageRegionIterator<MaskImageType> testIter(test.GetImage(), test.GetImage()->GetLargestPossibleRegion() );
106 
107  int trainElements = 0;
108  int testElements = 0;
109  while (!trainIter.IsAtEnd())
110  {
111  if (trainIter.Get() > 0)
112  {
113  ++trainElements;
114  }
115  if (testIter.Get() > 0)
116  {
117  ++testElements;
118  }
119  ++trainIter;
120  ++testIter;
121  }
122  trainIter.GoToBegin();
123  testIter.GoToBegin();
124 
125  std::vector<itk::ImageRegionIterator<FeatureImageType> > featureIter;
126  for (unsigned int i = 0; i < featureList.size(); ++i)
127  {
128  itk::ImageRegionIterator<FeatureImageType> iter(featureList[i].GetImage(), featureList[i].GetImage()->GetLargestPossibleRegion() );
129  featureIter.push_back(iter);
130  }
131 
132  vnl_vector<double> label(trainElements + testElements);
133  vnl_matrix<double> feature(trainElements + testElements, featureList.size() );
134  vnl_matrix<double> trainFeature(trainElements, featureList.size() );
135 
136  int index = 0;
137  int trainIndex = 0;
138  while (!trainIter.IsAtEnd())
139  {
140  if (trainIter.Get() > 0)
141  {
142  label(index) = 1;
143  for (unsigned int i = 0; i < featureIter.size(); ++i)
144  {
145  feature(index, i) = featureIter[i].Get();
146  trainFeature(trainIndex, i) = featureIter[i].Get();
147  }
148  ++index;
149  ++trainIndex;
150  }
151  if (testIter.Get() > 0)
152  {
153  label(index) = 0;
154  for (unsigned int i = 0; i < featureIter.size(); ++i)
155  {
156  feature(index, i) = featureIter[i].Get();
157  }
158  ++index;
159  }
160 
161  ++trainIter;
162  ++testIter;
163  for (unsigned int i = 0; i < featureIter.size(); ++i)
164  {
165  ++(featureIter[i]);
166  }
167  }
168 
169  mitk::GeneralizedLinearModel glm(feature, label);
170  vnl_vector<double> weightVector = glm.ExpMu(trainFeature);
171 
172  itk::ImageRegionIterator<FeatureImageType> weightIter(weight.GetImage(), weight.GetImage()->GetLargestPossibleRegion() );
173 
174  trainIter.GoToBegin();
175  index = 0;
176  //double ratio = (trainElements * 1.0) / (testElements * 1.0);
177  //ratio = 1.0 / weightVector.one_norm()*weightVector.size();
178  while(!trainIter.IsAtEnd())
179  {
180  if (trainIter.Get() > 0)
181  {
182  weightIter.Set(weightVector(index) );
183  ++index;
184  }
185  ++trainIter;
186  ++weightIter;
187  }
188 
189  //std::stringstream s;
190  // s<<"d:/tmp/img/datei"<<buchstabe<<".nrrd";
191  //std::string blub =s.str();
192  //MITK_INFO << blub;
193  //FeatureImageType::Pointer image = weight.GetImage();
194  //typedef itk::ImageFileWriter< FeatureImageType > WriterType;
195  //WriterType::Pointer writer = WriterType::New();
196  //writer->SetFileName(blub);
197  //writer->SetInput(image);
198  //writer->Update();
199  //++buchstabe;
200 
201  ++train;
202  ++test;
203  ++weight;
204  for (unsigned int i = 0; i < featureList.size(); ++i)
205  {
206  ++(featureList[i]);
207  }
208  }
209 }
210 
211 void
212  mitk::LRDensityEstimation::SetModalities(std::vector<std::string> modalities)
213 {
214  m_Modalities = modalities;
215 }
216 
217 std::vector<std::string>
219 {
220  return m_Modalities;
221 }
222 
223 //static std::size_t CountElementsInCollection(mitk::DataCollection::Pointer collection, std::string maskName)
224 //{
225 // std::size_t count = 0;
226 // mitk::DataCollectionImageIterator<unsigned char,3> iter(collection, maskName);
227 // while (!iter.IsAtEnd())
228 // {
229 // if (iter.GetVoxel() > 0)
230 // {
231 // ++count;
232 // }
233 // ++iter;
234 // }
235 // return count;
236 //}
237 
238 static void EnsureDataImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
239 {
240  typedef itk::Image<double, 3> FeatureImage;
241  typedef itk::Image<unsigned char, 3> LabelImage;
242 
244  while (!iter.IsAtEnd())
245  {
246  ++iter;
247  }
248 
249  if (collection->HasElement(origin))
250  {
251  LabelImage::Pointer originImage = dynamic_cast<LabelImage*>(collection->GetData(origin).GetPointer());
252  if (!collection->HasElement(target) && originImage.IsNotNull())
253  {
254  MITK_INFO << "New image necessary";
256  image->SetRegions(originImage->GetLargestPossibleRegion());
257  image->SetSpacing(originImage->GetSpacing());
258  image->SetOrigin(originImage->GetOrigin());
259  image->SetDirection(originImage->GetDirection());
260  image->Allocate();
261 
262  collection->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),target,"");
263  }
264  }
265  for (std::size_t i = 0; i < collection->Size();++i)
266  {
267  mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(collection->GetData(i).GetPointer());
268  if (newCol != 0)
269  {
270  EnsureDataImageInCollection(newCol, origin, target);
271  }
272  }
273 }
274 
275 void
277 {
279  EnsureDataImageInCollection(train, m_TrainMask, m_WeightName);
280 
282  DataCollectionImageIterator<unsigned char, 3> trainIter(train, m_TrainMask);
283  DataCollectionImageIterator<unsigned char, 3> testIter (test, m_TestMask);
284  DataCollectionImageIterator<double, 3> weightIter (train, m_WeightName);
285  std::vector<DataCollectionImageIterator<double, 3> > trainFeatureList;
286  std::vector<DataCollectionImageIterator<double, 3> > testFeatureList;
287  for (unsigned int i = 0; i < m_Modalities.size(); ++i)
288  {
289  DataCollectionImageIterator<double, 3> iter(train, m_Modalities[i]);
290  trainFeatureList.push_back(iter);
291  DataCollectionImageIterator<double, 3> iter2(train, m_Modalities[i]);
292  testFeatureList.push_back(iter2);
293  }
294 
295  int trainElements = 0;
296  int testElements = 0;
297  while (!trainIter.IsAtEnd())
298  {
299  if (trainIter.GetVoxel() > 0)
300  ++trainElements;
301  ++trainIter;
302  }
303  while (!testIter.IsAtEnd() )
304  {
305  if (testIter.GetVoxel() > 0)
306  ++testElements;
307  ++testIter;
308  }
309  trainIter.ToBegin();
310  testIter.ToBegin();
311 
312  vnl_vector<double> label(trainElements + testElements);
313  vnl_matrix<double> feature(trainElements + testElements, trainFeatureList.size() );
314  vnl_matrix<double> trainFeature(trainElements, trainFeatureList.size() );
315 
316  int trainIndex = 0;
317  int testIndex = 0;
318  while (!trainIter.IsAtEnd())
319  {
320  if (trainIter.GetVoxel() > 0)
321  {
322  label(trainIndex) = 1;
323  for (unsigned int i = 0; i < trainFeatureList.size(); ++i)
324  {
325  feature(trainIndex, i) = trainFeatureList[i].GetVoxel();
326  trainFeature(trainIndex, i) = trainFeatureList[i].GetVoxel();
327  }
328 
329  ++trainIndex;
330  }
331 
332  ++trainIter;
333  for (unsigned int i = 0; i < trainFeatureList.size(); ++i)
334  {
335  ++(trainFeatureList[i]);
336  }
337  }
338 
339  while (!testIter.IsAtEnd())
340  {
341  if (testIter.GetVoxel() > 0)
342  {
343  label(trainIndex) = 1;
344  for (unsigned int i = 0; i < testFeatureList.size(); ++i)
345  {
346  feature(trainIndex+testIndex, i) = testFeatureList[i].GetVoxel();
347  }
348  ++testIndex;
349  }
350 
351  ++testIter;
352  for (unsigned int i = 0; i < testFeatureList.size(); ++i)
353  {
354  ++(testFeatureList[i]);
355  }
356  }
357 
358  mitk::GeneralizedLinearModel glm(feature, label);
359  vnl_vector<double> weightVector = glm.ExpMu(trainFeature);
360 
361  trainIter.ToBegin();
362  int index = 0;
363  while (!trainIter.IsAtEnd())
364  {
365  if (trainIter.GetVoxel() > 0)
366  {
367  weightIter.SetVoxel(weightVector(index));
368  ++index;
369  }
370  ++trainIter;
371  ++weightIter;
372  }
373 }
374 
376 {
377  typedef itk::Image<unsigned char, 3> MaskImageType;
378  typedef itk::Image<double , 3> FeatureImageType;
379 
381  EnsureDataImageInCollection(m_Collection, m_TrainMask, m_WeightName);
382 
384  DataCollectionSingleImageIterator<unsigned char, 3> train(m_Collection, m_TrainMask);
386  DataCollectionSingleImageIterator<double, 3> weight (m_Collection, m_WeightName);
387  std::vector<DataCollectionSingleImageIterator<double, 3> > featureList;
388  for (unsigned int i = 0; i < m_Modalities.size(); ++i)
389  {
390  DataCollectionSingleImageIterator<double, 3> iter(m_Collection, m_Modalities[i]);
391  featureList.push_back(iter);
392  }
393 
394  while (!train.IsAtEnd())
395  {
396  itk::ImageRegionIterator<MaskImageType> trainIter(train.GetImage(), train.GetImage()->GetLargestPossibleRegion() );
397  itk::ImageRegionIterator<MaskImageType> testIter(test.GetImage(), test.GetImage()->GetLargestPossibleRegion() );
398 
399  int trainElements = 0;
400  int testElements = 0;
401  while (!trainIter.IsAtEnd())
402  {
403  if (trainIter.Get() > 0)
404  {
405  ++trainElements;
406  }
407  if (testIter.Get() > 0)
408  {
409  ++testElements;
410  }
411  ++trainIter;
412  ++testIter;
413  }
414  trainIter.GoToBegin();
415  testIter.GoToBegin();
416 
417  std::vector<itk::ImageRegionIterator<FeatureImageType> > featureIter;
418  for (unsigned int i = 0; i < featureList.size(); ++i)
419  {
420  itk::ImageRegionIterator<FeatureImageType> iter(featureList[i].GetImage(), featureList[i].GetImage()->GetLargestPossibleRegion() );
421  featureIter.push_back(iter);
422  }
423 
424  vnl_vector<double> label(trainElements + testElements);
425  vnl_matrix<double> feature(trainElements + testElements, featureList.size() );
426  vnl_matrix<double> trainFeature(trainElements, featureList.size() );
427 
428  int index = 0;
429  int trainIndex = 0;
430  while (!trainIter.IsAtEnd())
431  {
432  if (trainIter.Get() > 0)
433  {
434  label(index) = 1;
435  for (unsigned int i = 0; i < featureIter.size(); ++i)
436  {
437  feature(index, i) = featureIter[i].Get();
438  trainFeature(trainIndex, i) = featureIter[i].Get();
439  }
440  ++index;
441  ++trainIndex;
442  }
443  if (testIter.Get() > 0)
444  {
445  label(index) = 0;
446  for (unsigned int i = 0; i < featureIter.size(); ++i)
447  {
448  feature(index, i) = featureIter[i].Get();
449  }
450  ++index;
451  }
452 
453  ++trainIter;
454  ++testIter;
455  for (unsigned int i = 0; i < featureIter.size(); ++i)
456  {
457  ++(featureIter[i]);
458  }
459  }
460 
461  mitk::GeneralizedLinearModel glm(feature, label);
462  vnl_vector<double> weightVector = glm.Predict(trainFeature);
463 
464  itk::ImageRegionIterator<FeatureImageType> weightIter(weight.GetImage(), weight.GetImage()->GetLargestPossibleRegion() );
465 
466  trainIter.GoToBegin();
467  index = 0;
468 
469  while(!trainIter.IsAtEnd())
470  {
471  if (trainIter.Get() > 0)
472  {
473  weightIter.Set(weightVector(index) );
474  ++index;
475  }
476  ++trainIter;
477  ++weightIter;
478  }
479 
480  ++train;
481  ++test;
482  ++weight;
483  for (unsigned int i = 0; i < featureList.size(); ++i)
484  {
485  ++(featureList[i]);
486  }
487  }
488 }
void SetWeightName(std::string name)
itk::Image< mitk::ScalarType, 3 > FeatureImage
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:32
double Predict(const vnl_vector< double > &c)
Predicts the value corresponding to the given vector.
void SetTrainMask(std::string name)
DataCollection::Pointer GetCollection()
Generalized Linear Model that allows linear models for non-gaussian data.
void SetTestMask(std::string name)
static void EnsureDataImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
std::vector< std::string > GetModalities()
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:36
void SetModalities(std::vector< std::string > modalities)
void SetCollection(DataCollection::Pointer data)
vnl_vector< double > ExpMu(const vnl_matrix< double > &x)
Estimation of the exponential factor for a given function.
itk::SmartPointer< Self > Pointer
Definition: mitkBaseData.h:42
void WeightsForAll(mitk::DataCollection::Pointer train, mitk::DataCollection::Pointer test)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.