File: itkMeanShiftModeCacheMethod.txx

package info (click to toggle)
insighttoolkit 3.18.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 110,432 kB
  • ctags: 74,559
  • sloc: cpp: 412,627; ansic: 196,210; fortran: 28,000; python: 3,852; tcl: 2,005; sh: 1,186; java: 583; makefile: 458; csh: 220; perl: 193; xml: 20
file content (188 lines) | stat: -rw-r--r-- 5,184 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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkMeanShiftModeCacheMethod.txx,v $
  Language:  C++
  Date:      $Date: 2009-03-04 15:24:00 $
  Version:   $Revision: 1.5 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkMeanShiftModeCacheMethod_txx
#define __itkMeanShiftModeCacheMethod_txx

namespace itk { 
namespace Statistics {

template< class TMeasurementVector >
MeanShiftModeCacheMethod< TMeasurementVector >
::MeanShiftModeCacheMethod()
{
  m_MaximumEntries = 200;
  m_MaximumConsecutiveFailures = 5;
  m_HitRatioThreshold = 0.75;

  m_HitsSuccess = 0;
  m_NumberOfRequests = 0;
  m_ConsecutiveFailures = 0;

  m_TotalHitsSuccess = 0;
  m_TotalHitsFailure = 0;

  m_TotalTableSize = 0;
  m_TimesOfRebuilding = 0;
  m_TimesOfRebuildingByHitRatio = 0;
  m_TimesOfRebuildingByConsecutiveFailures = 0;
}

template< class TMeasurementVector >
MeanShiftModeCacheMethod< TMeasurementVector >
::~MeanShiftModeCacheMethod()
{
}

template< class TMeasurementVector >
void
MeanShiftModeCacheMethod< TMeasurementVector >
::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os,indent);

  os << indent << "Maximum entries: " << m_MaximumEntries << std::endl;
  os << indent << "Maximum consecutive failures: " 
     << m_MaximumConsecutiveFailures << std::endl;
  os << indent << "Hit ratio threshold: " 
     << m_HitRatioThreshold << std::endl;

  os << indent << "Last consecutive failures: " 
     << m_ConsecutiveFailures << std::endl;
  os << indent << "Last successful hits: " 
     << m_HitsSuccess << std::endl;
  os << indent << "Last number of requests: " 
     << m_NumberOfRequests << std::endl;

  os << indent << "Total successful hits: " << m_TotalHitsSuccess << std::endl;
  os << indent << "Total failed hits: " << m_TotalHitsFailure  << std::endl;

  os << indent << "Total table size before destuction: " 
     << m_TotalTableSize << std::endl;
  os << indent << "Number of cache rebuildings: " 
     << m_TimesOfRebuilding << std::endl;
  os << indent << "Average cache table size: ";
  if ( m_TimesOfRebuilding > 0 )
    {
    os << (float)(m_TotalTableSize / m_TimesOfRebuilding) << std::endl;
    }
  else
    {
    os << m_TotalTableSize << std::endl;
    }

  os << indent << "Number of cache rebuildings caused by hit ratio threshold: " 
     << m_TimesOfRebuildingByHitRatio << std::endl;
  os << indent << "Number of cache rebuildings caused by consecutive failures: " 
     << m_TimesOfRebuildingByConsecutiveFailures << std::endl;

  os << indent << "Cache table: " << &m_CacheTable << std::endl;
}

template< class TMeasurementVector >
bool
MeanShiftModeCacheMethod< TMeasurementVector >
::SetMeasurementVector(MeasurementVectorType& source, 
                       MeasurementVectorType& target)
{
  MeasurementVectorTraits::Assert( source,
    MeasurementVectorTraits::GetLength(target),
    "MeanShiftModeCacheMethod::SetMeasurementVector Map container must be used with MeasurementVectors of the same size." );
    
  if ( this->IsFull() )
    {
    return false;
    }
  else
    {
    m_CacheTable[source] = target;
    return true;
    }
}

template< class TMeasurementVector >
bool
MeanShiftModeCacheMethod< TMeasurementVector >
::GetMeasurementVector(MeasurementVectorType& source,
                       MeasurementVectorType& target)
{
  typename CacheTableType::iterator iter = m_CacheTable.find( source );
  ++m_NumberOfRequests;
  if ( iter != m_CacheTable.end() )
    {
    target = iter->second;
    ++m_HitsSuccess;
    ++m_TotalHitsSuccess;
    m_ConsecutiveFailures = 0;
    return true;
    }
  else
    {
    ++m_TotalHitsFailure;
    if ( this->IsFull() )
      {
      ++m_ConsecutiveFailures;
      if ( float(m_HitsSuccess / m_NumberOfRequests) < m_HitRatioThreshold )
        {
        ++m_TimesOfRebuildingByHitRatio;
        this->DestroyCacheTable();
        return false;
        }
      
      if ( m_ConsecutiveFailures > m_MaximumConsecutiveFailures )
        {
        ++m_TimesOfRebuildingByConsecutiveFailures;
        this->DestroyCacheTable();
        return false;
        }
      }
    return false;
    }
}

template< class TMeasurementVector >
bool
MeanShiftModeCacheMethod< TMeasurementVector >
::IsFull()
{
  if ( m_CacheTable.size() < m_MaximumEntries )
    {
    return false;
    }
  else
    {
    return true;
    }
}

template< class TMeasurementVector >
void
MeanShiftModeCacheMethod< TMeasurementVector >
::DestroyCacheTable()
{
  ++m_TimesOfRebuilding;
  m_TotalTableSize += m_CacheTable.size();
  m_NumberOfRequests = 0;
  m_HitsSuccess = 0;
  m_ConsecutiveFailures = 0;
  m_CacheTable.clear();
}


} // end of namespace Statistics 
} // end of namespace itk

#endif