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