SIVIC API  0.9.26
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
svkKineticModelCostFunction.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2009-2016 The Regents of the University of California.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * • Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * • Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * • None of the names of any campus of the University of California, the name
13  * "The Regents of the University of California," or the names of any of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
26  * OF SUCH DAMAGE.
27  */
28 
29 
30 
31 /*
32  * $URL$
33  * $Rev$
34  * $Author$
35  * $Date$
36  *
37  * Authors:
38  * Jason C. Crane, Ph.D.
39  * Beck Olson,
40  */
41 
42 #ifndef SVK_KINETIC_MODEL_COST_FUNCTION_H
43 #define SVK_KINETIC_MODEL_COST_FUNCTION_H
44 
45 #include <itkParticleSwarmOptimizer.h>
46 using namespace svk;
47 
48 
49 /*
50  * Cost function for ITK optimizer:
51  * Abstract base class for kinetic modeling cost functions.
52  */
53 class svkKineticModelCostFunction : public itk::SingleValuedCostFunction
54 {
55 
56  public:
57 
59  typedef itk::SingleValuedCostFunction Superclass;
60  typedef itk::SmartPointer<Self> Pointer;
61  typedef itk::SmartPointer<const Self> ConstPointer;
62  itkTypeMacro( svkKineticModelCostFunction, SingleValuedCostFunction );
63 
64  typedef Superclass::ParametersType ParametersType;
65  typedef Superclass::DerivativeType DerivativeType;
66  typedef Superclass::MeasureType MeasureType;
67 
68 
69  /*
70  *
71  */
73  {
74  this->numTimePoints = 0;
75  this->numSignals = 0;
76  }
77 
78 
79  /*
80  *
81  */
83  {
84  }
85 
86 
87  /*
88  *
89  */
90  void GetDerivative( const ParametersType & ,
91  DerivativeType & ) const
92  {
93  }
94 
95 
100  MeasureType GetValue( const ParametersType & parameters ) const
101  {
102  double cost = GetResidual( parameters );
103  MeasureType measure = cost;
104  return measure;
105  }
106 
107 
111  void SetNumberOfSignals( int numSignals )
112  {
113  this->numSignals = numSignals;
114  this->signalVector.resize(numSignals);
115  this->modelSignalVector.resize(numSignals);
116  }
117 
118 
119  /*
120  * Return the number of signals in the concrete model instance
121  */
122  int GetNumberOfSignals( ) const
123  {
124  return this->numSignals;
125  }
126 
127 
128  /*
129  * Set the kinetic signal for metabolite N
130  */
131  void SetSignal( float* signal, int signalIndex, string signalName)
132  {
133  map<string, float*> signalMap;
134  signalMap[signalName] = signal;
135  if (signalIndex > this->signalVector.size() ) {
136  cout << "ERROR, signal index is out of range" << endl;
137  exit(1);
138  }
139  this->signalVector[signalIndex] = signalMap;
140  }
141 
142 
143  /*
144  * Get pointer to the vector of input kinetic signals
145  */
146  vector < map < string, float* > >* GetSignals( )
147  {
148  return &(this->signalVector);
149  }
150 
151 
152  /*
153  * Get the specific signal at the specified time point.
154  */
155  float GetSignalAtTime(int signalIndex, int timePt) const
156  {
157  //map< string, float*>::iterator mapIter;
158  return (this->signalVector[ signalIndex ].begin()->second)[timePt];
159  }
160 
161 
162  /*
163  * Get the specific signal at the specified time point.
164  */
165  string GetSignalName( int signalIndex ) const
166  {
167  return this->signalVector[ signalIndex ].begin()->first;
168  }
169 
170 
171  /*
172  * Get the specific signal (pointer to the float array)
173  */
174  float* GetSignal(int signalIndex) const
175  {
176  return (this->signalVector[ signalIndex ]).begin()->second;
177  }
178 
179 
180  /*
181  * Initializes the arrays that will hold the fitted data
182  * with the correct number of points.
183  */
184  void SetNumTimePoints( int numTimePoints )
185  {
186  this->numTimePoints = numTimePoints;
187  if ( this->numSignals == 0 ) {
188  cout << "ERROR, numSignals not yet initialized" << endl;
189  exit(1);
190  }
191  for ( int i = 0; i < this->numSignals; i++ ) {
192  float* model = new float [this->numTimePoints];
193  string modelSignalName = this->GetSignalName(i);
194  cout << "NAME: " << modelSignalName << endl;
195  this->SetModelSignal( model, i, modelSignalName);
196  }
197  }
198 
199 
203  void SetTR( float TR )
204  {
205  this->TR = TR;
206  }
207 
208 
209  /*
210  * Set the kinetic signal for metabolite N
211  */
212  void SetModelSignal( float* modelSignal, int modelSignalIndex, string modelSignalName)
213  {
214  map<string, float*> modelSignalMap;
215  modelSignalMap[modelSignalName] = modelSignal;
216  if ( modelSignalIndex > this->modelSignalVector.size() ) {
217  cout << "ERROR, signal index is out of range" << endl;
218  exit(1);
219  }
220  this->modelSignalVector[modelSignalIndex] = modelSignalMap;
221  }
222 
223 
224  /*
225  * Get the specific model at the specified time point.
226  */
227  float GetModelSignalAtTime(int modelSignalIndex, int timePt) const
228  {
229  return (this->modelSignalVector[ modelSignalIndex ].begin()->second)[timePt];
230  }
231 
232 
233  /*
234  * Get the specific signal (pointer to the float array)
235  */
236  float* GetModelSignal(int modelSignalIndex) const
237  {
238  return (this->modelSignalVector[ modelSignalIndex ]).begin()->second;
239  }
240 
252  unsigned int GetNumberOfOutputPorts(void) const
253  {
254  int numOutputPorts = this->GetNumberOfSignals() + this->GetNumberOfParameters();
255  return numOutputPorts;
256  }
257 
258 
269  virtual void GetKineticModel( const ParametersType& parameters ) const = 0;
270 
271 
275  virtual unsigned int GetNumberOfParameters(void) const = 0;
276 
277 
281  virtual void InitNumberOfSignals(void) = 0;
282 
283 
287  virtual void InitOutputDescriptionVector(vector<string>* outputDescriptionVector ) const = 0;
288 
289 
293  virtual void InitParamBounds( float* lowerBounds, float* upperBounds ) = 0;
294 
295 
299  virtual void InitParamInitialPosition( ParametersType* initialPosition ) = 0;
300 
301 
305  virtual void GetParamFinalScaledPosition( ParametersType* finalPosition ) = 0;
306 
307 
308 
309  protected:
310 
311 
317  MeasureType GetResidual( const ParametersType& parameters) const
318  {
319  //cout << "GUESS: " << parameters[0] << " " << parameters[1] << endl;;
320  this->GetKineticModel( parameters );
321  double residual = 0;
322  for ( int t = 0; t < this->numTimePoints; t++ ) {
323  for (int sigNumber = 0; sigNumber < this->GetNumberOfSignals(); sigNumber++ ) {
324  residual += ( this->GetSignalAtTime(sigNumber, t) - this->GetModelSignal(sigNumber)[t] )
325  * ( this->GetSignalAtTime(sigNumber, t) - this->GetModelSignal(sigNumber)[t] );
326  }
327  }
328  MeasureType measure = residual ;
329  return measure;
330  }
331 
332 
333  // this is the vector of float arrays representing the observed metabolite signals as a function of time
334  // for different number of sites this should be flexibile
335  vector< map < string, float* > > signalVector;
336 
337  // this is the vector of float arrays representing the modeled/computed metabolite signals as a function of time
338  // for different number of sites this should be flexibile based on the model parameters.
339  vector< map < string, float* > > modelSignalVector;
342  float TR;
343 
344 
345 
346 };
347 
348 
349 
350 #endif// SVK_KINETIC_MODEL_COST_FUNCTION_H
MeasureType GetValue(const ParametersType &parameters) const
Definition: svkKineticModelCostFunction.h:100
Definition: svkKineticModelCostFunction.h:53
float * GetModelSignal(int modelSignalIndex) const
Definition: svkKineticModelCostFunction.h:236
string GetSignalName(int signalIndex) const
Definition: svkKineticModelCostFunction.h:165
itk::SingleValuedCostFunction Superclass
Definition: svkKineticModelCostFunction.h:59
float GetSignalAtTime(int signalIndex, int timePt) const
Definition: svkKineticModelCostFunction.h:155
void SetTR(float TR)
Definition: svkKineticModelCostFunction.h:203
itk::SmartPointer< const Self > ConstPointer
Definition: svkKineticModelCostFunction.h:61
int numSignals
Definition: svkKineticModelCostFunction.h:341
float GetModelSignalAtTime(int modelSignalIndex, int timePt) const
Definition: svkKineticModelCostFunction.h:227
MeasureType GetResidual(const ParametersType &parameters) const
Definition: svkKineticModelCostFunction.h:317
~svkKineticModelCostFunction()
Definition: svkKineticModelCostFunction.h:82
void SetSignal(float *signal, int signalIndex, string signalName)
Definition: svkKineticModelCostFunction.h:131
vector< map< string, float * > > * GetSignals()
Definition: svkKineticModelCostFunction.h:146
float * GetSignal(int signalIndex) const
Definition: svkKineticModelCostFunction.h:174
Superclass::DerivativeType DerivativeType
Definition: svkKineticModelCostFunction.h:65
itk::SmartPointer< Self > Pointer
Definition: svkKineticModelCostFunction.h:60
void SetModelSignal(float *modelSignal, int modelSignalIndex, string modelSignalName)
Definition: svkKineticModelCostFunction.h:212
unsigned int GetNumberOfOutputPorts(void) const
Definition: svkKineticModelCostFunction.h:252
void SetNumberOfSignals(int numSignals)
Definition: svkKineticModelCostFunction.h:111
float TR
Definition: svkKineticModelCostFunction.h:342
Superclass::ParametersType ParametersType
Definition: svkKineticModelCostFunction.h:64
vector< map< string, float * > > modelSignalVector
Definition: svkKineticModelCostFunction.h:339
svkKineticModelCostFunction()
Definition: svkKineticModelCostFunction.h:72
int GetNumberOfSignals() const
Definition: svkKineticModelCostFunction.h:122
vector< map< string, float * > > signalVector
Definition: svkKineticModelCostFunction.h:335
Superclass::MeasureType MeasureType
Definition: svkKineticModelCostFunction.h:66
void GetDerivative(const ParametersType &, DerivativeType &) const
Definition: svkKineticModelCostFunction.h:90
svkKineticModelCostFunction Self
Definition: svkKineticModelCostFunction.h:58
int numTimePoints
Definition: svkKineticModelCostFunction.h:340
void SetNumTimePoints(int numTimePoints)
Definition: svkKineticModelCostFunction.h:184