Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
StatServices Class Reference

The Service class used to manage the StatHandler objects. More...

#include <statservices.h>

+ Inheritance diagram for StatServices:
+ Collaboration diagram for StatServices:

Public Types

typedef list< StatHandlerBase * >::const_iterator stat_it
 
typedef list< StatRecBase * >::const_iterator rec_it
 

Public Member Functions

 StatServices ()
 
virtual ~StatServices ()
 
virtual bool init ()
 
Metapopget_pop_ptr ()
 
void set_pop_ptr (Metapop *pop)
 
void set (string &str, unsigned int occ)
 
void setStatOptions (string &str)
 
string & getStatArg ()
 
unsigned int getOccurrence ()
 
void setOcccurrence (unsigned int value)
 
void setOccurrences (map< unsigned int, unsigned int > timeTable)
 Sets the list of generation for which statistics must be recorded during a run. More...
 
unsigned int getNumOccurrences ()
 Returns the maximum number of generation records per replicate. More...
 
unsigned int getNumOccurrences (unsigned int replicate)
 Returns the number of generation records present in the stat table for a replicate. More...
 
unsigned int getCurrentOccurrence ()
 Returns the last generation recorded for current replicate. More...
 
void resetCurrentOccurrence ()
 Resets the occurrence iterator to the beginning of the list of generation occurrences. More...
 
void printStatHeaders (ofstream &FH)
 
void doPrintAverages ()
 
void cancelPrintAverages ()
 
bool getPrintAveragesOpt ()
 
void printStatValue (ofstream &FH, unsigned int repl_idx)
 Prints the stat values to the '.txt' output file. More...
 
void printStatAverage (ofstream &FH)
 
void setCompactOutputFormat ()
 
void setFieldWidth (unsigned int val)
 
void setFieldPrecision (unsigned int val)
 
void setFieldSeparator (unsigned char c)
 
void setDefaultOutputFormat ()
 
list< StatRecBase * > getAllStats ()
 
unsigned int getNumStats ()
 
stat_it getFirst ()
 
stat_it getLast ()
 
vector< double * > * getReplicateStatRecords (unsigned int replicate)
 
double * getGenerationStatValues (unsigned int replicate, unsigned int occurence) const
 
void copyGenerationStatValues (unsigned int replicate, unsigned int occurence, double *values, unsigned int size)
 
void recordStats (unsigned int gen)
 record stat values in stat table by calling all stat recorders. More...
 
virtual void notify ()
 
virtual void load (SimComponent *sc)
 tell the SimComponent to load its stat handlers More...
 
virtual void attach (Handler *H)
 attach the StatHandler to the current list (_statHandlers) of the StatServices More...
 
virtual void reset ()
 clear the list of StatHandler More...
 
void reset_stat_table ()
 Deletes the stat tables. More...
 
- Public Member Functions inherited from Service
 Service ()
 
virtual ~Service ()
 

Private Attributes

Metapop_popPtr
 
list< StatHandlerBase * > _statHandlers
 List of stat handlers declared by currently active simulation components. More...
 
list< StatRecBase * > _statRecorders
 List of stat recorders. More...
 
unsigned int _numRecorders
 Number of stats to record. More...
 
vector< vector< double * > > _statValues
 Table containing all recorded stats, replicate x generation x (num recorders + 2) . More...
 
double * _currentStatValues
 Pointer to the last recorded stats. More...
 
string _statArg
 The string argument of the 'stat' input option. More...
 
unsigned int _occurrence
 Deprecated. More...
 
list< unsigned int > _occurrences
 List of all generations to record. More...
 
list< unsigned int >::const_iterator _current_occurrence
 Iterator pointing to the current generation to record. More...
 
unsigned int _width
 
unsigned int _precision
 
unsigned char _separator
 
bool _printAverages
 

Detailed Description

The Service class used to manage the StatHandler objects.

Member Typedef Documentation

◆ rec_it

typedef list< StatRecBase* >::const_iterator StatServices::rec_it

◆ stat_it

typedef list< StatHandlerBase* >::const_iterator StatServices::stat_it

Constructor & Destructor Documentation

◆ StatServices()

