CARLsim  5.0.0
CARLsim: a GPU-accelerated SNN simulator
spike_monitor.cpp
Go to the documentation of this file.
1 /* * Copyright (c) 2016 Regents of the University of California. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. The names of its contributors may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * *********************************************************************************************** *
31 * CARLsim
32 * created by: (MDR) Micah Richert, (JN) Jayram M. Nageswaran
33 * maintained by:
34 * (MA) Mike Avery <averym@uci.edu>
35 * (MB) Michael Beyeler <mbeyeler@uci.edu>,
36 * (KDC) Kristofor Carlson <kdcarlso@uci.edu>
37 * (TSC) Ting-Shuo Chou <tingshuc@uci.edu>
38 * (HK) Hirak J Kashyap <kashyaph@uci.edu>
39 *
40 * CARLsim v1.0: JM, MDR
41 * CARLsim v2.0/v2.1/v2.2: JM, MDR, MA, MB, KDC
42 * CARLsim3: MB, KDC, TSC
43 * CARLsim4: TSC, HK
44 * CARLsim5: HK, JX, KC
45 *
46 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
47 * Ver 12/31/2016
48 */
49 #include <spike_monitor.h>
50 
51 #include <spike_monitor_core.h> // SpikeMonitor private implementation
52 #include <user_errors.h> // fancy user error messages
53 
54 #include <sstream> // std::stringstream
55 #include <algorithm> // std::transform
56 
57 
58 // we aren't using namespace std so pay attention!
60  // make sure the pointer is NULL
61  spikeMonitorCorePtr_ = spikeMonitorCorePtr;
62 }
63 
65  delete spikeMonitorCorePtr_;
66 }
67 
68 // +++++ PUBLIC METHODS: +++++++++++++++++++++++++++++++++++++++++++++++//
69 
71  std::string funcName = "clear()";
73 
74  spikeMonitorCorePtr_->clear();
75 }
76 
78  std::string funcName = "getPopMeanFiringRate()";
80 
81  return spikeMonitorCorePtr_->getPopMeanFiringRate();
82 }
83 
85  std::string funcName = "getPopStdFiringRate()";
87 
88  return spikeMonitorCorePtr_->getPopStdFiringRate();
89 }
90 
92  std::string funcName = "getPopNumSpikes()";
94 
95  // \TODO
97  "This function is not yet supported in this mode.");
98 
99  return spikeMonitorCorePtr_->getPopNumSpikes();
100 }
101 
103  std::string funcName = "getMaxFiringRate()";
104  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
105 
106  return spikeMonitorCorePtr_->getMaxFiringRate();
107 }
108 
110  std::string funcName = "getMinFiringRate()";
111  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
112 
113  return spikeMonitorCorePtr_->getMinFiringRate();
114 }
115 
116 std::vector<float> SpikeMonitor::getAllFiringRates(){
117  std::string funcName = "getAllFiringRates()";
118  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
119 
120  return spikeMonitorCorePtr_->getAllFiringRates();
121 }
122 
124  std::string funcName = "getNeuronMeanFiringRate()";
125  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
126 
127  return spikeMonitorCorePtr_->getNeuronMeanFiringRate(neurId);
128 
129 }
130 
132  std::string funcName = "getNeuronNumSpikes()";
133  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
134 
135  // \TODO
137  "This function is not yet supported in this mode.");
138 
139  return spikeMonitorCorePtr_->getNeuronNumSpikes(neurId);
140 }
141 
142 // need to do error check here and maybe throw CARLsim errors.
144  std::string funcName = "getNumNeuronsWithFiringRate()";
145  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
146 
147  return spikeMonitorCorePtr_->getNumNeuronsWithFiringRate(min,max);
148 }
149 
150 // need to do error check here and maybe throw CARLsim errors.
152  std::stringstream funcName; funcName << "getPercentNeuronsWithFiringRate(" << min << "," << max << ")";
153  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName.str(), "Recording");
154  UserErrors::assertTrue(min>=0.0f, UserErrors::CANNOT_BE_NEGATIVE, funcName.str(), "min");
155  UserErrors::assertTrue(max>=0.0f, UserErrors::CANNOT_BE_NEGATIVE, funcName.str(), "max");
156  UserErrors::assertTrue(max>=min, UserErrors::CANNOT_BE_LARGER, funcName.str(), "min", "max");
157 
158  return spikeMonitorCorePtr_->getPercentNeuronsWithFiringRate(min,max);
159 }
160 
162  std::string funcName = "getNumSilentNeurons()";
163  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
164 
165  return spikeMonitorCorePtr_->getNumSilentNeurons();
166 }
167 
169  std::string funcName = "getPercentSilentNeurons()";
170  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
171 
172  return spikeMonitorCorePtr_->getPercentSilentNeurons();
173 }
174 
175 std::vector<std::vector<int> > SpikeMonitor::getSpikeVector2D() {
176  std::string funcName = "getSpikeVector2D()";
177  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
179 
180  return spikeMonitorCorePtr_->getSpikeVector2D();
181 }
182 
184  std::string funcName = "getAllFiringRatesSorted()";
185  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
186 
187  return spikeMonitorCorePtr_->getAllFiringRatesSorted();
188 }
189 
191  return spikeMonitorCorePtr_->isRecording();
192 }
193 
194 void SpikeMonitor::print(bool printSpikeTimes) {
195  std::string funcName = "print()";
196  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
197 
198  spikeMonitorCorePtr_->print(printSpikeTimes);
199 }
200 
202  std::string funcName = "startRecording()";
203  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
204 
205  spikeMonitorCorePtr_->startRecording();
206 }
207 
209  std::string funcName = "stopRecording()";
210  UserErrors::assertTrue(isRecording(), UserErrors::MUST_BE_ON, funcName, "Recording");
211 
212  spikeMonitorCorePtr_->stopRecording();
213 }
214 
216  std::string funcName = "getRecordingTotalTime()";
217  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
218 
219  return spikeMonitorCorePtr_->getRecordingTotalTime();
220 }
221 
223  std::string funcName = "getRecordingLastStartTime()";
224  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
225 
226  return spikeMonitorCorePtr_->getRecordingLastStartTime();
227 }
228 
230  std::string funcName = "getRecordingStartTime()";
231  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
232 
233  return spikeMonitorCorePtr_->getRecordingStartTime();
234 }
235 
237  std::string funcName = "getRecordingStopTime()";
238  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
239 
240  return spikeMonitorCorePtr_->getRecordingStopTime();
241 }
242 
244  return spikeMonitorCorePtr_->getPersistentData();
245 }
246 
247 void SpikeMonitor::setPersistentData(bool persistentData) {
248  spikeMonitorCorePtr_->setPersistentData(persistentData);
249 }
250 
252  return spikeMonitorCorePtr_->getMode();
253 }
254 
256  // \TODO
257  UserErrors::assertTrue(false, UserErrors::UNKNOWN, "setMode()", "",
258  "This function call is not yet supported.");
259 
260  spikeMonitorCorePtr_->setMode(mode);
261 }
262 
263 void SpikeMonitor::setLogFile(const std::string& fileName) {
264  std::string funcName = "setLogFile";
265 
266  FILE* fid;
267  std::string fileNameLower = fileName;
268  std::transform(fileNameLower.begin(), fileNameLower.end(), fileNameLower.begin(), ::tolower);
269 
270  if (fileNameLower == "null") {
271  // user does not want a binary created
272  fid = NULL;
273  } else {
274  fid = fopen(fileName.c_str(),"wb");
275  if (fid==NULL) {
276  // default case: print error and exit
277  std::string fileError = " Double-check file permissions and make sure directory exists.";
278  UserErrors::assertTrue(false, UserErrors::FILE_CANNOT_OPEN, funcName, fileName, fileError);
279  }
280  }
281 
282  // tell new file id to core object
283  spikeMonitorCorePtr_->setSpikeFileId(fid);
284 }
UserErrors::CANNOT_BE_LARGER
@ CANNOT_BE_LARGER
parameter cannot have larger vaule than some vaule
Definition: user_errors.h:35
AER
@ AER
mode in which spike information is collected in AER format
Definition: carlsim_datastructures.h:203
SpikeMonitorCore
Definition: spike_monitor_core.h:71
SpikeMonitorCore::getRecordingLastStartTime
long int getRecordingLastStartTime()
returns the timestamp of the last startRecording in ms
Definition: spike_monitor_core.h:143
UserErrors::CANNOT_BE_ON
@ CANNOT_BE_ON
parameter cannot be on
Definition: user_errors.h:38
spike_monitor.h
SpikeMonitor::clear
void clear()
Truncates the 2D spike vector.
Definition: spike_monitor.cpp:70
SpikeMonitor::getPercentSilentNeurons
float getPercentSilentNeurons()
returns the percentage of total neurons in group that are silent.
Definition: spike_monitor.cpp:168
SpikeMonitor::setPersistentData
void setPersistentData(bool persistentData)
Sets PersistentMode either on (true) or off (false)
Definition: spike_monitor.cpp:247
user_errors.h
SpikeMonitor::~SpikeMonitor
~SpikeMonitor()
SpikeMonitor destructor.
Definition: spike_monitor.cpp:64
SpikeMonitorCore::getRecordingStartTime
long int getRecordingStartTime()
retunrs the timestamp of the first startRecording in ms
Definition: spike_monitor_core.h:140
SpikeMonitor::isRecording
bool isRecording()
Recording status (true=recording, false=not recording)
Definition: spike_monitor.cpp:190
SpikeMonitorCore::getMinFiringRate
float getMinFiringRate()
returns the smallest recorded firing rate
Definition: spike_monitor_core.cpp:181
SpikeMonitor::getPopNumSpikes
int getPopNumSpikes()
Returns the total number of spikes in the group.
Definition: spike_monitor.cpp:91
SpikeMonitor::getMinFiringRate
float getMinFiringRate()
returns the smallest neuronal mean firing rate in the group
Definition: spike_monitor.cpp:109
SpikeMonitor::getAllFiringRates
std::vector< float > getAllFiringRates()
Returns the average firing rate of all the neurons in the group as a vector of floats.
Definition: spike_monitor.cpp:116
SpikeMonitor::print
void print(bool printSpikeTimes=true)
prints the 2D spike vector.
Definition: spike_monitor.cpp:194
SpikeMonitor::stopRecording
void stopRecording()
Ends a recording period.
Definition: spike_monitor.cpp:208
SpikeMonitor::getRecordingLastStartTime
long int getRecordingLastStartTime()
Returns the simulation time (ms) of the last call to startRecording()
Definition: spike_monitor.cpp:222
SpikeMonitor::SpikeMonitor
SpikeMonitor(SpikeMonitorCore *spikeMonitorCorePtr)
SpikeMonitor constructor.
Definition: spike_monitor.cpp:59
SpikeMonitor::getMode
SpikeMonMode getMode()
Returns the current SpikeMonitor mode.
Definition: spike_monitor.cpp:251
SpikeMonitorCore::print
void print(bool printSpikeTimes)
prints the AER vector in human-readable format
Definition: spike_monitor_core.cpp:258
SpikeMonitorCore::isRecording
bool isRecording()
returns recording status
Definition: spike_monitor_core.h:152
SpikeMonitorCore::getPopNumSpikes
int getPopNumSpikes()
returns the total number of recorded spikes in the group
Definition: spike_monitor_core.cpp:154
SpikeMonitorCore::getPersistentData
bool getPersistentData()
returns status of PersistentData mode
Definition: spike_monitor_core.h:125
UserErrors::MUST_BE_ON
@ MUST_BE_ON
parameter must be on
Definition: user_errors.h:52
SpikeMonitor::getPopStdFiringRate
float getPopStdFiringRate()
Returns the standard deviation of firing rates in the entire neuronal population.
Definition: spike_monitor.cpp:84
SpikeMonitor::getRecordingStartTime
long int getRecordingStartTime()
Returns the simulation time (ms) of the first call to startRecording()
Definition: spike_monitor.cpp:229
SpikeMonitor::getPopMeanFiringRate
float getPopMeanFiringRate()
Returns the mean firing rate of the entire neuronal population.
Definition: spike_monitor.cpp:77
UserErrors::FILE_CANNOT_OPEN
@ FILE_CANNOT_OPEN
could not open file
Definition: user_errors.h:44
SpikeMonitorCore::getPopStdFiringRate
float getPopStdFiringRate()
computes the standard deviation of firing rates in the group
Definition: spike_monitor_core.cpp:136
SpikeMonitorCore::setMode
void setMode(SpikeMonMode mode)
sets recording mode
Definition: spike_monitor_core.h:161
SpikeMonitor::getSpikeVector2D
std::vector< std::vector< int > > getSpikeVector2D()
returns the 2D spike vector
Definition: spike_monitor.cpp:175
SpikeMonitorCore::getAllFiringRates
std::vector< float > getAllFiringRates()
returns a list of firing rates for all neurons in the group (sorted by neuron ID ascending)
Definition: spike_monitor_core.cpp:164
SpikeMonitor::getPersistentData
bool getPersistentData()
Returns a flag that indicates whether PersistentMode is on (true) or off (false)
Definition: spike_monitor.cpp:243
SpikeMonitorCore::getPopMeanFiringRate
float getPopMeanFiringRate()
returns the recorded mean firing rate of the group
Definition: spike_monitor_core.cpp:127
SpikeMonitorCore::getRecordingStopTime
long int getRecordingStopTime()
returns the timestamp of stopRecording
Definition: spike_monitor_core.h:146
SpikeMonitorCore::stopRecording
void stopRecording()
stops recording AER data
Definition: spike_monitor_core.cpp:343
SpikeMonitor::getPercentNeuronsWithFiringRate
float getPercentNeuronsWithFiringRate(float min, float max)
returns the percentage of total neurons in that are in the range specified by the user,...
Definition: spike_monitor.cpp:151
SpikeMonitorCore::getRecordingTotalTime
long int getRecordingTotalTime()
returns the total recorded time in ms
Definition: spike_monitor_core.h:137
SpikeMonitor::getNeuronMeanFiringRate
float getNeuronMeanFiringRate(int neurId)
returns the mean firing rate of a specific neuron in the group
Definition: spike_monitor.cpp:123
SpikeMonitorCore::getPercentSilentNeurons
float getPercentSilentNeurons()
returns percentage of neurons that didn't spike during recording
Definition: spike_monitor_core.cpp:245
SpikeMonitor::getNumSilentNeurons
int getNumSilentNeurons()
returns the number of neurons that are silent.
Definition: spike_monitor.cpp:161
SpikeMonitorCore::getNumNeuronsWithFiringRate
int getNumNeuronsWithFiringRate(float min, float max)
returns number of neurons whose firing rate was in [min,max] during recording
Definition: spike_monitor_core.cpp:213
UserErrors::CAN_ONLY_BE_CALLED_IN_MODE
@ CAN_ONLY_BE_CALLED_IN_MODE
function can only be called in certain mode
Definition: user_errors.h:26
UserErrors::UNKNOWN
@ UNKNOWN
an unknown error
Definition: user_errors.h:61
SpikeMonitorCore::getAllFiringRatesSorted
std::vector< float > getAllFiringRatesSorted()
returns a list of firing rates for all neurons in the group (sorted by firing rate ascending)
Definition: spike_monitor_core.cpp:204
SpikeMonitor::getRecordingStopTime
long int getRecordingStopTime()
Returns the simulation time (ms) of the last call to stopRecording()
Definition: spike_monitor.cpp:236
SpikeMonitorCore::getNumSilentNeurons
int getNumSilentNeurons()
returns number of neurons that didn't spike while recording
Definition: spike_monitor_core.cpp:232
SpikeMonMode
SpikeMonMode
SpikeMonitor mode.
Definition: carlsim_datastructures.h:201
SpikeMonitor::startRecording
void startRecording()
Starts a new recording period.
Definition: spike_monitor.cpp:201
SpikeMonitor::setLogFile
void setLogFile(const std::string &logFileName)
Sets the name of the spike file binary.
Definition: spike_monitor.cpp:263
SpikeMonitorCore::clear
void clear()
deletes data from the 2D spike vector
Definition: spike_monitor_core.cpp:106
SpikeMonitorCore::getMode
SpikeMonMode getMode()
returns recording mode
Definition: spike_monitor_core.h:101
SpikeMonitor::getNumNeuronsWithFiringRate
int getNumNeuronsWithFiringRate(float min, float max)
Returns the number of neurons that fall within this particular min/max range (inclusive).
Definition: spike_monitor.cpp:143
SpikeMonitor::getNeuronNumSpikes
int getNeuronNumSpikes(int neurId)
returns the total number of spikes of a specific neuron in the group
Definition: spike_monitor.cpp:131
SpikeMonitor::getRecordingTotalTime
long int getRecordingTotalTime()
Returns the total recording time (ms)
Definition: spike_monitor.cpp:215
SpikeMonitorCore::getNeuronNumSpikes
int getNeuronNumSpikes(int neurId)
returns the number of recorded spikes of a specific neuron
Definition: spike_monitor_core.cpp:196
SpikeMonitorCore::setSpikeFileId
void setSpikeFileId(FILE *spikeFileId)
sets pointer to spike file
Definition: spike_monitor_core.cpp:360
SpikeMonitor::getAllFiringRatesSorted
std::vector< float > getAllFiringRatesSorted()
Returns all the neuronal mean firing rates in ascending order.
Definition: spike_monitor.cpp:183
SpikeMonitorCore::getNeuronMeanFiringRate
float getNeuronMeanFiringRate(int neurId)
returns the recorded mean firing rate for a specific neuron
Definition: spike_monitor_core.cpp:189
UserErrors::assertTrue
static void assertTrue(bool statement, errorType errorIfAssertionFails, std::string errorFunc, std::string errorMsgPrefix="", std::string errorMsgSuffix="")
simple wrapper for assert statement
Definition: user_errors.cpp:15
UserErrors::CANNOT_BE_NEGATIVE
@ CANNOT_BE_NEGATIVE
parameter cannot have negative value (opposite to "must be", but includes zero)
Definition: user_errors.h:33
SpikeMonitor::getMaxFiringRate
float getMaxFiringRate()
returns the largest neuronal mean firing rate in the group
Definition: spike_monitor.cpp:102
SpikeMonitorCore::getSpikeVector2D
std::vector< std::vector< int > > getSpikeVector2D()
returns the 2D AER vector
Definition: spike_monitor_core.cpp:251
SpikeMonitorCore::getMaxFiringRate
float getMaxFiringRate()
returns the largest recorded firing rate
Definition: spike_monitor_core.cpp:173
spike_monitor_core.h
SpikeMonitorCore::setPersistentData
void setPersistentData(bool persistentData)
sets status of PersistentData mode
Definition: spike_monitor_core.h:164
SpikeMonitorCore::getPercentNeuronsWithFiringRate
float getPercentNeuronsWithFiringRate(float min, float max)
returns percentage of neurons whose firing rate was in [min,max] during recording
Definition: spike_monitor_core.cpp:239
SpikeMonitor::setMode
void setMode(SpikeMonMode mode=AER)
Sets the current SpikeMonitor mode.
Definition: spike_monitor.cpp:255
SpikeMonitorCore::startRecording
void startRecording()
starts recording AER data
Definition: spike_monitor_core.cpp:311