19 #ifndef __itkPSMMixedEffectsShapeMatrixAttribute_h 20 #define __itkPSMMixedEffectsShapeMatrixAttribute_h 22 #include "itkPSMShapeMatrixAttribute.h" 23 #include "vnl/vnl_vector.h" 24 #include "itkPSMParticleSystem.h" 25 #include "vnl/vnl_trace.h" 34 template <
class T,
unsigned int VDimension>
43 typedef SmartPointer<Self> Pointer;
44 typedef SmartPointer<const Self> ConstPointer;
45 typedef WeakPointer<const Self> ConstWeakPointer;
53 inline vnl_vector<double> ComputeMean(
double k)
const 55 return m_Intercept + m_Slope * k;
58 void ResizeParameters(
unsigned int n)
60 vnl_vector<double> tmpA = m_Intercept;
61 vnl_vector<double> tmpB = m_Slope;
64 m_Intercept.set_size(n);
68 for (
unsigned int r = 0; r < tmpA.size(); r++)
70 m_Intercept(r) = tmpA(r);
75 virtual void ResizeMeanMatrix(
int rs,
int cs)
77 vnl_matrix<T> tmp = m_MeanMatrix;
80 m_MeanMatrix.set_size(rs, cs);
81 m_MeanMatrix.fill(0.0);
84 for (
unsigned int c = 0; c < tmp.cols(); c++)
86 for (
unsigned int r = 0; r < tmp.rows(); r++)
88 m_MeanMatrix(r,c) = tmp(r,c);
93 void ResizeExplanatory(
unsigned int n)
95 if (n > m_Expl.size())
97 vnl_vector<double> tmp = m_Expl;
104 for (
unsigned int r = 0; r < tmp.size(); r++)
117 const itk::ParticleDomainAddEvent &
event 118 =
dynamic_cast<const itk::ParticleDomainAddEvent &
>(e);
119 unsigned int d =
event.GetDomainIndex();
121 if ( d % this->m_DomainsPerShape == 0 )
123 this->ResizeMatrix(this->rows(), this->cols()+1);
124 this->ResizeMeanMatrix(this->rows(), this->cols()+1);
125 this->ResizeExplanatory(this->cols());
131 const itk::ParticlePositionAddEvent &
event 132 =
dynamic_cast<const itk::ParticlePositionAddEvent &
>(e);
135 const int d =
event.GetDomainIndex();
136 const unsigned int idx =
event.GetPositionIndex();
138 = ps->GetTransformedPosition(idx, d);
145 this->ResizeParameters(PointsPerDomain * VDimension * this->m_DomainsPerShape);
146 this->ResizeMatrix(PointsPerDomain * VDimension * this->m_DomainsPerShape,
148 this->ResizeMeanMatrix(PointsPerDomain * VDimension * this->m_DomainsPerShape,
154 unsigned int k = ((d % this->m_DomainsPerShape) * PointsPerDomain * VDimension)
155 + (idx * VDimension);
156 for (
unsigned int i = 0; i < VDimension; i++)
158 this->operator()(i+k, d / this->m_DomainsPerShape) = pos[i];
164 const itk::ParticlePositionSetEvent &
event 165 = dynamic_cast <
const itk::ParticlePositionSetEvent &>(e);
169 const int d =
event.GetDomainIndex();
170 const unsigned int idx =
event.GetPositionIndex();
172 const unsigned int PointsPerDomain = ps ->GetNumberOfParticles(d);
175 unsigned int k = ((d % this->m_DomainsPerShape) * PointsPerDomain * VDimension)
176 + (idx * VDimension);
178 for (
unsigned int i = 0; i < VDimension; i++)
180 this->operator()(i+k, d / this->m_DomainsPerShape) =
181 pos[i] - m_MeanMatrix(i+k, d/ this->m_DomainsPerShape);
194 this->m_DomainsPerShape = i;
197 int GetDomainsPerShape()
const 199 return this->m_DomainsPerShape;
202 void SetTimePointsPerIndividual(
const vnl_vector<int> & v)
204 (this->m_TimeptsPerIndividual).set_size(v.size());
206 for (
int i = 0; i < v.size(); i++)
208 (this->m_TimeptsPerIndividual)(i) = v(i);
212 vnl_vector<int> &GetTimePointsPerIndividual()
const 214 return this->m_TimeptsPerIndividual;
217 vnl_vector<int> &GetTimePointsPerIndividual()
219 return this->m_TimeptsPerIndividual;
222 void SetNumIndividuals(
int i)
224 this->m_NumIndividuals = i;
227 int GetNumIndividuals()
229 return this->m_NumIndividuals;
232 void SetExplanatory(std::vector<double> v)
234 ResizeExplanatory(v.size());
235 for (
unsigned int i = 0; i < v.size(); i++)
241 void SetExplanatory(
unsigned int i,
double q)
246 const double &GetExplanatory(
unsigned int i)
const 251 double &GetExplanatory(
unsigned int i)
256 const vnl_vector<double> &GetSlope()
const 261 const vnl_vector<double> &GetIntercept()
const 266 const vnl_matrix<double> &GetSlopeRandom()
const 271 const vnl_matrix<double> &GetInterceptRandom()
const 273 return m_InterceptRand;
276 void SetSlope(
const std::vector<double> &v)
278 ResizeParameters(v.size());
279 for (
unsigned int i = 0; i < v.size(); i++)
285 void SetIntercept(
const std::vector<double> &v)
287 ResizeParameters(v.size());
288 for (
unsigned int i = 0; i < v.size(); i++)
290 m_Intercept[i] = v[i];
297 m_Intercept.fill(0.0);
299 m_MeanMatrix.fill(0.0);
301 m_SlopeRand.fill(0.0);
302 m_InterceptRand.fill(0.0);
308 if (m_UpdateCounter >= m_RegressionInterval)
311 this->EstimateParameters();
312 this->UpdateMeanMatrix();
316 void SetRegressionInterval(
int i)
318 m_RegressionInterval = i;
321 int GetRegressionInterval()
const 323 return m_RegressionInterval;
326 virtual void UpdateMeanMatrix();
328 virtual void EstimateParameters();
333 this->m_DefinedCallbacks.DomainAddEvent =
true;
334 this->m_DefinedCallbacks.PositionAddEvent =
true;
335 this->m_DefinedCallbacks.PositionSetEvent =
true;
336 this->m_DefinedCallbacks.PositionRemoveEvent =
true;
338 m_RegressionInterval = 1;
339 m_NumIndividuals = 1;
340 m_TimeptsPerIndividual.set_size(m_NumIndividuals);
341 for(
int i = 0; i < m_NumIndividuals; i++)
342 m_TimeptsPerIndividual(i) = 2;
347 void PrintSelf(std::ostream& os, Indent indent)
const 349 Superclass::PrintSelf(os,indent);
354 void operator=(
const Self&);
357 int m_RegressionInterval;
360 vnl_vector<double> m_Intercept;
361 vnl_vector<double> m_Slope;
364 vnl_vector<double> m_Expl;
367 vnl_matrix<double> m_MeanMatrix;
369 vnl_matrix<double> m_InterceptRand;
370 vnl_matrix<double> m_SlopeRand;
372 int m_NumIndividuals;
374 vnl_vector<int> m_TimeptsPerIndividual;
379 #ifndef ITK_MANUAL_INSTANTIATION 380 #include "itkPSMMixedEffectsShapeMatrixAttribute.hxx" Each column describes a shape. A shape may be composed of m_DomainsPerShape domains (default 1)...
virtual void BeforeIteration()
virtual void PositionRemoveEventCallback(Object *, const EventObject &)
virtual void DomainAddEventCallback(Object *, const EventObject &e)
unsigned long int GetNumberOfParticles(unsigned int d=0) const
void SetDomainsPerShape(int i)
A facade class that manages interactions with a particle system.
virtual void PositionSetEventCallback(Object *o, const EventObject &e)
virtual void PositionAddEventCallback(Object *o, const EventObject &e)
Base class for PSMParticleSystem attribute classes.