StatServices::StatServices ( )
inline
100  { }
Metapop * _popPtr
Definition: statservices.h:54
unsigned char _separator
Definition: statservices.h:88
unsigned int _occurrence
Deprecated.
Definition: statservices.h:75
unsigned int _width
Definition: statservices.h:86
unsigned int _precision
Definition: statservices.h:86
unsigned int _numRecorders
Number of stats to record.
Definition: statservices.h:63
bool _printAverages
Definition: statservices.h:90
double * _currentStatValues
Pointer to the last recorded stats.
Definition: statservices.h:69

◆ ~StatServices()

virtual StatServices::~StatServices ( )
inlinevirtual
102 { reset_stat_table(); }
void reset_stat_table()
Deletes the stat tables.
Definition: statservices.cc:240

Member Function Documentation

◆ attach()

void StatServices::attach ( Handler H)
virtual

attach the StatHandler to the current list (_statHandlers) of the StatServices

Parameters
Hthe StatHandler

Implements Service.

178 {
179  StatHandlerBase* SH = dynamic_cast<StatHandlerBase*> (H);
180 
181  _statHandlers.push_back(SH);
182 
183  SH->set_service(this);
184 }
Base class of the StatHandler class, implements the Handler interface.
Definition: stathandler.h:48
void set_service(StatServices *srv)
Definition: stathandler.h:73
list< StatHandlerBase * > _statHandlers
List of stat handlers declared by currently active simulation components.
Definition: statservices.h:57

References StatHandlerBase::set_service().

Referenced by LCE_Selection_base::loadStatServices(), Metapop::loadStatServices(), TProtoBDMI::loadStatServices(), TProtoDeletMutations_bitstring::loadStatServices(), TProtoDispersal::loadStatServices(), TProtoNeutralGenes::loadStatServices(), TProtoQuanti::loadStatServices(), and TProtoWolbachia::loadStatServices().

◆ cancelPrintAverages()

void StatServices::cancelPrintAverages ( )
inline

◆ copyGenerationStatValues()

void StatServices::copyGenerationStatValues ( unsigned int  replicate,
unsigned int  occurence,
double *  values,
unsigned int  size 
)
inline
184  {
185  assert( _statValues[ replicate-1 ].size() < _occurrences.size() );
186 
187  assert( size == _numRecorders+2);
188 
189  double* new_record = new double[size];
190 
191  memcpy(new_record, values, size*sizeof(double));
192 
193  _statValues[ replicate-1 ].push_back(new_record);
194  }
list< unsigned int > _occurrences
List of all generations to record.
Definition: statservices.h:78
vector< vector< double * > > _statValues
Table containing all recorded stats, replicate x generation x (num recorders + 2) .
Definition: statservices.h:66

◆ doPrintAverages()

void StatServices::doPrintAverages ( )
inline

◆ get_pop_ptr()

Metapop* StatServices::get_pop_ptr ( )
inline
106 {return _popPtr;}

Referenced by StatHandlerBase::get_pop_ptr().

◆ getAllStats()

list< StatRecBase * > StatServices::getAllStats ( )
406 {
407  list< StatHandlerBase* >::iterator HIT = _statHandlers.begin();
408 
409  list<StatRecBase*> allStats, stats;
410 
411  while(HIT != _statHandlers.end()) {
412  stats = (*HIT)->getStats();
413  allStats.insert(allStats.end(), stats.begin(), stats.end());
414  HIT++;
415  }
416 
417  return allStats;
418 }

Referenced by SimRunner::get_allRegisteredStats().

◆ getCurrentOccurrence()

unsigned int StatServices::getCurrentOccurrence ( )
inline

Returns the last generation recorded for current replicate.

136 {return *_current_occurrence;}
list< unsigned int >::const_iterator _current_occurrence
Iterator pointing to the current generation to record.
Definition: statservices.h:83

Referenced by StatHandlerBase::getCurrentOccurrence().

◆ getFirst()

stat_it StatServices::getFirst ( )
inline
168 {return _statHandlers.begin();}

◆ getGenerationStatValues()

double* StatServices::getGenerationStatValues ( unsigned int  replicate,
unsigned int  occurence 
) const
inline
178  {
179  assert(occurence < _occurrences.size());
180  return _statValues[ replicate-1 ][ occurence ];
181  }

◆ getLast()

stat_it StatServices::getLast ( )
inline
170 {return _statHandlers.end();}

