62 monitorId_ = monitorId;
66 spkMonLastUpdated_ = 0;
69 persistentData_ =
false;
70 userHasBeenWarned_ =
false;
71 needToWriteFileHeader_ =
true;
72 spikeFileSignature_ = 206661989;
73 spikeFileVersion_ = 0.2f;
79 void SpikeMonitorCore::init() {
86 spkVector_.resize(nNeurons_);
98 if (spikeFileId_!=NULL) {
109 userHasBeenWarned_ =
false;
116 for (
int i=0; i<nNeurons_; i++)
117 spkVector_[i].
clear();
119 needToCalculateFiringRates_ =
true;
120 needToSortFiringRates_ =
true;
121 firingRates_.clear();
122 firingRatesSorted_.clear();
123 firingRates_.assign(nNeurons_,0);
124 firingRatesSorted_.assign(nNeurons_,0);
146 for (
int i=0; i<nNeurons_; i++)
147 std += (rates[i]-meanRate)*(rates[i]-meanRate);
148 std = sqrt(std/(nNeurons_-1));
158 for (
int i=0; i<nNeurons_; i++)
168 calculateFiringRates();
186 return rates.front();
191 assert(neurId>=0 && neurId<nNeurons_);
198 assert(neurId>=0 && neurId<nNeurons_);
201 return spkVector_[neurId].size();
210 return firingRatesSorted_;
215 assert(min>=0.0f && max>=0.0f);
222 std::vector<float>::const_iterator it_begin = firingRatesSorted_.begin();
223 std::vector<float>::const_iterator it_end = firingRatesSorted_.end();
224 for(std::vector<float>::const_iterator it=it_begin; it!=it_end; it++){
225 if((*it) >= min && (*it) <= max)
262 int dispSpkTimPerRow = 7;
264 KERNEL_INFO(
"(t=%.3fs) SpikeMonitor for group %s(%d) has %d spikes in %ld ms (%.2f +/- %.2f Hz)",
273 if (printSpikeTimes && mode_==
AER) {
275 KERNEL_INFO(
"| Neur ID | Rate (Hz) | Spike Times (ms)");
276 KERNEL_INFO(
"|- - - - -|- - - - - -|- - - - - - - - - - - - - - - - -- - - - - - - - - - - - -")
278 for (
int i=0; i<nNeurons_; i++) {
280 #if defined(WIN32) || defined(WIN64)
285 int nSpk = spkVector_[i].size();
286 for (
int j=0; j<nSpk; j++) {
288 #if defined(WIN32) || defined(WIN64)
289 _snprintf(times, 10,
"%8d", spkVector_[i][j]);
291 snprintf(times, 10,
"%8d", spkVector_[i][j]);
293 strcat(buffer, times);
294 if (j%dispSpkTimPerRow == dispSpkTimPerRow-1 && j<nSpk-1) {
296 strcpy(buffer,
"| | | ");
308 spkVector_[neurId].push_back(time);
314 if (!persistentData_) {
323 needToCalculateFiringRates_ =
true;
324 needToSortFiringRates_ =
true;
328 if (persistentData_) {
331 startTime_ = (startTime_<0) ? currentTime : startTime_;
332 startTimeLast_ = currentTime;
333 accumTime_ = (totalTime_>0) ? totalTime_ : 0;
337 startTime_ = currentTime;
338 startTimeLast_ = currentTime;
345 assert(startTime_>-1 && startTimeLast_>-1 && accumTime_>-1);
352 userHasBeenWarned_ =
false;
356 totalTime_ = stopTime_-startTimeLast_ + accumTime_;
357 assert(totalTime_>=0);
364 if (spikeFileId_!=NULL) {
365 fclose(spikeFileId_);
370 spikeFileId_=spikeFileId;
372 if (spikeFileId_==NULL)
373 needToWriteFileHeader_ =
false;
376 needToWriteFileHeader_ =
true;
377 writeSpikeFileHeader();
382 void SpikeMonitorCore::calculateFiringRates() {
384 if (!needToCalculateFiringRates_)
390 firingRates_.assign(nNeurons_,0);
391 firingRatesSorted_.assign(nNeurons_,0);
395 KERNEL_WARN(
"SpikeMonitorCore: calculateFiringRates has 0 totalTime");
400 assert(totalTime_>0);
401 for(
int i=0;i<nNeurons_;i++) {
402 firingRates_[i]=spkVector_[i].size()*1000.0/totalTime_;
405 needToCalculateFiringRates_ =
false;
409 void SpikeMonitorCore::sortFiringRates() {
411 if (!needToSortFiringRates_)
415 calculateFiringRates();
417 firingRatesSorted_=firingRates_;
418 std::sort(firingRatesSorted_.begin(),firingRatesSorted_.end());
420 needToSortFiringRates_ =
false;
425 void SpikeMonitorCore::writeSpikeFileHeader() {
426 if (!needToWriteFileHeader_)
430 if (!fwrite(&spikeFileSignature_,
sizeof(
int),1,spikeFileId_))
431 KERNEL_ERROR(
"SpikeMonitorCore: writeSpikeFileHeader has fwrite error");
434 if (!fwrite(&spikeFileVersion_,
sizeof(
float),1,spikeFileId_))
435 KERNEL_ERROR(
"SpikeMonitorCore: writeSpikeFileHeader has fwrite error");
439 int tmpInt = grid.
numX;
440 if (!fwrite(&tmpInt,
sizeof(
int),1,spikeFileId_))
441 KERNEL_ERROR(
"SpikeMonitorCore: writeSpikeFileHeader has fwrite error");
444 if (!fwrite(&tmpInt,
sizeof(
int),1,spikeFileId_))
445 KERNEL_ERROR(
"SpikeMonitorCore: writeSpikeFileHeader has fwrite error");
448 if (!fwrite(&tmpInt,
sizeof(
int),1,spikeFileId_))
449 KERNEL_ERROR(
"SpikeMonitorCore: writeSpikeFileHeader has fwrite error");
452 needToWriteFileHeader_ =
false;
459 long int bufferSize=0;
460 for(
int i=0; i<spkVector_.size();i++){
461 bufferSize+=spkVector_[i].size()*
sizeof(int);
469 if(userHasBeenWarned_)
474 userHasBeenWarned_=
true;