Medical Imaging Interaction Toolkit  2018.4.99-a28b433c
Medical Imaging Interaction Toolkit
mitkPhotoacousticIOTest.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 <mitkTestFixture.h>
14 #include <mitkTestingMacros.h>
15 
16 #include <mitkPAVesselTree.h>
17 #include <mitkPAIOUtil.h>
18 #include <mitkIOUtil.h>
19 #include <mitkCoreServices.h>
22 #include <itkFileTools.h>
23 
24 class mitkPhotoacousticIOTestSuite : public mitk::TestFixture
25 {
26  CPPUNIT_TEST_SUITE(mitkPhotoacousticIOTestSuite);
27  MITK_TEST(testLoadInSilicoTissueNrrdFile);
28  MITK_TEST(testLoad3DVolumeNrrdFile);
29  MITK_TEST(testLoad3DVolumeNrrdFileWithBlur);
30  MITK_TEST(testGetNumberOfNrrdFilesInTestDir);
31  MITK_TEST(testGetChildFoldersFromFolder);
32  MITK_TEST(testLoadFCMs);
33  CPPUNIT_TEST_SUITE_END();
34 
35 private:
36 
37  const std::string TEST_FOLDER_PATH = "testFiles/";
38  const std::string TEST_IN_SILICO_VOLUME_PATH = "testInSilicoVolume";
39  const std::string TEST_3D_Volume_PATH = "test3DVolume";
40  const std::string TEST_FILE_ENDING = ".nrrd";
41  const std::string TEST_QUALIFIED_FOLDER_PATH = TEST_FOLDER_PATH + TEST_IN_SILICO_VOLUME_PATH + "/";
42  const std::string FOLDER_FOLDER = "folder/";
43  const std::string FCM_PATH = TEST_FOLDER_PATH + "fcms/";
44  const int NUMBER_OF_NRRD_FILES_IN_TEST_DIR = 2;
45  mitk::pa::TissueGeneratorParameters::Pointer m_VolumeProperties;
46  mitk::pa::InSilicoTissueVolume::Pointer m_TestInSilicoVolume;
47  mitk::pa::Volume::Pointer m_Test3DVolume;
48 
49 public:
50 
51  void setUp() override
52  {
53  m_VolumeProperties = createTestVolumeParameters();
54  auto rng = std::mt19937();
55  m_TestInSilicoVolume = mitk::pa::InSilicoTissueVolume::New(m_VolumeProperties, &rng);
56  m_Test3DVolume = createTest3DVolume(5);
57  itk::FileTools::CreateDirectory(TEST_FOLDER_PATH);
58  itk::FileTools::CreateDirectory(TEST_QUALIFIED_FOLDER_PATH);
59  itk::FileTools::CreateDirectory(TEST_FOLDER_PATH + FOLDER_FOLDER + FOLDER_FOLDER);
60  itk::FileTools::CreateDirectory(FCM_PATH);
61  CPPUNIT_ASSERT(itksys::SystemTools::FileIsDirectory(TEST_FOLDER_PATH));
62  CPPUNIT_ASSERT(itksys::SystemTools::FileIsDirectory(TEST_QUALIFIED_FOLDER_PATH));
63  CPPUNIT_ASSERT(itksys::SystemTools::FileIsDirectory(TEST_FOLDER_PATH + FOLDER_FOLDER + FOLDER_FOLDER));
64  CPPUNIT_ASSERT(itksys::SystemTools::FileIsDirectory(FCM_PATH));
65 
66  mitk::IOUtil::Save(m_TestInSilicoVolume->ConvertToMitkImage(),
67  TEST_FOLDER_PATH + TEST_IN_SILICO_VOLUME_PATH + TEST_FILE_ENDING);
68  mitk::IOUtil::Save(m_Test3DVolume->AsMitkImage(),
69  TEST_FOLDER_PATH + TEST_3D_Volume_PATH + TEST_FILE_ENDING);
70  auto yo0 = createTest3DVolume(1)->AsMitkImage();
71  auto yo1 = createTest3DVolume(2)->AsMitkImage();
72 
73  yo0->GetPropertyList()->SetStringProperty("y-offset", "0");
74  yo1->GetPropertyList()->SetStringProperty("y-offset", "1");
76 
77  mitk::IOUtil::Save(yo0, TEST_QUALIFIED_FOLDER_PATH + TEST_IN_SILICO_VOLUME_PATH + "_yo0" + TEST_FILE_ENDING);
78  mitk::IOUtil::Save(yo1, TEST_QUALIFIED_FOLDER_PATH + TEST_IN_SILICO_VOLUME_PATH + "_yo1" + TEST_FILE_ENDING);
79  }
80 
81  mitk::pa::Volume::Pointer createTest3DVolume(double value)
82  {
83  unsigned int xDim = 10;
84  unsigned int yDim = 10;
85  unsigned int zDim = 10;
86  unsigned int length = xDim * yDim * zDim;
87  auto* data = new double[length];
88  for (unsigned int i = 0; i < length; i++)
89  data[i] = value;
90 
91  return mitk::pa::Volume::New(data, xDim, yDim, zDim, 1);
92  }
93 
94  mitk::pa::TissueGeneratorParameters::Pointer createTestVolumeParameters()
95  {
96  auto returnParameters = mitk::pa::TissueGeneratorParameters::New();
97  returnParameters->SetXDim(10);
98  returnParameters->SetYDim(10);
99  returnParameters->SetZDim(10);
100  returnParameters->SetMinBackgroundAbsorption(0);
101  returnParameters->SetMaxBackgroundAbsorption(0);
102  returnParameters->SetBackgroundScattering(0);
103  returnParameters->SetBackgroundAnisotropy(0);
104  return returnParameters;
105  }
106 
107  void assertEqual(mitk::pa::Volume::Pointer first, mitk::pa::Volume::Pointer second)
108  {
109  CPPUNIT_ASSERT(first->GetXDim() == second->GetXDim());
110  CPPUNIT_ASSERT(first->GetYDim() == second->GetYDim());
111  CPPUNIT_ASSERT(first->GetZDim() == second->GetZDim());
112  for (unsigned int x = 0; x < first->GetXDim(); ++x)
113  for (unsigned int y = 0; y < first->GetYDim(); ++y)
114  for (unsigned int z = 0; z < first->GetZDim(); ++z)
115  {
116  std::string message = "Expected " + std::to_string(first->GetData(x, y, z)) + " but was " + std::to_string(second->GetData(x, y, z));
117  CPPUNIT_ASSERT_MESSAGE(message, std::abs(first->GetData(x, y, z) - second->GetData(x, y, z)) < 1e-6);
118  }
119  }
120 
121  void testLoadInSilicoTissueNrrdFile()
122  {
123  auto loadedVolume = mitk::pa::IOUtil::LoadInSilicoTissueVolumeFromNrrdFile(TEST_FOLDER_PATH + TEST_IN_SILICO_VOLUME_PATH + TEST_FILE_ENDING);
124  CPPUNIT_ASSERT(loadedVolume->GetTDim() == m_TestInSilicoVolume->GetTDim());
125  assertEqual(m_TestInSilicoVolume->GetAbsorptionVolume(), loadedVolume->GetAbsorptionVolume());
126  assertEqual(m_TestInSilicoVolume->GetScatteringVolume(), loadedVolume->GetScatteringVolume());
127  assertEqual(m_TestInSilicoVolume->GetAnisotropyVolume(), loadedVolume->GetAnisotropyVolume());
128  }
129 
130  void testLoad3DVolumeNrrdFile()
131  {
132  auto loadedVolume = mitk::pa::IOUtil::LoadNrrd(TEST_FOLDER_PATH + TEST_3D_Volume_PATH + TEST_FILE_ENDING);
133  assertEqual(loadedVolume, m_Test3DVolume);
134  }
135 
136  void testLoad3DVolumeNrrdFileWithBlur()
137  {
138  auto loadedVolume = mitk::pa::IOUtil::LoadNrrd(TEST_FOLDER_PATH + TEST_3D_Volume_PATH + TEST_FILE_ENDING, 1);
139  assertEqual(loadedVolume, m_Test3DVolume);
140  }
141 
142  void testGetNumberOfNrrdFilesInTestDir()
143  {
144  int numberOfFiles = mitk::pa::IOUtil::GetNumberOfNrrdFilesInDirectory(TEST_FOLDER_PATH);
145  CPPUNIT_ASSERT(numberOfFiles == NUMBER_OF_NRRD_FILES_IN_TEST_DIR);
146  }
147 
148  void testGetChildFoldersFromFolder()
149  {
150  std::vector<std::string> childFolders = mitk::pa::IOUtil::GetAllChildfoldersFromFolder(TEST_FOLDER_PATH);
151  CPPUNIT_ASSERT(childFolders.size() == 1);
152  CPPUNIT_ASSERT(childFolders[0] == TEST_FOLDER_PATH);
153  childFolders = mitk::pa::IOUtil::GetAllChildfoldersFromFolder(TEST_FOLDER_PATH + FOLDER_FOLDER);
154  MITK_INFO << "ChildFolders: " << childFolders.size();
155  CPPUNIT_ASSERT(childFolders.size() == 1);
156  CPPUNIT_ASSERT(childFolders[0] == TEST_FOLDER_PATH + FOLDER_FOLDER + "/folder");
157  }
158 
159  void testLoadFCMs()
160  {
161  auto fcm1 = createTest3DVolume(1);
162  auto fcm2 = createTest3DVolume(2);
163  auto fcm3 = createTest3DVolume(3);
164  auto fcm4 = createTest3DVolume(4);
165  mitk::IOUtil::Save(fcm1->AsMitkImage(), FCM_PATH + "fcm1_p0,0,0FluenceContributionMap.nrrd");
166  mitk::IOUtil::Save(fcm2->AsMitkImage(), FCM_PATH + "fcm1_p0,0,1FluenceContributionMap.nrrd");
167  mitk::IOUtil::Save(fcm3->AsMitkImage(), FCM_PATH + "fcm1_p1,0,0FluenceContributionMap.nrrd");
168  mitk::IOUtil::Save(fcm4->AsMitkImage(), FCM_PATH + "fcm1_p1,0,1FluenceContributionMap.nrrd");
169 
170  int prog = 0;
171  auto map = mitk::pa::IOUtil::LoadFluenceContributionMaps(FCM_PATH, 0, &prog);
172 
173  assertEqual(fcm1, map[mitk::pa::IOUtil::Position{ 0,0 }]);
174  assertEqual(fcm2, map[mitk::pa::IOUtil::Position{ 0,1 }]);
175  assertEqual(fcm3, map[mitk::pa::IOUtil::Position{ 1,0 }]);
176  assertEqual(fcm4, map[mitk::pa::IOUtil::Position{ 1,1 }]);
177  }
178 
179  void tearDown() override
180  {
181  //CPPUNIT_ASSERT_MESSAGE("Resource leak of test files onto hard drive..", itksys::SystemTools::RemoveADirectory(TEST_FOLDER_PATH) == true);
182  }
183 };
184 
185 MITK_TEST_SUITE_REGISTRATION(mitkPhotoacousticIO)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_INFO
Definition: mitkLogMacros.h:18
static int GetNumberOfNrrdFilesInDirectory(std::string directory)
returns the number of .nrrd files in a given directory
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static std::map< Position, Volume::Pointer > LoadFluenceContributionMaps(std::string foldername, double blur, int *progress, bool log10=false)
LoadFluenceContributionMaps loads multiple nrrd files from a given folder and sorts them into a posit...
static InSilicoTissueVolume::Pointer New(mitk::pa::Volume::Pointer absorptionVolume, Volume::Pointer scatteringVolume, Volume::Pointer anisotropyVolume, Volume::Pointer segmentationVolume, TissueGeneratorParameters::Pointer tissueParameters, mitk::PropertyList::Pointer propertyList)
static Volume::Pointer LoadNrrd(std::string filename, double sigma=0)
loads a nrrd file from a given filename.
static IPropertyPersistence * GetPropertyPersistence(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyPersistence instance.
Test fixture for parameterized tests.
static std::vector< std::string > GetAllChildfoldersFromFolder(std::string folderPath)
returns all child folders from a folder if there is no .nrrd file in the folder If there IS a ...
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
virtual bool AddInfo(const PropertyPersistenceInfo *info, bool overwrite=false)=0
Add persistence info for a specific base data property. If there is already a property info instance ...
static InSilicoTissueVolume::Pointer LoadInSilicoTissueVolumeFromNrrdFile(std::string filePath)
loads an in silico tissue volume from a saved nrrd file.
static Volume::Pointer New(double *data, unsigned int xDim, unsigned int yDim, unsigned int zDim, double spacing)
returns smartpointer reference to a new instance of this objects. The given data array will be freed ...