◆ getNumOccurrences() [1/2]

unsigned int StatServices::getNumOccurrences ( )
inline

Returns the maximum number of generation records per replicate.

126 {return _occurrences.size();}

Referenced by StatHandlerBase::getNumOccurrences().

◆ getNumOccurrences() [2/2]

unsigned int StatServices::getNumOccurrences ( unsigned int  replicate)
inline

Returns the number of generation records present in the stat table for a replicate.

Parameters
replicatethe replicate number (not the index in the stat table).
132  {
133  return _statValues[replicate-1].size();
134  }

◆ getNumStats()

unsigned int StatServices::getNumStats ( )
inline
166 {return _statRecorders.size();}
list< StatRecBase * > _statRecorders
List of stat recorders.
Definition: statservices.h:60

◆ getOccurrence()

unsigned int StatServices::getOccurrence ( )
inline

◆ getPrintAveragesOpt()

bool StatServices::getPrintAveragesOpt ( )
inline
144 {return _printAverages;}

Referenced by LCE_StatFH::FHwrite().

◆ getReplicateStatRecords()

vector<double*>* StatServices::getReplicateStatRecords ( unsigned int  replicate)
inline
173  {
174  return &_statValues[ replicate-1 ];
175  }

◆ getStatArg()

string& StatServices::getStatArg ( )
inline
115 {return _statArg;}
string _statArg
The string argument of the 'stat' input option.
Definition: statservices.h:72

◆ init()

bool StatServices::init ( )
virtual

Implements Service.

48 {
49  stat_it HIT;
50 
51  HIT = _statHandlers.begin();
52 
53  //first init the stat handlers -> will define the dims of the stat tables
54  while(HIT != _statHandlers.end()) {
55  (*HIT)->init();
56  HIT++;
57  }
58 
59  istringstream stats(_statArg);
60  string arg;
61  list<string> args;
62 
63  //extract the stat options from the string read in the init file
64  while(stats>>arg) args.push_back(arg);
65 
66  list<string>::iterator token = args.begin();
67 
68  bool is_set = false;
69 
70  while(token != args.end()) {
71 
72  HIT = _statHandlers.begin();
73 
74  is_set = false;
75 
76  while(HIT != _statHandlers.end() && !is_set) {
77  is_set |= (*HIT)->setStatRecorders( (*token) );
78  HIT++;
79  }
80 
81  if(!is_set) {
82  error("the string \"%s\" is not a valid stat option\n",(*token).c_str());
83  return false;
84  }
85 
86  token++;
87  }
88 
89  //---- set the list of stat recorders:
90  _statRecorders.clear();
91 
92  for (HIT = _statHandlers.begin(); HIT != _statHandlers.end(); ++HIT) {
93 
94  _statRecorders.splice(_statRecorders.end(), (*HIT)->getStats());
95  //this removes the recorders from the handler's list (in principle)
96  //this means we need to delete the recorders within the StatServices!!
97  }
98 
100 
101 
102  //---- set iterator to first generation occurrence to record
104  //this is safe as the occurrence list is set before init() is called.
105 
106 // //reset output option for per-gen mean stats
107 // _printAverages = 1; //this resets user-defined option!!
108 
109  //---- set the stat table:
111 
112  for (unsigned int i = 0; i < SIMenv::getReplicates(); ++i) {
113  _statValues.push_back(vector<double*>());
114  }
115 
116  return true;
117 }
static unsigned int getReplicates()
Definition: simenv.h:63
list< StatHandlerBase * >::const_iterator stat_it
Definition: statservices.h:94
int error(const char *str,...)
Definition: output.cc:77

References error(), and SIMenv::getReplicates().

Referenced by SimRunner::init_components().

◆ load()

void StatServices::load ( SimComponent sc)
virtual

tell the SimComponent to load its stat handlers

Parameters
scthe SimComponent

Implements Service.

171 {
172  sc->loadStatServices(this);
173 }
virtual void loadStatServices(StatServices *loader)=0
Loads the component's StatHandler onto the StatServices.

References SimComponent::loadStatServices().

Referenced by SimRunner::register_component().

◆ notify()

void StatServices::notify ( )
virtual

Implements Service.

