File: antsBoxPlotQuantileListSampleFilter.hxx

package info (click to toggle)
ants 2.5.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,672 kB
  • sloc: cpp: 85,685; sh: 15,850; perl: 863; xml: 115; python: 111; makefile: 68
file content (168 lines) | stat: -rw-r--r-- 6,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*=========================================================================

  Program:   Advanced Normalization Tools

  Copyright (c) ConsortiumOfANTS. All rights reserved.
  See accompanying COPYING.txt or
  https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt
  for details.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __antsBoxPlotQuantileListSampleFilter_hxx
#define __antsBoxPlotQuantileListSampleFilter_hxx


#include "itkDenseFrequencyContainer2.h"
#include "itkHistogram.h"
#include "itkSampleToHistogramFilter.h"

namespace itk
{
namespace ants
{
namespace Statistics
{
template <typename TScalarListSample>
BoxPlotQuantileListSampleFilter<TScalarListSample>::BoxPlotQuantileListSampleFilter()
{
  this->AllocateOutput();
  this->GetOutput()->SetMeasurementVectorSize(1);

  this->m_OutlierHandling = Winsorize;
  this->m_WhiskerScalingFactor = 1.5;
  this->m_LowerPercentile = 0.25;
  this->m_UpperPercentile = 0.75;
}

template <typename TScalarListSample>
BoxPlotQuantileListSampleFilter<TScalarListSample>::~BoxPlotQuantileListSampleFilter() = default;

template <typename TScalarListSample>
void
BoxPlotQuantileListSampleFilter<TScalarListSample>::GenerateData()
{
  if (this->GetInput()->GetMeasurementVectorSize() != 1)
  {
    itkExceptionMacro("The input sample must be univariate.");
  }
  if (this->m_LowerPercentile >= this->m_UpperPercentile)
  {
    itkExceptionMacro("Lower percentile must be less than upper percentile.");
  }

  const unsigned int scalarMeasurementVectorSize = this->GetInput()->GetMeasurementVectorSize();
  this->GetOutput()->SetMeasurementVectorSize(scalarMeasurementVectorSize);

  /**
   * Initialize the histogram in preparation
   */

  typedef itk::Statistics::Histogram<RealType, itk::Statistics::DenseFrequencyContainer2> HistogramType;
  typedef itk::Statistics::SampleToHistogramFilter<ScalarListSampleType, HistogramType>   SampleFilterType;
  typename SampleFilterType::HistogramSizeType                                            histogramSize(1);
  histogramSize.Fill(200);

  typename SampleFilterType::Pointer sampleFilter = SampleFilterType::New();
  sampleFilter->SetInput(this->GetInput());
  sampleFilter->SetHistogramSize(histogramSize);
  sampleFilter->Update();

  RealType lowerQuantile = sampleFilter->GetOutput()->Quantile(0, this->m_LowerPercentile);
  RealType upperQuantile = sampleFilter->GetOutput()->Quantile(0, this->m_UpperPercentile);

  RealType upperBound = upperQuantile + this->m_WhiskerScalingFactor * (upperQuantile - lowerQuantile);
  RealType lowerBound = lowerQuantile - this->m_WhiskerScalingFactor * (upperQuantile - lowerQuantile);

  typename ScalarListSampleType::ConstIterator It = this->GetInput()->Begin();
  It = this->GetInput()->Begin();
  while (It != this->GetInput()->End())
  {
    MeasurementVectorType                                inputMeasurement = It.GetMeasurementVector();
    typename ScalarListSampleType::MeasurementVectorType outputMeasurement;
    outputMeasurement.SetSize(scalarMeasurementVectorSize);
    if (static_cast<RealType>(inputMeasurement[0]) < lowerBound ||
        static_cast<RealType>(inputMeasurement[0]) > upperBound)
    {
      this->m_OutlierInstanceIdentifiers.push_back(It.GetInstanceIdentifier());
      if (this->m_OutlierHandling == None)
      {
        outputMeasurement[0] = inputMeasurement[0];
        this->GetOutput()->PushBack(outputMeasurement);
      }
      // else trim from the output
    }
    else
    {
      outputMeasurement[0] = inputMeasurement[0];
      this->GetOutput()->PushBack(outputMeasurement);
    }
    ++It;
  }

  if (this->m_OutlierHandling == Winsorize)
  {
    /** Retabulate the histogram with the outliers removed */
    typename SampleFilterType::Pointer sampleFilter2 = SampleFilterType::New();
    sampleFilter2->SetInput(this->GetOutput());
    sampleFilter2->SetHistogramSize(histogramSize);
    sampleFilter2->Update();

    RealType lowerQuantile2 = sampleFilter2->GetOutput()->Quantile(0, this->m_LowerPercentile);
    RealType upperQuantile2 = sampleFilter2->GetOutput()->Quantile(0, this->m_UpperPercentile);

    RealType upperBound2 = upperQuantile2 + this->m_WhiskerScalingFactor * (upperQuantile2 - lowerQuantile2);
    RealType lowerBound2 = lowerQuantile2 - this->m_WhiskerScalingFactor * (upperQuantile2 - lowerQuantile2);

    this->GetOutput()->Clear();

    It = this->GetInput()->Begin();
    while (It != this->GetInput()->End())
    {
      MeasurementVectorType                                inputMeasurement = It.GetMeasurementVector();
      typename ScalarListSampleType::MeasurementVectorType outputMeasurement;
      outputMeasurement.SetSize(scalarMeasurementVectorSize);
      outputMeasurement[0] = inputMeasurement[0];
      if (static_cast<RealType>(inputMeasurement[0]) < lowerBound)
      {
        outputMeasurement[0] = lowerBound2;
      }
      else if (static_cast<RealType>(inputMeasurement[0]) > upperBound)
      {
        outputMeasurement[0] = upperBound2;
      }
      this->GetOutput()->PushBack(outputMeasurement);
      ++It;
    }
  }
}

template <typename TScalarListSample>
void
BoxPlotQuantileListSampleFilter<TScalarListSample>::PrintSelf(std::ostream & os, Indent indent) const
{
  os << indent << "Percentile Bounds: [" << this->m_LowerPercentile << ", " << this->m_UpperPercentile << "]"
     << std::endl;
  os << indent << "Whisker scaling factor: " << this->m_WhiskerScalingFactor << std::endl;
  os << indent << "Outlier handling: ";
  if (this->m_OutlierHandling == None)
  {
    os << "None" << std::endl;
  }
  if (this->m_OutlierHandling == Trim)
  {
    os << "Trim" << std::endl;
  }
  if (this->m_OutlierHandling == Winsorize)
  {
    os << "Winsorize" << std::endl;
  }
}
} // end of namespace Statistics
} // end of namespace ants
} // end of namespace itk

#endif