forked from ANTsX/ANTs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
antsGrubbsRosnerListSampleFilter.hxx
256 lines (220 loc) · 8.89 KB
/
antsGrubbsRosnerListSampleFilter.hxx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*=========================================================================
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 __antsGrubbsRosnerListSampleFilter_hxx
#define __antsGrubbsRosnerListSampleFilter_hxx
#include "itkTDistribution.h"
namespace itk
{
namespace ants
{
namespace Statistics
{
template <typename TScalarListSample>
GrubbsRosnerListSampleFilter<TScalarListSample>::GrubbsRosnerListSampleFilter()
{
this->AllocateOutput();
this->GetOutput()->SetMeasurementVectorSize(1);
this->m_OutlierHandling = Winsorize;
this->m_WinsorizingLevel = 0.10;
this->m_SignificanceLevel = 0.05;
}
template <typename TScalarListSample>
GrubbsRosnerListSampleFilter<TScalarListSample>::~GrubbsRosnerListSampleFilter() = default;
template <typename TScalarListSample>
void
GrubbsRosnerListSampleFilter<TScalarListSample>::GenerateData()
{
if (this->GetInput()->GetMeasurementVectorSize() != 1)
{
itkExceptionMacro("The input sample must be univariate.");
}
const unsigned int scalarMeasurementVectorSize = this->GetOutput()->GetMeasurementVectorSize();
this->GetOutput()->SetMeasurementVectorSize(scalarMeasurementVectorSize);
/**
* A common hueristic is that Grubbs-Rosner outlier removal does not work for
* sample sizes less than or equal to 6.
*/
if (this->GetInput()->Size() <= 6)
{
typename ScalarListSampleType::ConstIterator It = this->GetInput()->Begin();
while (It != this->GetInput()->End())
{
MeasurementVectorType inputMeasurement = It.GetMeasurementVector();
MeasurementVectorType outputMeasurement;
outputMeasurement.SetSize(scalarMeasurementVectorSize);
for (unsigned int d = 0; d < scalarMeasurementVectorSize; d++)
{
outputMeasurement[d] = inputMeasurement[d];
}
this->GetOutput()->PushBack(outputMeasurement);
++It;
}
return;
}
/**
* Otherwise, iterate through the input list, removing t
*/
RealType mean = 0.0;
RealType variance = 0.0;
RealType count = 0.0;
typename ScalarListSampleType::ConstIterator It = this->GetInput()->Begin();
while (It != this->GetInput()->End())
{
MeasurementVectorType inputMeasurement = It.GetMeasurementVector();
count += NumericTraits<RealType>::OneValue();
variance += (count - NumericTraits<RealType>::OneValue()) *
itk::Math::sqr(static_cast<RealType>(inputMeasurement[0]) - mean) / count;
mean = mean + (static_cast<RealType>(inputMeasurement[0]) - mean) / count;
++It;
}
variance /= (count - NumericTraits<RealType>::OneValue());
bool outlierFound = true;
this->m_OutlierInstanceIdentifiers.clear();
while (outlierFound == true && (this->GetInput()->Size() - this->m_OutlierInstanceIdentifiers.size() > 6))
{
outlierFound = false;
InstanceIdentifierType id = this->FindMaximumNonOutlierDeviationValue(mean, variance);
if (this->GetInput()->GetFrequency(id) > 0)
{
MeasurementVectorType measurement = this->GetInput()->GetMeasurementVector(id);
outlierFound = this->IsMeasurementAnOutlier(
measurement[0], mean, variance, this->GetInput()->Size() - this->m_OutlierInstanceIdentifiers.size());
if (outlierFound)
{
/** Retabulate the variance and mean by removing the previous estimate */
RealType count2 = this->GetInput()->Size() - this->m_OutlierInstanceIdentifiers.size();
mean = (mean * count2 - static_cast<RealType>(measurement[0])) / (count2 - NumericTraits<RealType>::OneValue());
variance = (count2 - 1.0) * variance - (count2 - NumericTraits<RealType>::OneValue()) *
itk::Math::sqr(static_cast<RealType>(measurement[0]) - mean) / count2;
variance /= (count2 - static_cast<RealType>(2.0));
this->m_OutlierInstanceIdentifiers.push_back(id);
}
}
}
RealType lowerWinsorBound = 0.0;
RealType upperWinsorBound = 0.0;
if (this->m_OutlierHandling == Winsorize)
{
typename itk::Statistics::TDistribution::Pointer tdistribution = itk::Statistics::TDistribution::New();
RealType t = tdistribution->EvaluateInverseCDF(
1.0 - 0.5 * this->m_WinsorizingLevel, this->GetInput()->Size() - this->m_OutlierInstanceIdentifiers.size());
lowerWinsorBound = mean - t * std::sqrt(variance);
upperWinsorBound = mean + t * std::sqrt(variance);
}
It = this->GetInput()->Begin();
while (It != this->GetInput()->End())
{
MeasurementVectorType inputMeasurement = It.GetMeasurementVector();
MeasurementVectorType outputMeasurement;
outputMeasurement.SetSize(scalarMeasurementVectorSize);
if (this->m_OutlierHandling == None ||
std::find(this->m_OutlierInstanceIdentifiers.begin(),
this->m_OutlierInstanceIdentifiers.end(),
It.GetInstanceIdentifier()) == this->m_OutlierInstanceIdentifiers.end())
{
outputMeasurement[0] = inputMeasurement[0];
this->GetOutput()->PushBack(outputMeasurement);
}
else if (this->m_OutlierHandling == Winsorize)
{
if (static_cast<RealType>(inputMeasurement[0]) < lowerWinsorBound)
{
outputMeasurement[0] = lowerWinsorBound;
}
else
{
outputMeasurement[0] = upperWinsorBound;
}
this->GetOutput()->PushBack(outputMeasurement);
}
++It;
}
}
template <typename TScalarListSample>
typename GrubbsRosnerListSampleFilter<TScalarListSample>::InstanceIdentifierType
GrubbsRosnerListSampleFilter<TScalarListSample>::FindMaximumNonOutlierDeviationValue(RealType mean,
RealType itkNotUsed(variance))
{
RealType maximumDeviation = 0.0;
InstanceIdentifierType maximumID = NumericTraits<InstanceIdentifierType>::max();
typename ScalarListSampleType::ConstIterator It = this->GetInput()->Begin();
while (It != this->GetInput()->End())
{
MeasurementVectorType inputMeasurement = It.GetMeasurementVector();
InstanceIdentifierType inputID = It.GetInstanceIdentifier();
if (std::find(this->m_OutlierInstanceIdentifiers.begin(), this->m_OutlierInstanceIdentifiers.end(), inputID) ==
this->m_OutlierInstanceIdentifiers.end())
{
if (Math::abs(static_cast<RealType>(inputMeasurement[0]) - mean) > maximumDeviation)
{
maximumDeviation = Math::abs(static_cast<RealType>(inputMeasurement[0]) - mean);
maximumID = inputID;
}
}
++It;
}
return maximumID;
}
template <typename TScalarListSample>
bool
GrubbsRosnerListSampleFilter<TScalarListSample>::IsMeasurementAnOutlier(RealType x,
RealType mean,
RealType variance,
unsigned long N)
{
/**
* The Grubb critical two-sided value is defined to be
* (N-1)/sqrt(N)*sqrt( t*t / (N-2+t*t) ) where t is at the
* (alpha / (2N)) signficance level with N-2 degrees of freedom.
*/
RealType sig = this->m_SignificanceLevel / (2.0 * static_cast<RealType>(N));
typename itk::Statistics::TDistribution::Pointer tdistribution = itk::Statistics::TDistribution::New();
RealType t = tdistribution->EvaluateInverseCDF(1.0 - sig, N - 2);
RealType nu = static_cast<RealType>(N - 1);
RealType g = nu / std::sqrt(nu + 1.0) * std::sqrt(t * t / (nu - 1 + t * t));
return g < (itk::Math::abs(x - mean) / std::sqrt(variance));
}
template <typename TScalarListSample>
void
GrubbsRosnerListSampleFilter<TScalarListSample>::PrintSelf(std::ostream & os, Indent indent) const
{
os << indent << "Significance level: " << this->m_SignificanceLevel << 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";
os << " (level = " << this->m_WinsorizingLevel << ")" << std::endl;
}
if (this->m_OutlierInstanceIdentifiers.size() > 0)
{
os << indent << "Outlier Identifiers: " << std::endl;
for (unsigned int d = 0; d < this->m_OutlierInstanceIdentifiers.size(); d++)
{
os << indent << " " << this->m_OutlierInstanceIdentifiers[d] << std::endl;
}
}
else
{
os << indent << "There are no outliers." << std::endl;
}
}
} // end of namespace Statistics
} // end of namespace ants
} // end of namespace itk
#endif