189 {
190 #ifdef _DEBUG_
191  message("StatServices::notify (%i/%i): ", SIMenv::getCurrentGeneration(),
193  fflush(stdout);
194 #endif
195 
199  }
200 
201 #ifdef _DEBUG_
202  message("\n");
203 #endif
204 }
static unsigned int getCurrentGeneration()
Definition: simenv.h:60
void recordStats(unsigned int gen)
record stat values in stat table by calling all stat recorders.
Definition: statservices.cc:255
void message(const char *message,...)
Definition: output.cc:40

References SIMenv::getCurrentGeneration(), and message().

Referenced by LCE_StatServiceNotifier::execute().

◆ printStatAverage()

void StatServices::printStatAverage ( ofstream &  FH)
338 {
339  vector< double > means;
340  vector< unsigned int > stat_rep_cntr;
341  unsigned int num_repl = SIMenv::getReplicates(), rep_cntr;
342  unsigned int num_gen = getNumOccurrences();
343  unsigned int current_gen = 0;
344 
345  //print the first row with stats headers:
346 // FH.setf(ios::left,ios::adjustfield);
347  FH << setw(_width) << left <<"generation";
348 
349  //print the stat names:
350  printStatHeaders(FH);
351 
352  FH<< _separator << setw(_width) << "alive.rpl" << endl;
353 
354 // FH << setprecision(_precision);
355 
356  for (unsigned int gen = 0; gen < num_gen; ++gen) {
357 
358  rep_cntr = 0;
359 
360  means.assign(_numRecorders,0);
361  stat_rep_cntr.assign(_numRecorders, 0);
362 
363  for (unsigned int rep = 0; rep < num_repl; ++rep) {
364 
365  //verify if this replicate was not extinct at this generation
366  if ( _statValues[ rep ].size() > gen) rep_cntr++;
367  else continue; //skip this replicate
368 
369  for (unsigned int j = 0, pos = 0; j < _numRecorders; ++j) {
370  pos = j+2;
371  // verify that the value is not NAN
372  if( !isnan(_statValues[ rep ][ gen ][pos]) ) {
373  means[j] += _statValues[rep][gen][pos]; //two first are replicate and generation
374  stat_rep_cntr[j]++;
375  }
376  }
377  current_gen = _statValues[rep][gen][1];
378  }
379 
380  if(rep_cntr != 0) {
381 
382 // FH.width(_width);
383 // FH.setf(ios::left,ios::adjustfield);
384  //FH<<_statValues[0][gen][1];
385  FH << setw(_width) << left << current_gen;
386 
387  FH << setprecision(_precision);
388 
389  for (unsigned int j = 0; j < _numRecorders; ++j) {
390  FH << _separator << setw(_width) << means[j] / stat_rep_cntr[j];
391  }
392  //print number of alive replicates 'til this generation
393  FH << _separator << setw(_width) << rep_cntr;
394 
395  FH << endl;
396  }
397 
398  }
399 
400 
401 }
unsigned int getNumOccurrences()
Returns the maximum number of generation records per replicate.
Definition: statservices.h:126
void printStatHeaders(ofstream &FH)
Definition: statservices.cc:280

References SIMenv::getReplicates().

Referenced by LCE_StatFH::PrintStat_byGen().

◆ printStatHeaders()

void StatServices::printStatHeaders ( ofstream &  FH)
281 {
282  for (rec_it IT = _statRecorders.begin(); IT != _statRecorders.end(); ++IT) {
283  FH << _separator << setw(_width) << (*IT)->getName();
284  }
285 }
list< StatRecBase * >::const_iterator rec_it
Definition: statservices.h:96

◆ printStatValue()

void StatServices::printStatValue ( ofstream &  FH,
unsigned int  repl_idx 
)

Prints the stat values to the '.txt' output file.

