Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkImageStatisticsContainer.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 ============================================================================*/
13 
14 #include <algorithm>
15 
16 namespace mitk
17 {
19 
20  // The order is derived from the old (<2018) image statistics plugin.
22  ImageStatisticsContainer::ImageStatisticsObject::m_DefaultNames = {ImageStatisticsConstants::MEAN(),
38 
40 
42  {
43  m_Statistics.emplace(key, value);
44 
45  if (std::find(m_DefaultNames.cbegin(), m_DefaultNames.cend(), key) == m_DefaultNames.cend())
46  {
47  if (std::find(m_CustomNames.cbegin(), m_CustomNames.cend(), key) == m_CustomNames.cend())
48  {
49  m_CustomNames.emplace_back(key);
50  }
51  }
52  }
53 
56  {
57  return m_DefaultNames;
58  }
59 
62  {
63  return m_CustomNames;
64  }
65 
68  {
69  StatisticNameVector names = GetDefaultStatisticNames();
70 
71  names.insert(names.cend(), m_CustomNames.cbegin(), m_CustomNames.cend());
72 
73  return names;
74  }
75 
78  {
79  StatisticNameVector names;
80 
81  std::transform(m_Statistics.begin(), m_Statistics.end(), std::back_inserter(names), [](const auto &pair) {
82  return pair.first;
83  });
84 
85  return names;
86  }
87 
89  {
90  return m_Statistics.find(name) != m_Statistics.cend();
91  }
92 
94  const std::string &name) const
95  {
96  if (HasStatistic(name))
97  {
98  return m_Statistics.find(name)->second;
99  }
100  else
101  {
102  mitkThrow() << "invalid statistic key, could not find";
103  }
104  }
105 
107  {
108  m_Statistics.clear();
109  m_CustomNames.clear();
110  }
111 
113  {
114  return m_TimeStepMap.find(timeStep) != m_TimeStepMap.end();
115  }
116 
118  TimeStepType timeStep) const
119  {
120  auto it = m_TimeStepMap.find(timeStep);
121  if (it != m_TimeStepMap.end())
122  {
123  return it->second;
124  }
125  mitkThrow() << "StatisticsObject for timeStep " << timeStep << " not found!";
126  }
127 
129  {
130  if (timeStep < this->GetTimeSteps())
131  {
132  m_TimeStepMap.emplace(timeStep, statistics);
133  this->Modified();
134  }
135  else
136  {
137  mitkThrow() << "Given timeStep " << timeStep
138  << " out of timeStep geometry bounds. TimeSteps in geometry: " << this->GetTimeSteps();
139  }
140  }
141 
142  void ImageStatisticsContainer::PrintSelf(std::ostream &os, itk::Indent indent) const
143  {
144  Superclass::PrintSelf(os, indent);
145  for (unsigned int i = 0; i < this->GetTimeSteps(); i++)
146  {
147  auto statisticsValues = GetStatisticsForTimeStep(i);
148  os << std::endl << indent << "Statistics instance for timeStep " << i << ":";
149  auto statisticKeys = statisticsValues.GetExistingStatisticNames();
150  os << std::endl << indent << "Number of entries: " << statisticKeys.size();
151  for (const auto &aKey : statisticKeys)
152  {
153  os << std::endl << indent.GetNextIndent() << aKey << ": " << statisticsValues.GetValueNonConverted(aKey);
154  }
155  }
156  }
157 
158  unsigned int ImageStatisticsContainer::GetNumberOfTimeSteps() const { return this->GetTimeSteps(); }
159 
161  {
162  for (auto iter = m_TimeStepMap.begin(); iter != m_TimeStepMap.end(); iter++)
163  {
164  iter->second.Reset();
165  }
166  }
167 
168  itk::LightObject::Pointer ImageStatisticsContainer::InternalClone() const
169  {
170  itk::LightObject::Pointer ioPtr = Superclass::InternalClone();
171  Self::Pointer rval = dynamic_cast<Self *>(ioPtr.GetPointer());
172  if (rval.IsNull())
173  {
174  itkExceptionMacro(<< "downcast to type "
175  << "StatisticsContainer"
176  << " failed.");
177  }
178 
179  rval->SetTimeStepMap(m_TimeStepMap);
180  rval->SetTimeGeometry(this->GetTimeGeometry()->Clone());
181 
182  return ioPtr;
183  }
184 
185  void ImageStatisticsContainer::SetTimeStepMap(TimeStepMapType map) { m_TimeStepMap = map; }
186 
188  const ImageStatisticsContainer *container)
189  {
192 
193  if (container)
194  {
195  std::set<std::string> customKeys;
196 
197  for (unsigned int i = 0; i < container->GetTimeSteps(); i++)
198  {
199  auto statisticKeys = container->GetStatisticsForTimeStep(i).GetCustomStatisticNames();
200  customKeys.insert(statisticKeys.cbegin(), statisticKeys.cend());
201  }
202 
203  names.insert(names.cend(), customKeys.cbegin(), customKeys.cend());
204  }
205 
206  return names;
207  }
208 
210  std::vector<ImageStatisticsContainer::ConstPointer> containers)
211  {
214 
215  std::set<std::string> customKeys;
216 
217  for (auto container : containers)
218  {
219  for (unsigned int i = 0; i < container->GetTimeSteps(); i++)
220  {
221  if(container->TimeStepExists(i))
222  {
223  auto statisticKeys = container->GetStatisticsForTimeStep(i).GetCustomStatisticNames();
224  customKeys.insert(statisticKeys.cbegin(), statisticKeys.cend());
225  }
226  }
227  }
228 
229  names.insert(names.end(), customKeys.begin(), customKeys.end());
230 
231  return names;
232  };
233 
234 } // namespace mitk
void Reset()
Deletes all stored values.
Base of all data objects.
Definition: mitkBaseData.h:37
DataCollection - Class to facilitate loading/accessing structured data.
void AddStatistic(const std::string &key, StatisticsVariantType value)
Adds a statistic to the statistics object.
void PrintSelf(std::ostream &os, itk::Indent indent) const override
const StatisticNameVector & GetCustomStatisticNames() const
Returns the names of all custom statistics (defined at runtime and no default names).
StatisticNameVector GetAllStatisticNames() const
Returns the names of all statistics (default and custom defined) Additional custom keys are added at ...
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:61
#define mitkThrow()
void SetStatisticsForTimeStep(TimeStepType timeStep, ImageStatisticsObject statistics)
Sets the statisticObject for the given Timestep.
ImageStatisticsContainer::ImageStatisticsObject::StatisticNameVector GetAllStatisticNames(const ImageStatisticsContainer *container)
static const StatisticNameVector & GetDefaultStatisticNames()
Returns the names of the default statistics.
const ImageStatisticsObject & GetStatisticsForTimeStep(TimeStepType timeStep) const
Returns the statisticObject for the given Timestep.
std::vcl_size_t TimeStepType
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=nullptr, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
static const std::string STANDARDDEVIATION()
boost::variant< RealType, VoxelCountType, IndexType > StatisticsVariantType
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry As the base data has not a data vector given by it...
Definition: mitkBaseData.h:355
bool TimeStepExists(TimeStepType timeStep) const
Checks if the Time step exists.
Container class for storing the computed image statistics.
StatisticsVariantType GetValueNonConverted(const std::string &name) const
Returns the requested value.
Container class for storing a StatisticsObject for each timestep.
std::map< TimeStepType, ImageStatisticsObject > TimeStepMapType