File: itkGoodnessOfFitMixtureModelCostFunction.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 (163 lines) | stat: -rwxr-xr-x 4,511 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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkGoodnessOfFitMixtureModelCostFunction.txx,v $
  Language:  C++
  Date:      $Date: 2009-03-04 15:23:50 $
  Version:   $Revision: 1.9 $

  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 __itkGoodnessOfFitMixtureModelCostFunction_txx
#define __itkGoodnessOfFitMixtureModelCostFunction_txx

#include "itkGoodnessOfFitMixtureModelCostFunction.h"

namespace itk { 
namespace Statistics {

template< class TInputSample >
GoodnessOfFitMixtureModelCostFunction< TInputSample >
::GoodnessOfFitMixtureModelCostFunction()
{
  m_Function = 0;
}

template< class TInputSample >
GoodnessOfFitMixtureModelCostFunction< TInputSample >
::~GoodnessOfFitMixtureModelCostFunction()
{
}

template< class TInputSample >
void
GoodnessOfFitMixtureModelCostFunction< TInputSample >
::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os,indent);
  os << indent << "Function  " << m_Function << std::endl;
  for ( unsigned int i = 0; i < m_Components.size(); i++)
    {
    os << indent << "Components["<< i <<"]  "  << m_Components[i]  << std::endl;
    }
}

template< class TInputSample >
void
GoodnessOfFitMixtureModelCostFunction< TInputSample >
::AddComponent(ComponentType* component)
{
  m_Components.push_back(component);
}

template< class TInputSample >
void
GoodnessOfFitMixtureModelCostFunction< TInputSample >
::SetFunction(FunctionType* core)
{
  if ( m_Function != core )
    {
    m_Function = core;
    this->Modified();
    }
}

template< class TInputSample >
unsigned int
GoodnessOfFitMixtureModelCostFunction< TInputSample >
::GetNumberOfParameters() const 
{
  unsigned int size = 0;
  ComponentType* component;
  for ( unsigned int componentIndex = 0; componentIndex < m_Components.size();
        componentIndex++ )
    {
    component = m_Components[componentIndex];
    size += component->GetNumberOfParameters();
    }

  return size;
}

template< class TInputSample >
typename GoodnessOfFitMixtureModelCostFunction< TInputSample >::MeasureType
GoodnessOfFitMixtureModelCostFunction< TInputSample >
::GetValue(const ParametersType &parameters) const
{
  unsigned int i;

  double value = 0.0;
 
  unsigned int index = 0;
  unsigned int paramSize;
  ComponentType* component;
  for ( unsigned int  componentIndex = 0; componentIndex < m_Components.size();
        componentIndex++ )
    {
    component = m_Components[componentIndex];
    paramSize = component->GetNumberOfParameters();
    ParametersType params(paramSize);
    for ( i = 0; i < paramSize; i++)
      {
      params[i] = parameters[index];
      index++;
      }

    component->SetParameters(params);
    component->SetUseExpectedHistogram(m_Function->GetUseExpectedHistogram());
    if ( component->GetObservedHistogram() == 0 )
      {
      component->CreateHistograms();
      }

    component->Resample();

    if ( component->GetResampledSample()->GetTotalFrequency() == 0 ) 
      {
      return NumericTraits< double >::max();
      }

    component->CalculateProjectionAxes();
      
    m_Function->
      SetTotalObservedScale(component->GetTotalObservedScale());
      
    m_Function->SetObservedHistogram(component->GetObservedHistogram());
      
    if ( m_Function->GetUseExpectedHistogram() )
      {
      m_Function->SetExpectedHistogram(component->GetExpectedHistogram());
      }
      
    MeasurementVectorSizeType measurementVectorSize = 
                    component->GetMeasurementVectorSize();
    if( measurementVectorSize == 0 )
      { 
      itkExceptionMacro( << "Must set MeasurementVectorSize for the sample" );
      }
    for (i = 0; i < measurementVectorSize; i++)
      {
      component->Project(i);
      if ( m_Function->GetUseExpectedHistogram() )
        {
        component->UpdateExpectedHistogram();
        }
          
      m_Function->Update();
      value += m_Function->GetOutput();
      }
    } // end of while ( iter ...

  return value;
}

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

#endif