Parameters
FHthe file output stream
repl_idxthe replicate index in the stat tables
290 {
291 
292 // FH.width(_width);
293 // FH.setf(ios::left,ios::adjustfield);
294 
295  if(SIMenv::getCurrentReplicate() == 1) {
296 
297  //print the first row with stat headers:
298  FH << setw(_width) << left <<"replicate"<< _separator << setw(_width) <<"generation";
299 
300  //print the stat names:
301  printStatHeaders(FH);
302 
303  FH << endl;
304  }
305 
306  for (unsigned int gen = 0, i = 2; gen < _statValues[repl_idx].size(); ++gen) {
307  //for all generations recorded, print stuff:
308 
309  assert( repl_idx + 1 == _statValues[repl_idx][gen][0]); //check if replicate is ok
310 
311 // FH.width(_width);
312  FH << setw(_width) << left << _statValues[repl_idx][gen][0]; //replicate number
313  FH << _separator;
314  FH << setprecision(16); //to avoid scientific notation for generation number
315  FH << setw(_width) << _statValues[repl_idx][gen][1]; //generation recorded
316  FH << setprecision(_precision); //this precision is needed only for stat values
317 
318  i = 2;
319  //print all stats on one line
320  for (rec_it IT = _statRecorders.begin(); IT != _statRecorders.end(); ++IT) {
321 
322  FH << _separator << setw(_width) << _statValues[repl_idx][gen][i];
323 
324  i++;
325 
326  }
327 
328  FH<<endl;
329 
330  assert( i == _numRecorders + 2 );
331  }
332 
333 }
static unsigned int getCurrentReplicate()
Definition: simenv.h:62

References SIMenv::getCurrentReplicate().

Referenced by LCE_StatFH::FHwrite().

◆ recordStats()

void StatServices::recordStats ( unsigned int  gen)

record stat values in stat table by calling all stat recorders.

256 {
257  _currentStatValues = new double [ _numRecorders + 2]; //+ replicate + generation
258 
260  _currentStatValues[1] = gen;
261 
262  unsigned int i = 2, tab_idx = SIMenv::getCurrentReplicate() - 1;
263 
264  for (rec_it REC = _statRecorders.begin(); REC != _statRecorders.end(); ++REC)
265  {
266 
267  assert(i < _numRecorders+2 );
268 
269  _currentStatValues[i++] = (*REC)->setVal( _popPtr->getCurrentAge() );
270 
271  }
272 
273  _statValues[ tab_idx ].push_back(_currentStatValues);
274 
275  assert( _statValues[ tab_idx ].size() <= getNumOccurrences() );
276 }
age_t getCurrentAge()
Definition: metapop.h:299

References SIMenv::getCurrentReplicate().

◆ reset()

void StatServices::reset ( )
virtual

clear the list of StatHandler

Implements Service.

209 {
210 #ifdef _DEBUG_
211  message("StatServices::reset\n");
212 #endif
213 
214  stat_it HIT = _statHandlers.begin();
215 
216  while(HIT != _statHandlers.end()) {
217  (*HIT)->reset();
218  HIT++;
219  }
220 
221  _statHandlers.clear();
222 
223  //the stat recorders must be freed here, they have been removed from the StatHandlers
224  rec_it REC = _statRecorders.begin();
225 
226  for(; REC != _statRecorders.end(); ++REC) delete (*REC);
227 
228  _statRecorders.clear();
229 
231 
232 #ifdef _DEBUG_
233  message("StatServices::reset done\n");
234 #endif
235 
236 }

References message().

Referenced by SimRunner::reset_services().

◆ reset_stat_table()

void StatServices::reset_stat_table ( )

Deletes the stat tables.

241 {
242  for (unsigned int i = 0; i < _statValues.size(); ++i) {
243  for (unsigned int j = 0; j < _statValues[i].size(); ++j) {
244  delete [] _statValues[i][j];
245  }
246  _statValues[i].clear();
247  }
248  _statValues.clear();
249 
250  _currentStatValues = 0;
251 }

◆ resetCurrentOccurrence()

void StatServices::resetCurrentOccurrence ( )
inline

Resets the occurrence iterator to the beginning of the list of generation occurrences.

Referenced by SimRunner::setForFirstGeneration().

◆ set()

void StatServices::set ( string &  str,
unsigned int  occ 
)
inline
111  {_statArg = str; _occurrence = occ;}

◆ set_pop_ptr()

void StatServices::set_pop_ptr ( Metapop pop)
inline
108 {_popPtr=pop;}

Referenced by SimRunner::init_components().

◆ setCompactOutputFormat()

void StatServices::setCompactOutputFormat ( )
inline
153 {_width = 0; _separator = ' ';}

Referenced by LCE_StatServiceNotifier::setParameters().

