Medical Imaging Interaction Toolkit  2018.4.99-6aa36ba9
Medical Imaging Interaction Toolkit
mitkCropTimestepsImageFilterTest.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 // Testing
13 #include "mitkTestingMacros.h"
14 #include "mitkTestFixture.h"
15 
16 //MITK includes
17 #include <mitkIOUtil.h>
19 
20 class mitkCropTimestepsImageFilterTestSuite : public mitk::TestFixture
21 {
22  CPPUNIT_TEST_SUITE(mitkCropTimestepsImageFilterTestSuite);
23  MITK_TEST(Constructor_Null);
24  MITK_TEST(Constructor_NoTime);
25  MITK_TEST(Setter_UpperBoundaryTimestepSmallerThanLowerBoundaryTimestep);
26  MITK_TEST(Setter_UpperTimestepsGreaterThanMaxImageTimestep);
27  MITK_TEST(Filter_Default);
28  MITK_TEST(Filter_OnlyLowerBoundary);
29  MITK_TEST(Filter_OnlyUpperBoundary);
30  MITK_TEST(Filter_BothBoundaries);
31  MITK_TEST(Filter_BothBoundaries2Dt);
32  CPPUNIT_TEST_SUITE_END();
33 private:
34  std::string m_ImageFilename2D, m_ImageFilename3D, m_ImageFilename2Dt, m_ImageFilename3Dt;
35  mitk::CropTimestepsImageFilter::Pointer m_cropTimestepsFilter;
36 public:
37  void setUp() override
38  {
39  m_ImageFilename2D = GetTestDataFilePath("Png2D-bw.png");
40  m_ImageFilename3D = GetTestDataFilePath("Pic3D.nrrd");
41  m_ImageFilename2Dt = GetTestDataFilePath("Pic2DplusT.nrrd");
42  m_ImageFilename3Dt = GetTestDataFilePath("3D+t-ITKIO-TestData/LinearModel_4D_arbitrary_time_geometry.nrrd");
43  m_cropTimestepsFilter = mitk::CropTimestepsImageFilter::New();
44  }
45 
46  void tearDown() override
47  {
48  }
49 
50  void Constructor_Null()
51  {
52  m_cropTimestepsFilter->SetInput(nullptr);
53  CPPUNIT_ASSERT_THROW(m_cropTimestepsFilter->Update(), itk::ExceptionObject);
54  }
55 
56  void Constructor_NoTime()
57  {
58  auto refImage2D = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename2D);
59  auto refImage3D = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename3D);
60  m_cropTimestepsFilter->SetInput(refImage2D);
61  CPPUNIT_ASSERT_THROW(m_cropTimestepsFilter->Update(), mitk::Exception);
62  m_cropTimestepsFilter->SetInput(refImage3D);
63  CPPUNIT_ASSERT_THROW(m_cropTimestepsFilter->Update(), mitk::Exception);
64  }
65 
66  void Setter_UpperBoundaryTimestepSmallerThanLowerBoundaryTimestep()
67  {
68  //check for Exception if UpperBoundaryTimestep < LowerBoundaryTimestep
69  auto refImage2Dt = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename2Dt);
70  m_cropTimestepsFilter->SetInput(refImage2Dt);
71  unsigned int lowerTimeStep = 5;
72  unsigned int upperTimeStep = 4;
73  m_cropTimestepsFilter->SetLowerBoundaryTimestep(lowerTimeStep);
74  m_cropTimestepsFilter->SetUpperBoundaryTimestep(upperTimeStep);
75  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetLowerBoundaryTimestep(), lowerTimeStep);
76  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetUpperBoundaryTimestep(), upperTimeStep);
77  CPPUNIT_ASSERT_THROW(m_cropTimestepsFilter->Update(), mitk::Exception);
78  }
79 
80  void Setter_UpperTimestepsGreaterThanMaxImageTimestep()
81  {
82  //check for correction if UpperBoundaryTimestep > image->GetTimestep()
83  auto refImage2Dt = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename2Dt);
84  m_cropTimestepsFilter->SetInput(refImage2Dt);
85  unsigned int upperTimeStep = 11;
86  m_cropTimestepsFilter->SetUpperBoundaryTimestep(upperTimeStep);
87  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetUpperBoundaryTimestep(), upperTimeStep);
88  CPPUNIT_ASSERT_NO_THROW(m_cropTimestepsFilter->Update());
89  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetUpperBoundaryTimestep(), refImage2Dt->GetTimeSteps());
90  }
91 
92  void Filter_Default()
93  {
94  //Everything default: timesteps should stay the same
95  auto refImage3Dt = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename3Dt);
96  auto timeGeometryBefore = refImage3Dt->GetTimeGeometry()->Clone();
97  m_cropTimestepsFilter->SetInput(refImage3Dt);
98  unsigned int expectedLowerTimestep = 0;
99  unsigned int expectedUpperTimestep = std::numeric_limits<unsigned int>::max();
100  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetLowerBoundaryTimestep(), expectedLowerTimestep);
101  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetUpperBoundaryTimestep(), expectedUpperTimestep);
102  CPPUNIT_ASSERT_NO_THROW(m_cropTimestepsFilter->Update());
103  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetLowerBoundaryTimestep(), expectedLowerTimestep);
104  CPPUNIT_ASSERT_EQUAL(m_cropTimestepsFilter->GetUpperBoundaryTimestep(), refImage3Dt->GetTimeSteps());
105  auto result = m_cropTimestepsFilter->GetOutput();
106  auto timeGeometry = result->GetTimeGeometry();
107  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->GetMinimumTimePoint(), timeGeometry->GetMinimumTimePoint());
108  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->GetMaximumTimePoint(), timeGeometry->GetMaximumTimePoint());
109 
110  CPPUNIT_ASSERT_EQUAL(result->GetTimeSteps(), refImage3Dt->GetTimeSteps());
111  }
112 
113  void Filter_OnlyLowerBoundary()
114  {
115  //Crop lower 2 timesteps
116  auto refImage3Dt = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename3Dt);
117  MITK_WARN << "before: " << *refImage3Dt;
118  auto timeGeometryBefore = refImage3Dt->GetTimeGeometry()->Clone();
119  m_cropTimestepsFilter->SetInput(refImage3Dt);
120  unsigned int lowerTimestep = 2;
121  m_cropTimestepsFilter->SetLowerBoundaryTimestep(lowerTimestep);
122  CPPUNIT_ASSERT_NO_THROW(m_cropTimestepsFilter->Update());
123  auto result = m_cropTimestepsFilter->GetOutput();
124  auto timeGeometry = result->GetTimeGeometry();
125  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->TimeStepToTimePoint(lowerTimestep), timeGeometry->GetMinimumTimePoint());
126  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->GetMaximumTimePoint(), timeGeometry->GetMaximumTimePoint());
127  CPPUNIT_ASSERT_EQUAL(refImage3Dt->GetTimeSteps()-lowerTimestep, result->GetTimeSteps());
128  }
129 
130  void Filter_OnlyUpperBoundary()
131  {
132  // Crop upper 3 timesteps
133  auto refImage3Dt = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename3Dt);
134  auto timeGeometryBefore = refImage3Dt->GetTimeGeometry()->Clone();
135  m_cropTimestepsFilter->SetInput(refImage3Dt);
136  unsigned int upperTimestep = 7;
137  m_cropTimestepsFilter->SetUpperBoundaryTimestep(upperTimestep);
138  CPPUNIT_ASSERT_NO_THROW(m_cropTimestepsFilter->Update());
139  auto result = m_cropTimestepsFilter->GetOutput();
140  auto timeGeometry = result->GetTimeGeometry();
141  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->TimeStepToTimePoint(upperTimestep), timeGeometry->GetMaximumTimePoint());
142  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->GetMinimumTimePoint(), timeGeometry->GetMinimumTimePoint());
143  CPPUNIT_ASSERT_EQUAL(upperTimestep, result->GetTimeSteps());
144  }
145 
146  void Filter_BothBoundaries()
147  {
148  //Crop lower 2 and upper 3 timesteps
149  auto refImage3Dt = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename3Dt);
150  m_cropTimestepsFilter->SetInput(refImage3Dt);
151  auto timeGeometryBefore = refImage3Dt->GetTimeGeometry()->Clone();
152  unsigned int lowerTimestep = 1;
153  unsigned int upperTimestep = 7;
154  m_cropTimestepsFilter->SetLowerBoundaryTimestep(lowerTimestep);
155  m_cropTimestepsFilter->SetUpperBoundaryTimestep(upperTimestep);
156  CPPUNIT_ASSERT_NO_THROW(m_cropTimestepsFilter->Update());
157  auto result = m_cropTimestepsFilter->GetOutput();
158  auto timeGeometry = result->GetTimeGeometry();
159  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->TimeStepToTimePoint(upperTimestep), timeGeometry->GetMaximumTimePoint());
160  CPPUNIT_ASSERT_EQUAL(timeGeometryBefore->TimeStepToTimePoint(lowerTimestep), timeGeometry->GetMinimumTimePoint());
161  CPPUNIT_ASSERT_EQUAL(upperTimestep-lowerTimestep, result->GetTimeSteps());
162  }
163 
164  void Filter_BothBoundaries2Dt()
165  {
166  //Crop lower 1 and upper 1 timestep, resulting in 1 remaining timestep (2D+t input)
167  auto refImage2Dt = mitk::IOUtil::Load<mitk::Image>(m_ImageFilename2Dt);
168  m_cropTimestepsFilter->SetInput(refImage2Dt);
169  unsigned int lowerTimestep = 1;
170  unsigned int upperTimestep = 2;
171  m_cropTimestepsFilter->SetLowerBoundaryTimestep(lowerTimestep);
172  m_cropTimestepsFilter->SetUpperBoundaryTimestep(upperTimestep);
173  CPPUNIT_ASSERT_NO_THROW(m_cropTimestepsFilter->Update());
174  auto result = m_cropTimestepsFilter->GetOutput();
175  auto timeGeometry = result->GetTimeGeometry();
176  CPPUNIT_ASSERT_EQUAL(mitk::TimePointType(upperTimestep), timeGeometry->GetMaximumTimePoint());
177  CPPUNIT_ASSERT_EQUAL(mitk::TimePointType(lowerTimestep), timeGeometry->GetMinimumTimePoint());
178  CPPUNIT_ASSERT_EQUAL(upperTimestep - lowerTimestep, result->GetTimeSteps());
179  }
180 
181 };
182 MITK_TEST_SUITE_REGISTRATION(mitkCropTimestepsImageFilter)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static Pointer New()
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
#define MITK_WARN
Definition: mitkLogMacros.h:19
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
Test fixture for parameterized tests.
mitk::ScalarType TimePointType
static T max(T x, T y)
Definition: svm.cpp:56