18 #ifndef __itkPSMRegressionShapeMatrixAttribute_hxx 19 #define __itkPSMRegressionShapeMatrixAttribute_hxx 20 #include "itkPSMRegressionShapeMatrixAttribute.h" 25 template <
class T,
unsigned int VDimension>
26 void PSMRegressionShapeMatrixAttribute<T,VDimension>
30 for (
unsigned int i = 0; i < m_MeanMatrix.cols(); i++)
33 m_MeanMatrix.set_column(i, m_Intercept + m_Slope * m_Explanatory(i));
37 template <
class T,
unsigned int VDimension>
38 void PSMRegressionShapeMatrixAttribute<T,VDimension>
39 ::ResizeParameters(
unsigned int n)
41 vnl_vector<double> tmpA = m_Intercept;
42 vnl_vector<double> tmpB = m_Slope;
45 m_Intercept.set_size(n);
49 for (
unsigned int r = 0; r < tmpA.size(); r++)
51 m_Intercept(r) = tmpA(r);
56 template <
class T,
unsigned int VDimension>
57 void PSMRegressionShapeMatrixAttribute<T,VDimension>
58 ::ResizeMeanMatrix(
int rs,
int cs)
60 vnl_matrix<T> tmp = m_MeanMatrix;
63 m_MeanMatrix.set_size(rs, cs);
65 m_MeanMatrix.fill(0.0);
68 for (
unsigned int c = 0; c < tmp.cols(); c++)
70 for (
unsigned int r = 0; r < tmp.rows(); r++)
72 m_MeanMatrix(r,c) = tmp(r,c);
77 template <
class T,
unsigned int VDimension>
78 void PSMRegressionShapeMatrixAttribute<T,VDimension>
79 ::ResizeExplanatory(
unsigned int n)
81 if (n > m_Explanatory.size())
83 vnl_vector<double> tmp = m_Explanatory;
86 m_Explanatory.set_size(n);
87 m_Explanatory.fill(0.0);
90 for (
unsigned int r = 0; r < tmp.size(); r++)
92 m_Explanatory(r) = tmp(r);
97 template <
class T,
unsigned int VDimension>
101 const ParticleDomainAddEvent &
event 102 =
dynamic_cast<const ParticleDomainAddEvent &
>(e);
103 unsigned int d =
event.GetDomainIndex();
105 if ( d % this->m_DomainsPerShape == 0 )
107 this->ResizeMatrix(this->rows(), this->cols()+1);
108 this->ResizeMeanMatrix(this->rows(), this->cols()+1);
109 this->ResizeExplanatory(this->cols());
113 template <
class T,
unsigned int VDimension>
117 const ParticlePositionAddEvent &
event 118 =
dynamic_cast<const ParticlePositionAddEvent &
>(e);
121 const int d =
event.GetDomainIndex();
122 const unsigned int idx =
event.GetPositionIndex();
124 = ps->GetTransformedPosition(idx, d);
132 this->ResizeParameters(PointsPerDomain * VDimension * this->m_DomainsPerShape);
133 this->ResizeMatrix(PointsPerDomain * VDimension * this->m_DomainsPerShape,
135 this->ResizeMeanMatrix(PointsPerDomain * VDimension * this->m_DomainsPerShape,
141 unsigned int k = ((d % this->m_DomainsPerShape) * PointsPerDomain * VDimension)
142 + (idx * VDimension);
143 for (
unsigned int i = 0; i < VDimension; i++)
145 this->operator()(i+k, d / this->m_DomainsPerShape) = pos[i];
149 template <
class T,
unsigned int VDimension>
153 const ParticlePositionSetEvent &
event 154 = dynamic_cast <
const ParticlePositionSetEvent &>(e);
158 const int d =
event.GetDomainIndex();
159 const unsigned int idx =
event.GetPositionIndex();
161 const unsigned int PointsPerDomain = ps ->GetNumberOfParticles(d);
165 unsigned int k = ((d % this->m_DomainsPerShape) * PointsPerDomain * VDimension)
166 + (idx * VDimension);
168 for (
unsigned int i = 0; i < VDimension; i++)
170 this->operator()(i+k, d / this->m_DomainsPerShape) =
171 pos[i] - m_MeanMatrix(i+k, d/ this->m_DomainsPerShape);
175 template <
class T,
unsigned int VDimension>
179 this->ResizeExplanatory(v.size());
180 for (
unsigned int i = 0; i < v.size(); i++)
182 m_Explanatory[i] = v[i];
186 template <
class T,
unsigned int VDimension>
190 ResizeParameters(v.size());
191 for (
unsigned int i = 0; i < v.size(); i++)
197 template <
class T,
unsigned int VDimension>
201 ResizeParameters(v.size());
202 for (
unsigned int i = 0; i < v.size(); i++)
204 m_Intercept[i] = v[i];
209 template <
class T,
unsigned int VDimension>
213 const double tol = 1.0e-12;
214 vnl_matrix<double> X = *
this + m_MeanMatrix;
217 double n = (double)(X.cols());
219 vnl_vector<double> sumtx = m_Explanatory[0] * X.get_column(0);
220 vnl_vector<double> sumx = X.get_column(0);
221 double sumt = m_Explanatory[0];
222 double sumt2 = m_Explanatory[0] * m_Explanatory[0];
223 for (
unsigned int k = 1; k < X.cols(); k++)
225 sumtx += m_Explanatory[k] * X.get_column(k);
226 sumx += X.get_column(k);
227 sumt += m_Explanatory[k];
228 sumt2 += m_Explanatory[k] * m_Explanatory[k];
231 m_Slope = (n * sumtx - (sumx * sumt)) / ((n * sumt2 - (sumt*sumt)) + tol);
233 vnl_vector<double> sumbt = m_Slope * m_Explanatory[0];
234 for (
unsigned int k = 1; k < X.cols(); k++)
236 sumbt += m_Slope * m_Explanatory[k];
239 m_Intercept = (sumx - sumbt) / n;
242 template <
class T,
unsigned int VDimension>
255 for (
unsigned int i = 1; i < m_Explanatory.size(); i++)
257 if (m_Explanatory[i] != m_Explanatory[0]) ok =
true;
261 itkExceptionMacro(
"All explanatory variables are the same value. Please set these values before running the filter.");
263 m_Intercept.fill(0.0);
265 m_MeanMatrix.fill(0.0);
268 template <
class T,
unsigned int VDimension>
273 if (m_UpdateCounter >= m_RegressionInterval)
276 this->EstimateParameters();
277 this->UpdateMeanMatrix();
virtual void Initialize()
virtual void DomainAddEventCallback(Object *, const EventObject &e)
unsigned long int GetNumberOfParticles(unsigned int d=0) const
void SetIntercept(const std::vector< double > &v)
virtual void EstimateParameters()
A facade class that manages interactions with a particle system.
virtual void BeforeIteration()
virtual void PositionSetEventCallback(Object *o, const EventObject &e)
virtual void PositionAddEventCallback(Object *o, const EventObject &e)
void SetSlope(const std::vector< double > &v)