◆ setDefaultOutputFormat()

void StatServices::setDefaultOutputFormat ( )
inline
161 {_width = 12; _precision = 6; _separator = '\t';}

Referenced by LCE_StatServiceNotifier::setParameters().

◆ setFieldPrecision()

void StatServices::setFieldPrecision ( unsigned int  val)
inline

◆ setFieldSeparator()

void StatServices::setFieldSeparator ( unsigned char  c)
inline

◆ setFieldWidth()

void StatServices::setFieldWidth ( unsigned int  val)
inline
155 {_width = val;}

Referenced by LCE_StatServiceNotifier::setParameters().

◆ setOcccurrence()

void StatServices::setOcccurrence ( unsigned int  value)
inline
120 {_occurrence = value;}

◆ setOccurrences()

void StatServices::setOccurrences ( map< unsigned int, unsigned int >  timeTable)

Sets the list of generation for which statistics must be recorded during a run.

Parameters
timeTablea map containing the generations at which stats must be recorded
122 {
123  map<unsigned int, unsigned int>::const_iterator time = timeTable.begin(), next;
124 
125  _occurrences.clear();
126  _occurrences.push_front(0);
127 
128  if (time->first == 0 && time->second != 0) { //this is a recursive logtime
129 
130  unsigned int next_term;
131 
132  for (;time != timeTable.end(); ++time) {
133 
134  next = time;
135  next++;
136  next_term = ( next != timeTable.end() ?
137  next->first : SIMenv::getGenerations() );
138 
139  while( _occurrences.back() + time->second < next_term)
140  {
141  _occurrences.push_back( _occurrences.back() + time->second );
142  }
143  _occurrences.push_back(next_term);
144  }
145 
146 
147  } else if (time->second == 0) { //non-recursive logtime, only records specific times
148 
149  for (;time != timeTable.end(); ++time) _occurrences.push_back(time->first);
150 
151  } else {
152 
153  fatal("something went wrong while setting stat recording times (first=%i, second=%i)\n",
154  time->first, time->second);
155  }
156 
157  //clean up:
158  while (_occurrences.back() > SIMenv::getGenerations()) _occurrences.pop_back();
159 
160  if(_occurrences.back() != SIMenv::getGenerations()) //automatically record last gen
162 
163  if (_occurrences.front() == 0) _occurrences.pop_front();
164  if (_occurrences.front() != 1) _occurrences.push_front(1); //automatically record first gen
165 }
static unsigned int getGenerations()
Definition: simenv.h:61
void fatal(const char *str,...)
Definition: output.cc:96

References fatal(), and SIMenv::getGenerations().

Referenced by LCE_StatServiceNotifier::setOccurence().

◆ setStatOptions()

void StatServices::setStatOptions ( string &  str)
inline

Member Data Documentation

◆ _current_occurrence

list< unsigned int >::const_iterator StatServices::_current_occurrence
private

Iterator pointing to the current generation to record.

Is incremented once all stats have been recorded for the current generation.

◆ _currentStatValues

double* StatServices::_currentStatValues
private

Pointer to the last recorded stats.

◆ _numRecorders

unsigned int StatServices::_numRecorders
private

Number of stats to record.

◆ _occurrence

unsigned int StatServices::_occurrence
private

Deprecated.

◆ _occurrences

list< unsigned int > StatServices::_occurrences
private

List of all generations to record.

◆ _popPtr

Metapop* StatServices::_popPtr
private

◆ _precision

unsigned int StatServices::_precision
private

◆ _printAverages

bool StatServices::_printAverages
private

◆ _separator

unsigned char StatServices::_separator
private

◆ _statArg

string StatServices::_statArg
private

The string argument of the 'stat' input option.

◆ _statHandlers

list< StatHandlerBase* > StatServices::_statHandlers
private

List of stat handlers declared by currently active simulation components.

◆ _statRecorders

list< StatRecBase *> StatServices::_statRecorders
private

List of stat recorders.

◆ _statValues

vector< vector< double*> > StatServices::_statValues
private

Table containing all recorded stats, replicate x generation x (num recorders + 2) .

◆ _width

unsigned int StatServices::_width
private

The documentation for this class was generated from the following files:

Generated for Nemo v2.4.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR