CARLsim  4.1.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 *
45 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
46 * Ver 12/31/2016
47 */
48 #include <spike_monitor.h>
49 
50 #include <spike_monitor_core.h> // SpikeMonitor private implementation
51 #include <user_errors.h> // fancy user error messages
52 
53 #include <sstream> // std::stringstream
54 #include <algorithm> // std::transform
55 
56 
57 // we aren't using namespace std so pay attention!
59  // make sure the pointer is NULL
60  spikeMonitorCorePtr_ = spikeMonitorCorePtr;
61 }
62 
64  delete spikeMonitorCorePtr_;
65 }
66 
67 // +++++ PUBLIC METHODS: +++++++++++++++++++++++++++++++++++++++++++++++//
68 
70  std::string funcName = "clear()";
72 
73  spikeMonitorCorePtr_->clear();
74 }
75 
77  std::string funcName = "getPopMeanFiringRate()";
79 
80  return spikeMonitorCorePtr_->getPopMeanFiringRate();
81 }
82 
84  std::string funcName = "getPopStdFiringRate()";
86 
87  return spikeMonitorCorePtr_->getPopStdFiringRate();
88 }
89 
91  std::string funcName = "getPopNumSpikes()";
93 
94  // \TODO
96  "This function is not yet supported in this mode.");
97 
98  return spikeMonitorCorePtr_->getPopNumSpikes();
99 }
100 
102  std::string funcName = "getMaxFiringRate()";
103  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
104 
105  return spikeMonitorCorePtr_->getMaxFiringRate();
106 }
107 
109  std::string funcName = "getMinFiringRate()";
110  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
111 
112  return spikeMonitorCorePtr_->getMinFiringRate();
113 }
114 
115 std::vector<float> SpikeMonitor::getAllFiringRates(){
116  std::string funcName = "getAllFiringRates()";
117  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
118 
119  return spikeMonitorCorePtr_->getAllFiringRates();
120 }
121 
123  std::string funcName = "getNeuronMeanFiringRate()";
124  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
125 
126  return spikeMonitorCorePtr_->getNeuronMeanFiringRate(neurId);
127 
128 }
129 
131  std::string funcName = "getNeuronNumSpikes()";
132  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
133 
134  // \TODO
136  "This function is not yet supported in this mode.");
137 
138  return spikeMonitorCorePtr_->getNeuronNumSpikes(neurId);
139 }
140 
141 // need to do error check here and maybe throw CARLsim errors.
143  std::string funcName = "getNumNeuronsWithFiringRate()";
144  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
145 
146  return spikeMonitorCorePtr_->getNumNeuronsWithFiringRate(min,max);
147 }
148 
149 // need to do error check here and maybe throw CARLsim errors.
151  std::stringstream funcName; funcName << "getPercentNeuronsWithFiringRate(" << min << "," << max << ")";
152  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName.str(), "Recording");
153  UserErrors::assertTrue(min>=0.0f, UserErrors::CANNOT_BE_NEGATIVE, funcName.str(), "min");
154  UserErrors::assertTrue(max>=0.0f, UserErrors::CANNOT_BE_NEGATIVE, funcName.str(), "max");
155  UserErrors::assertTrue(max>=min, UserErrors::CANNOT_BE_LARGER, funcName.str(), "min", "max");
156 
157  return spikeMonitorCorePtr_->getPercentNeuronsWithFiringRate(min,max);
158 }
159 
161  std::string funcName = "getNumSilentNeurons()";
162  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
163 
164  return spikeMonitorCorePtr_->getNumSilentNeurons();
165 }
166 
168  std::string funcName = "getPercentSilentNeurons()";
169  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
170 
171  return spikeMonitorCorePtr_->getPercentSilentNeurons();
172 }
173 
174 std::vector<std::vector<int> > SpikeMonitor::getSpikeVector2D() {
175  std::string funcName = "getSpikeVector2D()";
176  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
178 
179  return spikeMonitorCorePtr_->getSpikeVector2D();
180 }
181 
183  std::string funcName = "getAllFiringRatesSorted()";
184  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
185 
186  return spikeMonitorCorePtr_->getAllFiringRatesSorted();
187 }
188 
190  return spikeMonitorCorePtr_->isRecording();
191 }
192 
193 void SpikeMonitor::print(bool printSpikeTimes) {
194  std::string funcName = "print()";
195  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
196 
197  spikeMonitorCorePtr_->print(printSpikeTimes);
198 }
199 
201  std::string funcName = "startRecording()";
202  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
203 
204  spikeMonitorCorePtr_->startRecording();
205 }
206 
208  std::string funcName = "stopRecording()";
209  UserErrors::assertTrue(isRecording(), UserErrors::MUST_BE_ON, funcName, "Recording");
210 
211  spikeMonitorCorePtr_->stopRecording();
212 }
213 
215  std::string funcName = "getRecordingTotalTime()";
216  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
217 
218  return spikeMonitorCorePtr_->getRecordingTotalTime();
219 }
220 
222  std::string funcName = "getRecordingLastStartTime()";
223  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
224 
225  return spikeMonitorCorePtr_->getRecordingLastStartTime();
226 }
227 
229  std::string funcName = "getRecordingStartTime()";
230  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
231 
232  return spikeMonitorCorePtr_->getRecordingStartTime();
233 }
234 
236  std::string funcName = "getRecordingStopTime()";
237  UserErrors::assertTrue(!isRecording(), UserErrors::CANNOT_BE_ON, funcName, "Recording");
238 
239  return spikeMonitorCorePtr_->getRecordingStopTime();
240 }
241 
243  return spikeMonitorCorePtr_->getPersistentData();
244 }
245 
246 void SpikeMonitor::setPersistentData(bool persistentData) {
247  spikeMonitorCorePtr_->setPersistentData(persistentData);
248 }
249 
251  return spikeMonitorCorePtr_->getMode();
252 }
253 
255  // \TODO
256  UserErrors::assertTrue(false, UserErrors::UNKNOWN, "setMode()", "",
257  "This function call is not yet supported.");
258 
259  spikeMonitorCorePtr_->setMode(mode);
260 }
261 
262 void SpikeMonitor::setLogFile(const std::string& fileName) {
263  std::string funcName = "setLogFile";
264 
265  FILE* fid;
266  std::string fileNameLower = fileName;
267  std::transform(fileNameLower.begin(), fileNameLower.end(), fileNameLower.begin(), ::tolower);
268 
269  if (fileNameLower == "null") {
270  // user does not want a binary created
271  fid = NULL;
272  } else {
273  fid = fopen(fileName.c_str(),"wb");
274  if (fid==NULL) {
275  // default case: print error and exit
276  std::string fileError = " Double-check file permissions and make sure directory exists.";
277  UserErrors::assertTrue(false, UserErrors::FILE_CANNOT_OPEN, funcName, fileName, fileError);
278  }
279  }
280 
281  // tell new file id to core object
282  spikeMonitorCorePtr_->setSpikeFileId(fid);
283 }
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:202
SpikeMonitorCore
Definition: spike_monitor_core.h:70
SpikeMonitorCore::getRecordingLastStartTime
long int getRecordingLastStartTime()
returns the timestamp of the last startRecording in ms
Definition: spike_monitor_core.h:142
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:69
SpikeMonitor::getPercentSilentNeurons
float getPercentSilentNeurons()
returns the percentage of total neurons in group that are silent.
Definition: spike_monitor.cpp:167
SpikeMonitor::setPersistentData
void setPersistentData(bool persistentData)
Sets PersistentMode either on (true) or off (false)
Definition: spike_monitor.cpp:246
user_errors.h
SpikeMonitor::~SpikeMonitor
~SpikeMonitor()
SpikeMonitor destructor.
Definition: spike_monitor.cpp:63
SpikeMonitorCore::getRecordingStartTime
long int getRecordingStartTime()
retunrs the timestamp of the first startRecording in ms
Definition: spike_monitor_core.h:139
SpikeMonitor::isRecording
bool isRecording()
Recording status (true=recording, false=not recording)
Definition: spike_monitor.cpp:189
SpikeMonitorCore::getMinFiringRate
float getMinFiringRate()
returns the smallest recorded firing rate
Definition: spike_monitor_core.cpp:180
SpikeMonitor::getPopNumSpikes
int getPopNumSpikes()
Returns the total number of spikes in the group.
Definition: spike_monitor.cpp:90
SpikeMonitor::getMinFiringRate
float getMinFiringRate()
returns the smallest neuronal mean firing rate in the group
Definition: spike_monitor.cpp:108
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:115
SpikeMonitor::print
void print(bool printSpikeTimes=true)
prints the 2D spike vector.
Definition: spike_monitor.cpp:193
SpikeMonitor::stopRecording
void stopRecording()
Ends a recording period.
Definition: spike_monitor.cpp:207
SpikeMonitor::getRecordingLastStartTime
long int getRecordingLastStartTime()
Returns the simulation time (ms) of the last call to startRecording()
Definition: spike_monitor.cpp:221
SpikeMonitor::SpikeMonitor
SpikeMonitor(SpikeMonitorCore *spikeMonitorCorePtr)
SpikeMonitor constructor.
Definition: spike_monitor.cpp:58
SpikeMonitor::getMode
SpikeMonMode getMode()
Returns the current SpikeMonitor mode.
Definition: spike_monitor.cpp:250
SpikeMonitorCore::print
void print(bool printSpikeTimes)
prints the AER vector in human-readable format
Definition: spike_monitor_core.cpp:257
SpikeMonitorCore::isRecording
bool isRecording()
returns recording status
Definition: spike_monitor_core.h:151
SpikeMonitorCore::getPopNumSpikes
int getPopNumSpikes()
returns the total number of recorded spikes in the group
Definition: spike_monitor_core.cpp:153
SpikeMonitorCore::getPersistentData
bool getPersistentData()
returns status of PersistentData mode
Definition: spike_monitor_core.h:124
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:83
SpikeMonitor::getRecordingStartTime
long int getRecordingStartTime()
Returns the simulation time (ms) of the first call to startRecording()
Definition: spike_monitor.cpp:228
SpikeMonitor::getPopMeanFiringRate
float getPopMeanFiringRate()
Returns the mean firing rate of the entire neuronal population.
Definition: spike_monitor.cpp:76
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:135
SpikeMonitorCore::setMode
void setMode(SpikeMonMode mode)
sets recording mode
Definition: spike_monitor_core.h:160
SpikeMonitor::getSpikeVector2D
std::vector< std::vector< int > > getSpikeVector2D()
returns the 2D spike vector
Definition: spike_monitor.cpp:174
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:163
SpikeMonitor::getPersistentData
bool getPersistentData()
Returns a flag that indicates whether PersistentMode is on (true) or off (false)
Definition: spike_monitor.cpp:242
SpikeMonitorCore::getPopMeanFiringRate
float getPopMeanFiringRate()
returns the recorded mean firing rate of the group
Definition: spike_monitor_core.cpp:126
SpikeMonitorCore::getRecordingStopTime
long int getRecordingStopTime()
returns the timestamp of stopRecording
Definition: spike_monitor_core.h:145
SpikeMonitorCore::stopRecording
void stopRecording()
stops recording AER data
Definition: spike_monitor_core.cpp:342
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:150
SpikeMonitorCore::getRecordingTotalTime
long int getRecordingTotalTime()
returns the total recorded time in ms
Definition: spike_monitor_core.h:136
SpikeMonitor::getNeuronMeanFiringRate
float getNeuronMeanFiringRate(int neurId)
returns the mean firing rate of a specific neuron in the group
Definition: spike_monitor.cpp:122
SpikeMonitorCore::getPercentSilentNeurons
float getPercentSilentNeurons()
returns percentage of neurons that didn't spike during recording
Definition: spike_monitor_core.cpp:244
SpikeMonitor::getNumSilentNeurons
int getNumSilentNeurons()
returns the number of neurons that are silent.
Definition: spike_monitor.cpp:160
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:212
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:203
SpikeMonitor::getRecordingStopTime
long int getRecordingStopTime()
Returns the simulation time (ms) of the last call to stopRecording()
Definition: spike_monitor.cpp:235
SpikeMonitorCore::getNumSilentNeurons
int getNumSilentNeurons()
returns number of neurons that didn't spike while recording
Definition: spike_monitor_core.cpp:231
SpikeMonMode
SpikeMonMode
SpikeMonitor mode.
Definition: carlsim_datastructures.h:200
SpikeMonitor::startRecording
void startRecording()
Starts a new recording period.
Definition: spike_monitor.cpp:200
SpikeMonitor::setLogFile
void setLogFile(const std::string &logFileName)
Sets the name of the spike file binary.
Definition: spike_monitor.cpp:262
SpikeMonitorCore::clear
void clear()
deletes data from the 2D spike vector
Definition: spike_monitor_core.cpp:105
SpikeMonitorCore::getMode
SpikeMonMode getMode()
returns recording mode
Definition: spike_monitor_core.h:100
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:142
SpikeMonitor::getNeuronNumSpikes
int getNeuronNumSpikes(int neurId)
returns the total number of spikes of a specific neuron in the group
Definition: spike_monitor.cpp:130
SpikeMonitor::getRecordingTotalTime
long int getRecordingTotalTime()
Returns the total recording time (ms)
Definition: spike_monitor.cpp:214
SpikeMonitorCore::getNeuronNumSpikes
int getNeuronNumSpikes(int neurId)
returns the number of recorded spikes of a specific neuron
Definition: spike_monitor_core.cpp:195
SpikeMonitorCore::setSpikeFileId
void setSpikeFileId(FILE *spikeFileId)
sets pointer to spike file
Definition: spike_monitor_core.cpp:359
SpikeMonitor::getAllFiringRatesSorted
std::vector< float > getAllFiringRatesSorted()
Returns all the neuronal mean firing rates in ascending order.
Definition: spike_monitor.cpp:182
SpikeMonitorCore::getNeuronMeanFiringRate
float getNeuronMeanFiringRate(int neurId)
returns the recorded mean firing rate for a specific neuron
Definition: spike_monitor_core.cpp:188
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:101
SpikeMonitorCore::getSpikeVector2D
std::vector< std::vector< int > > getSpikeVector2D()
returns the 2D AER vector
Definition: spike_monitor_core.cpp:250
SpikeMonitorCore::getMaxFiringRate
float getMaxFiringRate()
returns the largest recorded firing rate
Definition: spike_monitor_core.cpp:172
spike_monitor_core.h
SpikeMonitorCore::setPersistentData
void setPersistentData(bool persistentData)
sets status of PersistentData mode
Definition: spike_monitor_core.h:163
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:238
SpikeMonitor::setMode
void setMode(SpikeMonMode mode=AER)
Sets the current SpikeMonitor mode.
Definition: spike_monitor.cpp:254
SpikeMonitorCore::startRecording
void startRecording()
starts recording AER data
Definition: spike_monitor_core.cpp:310