Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkDICOMFileReaderTestHelper.h
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 
17 #ifndef mitkDICOMFileReaderTestHelper_h
18 #define mitkDICOMFileReaderTestHelper_h
19 
20 #include "mitkDICOMFileReader.h"
21 #include "mitkDICOMEnums.h"
22 
23 #include "mitkTestingMacros.h"
24 
25 namespace mitk
26 {
27 
29 {
30  public:
31 
33 {
34  static StringList inputs;
35  return inputs;
36 }
37 
38 static void SetTestInputFilenames(int argc, char* argv[])
39 {
40  mitk::StringList inputFiles;
41 
42  for (int a = 1; a < argc; ++a)
43  {
44  inputFiles.push_back( argv[a] );
45  }
46 
47  GetInputFilenames() = inputFiles;
48 }
49 
50 
51 static void SetTestInputFilenames(const StringList& filenames)
52 {
53  GetInputFilenames() = filenames;
54 }
55 
56 static void TestInputFilenames(DICOMFileReader* reader)
57 {
58  StringList inputFiles = GetInputFilenames();
59  reader->SetInputFiles( inputFiles );
60 
61  const StringList& inputFilesReturned = reader->GetInputFiles();
62  MITK_TEST_CONDITION( inputFilesReturned.size() == inputFiles.size(), "Input file list is received")
63  MITK_TEST_CONDITION( reader->GetNumberOfOutputs() == 0, "No outputs without analysis")
64 }
65 
67 {
68  StringList inputFiles = GetInputFilenames();
69  reader->SetInputFiles( inputFiles );
70 
71  reader->AnalyzeInputFiles();
72 
73  StringList allSortedInputsFiles;
74 
75  unsigned int numberOfOutputs = reader->GetNumberOfOutputs();
76  for (unsigned int o = 0; o < numberOfOutputs; ++o)
77  {
78  const DICOMImageBlockDescriptor block = reader->GetOutput(o);
79 
80  const DICOMImageFrameList& outputFiles = block.GetImageFrameList();
81  for(auto iter = outputFiles.cbegin();
82  iter != outputFiles.cend();
83  ++iter)
84  {
85  // check that output is part of input
86  auto inputPositionOfCurrentOutput = std::find( inputFiles.cbegin(), inputFiles.cend(), (*iter)->Filename );
87  if (inputPositionOfCurrentOutput != inputFiles.cend())
88  {
89  // check that output is only part of ONE output
90  auto outputPositionOfCurrentOutput = std::find( allSortedInputsFiles.cbegin(), allSortedInputsFiles.cend(), (*iter)->Filename );
91  if (outputPositionOfCurrentOutput == allSortedInputsFiles.cend())
92  {
93  // was not in list before
94  allSortedInputsFiles.push_back( *inputPositionOfCurrentOutput );
95  }
96  else
97  {
98  reader->PrintOutputs(std::cout);
99  MITK_TEST_CONDITION_REQUIRED(false, "File '" << (*iter)->Filename << "' appears in TWO outputs. Readers are expected to use each frame only once." )
100  }
101  }
102  else
103  {
104  reader->PrintOutputs(std::cout);
105  MITK_TEST_CONDITION_REQUIRED(false, "File '" << (*iter)->Filename << "' appears in output, but it was never part of the input list." )
106  }
107  }
108  }
109 
110  MITK_TEST_CONDITION( allSortedInputsFiles.size() == inputFiles.size(), "Output list size (" << allSortedInputsFiles.size() << ") equals input list size (" << inputFiles.size() << ")" )
111 
112  try
113  {
114  const DICOMImageBlockDescriptor block = reader->GetOutput( inputFiles.size() );
115  MITK_TEST_CONDITION(false, "Invalid indices for GetOutput() should throw exception")
116  }
117  catch( std::invalid_argument& )
118  {
119  MITK_TEST_CONDITION(true, "Invalid indices for GetOutput() should throw exception")
120  }
121 }
122 
124  const DICOMFileReader::AdditionalTagsMapType& requestedTags,
125  const std::unordered_map<std::string, std::string>& expectedProperties )
126 {
127  StringList inputFiles = GetInputFilenames();
128  reader->SetInputFiles( inputFiles );
129 
130  reader->AnalyzeInputFiles();
131  reader->LoadImages();
132 
133  unsigned int numberOfOutputs = reader->GetNumberOfOutputs();
134  for (unsigned int o = 0; o < numberOfOutputs; ++o)
135  {
136  const DICOMImageBlockDescriptor block = reader->GetOutput(o);
137 
138  const DICOMImageFrameList& outputFiles = block.GetImageFrameList();
139  const mitk::Image::Pointer mitkImage = block.GetMitkImage();
140 
141  for ( auto iter = requestedTags.cbegin(); iter != requestedTags.cend(); ++iter)
142  {
143  mitk::BaseProperty* property = mitkImage->GetProperty( iter->second.c_str() ).GetPointer();
144  MITK_TEST_CONDITION( property != nullptr,
145  "Requested Tag is available as Property in Image" );
146  if (property)
147  {
148  MITK_INFO << iter->first.ToStr() << " / " << property->GetNameOfClass();
149  auto expectfinding = expectedProperties.find(iter->second);
150 
151  if (expectfinding != expectedProperties.end())
152  {
153  MITK_TEST_CONDITION(std::string(property->GetNameOfClass()) == expectfinding->second, "Property type is as expected");
154  }
155  }
156  }
157 
158 
159  MITK_DEBUG << "-------------------------------------------";
160  MITK_DEBUG << "Output " << o << " at " << (void*) mitkImage.GetPointer();
161  MITK_DEBUG << " Number of files: " << outputFiles.size();
162  MITK_DEBUG << " Dimensions: " << mitkImage->GetDimension(0) << " " << mitkImage->GetDimension(1) << " " << mitkImage->GetDimension(2);
163  }
164 }
165 
167 {
169 }
170 
171 
172 }; // end test class
173 
174 } // namespace
175 
176 #endif
static void TestMitkImagesAreLoaded(DICOMFileReader *reader, const DICOMFileReader::AdditionalTagsMapType &requestedTags, const std::unordered_map< std::string, std::string > &expectedProperties)
const DICOMImageFrameList & GetImageFrameList() const
List of frames that constitute the mitk::Image (DICOMImageFrames)
const DICOMImageBlockDescriptor & GetOutput(unsigned int index) const
Individual outputs, only meaningful after calling AnalyzeInputFiles().
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
static mitk::BaseProperty::Pointer DummyTagToPropertyFunctor(const mitk::StringLookupTable &)
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
DataCollection - Class to facilitate loading/accessing structured data.
virtual bool LoadImages()=0
void PrintOutputs(std::ostream &os, bool filenameDetails=false) const
Print output description to given stream, for human reader.
itk::SmartPointer< Self > Pointer
void SetInputFiles(const StringList &filenames)
This input files.
virtual void AnalyzeInputFiles()=0
Analyze input files.
Abstract base class for properties.
#define MITK_TEST_CONDITION(COND, MSG)
std::vector< DICOMImageFrameInfo::Pointer > DICOMImageFrameList
DICOMImageBlockDescriptor::AdditionalTagsMapType AdditionalTagsMapType
Output descriptor for DICOMFileReader.
static void TestOutputsContainInputs(DICOMFileReader *reader)
static void SetTestInputFilenames(const StringList &filenames)
std::vector< std::string > StringList
Image::Pointer GetMitkImage() const
the 3D mitk::Image that is loaded from the DICOM files of a DICOMImageFrameList
static void SetTestInputFilenames(int argc, char *argv[])
static void TestInputFilenames(DICOMFileReader *reader)
Interface for DICOM readers that produce mitk::Images.
unsigned int GetNumberOfOutputs() const
Number of outputs, only meaningful after calling AnalyzeInputFiles()
const StringList & GetInputFiles() const
This input files.