Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkSpectralUnmixingTest.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 #include <mitkTestFixture.h>
13 #include <mitkTestingMacros.h>
14 
20 #include <mitkImageReadAccessor.h>
21 
22 class mitkSpectralUnmixingTestSuite : public mitk::TestFixture
23 {
24  CPPUNIT_TEST_SUITE(mitkSpectralUnmixingTestSuite);
25  MITK_TEST(testEigenSUAlgorithm);
26  MITK_TEST(testVigraSUAlgorithm);
27  //MITK_TEST(testSimplexSUAlgorithm);
28  MITK_TEST(testSO2);
29  MITK_TEST(testExceptionSO2);
30  MITK_TEST(testWavelengthExceptions);
31  MITK_TEST(testNoChromophoresSelected);
32  MITK_TEST(testInputImage);
33  MITK_TEST(testAddOutput);
34  MITK_TEST(testWeightsError);
35  MITK_TEST(testOutputs);
36  CPPUNIT_TEST_SUITE_END();
37 
38 private:
39  mitk::pa::SpectralUnmixingFilterBase::Pointer m_SpectralUnmixingFilter;
40  mitk::Image::Pointer inputImage;
41  std::vector<int> m_inputWavelengths;
42  std::vector<double> m_inputWeights;
43  std::vector<float> m_CorrectResult;
44  float threshold;
45 
46 public:
47 
48  void setUp() override
49  {
50  //Set empty input image:
51  inputImage = mitk::Image::New();
52  mitk::PixelType pixelType = mitk::MakeScalarPixelType<float>();
53  const int NUMBER_OF_SPATIAL_DIMENSIONS = 3;
54  auto* dimensions = new unsigned int[NUMBER_OF_SPATIAL_DIMENSIONS];
55 
56  dimensions[0] = 1;
57  dimensions[1] = 1;
58  dimensions[2] = 5;
59 
60  inputImage->Initialize(pixelType, NUMBER_OF_SPATIAL_DIMENSIONS, dimensions);
61 
62  //Set wavelengths for unmixing:
63  m_inputWavelengths.push_back(750);
64  m_inputWavelengths.push_back(800);
65 
66  m_inputWeights.push_back(50);
67  m_inputWeights.push_back(100);
68 
69  //Set fraction of Hb and HbO2 to unmix:
70  float fracHb = 100;
71  float fracHbO2 = 300;
72  m_CorrectResult.push_back(fracHbO2);
73  m_CorrectResult.push_back(fracHb);
74  m_CorrectResult.push_back(fracHbO2 + 10);
75  m_CorrectResult.push_back(fracHb - 10);
76  threshold = 0.01;
77 
78  //Multiply values of wavelengths (750,800,850 nm) with fractions to get pixel values:
79  float px1 = fracHb * 7.52 + fracHbO2 * 2.77;
80  float px2 = fracHb * 4.08 + fracHbO2 * 4.37;
81  float px3 = (fracHb - 10) * 7.52 + (fracHbO2 + 10) * 2.77;
82  float px4 = (fracHb - 10) * 4.08 + (fracHbO2 + 10) * 4.37;
83 
84  float* data = new float[6];
85  data[0] = px1;
86  data[1] = px2;
87  data[2] = px3;
88  data[3] = px4;
89  data[5] = 0;
90 
91  inputImage->SetImportVolume(data, mitk::Image::ImportMemoryManagementType::CopyMemory);
92  delete[] data;
93  }
94 
95  // Tests implemented EIGEN algortihms with correct inputs
96  void testEigenSUAlgorithm()
97  {
98  MITK_INFO << "START FILTER TEST ... ";
99  // Set input image
100  auto m_SpectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New();
101  m_SpectralUnmixingFilter->Verbose(false);
102  m_SpectralUnmixingFilter->RelativeError(false);
103  m_SpectralUnmixingFilter->SetInput(inputImage);
104  m_SpectralUnmixingFilter->AddOutputs(2);
105 
106  //Set wavelengths to filter
107  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
108  {
109  unsigned int wavelength = m_inputWavelengths[imageIndex];
110  m_SpectralUnmixingFilter->AddWavelength(wavelength);
111  }
112 
113  //Set Chromophores to filter
114  m_SpectralUnmixingFilter->AddChromophore(
115  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
116  m_SpectralUnmixingFilter->AddChromophore(
117  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
118 
119  ofstream myfile;
120  myfile.open("EigenTestResult.txt");
121 
122  std::vector<mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType> m_Eigen = {
123  mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR, /* mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::LDLT,
124  mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::LLT,*/ mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::COLPIVHOUSEHOLDERQR,
125  mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::JACOBISVD, mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::FULLPIVLU,
126  mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::FULLPIVHOUSEHOLDERQR};
127 
128  for (unsigned int Algorithmidx = 0; Algorithmidx < m_Eigen.size();++Algorithmidx)
129  {
130  m_SpectralUnmixingFilter->SetAlgorithm(m_Eigen[Algorithmidx]);
131 
132  m_SpectralUnmixingFilter->Update();
133 
134 
135  /*For printed pixel values and results look at: [...]\mitk-superbuild\MITK-build\Modules\PhotoacousticsLib\test\*/
136 
137  for (int i = 0; i < 2; ++i)
138  {
139  mitk::Image::Pointer output = m_SpectralUnmixingFilter->GetOutput(i);
140  mitk::ImageReadAccessor readAccess(output);
141  const float* inputDataArray = ((const float*)readAccess.GetData());
142  auto pixel = inputDataArray[0];
143  auto pixel2 = inputDataArray[1];
144 
145  myfile << "Algorithmidx: " << Algorithmidx << "\n Output " << i << ": " << "\n" << inputDataArray[0] << "\n" << inputDataArray[1] << "\n";
146  myfile << "Correct Result: " << "\n" << m_CorrectResult[i] << "\n" << m_CorrectResult[i + 2] << "\n";
147 
148  CPPUNIT_ASSERT(std::abs(pixel - m_CorrectResult[i]) < threshold);
149  CPPUNIT_ASSERT(std::abs(pixel2 - m_CorrectResult[i + 2]) < threshold);
150  }
151  }
152  myfile.close();
153  MITK_INFO << "EIGEN FILTER TEST SUCCESFULL :)";
154  }
155 
156  // Tests implemented VIGRA algortihms with correct inputs
157  void testVigraSUAlgorithm()
158  {
159  MITK_INFO << "START FILTER TEST ... ";
160  // Set input image
161  auto m_SpectralUnmixingFilter = mitk::pa::SpectralUnmixingFilterVigra::New();
162  m_SpectralUnmixingFilter->SetInput(inputImage);
163  m_SpectralUnmixingFilter->AddOutputs(2);
164  m_SpectralUnmixingFilter->Verbose(false);
165  m_SpectralUnmixingFilter->RelativeError(false);
166 
167 
168  //Set wavelengths to filter
169  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
170  {
171  unsigned int wavelength = m_inputWavelengths[imageIndex];
172  double Weight = m_inputWeights[imageIndex];
173  m_SpectralUnmixingFilter->AddWavelength(wavelength);
174  m_SpectralUnmixingFilter->AddWeight(Weight);
175  }
176 
177  //Set Chromophores to filter
178  m_SpectralUnmixingFilter->AddChromophore(
179  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
180  m_SpectralUnmixingFilter->AddChromophore(
181  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
182 
183 
184  std::vector<mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType> Vigra = {
185  mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::LARS, mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::GOLDFARB,
186  mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::WEIGHTED, mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::LS/*,
187  mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::vigratest*/};
188 
189  ofstream myfile;
190  myfile.open("VigraTestResult.txt");
191 
192  for (unsigned int Algorithmidx = 0; Algorithmidx < Vigra.size();++Algorithmidx)
193  {
194  m_SpectralUnmixingFilter->SetAlgorithm(Vigra[0]);
195 
196  m_SpectralUnmixingFilter->Update();
197 
198 
199  /*For printed pixel values and results look at: [...]\mitk-superbuild\MITK-build\Modules\PhotoacousticsLib\test\*/
200 
201  for (int i = 0; i < 2; ++i)
202  {
203  mitk::Image::Pointer output = m_SpectralUnmixingFilter->GetOutput(i);
204  mitk::ImageReadAccessor readAccess(output);
205  const float* inputDataArray = ((const float*)readAccess.GetData());
206  auto pixel = inputDataArray[0];
207  auto pixel2 = inputDataArray[1];
208 
209  myfile << "Algorithmidx: " << Algorithmidx << "\n Output " << i << ": " << "\n" << inputDataArray[0] << "\n" << inputDataArray[1] << "\n";
210  myfile << "Correct Result: " << "\n" << m_CorrectResult[i] << "\n" << m_CorrectResult[i + 2] << "\n";
211 
212  CPPUNIT_ASSERT(std::abs(pixel - m_CorrectResult[i]) < threshold);
213  CPPUNIT_ASSERT(std::abs(pixel2 - m_CorrectResult[i + 2]) < threshold);
214  }
215  }
216  myfile.close();
217  MITK_INFO << "VIGRA FILTER TEST SUCCESFULL :)";
218  }
219 
220  void testSimplexSUAlgorithm()
221  {
222  MITK_INFO << "START FILTER TEST ... ";
223  // Set input image
224  auto m_SpectralUnmixingFilter = mitk::pa::SpectralUnmixingFilterSimplex::New();
225  m_SpectralUnmixingFilter->SetInput(inputImage);
226  m_SpectralUnmixingFilter->AddOutputs(2);
227  m_SpectralUnmixingFilter->Verbose(true);
228  m_SpectralUnmixingFilter->RelativeError(false);
229 
230  //Set wavelengths to filter
231  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
232  {
233  unsigned int wavelength = m_inputWavelengths[imageIndex];
234  m_SpectralUnmixingFilter->AddWavelength(wavelength);
235  }
236 
237  //Set Chromophores to filter
238  m_SpectralUnmixingFilter->AddChromophore(
239  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
240  m_SpectralUnmixingFilter->AddChromophore(
241  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
242 
243  m_SpectralUnmixingFilter->Update();
244 
245 
246  /*For printed pixel values and results look at: [...]\mitk-superbuild\MITK-build\Modules\PhotoacousticsLib\test\*/
247  ofstream myfile;
248  myfile.open("SimplexTestResult.txt");
249  for (int i = 0; i < 2; ++i)
250  {
251  mitk::Image::Pointer output = m_SpectralUnmixingFilter->GetOutput(i);
252  mitk::ImageReadAccessor readAccess(output);
253  const float* inputDataArray = ((const float*)readAccess.GetData());
254  auto pixel = inputDataArray[0];
255  auto pixel2 = inputDataArray[1];
256 
257  myfile << "Output " << i << ": " << "\n" << inputDataArray[0] << "\n" << inputDataArray[1] << "\n";
258  myfile << "Correct Result: " << "\n" << m_CorrectResult[i] << "\n" << m_CorrectResult[i + 2] << "\n";
259 
260  CPPUNIT_ASSERT(std::abs(pixel - m_CorrectResult[i])<threshold);
261  CPPUNIT_ASSERT(std::abs(pixel2 - m_CorrectResult[i + 2])<threshold);
262  }
263  myfile.close();
264  MITK_INFO << "SIMPLEX FILTER TEST SUCCESFULL :)";
265  }
266 
267  // Tests SO2 Filter with unequal inputs
268  void testExceptionSO2()
269  {
270  MITK_INFO << "START EXCEPTION SO2 TEST ... ";
271 
272  auto m_sO2 = mitk::pa::SpectralUnmixingSO2::New();
273  m_sO2->SetInput(0, inputImage);
274 
275  inputImage = nullptr;
276  inputImage = mitk::Image::New();
277  mitk::PixelType pixelType = mitk::MakeScalarPixelType<float>();
278  const int NUMBER_OF_SPATIAL_DIMENSIONS = 3;
279  auto* dimensions = new unsigned int[NUMBER_OF_SPATIAL_DIMENSIONS];
280 
281  dimensions[0] = 1;
282  dimensions[1] = 1;
283  dimensions[2] = 4;
284 
285  inputImage->Initialize(pixelType, NUMBER_OF_SPATIAL_DIMENSIONS, dimensions);
286 
287  float* data = new float[4];
288  data[0] = 1;
289  data[1] = 2;
290  data[2] = 3;
291  data[3] = 4;
292 
293  inputImage->SetImportVolume(data, mitk::Image::ImportMemoryManagementType::CopyMemory);
294  delete[] data;
295 
296  m_sO2->SetInput(1, inputImage);
297 
298  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
299  m_sO2->Update();
300  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
301  }
302 
303  // Tests SO2 Filter with correct inputs
304  void testSO2()
305  {
306  MITK_INFO << "START SO2 TEST ... ";
307  std::vector<float> CorrectSO2Result1 = { 0, 0, 0, 0, 0 };
308  std::vector<float> Test1 = { 0,0,0,51 };
309  std::vector<float> CorrectSO2Result2 = { 0, 0.5, 0, 0.5, 0 };
310  std::vector<float> Test2 = { 1584, 0, 0, 0 };
311  std::vector<float> CorrectSO2Result3 = { 0.5, 0.5, 0, 0.5, 0 };
312  std::vector<float> Test3 = { 0, 1536, 0, 0 };
313  std::vector<float> CorrectSO2Result4 = { 0.5, 0.5, 0, 0.5, 0 };
314  std::vector<float> Test4 = { 0, 0, 3072, 49 };
315  std::vector<float> CorrectSO2Result5 = { 0.5, 0.5, 0.5, 0.5, 0 };
316  std::vector<float> Test5 = { 1, 1, 1, 49 };
317 
318  std::vector<std::vector<float>> TestList;
319  std::vector<std::vector<float>> ResultList;
320 
321  TestList.push_back(Test1);
322  TestList.push_back(Test2);
323  TestList.push_back(Test3);
324  TestList.push_back(Test4);
325  TestList.push_back(Test5);
326 
327  ResultList.push_back(CorrectSO2Result1);
328  ResultList.push_back(CorrectSO2Result2);
329  ResultList.push_back(CorrectSO2Result3);
330  ResultList.push_back(CorrectSO2Result4);
331  ResultList.push_back(CorrectSO2Result5);
332 
333  /*For printed pixel values and results look at: [...]\mitk-superbuild\MITK-build\Modules\PhotoacousticsLib\test\*/
334  ofstream myfile;
335  myfile.open("SO2TestResult.txt");
336 
337  for (int k = 0; k < 5; ++k)
338  {
339  std::vector<float> SO2Settings = TestList[k];
340  std::vector<float> m_CorrectSO2Result = ResultList[k];
341  auto m_sO2 = mitk::pa::SpectralUnmixingSO2::New();
342  m_sO2->SetInput(0, inputImage);
343  m_sO2->SetInput(1, inputImage);
344  for (unsigned int i = 0; i < SO2Settings.size(); ++i)
345  m_sO2->AddSO2Settings(SO2Settings[i]);
346  m_sO2->Update();
347 
348  mitk::Image::Pointer output = m_sO2->GetOutput(0);
349  mitk::ImageReadAccessor readAccess(output);
350  const float* inputDataArray = ((const float*)readAccess.GetData());
351 
352  for (unsigned int Pixel = 0; Pixel < inputImage->GetDimensions()[2]; ++Pixel)
353  {
354  auto Value = inputDataArray[Pixel];
355 
356  myfile << "Output(Test " << k << ") " << Pixel << ": " << "\n" << Value << "\n";
357  myfile << "Correct Result: " << "\n" << m_CorrectSO2Result[Pixel] << "\n";
358 
359  CPPUNIT_ASSERT(std::abs(Value - m_CorrectSO2Result[Pixel]) < threshold);
360  }
361  }
362  myfile.close();
363  MITK_INFO << "SO2 TEST SUCCESFULL :)";
364  }
365 
366  // Test exceptions for wrong wavelength inputs
367  void testWavelengthExceptions()
368  {
369  MITK_INFO << "START WavelengthExceptions TEST ... ";
370  // Set input image
371  auto m_SpectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New();
372  m_SpectralUnmixingFilter->Verbose(false);
373  m_SpectralUnmixingFilter->RelativeError(false);
374  m_SpectralUnmixingFilter->SetInput(inputImage);
375  m_SpectralUnmixingFilter->AddOutputs(2);
376 
377  m_SpectralUnmixingFilter->AddChromophore(
378  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
379  m_SpectralUnmixingFilter->AddChromophore(
380  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
381 
382  m_SpectralUnmixingFilter->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR);
383 
384  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
385  m_SpectralUnmixingFilter->Update();
386  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
387 
388  m_SpectralUnmixingFilter->AddWavelength(300);
389 
390  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
391  m_SpectralUnmixingFilter->Update();
392  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
393 
394  m_SpectralUnmixingFilter->AddWavelength(299);
395 
396  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
397  m_SpectralUnmixingFilter->Update();
398  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
399  MITK_INFO << "DONE";
400  }
401 
402  // Test exceptions for wrong chromophore inputs
403  void testNoChromophoresSelected()
404  {
405  MITK_INFO << "testNoChromophoresSelected";
406  // Set input image
407  auto m_SpectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New();
408  m_SpectralUnmixingFilter->Verbose(false);
409  m_SpectralUnmixingFilter->RelativeError(false);
410  m_SpectralUnmixingFilter->SetInput(inputImage);
411  m_SpectralUnmixingFilter->AddOutputs(2);
412 
413  //Set wavelengths to filter
414  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
415  {
416  unsigned int wavelength = m_inputWavelengths[imageIndex];
417  m_SpectralUnmixingFilter->AddWavelength(wavelength);
418  }
419 
420  m_SpectralUnmixingFilter->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR);
421 
422  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
423  m_SpectralUnmixingFilter->Update();
424  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
425  }
426 
427  // Test exceptions for wrong input image
428  void testInputImage()
429  {
430  MITK_INFO << "INPUT IMAGE TEST";
431  inputImage = nullptr;
432  // Set input image
433  auto m_SpectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New();
434  m_SpectralUnmixingFilter->Verbose(false);
435  m_SpectralUnmixingFilter->RelativeError(false);
436  //m_SpectralUnmixingFilter->SetInput(inputImage);
437  m_SpectralUnmixingFilter->AddOutputs(2);
438 
439  //Set wavelengths to filter
440  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
441  {
442  unsigned int wavelength = m_inputWavelengths[imageIndex];
443  m_SpectralUnmixingFilter->AddWavelength(wavelength);
444  }
445 
446  m_SpectralUnmixingFilter->AddChromophore(
447  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
448  m_SpectralUnmixingFilter->AddChromophore(
449  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
450 
451  m_SpectralUnmixingFilter->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR);
452 
453  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
454  m_SpectralUnmixingFilter->Update();
455  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
456 
457  inputImage = mitk::Image::New();
458  mitk::PixelType pixelType = mitk::MakeScalarPixelType<double>();
459  const int NUMBER_OF_SPATIAL_DIMENSIONS = 3;
460  auto* dimensions = new unsigned int[NUMBER_OF_SPATIAL_DIMENSIONS];
461 
462  dimensions[0] = 1;
463  dimensions[1] = 1;
464  dimensions[2] = 5;
465 
466  inputImage->Initialize(pixelType, NUMBER_OF_SPATIAL_DIMENSIONS, dimensions);
467 
468  double* data = new double[6];
469  data[0] = 1;
470  data[1] = 2;
471  data[2] = 3;
472  data[3] = 4;
473  data[5] = 0;
474 
475  inputImage->SetImportVolume(data, mitk::Image::ImportMemoryManagementType::CopyMemory);
476  delete[] data;
477 
478  m_SpectralUnmixingFilter->SetInput(inputImage);
479 
480  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
481  m_SpectralUnmixingFilter->Update();
482  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
483  }
484 
485  // Test exceptions for addOutputs method
486  void testAddOutput()
487  {
488  MITK_INFO << "addOutputs TEST";
489 
490  // Set input image
491  auto m_SpectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New();
492  m_SpectralUnmixingFilter->Verbose(false);
493  m_SpectralUnmixingFilter->RelativeError(false);
494  m_SpectralUnmixingFilter->SetInput(inputImage);
495 
496  //Set wavelengths to filter
497  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
498  {
499  unsigned int wavelength = m_inputWavelengths[imageIndex];
500  m_SpectralUnmixingFilter->AddWavelength(wavelength);
501  }
502 
503  m_SpectralUnmixingFilter->AddChromophore(
504  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
505  m_SpectralUnmixingFilter->AddChromophore(
506  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
507 
508  m_SpectralUnmixingFilter->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR);
509 
510  for (int i = 0; i < 4; ++i)
511  {
512  MITK_INFO << "i: " << i;
513  if (i != 2)
514  {
515  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
516  m_SpectralUnmixingFilter->AddOutputs(i);
517  m_SpectralUnmixingFilter->Update();
518  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
519  }
520  else
521  {
522  m_SpectralUnmixingFilter->AddOutputs(2);
523  m_SpectralUnmixingFilter->Update();
524  for (int i = 0; i < 2; ++i)
525  {
526  mitk::Image::Pointer output = m_SpectralUnmixingFilter->GetOutput(i);
527  mitk::ImageReadAccessor readAccess(output);
528  const float* inputDataArray = ((const float*)readAccess.GetData());
529  auto pixel = inputDataArray[0];
530  auto pixel2 = inputDataArray[1];
531 
532  CPPUNIT_ASSERT(std::abs(pixel - m_CorrectResult[i]) < threshold);
533  CPPUNIT_ASSERT(std::abs(pixel2 - m_CorrectResult[i + 2]) < threshold);
534  }
535  }
536  }
537  }
538 
539  // Test exceptions for weights error
540  void testWeightsError()
541  {
542  MITK_INFO << "testWeightsError";
543 
544  // Set input image
545  auto m_SpectralUnmixingFilter = mitk::pa::SpectralUnmixingFilterVigra::New();
546  m_SpectralUnmixingFilter->Verbose(false);
547  m_SpectralUnmixingFilter->RelativeError(false);
548  m_SpectralUnmixingFilter->SetInput(inputImage);
549  m_SpectralUnmixingFilter->AddOutputs(2);
550 
551  //Set wavelengths to filter
552  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
553  {
554  unsigned int wavelength = m_inputWavelengths[imageIndex];
555  m_SpectralUnmixingFilter->AddWavelength(wavelength);
556  }
557 
558  m_SpectralUnmixingFilter->AddChromophore(
559  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
560  m_SpectralUnmixingFilter->AddChromophore(
561  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
562 
563  m_SpectralUnmixingFilter->SetAlgorithm(mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::WEIGHTED);
564 
565  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
566  m_SpectralUnmixingFilter->Update();
567  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
568 
569  m_SpectralUnmixingFilter->AddWeight(50);
570  MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
571  m_SpectralUnmixingFilter->Update();
572  MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
573 
574  m_SpectralUnmixingFilter->AddWeight(50);
575  m_SpectralUnmixingFilter->Update();
576 
577  for (int i = 0; i < 2; ++i)
578  {
579  mitk::Image::Pointer output = m_SpectralUnmixingFilter->GetOutput(i);
580  mitk::ImageReadAccessor readAccess(output);
581  const float* inputDataArray = ((const float*)readAccess.GetData());
582  auto pixel = inputDataArray[0];
583  auto pixel2 = inputDataArray[1];
584 
585  CPPUNIT_ASSERT(std::abs(pixel - m_CorrectResult[i]) < threshold);
586  CPPUNIT_ASSERT(std::abs(pixel2 - m_CorrectResult[i + 2]) < threshold);
587  }
588  }
589 
590  // Test correct outputs
591  void testOutputs()
592  {
593  MITK_INFO << "TEST";
594 
595  // Set input image
596  auto m_SpectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New();
597  m_SpectralUnmixingFilter->Verbose(false);
598  m_SpectralUnmixingFilter->RelativeError(false);
599  m_SpectralUnmixingFilter->SetInput(inputImage);
600  m_SpectralUnmixingFilter->AddOutputs(2);
601 
602  //Set wavelengths to filter
603  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
604  {
605  unsigned int wavelength = m_inputWavelengths[imageIndex];
606  m_SpectralUnmixingFilter->AddWavelength(wavelength);
607  }
608 
609  m_SpectralUnmixingFilter->AddChromophore(
610  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
611  m_SpectralUnmixingFilter->AddChromophore(
612  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
613 
614  m_SpectralUnmixingFilter->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR);
615 
616  m_SpectralUnmixingFilter->Update();
617 
618  for (int i = 0; i < 2; ++i)
619  {
620  mitk::Image::Pointer output = m_SpectralUnmixingFilter->GetOutput(i);
621  mitk::ImageReadAccessor readAccess(output);
622  const float* inputDataArray = ((const float*)readAccess.GetData());
623  auto pixel = inputDataArray[0];
624  auto pixel2 = inputDataArray[1];
625 
626  CPPUNIT_ASSERT(std::abs(pixel - m_CorrectResult[i]) < threshold);
627  CPPUNIT_ASSERT(std::abs(pixel2 - m_CorrectResult[i + 2]) < threshold);
628 
629  // test correct output dimensions and pixel type
630  CPPUNIT_ASSERT(inputImage->GetDimensions()[0] == output->GetDimensions()[0]);
631  CPPUNIT_ASSERT(inputImage->GetDimensions()[0] == output->GetDimensions()[1]);
632  CPPUNIT_ASSERT(2 == output->GetDimensions()[2]);
633  CPPUNIT_ASSERT(output->GetPixelType() == mitk::MakeScalarPixelType<float>());
634  }
635  }
636 
637  // TEST TEMPLATE:
638  /*
639  // Test exceptions for
640  void test()
641  {
642  MITK_INFO << "TEST";
643 
644  // Set input image
645  auto m_SpectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New();
646  m_SpectralUnmixingFilter->Verbose(false);
647  m_SpectralUnmixingFilter->RelativeError(false);
648  m_SpectralUnmixingFilter->SetInput(inputImage);
649  m_SpectralUnmixingFilter->AddOutputs(2);
650 
651  //Set wavelengths to filter
652  for (unsigned int imageIndex = 0; imageIndex < m_inputWavelengths.size(); imageIndex++)
653  {
654  unsigned int wavelength = m_inputWavelengths[imageIndex];
655  m_SpectralUnmixingFilter->AddWavelength(wavelength);
656  }
657 
658  m_SpectralUnmixingFilter->AddChromophore(
659  mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED);
660  m_SpectralUnmixingFilter->AddChromophore(
661  mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED);
662 
663  m_SpectralUnmixingFilter->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR);
664 
665  //MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)
666  m_SpectralUnmixingFilter->Update();
667  //MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
668 
669  }*/
670 
671  void tearDown() override
672  {
673  m_SpectralUnmixingFilter = nullptr;
674  inputImage = nullptr;
675  m_inputWavelengths.clear();
676  m_CorrectResult.clear();
677  }
678 };
679 
680 MITK_TEST_SUITE_REGISTRATION(mitkSpectralUnmixing)
float k(1.0)
#define MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Test fixture for parameterized tests.
#define MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS)
Begin block which should be checked for exceptions.
static Pointer New()
ImageReadAccessor class to get locked read access for a particular image part.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51