MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Stats.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2010 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include "header.h"
11 #include "Stats.h"
12 
15  "requestOut",
16  "Sends request for a field to target object"
17  );
18  return &requestOut;
19 }
20 
22 {
24  // Field Definitions
27  "mean",
28  "Mean of all sampled values or of spike rate.",
30  );
32  "sdev",
33  "Standard Deviation of all sampled values, or of rate.",
35  );
37  "sum",
38  "Sum of all sampled values, or total number of spikes.",
40  );
42  "num",
43  "Number of all sampled values, or total number of spikes.",
45  );
46  static ValueFinfo< Stats, unsigned int > windowLength(
47  "windowLength",
48  "Number of bins for windowed stats. "
49  "Ignores windowing if this value is zero. ",
52  );
54  "wmean",
55  "Mean of sampled values or of spike rate within window.",
57  );
59  "wsdev",
60  "Standard Deviation of sampled values, or rate, within window.",
62  );
64  "wsum",
65  "Sum of all sampled values, or total number of spikes, within window.",
67  );
69  "wnum",
70  "Number of all sampled values, or total number of spikes, "
71  "within window.",
73  );
74 
76  // MsgDest Definitions
78  static DestFinfo process( "process",
79  "Handles process call",
81  static DestFinfo reinit( "reinit",
82  "Handles reinit call",
84 
85  static DestFinfo input( "input",
86  "Handles continuous value input as a time-series. "
87  "Multiple inputs are allowed, they will be merged. ",
89 
91  // SharedFinfo Definitions
93  static Finfo* procShared[] = {
94  &process, &reinit
95  };
96  static SharedFinfo proc( "proc",
97  "Shared message for process and reinit",
98  procShared, sizeof( procShared ) / sizeof( const Finfo* )
99  );
100 
101  static Finfo* statsFinfos[] = {
102  &mean, // ReadOnlyValue
103  &sdev, // ReadOnlyValue
104  &sum, // ReadOnlyValue
105  &num, // ReadOnlyValue
106  &wmean, // ReadOnlyValue
107  &wsdev, // ReadOnlyValue
108  &wsum, // ReadOnlyValue
109  &wnum, // ReadOnlyValue
110  &windowLength, // Value
111  &input, // DestFinfo
112  requestOut(), // SrcFinfo
113  &proc // SharedFinfo
114  };
115 
116  static Dinfo< Stats > dinfo;
117  static Cinfo statsCinfo (
118  "Stats",
120  statsFinfos,
121  sizeof( statsFinfos ) / sizeof ( Finfo* ),
122  &dinfo
123  );
124 
125  return &statsCinfo;
126 }
127 
129 
131 // Inner class funcs
133 
135  :
136  mean_( 0.0 ), sdev_( 0.0 ), sum_( 0.0 ), num_( 0 ),
137  wmean_( 0.0 ), wsdev_( 0.0 ), wsum_( 0.0 ), wnum_( 0 ),
138  sumsq_( 0.0 ), isWindowDirty_( true )
139 {
140  ;
141 }
142 
144 // Process stuff.
146 
147 void Stats::process( const Eref& e, ProcPtr p )
148 {
149  this->vProcess( e, p );
150 }
151 
152 void Stats::vProcess( const Eref& e, ProcPtr p )
153 {
154  vector< double > v;
155  requestOut()->send( e, &v );
156  for ( vector< double >::const_iterator
157  i = v.begin(); i != v.end(); ++i )
158  input( *i );
159 }
160 
161 void Stats::reinit( const Eref& e, ProcPtr p )
162 {
163  this->vReinit( e, p );
164 }
165 
166 void Stats::vReinit( const Eref& e, ProcPtr p )
167 {
168  mean_ = 0.0;
169  sdev_ = 0.0;
170  sum_ = 0.0;
171  num_ = 0;
172  sumsq_ = 0.0;
173  wmean_ = 0.0;
174  wsdev_ = 0.0;
175  wsum_ = 0.0;
176  wnum_ = 0;
177  samples_.assign( samples_.size(), 0.0 );
178 }
180 // DestFinfos
182 
183 void Stats::input( double v )
184 {
185  sum_ += v;
186  sumsq_ += v * v;
187  if ( samples_.size() > 0 )
188  samples_[ num_ % samples_.size() ] = v;
189  ++num_;
190  isWindowDirty_ = true;
191 }
192 
194 // Fields
196 
197 double Stats::getMean() const
198 {
199  if ( num_ > 0 )
200  return sum_ / num_;
201  return 0.0;
202 }
203 
204 double Stats::getSdev() const
205 {
206  if ( num_ > 0 )
207  return sqrt( ( sumsq_ - sum_ * sum_ / num_ ) / num_ );
208  return 0.0;
209 }
210 
211 double Stats::getSum() const
212 {
213  return sum_;
214 }
215 
216 unsigned int Stats::getNum() const
217 {
218  return num_;
219 }
220 
221 double Stats::getWmean() const
222 {
224  return wmean_;
225 }
226 
227 double Stats::getWsdev() const
228 {
230  return wsdev_;
231 }
232 
233 double Stats::getWsum() const
234 {
236  return wsum_;
237 }
238 
239 unsigned int Stats::getWnum() const
240 {
242  return wnum_;
243 }
244 
245 void Stats::setWindowLength( unsigned int len )
246 {
247  if ( len < 1e6 ) {
248  samples_.resize( len, 0.0 );
249  isWindowDirty_ = true;
250  } else {
251  samples_.resize( 0 );
252  }
253 }
254 
255 unsigned int Stats::getWindowLength() const
256 {
257  return samples_.size();
258 }
259 
260 // Filthy function to const_cast the object. There is no particular
261 // reason other than the template requirements for this to be a const.
263 {
264  Stats* temp = const_cast< Stats* >( this );
265  temp->innerWindowCalculation();
266 }
267 
269 {
270  if ( isWindowDirty_ ) {
271  double wsumsq = 0.0;
272  wsum_ = 0.0;
273  unsigned int max = samples_.size();
274  if ( max > num_ )
275  max = num_;
276  for ( unsigned int i = 0; i < max; ++i ) {
277  wsum_ += samples_[i];
278  wsumsq += samples_[i] * samples_[i];
279  }
280  if ( max > 0 ) {
281  wmean_ = wsum_ / max;
282  wsdev_ = sqrt( ( wsumsq - wsum_ * wsum_ / max ) / max );
283  }
284  wnum_ = max;
285  isWindowDirty_ = false;
286  }
287 }
vector< double > samples_
Definition: Stats.h:65
unsigned int num_
Definition: Stats.h:58
bool isWindowDirty_
Definition: Stats.h:66
double getSdev() const
Definition: Stats.cpp:204
unsigned int getWindowLength() const
Definition: Stats.cpp:255
double wsdev_
Definition: Stats.h:60
double getWmean() const
Definition: Stats.cpp:221
static SrcFinfo1< vector< double > * > * requestOut()
Definition: Stats.cpp:13
void setWindowLength(unsigned int len)
Definition: Stats.cpp:245
Definition: Dinfo.h:60
double mean_
Definition: Stats.h:55
Stats()
Definition: Stats.cpp:134
static const Cinfo * statsCinfo
Definition: Stats.cpp:128
void process(const Eref &e, ProcPtr p)
Definition: Stats.cpp:147
double getSum() const
Definition: Stats.cpp:211
double sdev_
Definition: Stats.h:56
double sumsq_
Definition: Stats.h:63
double getWsdev() const
Definition: Stats.cpp:227
static const Cinfo * initCinfo()
Definition: Stats.cpp:21
double wsum_
Definition: Stats.h:61
double getMean() const
Definition: Stats.cpp:197
double sum_
Definition: Stats.h:57
double getWsum() const
Definition: Stats.cpp:233
unsigned int getNum() const
Definition: Stats.cpp:216
virtual void vReinit(const Eref &e, ProcPtr p)
Definition: Stats.cpp:166
unsigned int getWnum() const
Definition: Stats.cpp:239
unsigned int wnum_
Definition: Stats.h:62
virtual void vProcess(const Eref &e, ProcPtr p)
Virtual func for handling process calls for derived classes.
Definition: Stats.cpp:152
Definition: OpFunc.h:27
Definition: Eref.h:26
void reinit(const Eref &e, ProcPtr p)
Definition: Stats.cpp:161
Definition: Stats.h:12
void input(double v)
Definition: Stats.cpp:183
void innerWindowCalculation()
Definition: Stats.cpp:268
static const Cinfo * initCinfo()
Definition: Neutral.cpp:16
double wmean_
Definition: Stats.h:59
Definition: Cinfo.h:18
void doWindowCalculation() const
Definition: Stats.cpp:262
Definition: Finfo.h:12