File: itkLabelOverlapMeasuresImageFilter.hxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (475 lines) | stat: -rw-r--r-- 13,608 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkLabelOverlapMeasuresImageFilter_hxx
#define itkLabelOverlapMeasuresImageFilter_hxx


#include "itkImageRegionConstIterator.h"
#include "itkTotalProgressReporter.h"

namespace itk
{

template <typename TLabelImage>
LabelOverlapMeasuresImageFilter<TLabelImage>::LabelOverlapMeasuresImageFilter()
{
  Self::SetPrimaryInputName("SourceImage");
  Self::AddRequiredInputName("TargetImage", 1);

  // This filter requires two input images
  this->SetNumberOfRequiredInputs(2);
}

template <typename TLabelImage>
void
LabelOverlapMeasuresImageFilter<TLabelImage>::BeforeStreamedGenerateData()
{
  Superclass::BeforeStreamedGenerateData();

  // Initialize the final map
  this->m_LabelSetMeasures.clear();
}

template <typename TLabelImage>
void
LabelOverlapMeasuresImageFilter<TLabelImage>::MergeMap(MapType & m1, MapType & m2) const
{
  for (auto m2_value : m2)
  {
    // Does this label exist in the cumulative structure yet?
    auto m1It = m1.find(m2_value.first);
    if (m1It == m1.end())
    {
      // move m2 entry into m1, this reuses the histogram if needed.
      m1.emplace(m2_value.first, std::move(m2_value.second));
    }
    else
    {
      typename MapType::mapped_type & labelStats = m1It->second;

      // Accumulate the information into m1
      labelStats.m_Source += m2_value.second.m_Source;             // segmentation which will be compared (TP+FP)
      labelStats.m_Target += m2_value.second.m_Target;             // Ground Truth segmentation (TP+FN)
      labelStats.m_Union += m2_value.second.m_Union;               // (TP+FN+FP)
      labelStats.m_Intersection += m2_value.second.m_Intersection; //(TP)
      labelStats.m_SourceComplement += m2_value.second.m_SourceComplement; //(FP)
      labelStats.m_TargetComplement += m2_value.second.m_TargetComplement; //(FN)
    }
  }
}

template <typename TLabelImage>
void
LabelOverlapMeasuresImageFilter<TLabelImage>::ThreadedStreamedGenerateData(const RegionType & outputRegionForThread)
{

  MapType localStatistics;

  ImageRegionConstIterator<LabelImageType> itS(this->GetSourceImage(), outputRegionForThread);
  ImageRegionConstIterator<LabelImageType> itT(this->GetTargetImage(), outputRegionForThread);

  // Support progress methods/callbacks
  TotalProgressReporter progress(this, this->GetSourceImage()->GetLargestPossibleRegion().GetNumberOfPixels());

  for (itS.GoToBegin(), itT.GoToBegin(); !itS.IsAtEnd(); ++itS, ++itT)
  {
    LabelType sourceLabel = itS.Get();
    LabelType targetLabel = itT.Get();

    // Initialized to empty if key does not already exist
    auto & sValue = localStatistics[sourceLabel];
    auto & tValue = localStatistics[targetLabel];

    ++sValue.m_Source;
    ++tValue.m_Target;

    if (sourceLabel == targetLabel)
    {
      ++sValue.m_Intersection;
      ++sValue.m_Union;
    }
    else
    {
      ++sValue.m_Union;
      ++tValue.m_Union;

      ++sValue.m_SourceComplement;
      ++tValue.m_TargetComplement;
    }

    progress.CompletedPixel();
  }


  // Merge localStatistics and m_LabelSetMeasures concurrently safe in a
  // local copy, this thread may do multiple merges.
  while (true)
  {
    MapType tomerge{};
    {
      const std::lock_guard<std::mutex> lockGuard(m_Mutex);

      if (m_LabelSetMeasures.empty())
      {
        swap(m_LabelSetMeasures, localStatistics);
        break;
      }

      // Move the data of the output map to the local `tomerge` and clear the output map.
      swap(m_LabelSetMeasures, tomerge);

    } // release lock, allow other threads to merge data

    // Merge tomerge into localStatistics, locally
    MergeMap(localStatistics, tomerge);
  }
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetTotalOverlap() const -> RealType
{
  RealType numerator = 0.0;
  RealType denominator = 0.0;
  for (auto mapIt = this->m_LabelSetMeasures.begin(); mapIt != this->m_LabelSetMeasures.end(); ++mapIt)
  {
    // Do not include the background in the final value.
    if (mapIt->first == LabelType{})
    {
      continue;
    }
    numerator += static_cast<RealType>(mapIt->second.m_Intersection);
    denominator += static_cast<RealType>(mapIt->second.m_Target);
  }

  if (Math::ExactlyEquals(denominator, 0.0))
  {
    return NumericTraits<RealType>::max();
  }
  else
  {
    return (numerator / denominator);
  }
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetTargetOverlap(LabelType label) const -> RealType
{
  auto mapIt = this->m_LabelSetMeasures.find(label);
  if (mapIt == this->m_LabelSetMeasures.end())
  {
    itkWarningMacro("Label " << static_cast<PrintType>(label) << " not found.");
    return 0.0;
  }

  RealType value;

  if (mapIt->second.m_Target == 0)
  {
    value = NumericTraits<RealType>::max();
  }
  else
  {
    value = static_cast<RealType>(mapIt->second.m_Intersection) / static_cast<RealType>(mapIt->second.m_Target);
  }
  return value;
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetUnionOverlap() const -> RealType
{
  RealType numerator = 0.0;
  RealType denominator = 0.0;
  for (auto mapIt = this->m_LabelSetMeasures.begin(); mapIt != this->m_LabelSetMeasures.end(); ++mapIt)
  {
    // Do not include the background in the final value.
    if (mapIt->first == LabelType{})
    {
      continue;
    }
    numerator += static_cast<RealType>(mapIt->second.m_Intersection);
    denominator += static_cast<RealType>(mapIt->second.m_Union);
  }

  if (Math::ExactlyEquals(denominator, 0.0))
  {
    return NumericTraits<RealType>::max();
  }
  else
  {
    return (numerator / denominator);
  }
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetUnionOverlap(LabelType label) const -> RealType
{
  auto mapIt = this->m_LabelSetMeasures.find(label);
  if (mapIt == this->m_LabelSetMeasures.end())
  {
    itkWarningMacro("Label " << static_cast<PrintType>(label) << " not found.");
    return 0.0;
  }

  RealType value;
  if (Math::ExactlyEquals(mapIt->second.m_Union, 0.0))
  {
    value = NumericTraits<RealType>::max();
  }
  else
  {
    value = static_cast<RealType>(mapIt->second.m_Intersection) / static_cast<RealType>(mapIt->second.m_Union);
  }

  return value;
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetMeanOverlap() const -> RealType
{
  RealType uo = this->GetUnionOverlap();
  return (2.0 * uo / (1.0 + uo));
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetMeanOverlap(LabelType label) const -> RealType
{
  RealType uo = this->GetUnionOverlap(label);
  return (2.0 * uo / (1.0 + uo));
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetVolumeSimilarity() const -> RealType
{
  RealType numerator = 0.0;
  RealType denominator = 0.0;
  for (auto mapIt = this->m_LabelSetMeasures.begin(); mapIt != this->m_LabelSetMeasures.end(); ++mapIt)
  {
    // Do not include the background in the final value.
    if (mapIt->first == LabelType{})
    {
      continue;
    }
    numerator += ((static_cast<RealType>(mapIt->second.m_Source) - static_cast<RealType>(mapIt->second.m_Target)));
    denominator += ((static_cast<RealType>(mapIt->second.m_Source) + static_cast<RealType>(mapIt->second.m_Target)));
  }

  if (Math::ExactlyEquals(denominator, 0.0))
  {
    return NumericTraits<RealType>::max();
  }
  else
  {
    return (2.0 * numerator / denominator);
  }
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetVolumeSimilarity(LabelType label) const -> RealType
{
  auto mapIt = this->m_LabelSetMeasures.find(label);
  if (mapIt == this->m_LabelSetMeasures.end())
  {
    itkWarningMacro("Label " << static_cast<PrintType>(label) << " not found.");
    return 0.0;
  }
  RealType value = 2.0 *
                   (static_cast<RealType>(mapIt->second.m_Source) - static_cast<RealType>(mapIt->second.m_Target)) /
                   (static_cast<RealType>(mapIt->second.m_Source) + static_cast<RealType>(mapIt->second.m_Target));
  return value;
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetFalseNegativeError() const -> RealType
{
  RealType numerator = 0.0;
  RealType denominator = 0.0;
  for (auto mapIt = this->m_LabelSetMeasures.begin(); mapIt != this->m_LabelSetMeasures.end(); ++mapIt)
  {
    // Do not include the background in the final value.
    if (mapIt->first == LabelType{})
    {
      continue;
    }
    numerator += static_cast<RealType>(mapIt->second.m_TargetComplement);
    denominator += static_cast<RealType>(mapIt->second.m_Target);
  }

  if (Math::ExactlyEquals(denominator, 0.0))
  {
    return NumericTraits<RealType>::max();
  }
  else
  {
    return (numerator / denominator);
  }
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetFalseNegativeError(LabelType label) const -> RealType
{
  auto mapIt = this->m_LabelSetMeasures.find(label);
  if (mapIt == this->m_LabelSetMeasures.end())
  {
    itkWarningMacro("Label " << static_cast<PrintType>(label) << " not found.");
    return 0.0;
  }

  RealType value;
  if (Math::ExactlyEquals(mapIt->second.m_Target, 0.0))
  {
    value = NumericTraits<RealType>::max();
  }
  else
  {
    value = static_cast<RealType>(mapIt->second.m_TargetComplement) / static_cast<RealType>(mapIt->second.m_Target);
  }

  return value;
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetFalsePositiveError() const -> RealType
{
  RealType numerator = 0.0;
  RealType denominator = 0.0;

  auto nVox = this->GetInput(0)->GetLargestPossibleRegion().GetNumberOfPixels(); // TP+FP+FN+TN

  for (auto mapIt = this->m_LabelSetMeasures.begin(); mapIt != this->m_LabelSetMeasures.end(); ++mapIt)
  {
    // Do not include the background in the final value.
    if (mapIt->first == LabelType{})
    {
      continue;
    }
    auto nComplementIntersection = nVox - mapIt->second.m_Union;                                      // TN
    numerator += static_cast<RealType>(mapIt->second.m_SourceComplement);                             // FP
    denominator += static_cast<RealType>(mapIt->second.m_SourceComplement + nComplementIntersection); // FP+TN
  }

  if (Math::ExactlyEquals(denominator, 0.0))
  {
    return NumericTraits<RealType>::max();
  }
  else
  {
    return (numerator / denominator);
  }
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetFalsePositiveError(LabelType label) const -> RealType
{
  auto nVox = this->GetInput(0)->GetLargestPossibleRegion().GetNumberOfPixels(); // TP+FP+FN+TN
  auto mapIt = this->m_LabelSetMeasures.find(label);
  if (mapIt == this->m_LabelSetMeasures.end())
  {
    itkWarningMacro("Label " << static_cast<PrintType>(label) << " not found.");
    return 0.0;
  }

  RealType value;
  if (Math::ExactlyEquals(mapIt->second.m_Source, 0.0))
  {
    value = NumericTraits<RealType>::max();
  }
  else
  {
    auto nComplementIntersection = nVox - mapIt->second.m_Union; // TN

    value = static_cast<RealType>(mapIt->second.m_SourceComplement) /
            static_cast<RealType>(mapIt->second.m_SourceComplement + nComplementIntersection);
  }

  return value;
}


template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetFalseDiscoveryRate() const -> RealType
{
  RealType numerator = 0.0;
  RealType denominator = 0.0;
  for (auto mapIt = this->m_LabelSetMeasures.begin(); mapIt != this->m_LabelSetMeasures.end(); ++mapIt)
  {
    // Do not include the background in the final value.
    if (mapIt->first == LabelType{})
    {
      continue;
    }
    numerator += static_cast<RealType>(mapIt->second.m_SourceComplement); // FP
    denominator += static_cast<RealType>(mapIt->second.m_Source);         // FP+TP
  }

  if (Math::ExactlyEquals(denominator, 0.0))
  {
    return NumericTraits<RealType>::max();
  }
  else
  {
    return (numerator / denominator);
  }
}

template <typename TLabelImage>
auto
LabelOverlapMeasuresImageFilter<TLabelImage>::GetFalseDiscoveryRate(LabelType label) const -> RealType
{
  auto mapIt = this->m_LabelSetMeasures.find(label);
  if (mapIt == this->m_LabelSetMeasures.end())
  {
    itkWarningMacro("Label " << static_cast<PrintType>(label) << " not found.");
    return 0.0;
  }

  RealType value;
  if (Math::ExactlyEquals(mapIt->second.m_Source, 0.0))
  {
    value = NumericTraits<RealType>::max();
  }
  else
  {
    value = static_cast<RealType>(mapIt->second.m_SourceComplement) / static_cast<RealType>(mapIt->second.m_Source);
  }
  return value;
}

template <typename TLabelImage>
void
LabelOverlapMeasuresImageFilter<TLabelImage>::PrintSelf(std::ostream & os, Indent indent) const
{
  // todo!!!
  Superclass::PrintSelf(os, indent);
}


} // end namespace itk
#endif