NEST main@caf0ae8
 
Loading...
Searching...
No Matches
correlation_detector.h
Go to the documentation of this file.
1/*
2 * correlation_detector.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef CORRELATION_DETECTOR_H
24#define CORRELATION_DETECTOR_H
25
26
27// C++ includes:
28#include <deque>
29#include <vector>
30
31// Includes from nestkernel:
32#include "event.h"
33#include "nest_timeconverter.h"
34#include "nest_types.h"
35#include "node.h"
37
38
39namespace nest
40{
41
42/* BeginUserDocs: device, detector
43
44Short description
45+++++++++++++++++
46
47Device for evaluating cross correlation between two spike sources
48
49Description
50+++++++++++
51
52The ``correlation_detector`` is a device that receives spikes from two pools of
53spike inputs and calculates the ``count_histogram`` of inter-spike intervals
54(raw cross correlation) binned to bins of duration :math:`\delta_\tau`.
55The corresponding parameter ``delta_tau`` defaults to 5 times the simulation
56resolution.
57
58The result can be obtained from the node's status dictionary under the key
59``count_histogram``.
60
61In parallel it records a weighted histogram, where the connection weights
62are used to weight every count. In order to minimize numerical errors, the
63`Kahan summation algorithm <http://en.wikipedia.org/wiki/Kahan_summation_algorithm>`_
64is used when calculating the weighted histogram.
65Both ``histogram`` and ``count_histogram`` are arrays of
66:math:`2\cdot\tau_{max}/\delta_{\tau}+1` values, indexed by the bin number
67:math:`n`, and are filled in the following way:
68
69Let :math:`t_{1,i}` be the spike times of source 1 and
70:math:`t_{2,j}` the spike times of source 2.
71``histogram[n]`` then contains the sum of the weight products
72:math:`w_{1,i}\cdot w_{2,j}`, and ``count_histogram[n]`` contains 1 summed over
73all event pairs whose time difference :math:`t_{2,j}-t_{1,i}` falls in the
74half-open interval
75
76.. math::
77
78 \left[ n\cdot\delta_\tau - \tau_{max} - \delta_\tau/2,\;
79 n\cdot\delta_\tau - \tau_{max} + \delta_\tau/2 \right)
80
81The bins are centered around the time difference they represent and are
82left-closed and right-open. This means that events with time difference
83:math:`-\tau_{max}-\delta_\tau/2` are counted in the leftmost bin, but events
84with difference :math:`\tau_{max}+\delta_\tau/2` are not counted at all.
85
86The bin centers run from :math:`-\tau_{max}` to :math:`+\tau_{max}` in steps of
87:math:`\delta_\tau`. The corresponding array of time lags for the histogram bins
88can therefore be constructed in PyNEST as
89
90.. code-block:: python
91
92 import numpy as np
93
94 n_bins = int(2 * tau_max / delta_tau) + 1
95 times = np.linspace(-tau_max, tau_max, n_bins)
96
97The correlation detector has exactly two inputs, which are selected via the
98``receptor_type`` of the incoming connection: all incoming connections with
99``receptor_type = 0`` are pooled as spike source 1, the ones with
100``receptor_type = 1`` as spike source 2.
101
102Correlation detectors ignore any connection delays.
103
104This recorder does not record to file, screen, or memory in the usual sense.
105The recorded data is only available from the status dictionary.
106
107
108Parameters
109++++++++++
110
111The following parameters can be set in the status dictionary.
112
113============= ==== ==================================================================
114Parameter Unit Description
115============= ==== ==================================================================
116``Tstart`` ms Time at which to start counting events. Set this to at least
117 ``tau_max`` in order to avoid edge effects of the correlation
118 counts.
119``Tstop`` ms Time at which to stop counting events. Set this to at most
120 ``Tsim - tau_max``, where ``Tsim`` is the duration of the
121 simulation, in order to avoid edge effects of the correlation
122 counts.
123``delta_tau`` ms Bin width. This has to be an odd multiple of the simulation
124 resolution, to allow the symmetry between positive and negative
125 time lags. Defaults to 5 times the simulation resolution.
126``tau_max`` ms One-sided maximum absolute time lag. Time differences in the
127 range ``[-tau_max - delta_tau/2, tau_max + delta_tau/2)`` are
128 binned. Must be a multiple of ``delta_tau``. Defaults to 10 times
129 the value of ``delta_tau``.
130============= ==== ==================================================================
131
132The following read-only quantities are available in the status dictionary.
133
134======================== ===================================================================
135Recordable Description
136======================== ===================================================================
137``count_histogram`` Raw, unweighted cross-correlation counts (array of integers).
138``histogram`` Weighted cross-correlation counts, where each count is weighted by
139 the product of the connection weights. The unit is squared synaptic
140 weights and depends on the model (array of doubles).
141``histogram_correction`` Correction factors used internally for the Kahan summation
142 algorithm (array of doubles).
143``n_events`` Number of events from source 0 and source 1 (list of two integers).
144 Setting ``n_events`` to ``[0, 0]`` clears the histograms.
145======================== ===================================================================
146
147Receives
148++++++++
149
150SpikeEvent
151
152See also
153++++++++
154
155spike_recorder
156
157Examples using this model
158+++++++++++++++++++++++++
159
160.. listexamples:: correlation_detector
161
162EndUserDocs */
163
179void register_correlation_detector( const std::string& name );
180
182{
183
184public:
187
192 bool
193 has_proxies() const override
194 {
195 return true;
196 }
197
198 std::string
199 get_element_type() const override
200 {
201 return names::recorder;
202 }
203
209 using Node::handle;
211
212 void handle( SpikeEvent& ) override;
213
214 size_t handles_test_event( SpikeEvent&, size_t ) override;
215
216 void get_status( Dictionary& ) const override;
217 void set_status( const Dictionary& ) override;
218
219 void calibrate_time( const TimeConverter& tc ) override;
220
221private:
222 void init_state_() override;
223 void init_buffers_() override;
224 void pre_run_hook() override;
225
226 void update( Time const&, const long, const long ) override;
227
228 // ------------------------------------------------------------
229
234 struct Spike_
235 {
237 double weight_;
238
239 Spike_( long timestep, double weight )
240 : timestep_( timestep )
241 , weight_( weight )
242 {
243 }
244
248 inline bool
249 operator>( const Spike_& second ) const
250 {
251 return timestep_ > second.timestep_;
252 }
253 };
254
255 typedef std::deque< Spike_ > SpikelistType;
256
257 // ------------------------------------------------------------
258
259 struct State_;
260
262 {
267
268 Parameters_();
269 Parameters_( const Parameters_& );
270
272
273 void get( Dictionary& ) const;
274
280 bool set( const Dictionary&, const correlation_detector&, Node* );
281
283 };
284
285 // ------------------------------------------------------------
286
296 struct State_
297 {
298 std::vector< long > n_events_;
299 std::vector< SpikelistType > incoming_;
300
304 std::vector< double > histogram_;
305
307 std::vector< double > histogram_correction_;
308
310 std::vector< long > count_histogram_;
311
312 State_();
313
314 void get( Dictionary& ) const;
315
319 void set( const Dictionary&, const Parameters_&, bool, Node* );
320
321 void reset( const Parameters_& );
322 };
323
324 // ------------------------------------------------------------
325
329};
330
331inline size_t
333{
334 if ( receptor_type > 1 )
335 {
336 throw UnknownReceptorType( receptor_type, get_name() );
337 }
338
339 return receptor_type;
340}
341
342inline void
344{
345 device_.get_status( d );
346 P_.get( d );
347 S_.get( d );
348}
349
350inline void
352{
353 Parameters_ ptmp = P_;
354 const bool reset_required = ptmp.set( d, *this, this );
355 State_ stmp = S_;
356 stmp.set( d, P_, reset_required, this );
357
358 device_.set_status( d );
359 P_ = ptmp;
360 S_ = stmp;
361}
362
363inline Time
368
369
370} // namespace
371
372#endif /* #ifndef CORRELATION_DETECTOR_H */
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
virtual void get_status(Dictionary &) const
Definition device.h:179
virtual void set_status(const Dictionary &)
Definition device.h:185
Base class for all NEST network objects.
Definition node.h:99
std::string get_name() const
Return class name.
Definition node.cpp:105
Common properties of all pseudo-recording devices.
Definition pseudo_recording_device.h:72
Event for spike information.
Definition event.h:418
Class to convert times from one representation to another.
Definition nest_timeconverter.h:42
Definition nest_time.h:135
static Time get_resolution()
Definition nest_time.h:325
Exception to be thrown if the specified receptor type does not exist in the node.
Definition exceptions.h:417
Definition correlation_detector.h:182
void init_state_() override
Configure state variables depending on runtime information.
Definition correlation_detector.cpp:236
std::string get_element_type() const override
Return the element type of the node.
Definition correlation_detector.h:199
std::deque< Spike_ > SpikelistType
Definition correlation_detector.h:255
PseudoRecordingDevice device_
Definition correlation_detector.h:326
void set_status(const Dictionary &) override
Change properties of the node according to the entries in the dictionary.
Definition correlation_detector.h:351
void handle(SpikeEvent &) override
Handle incoming spike events.
Definition correlation_detector.cpp:265
State_ S_
Definition correlation_detector.h:328
void get_status(Dictionary &) const override
Export properties of the node by setting entries in the status dictionary.
Definition correlation_detector.h:343
size_t handles_test_event(SpikeEvent &, size_t) override
Check if the node can handle a particular event and receptor type.
Definition correlation_detector.h:332
Parameters_ P_
Definition correlation_detector.h:327
bool has_proxies() const override
This device has proxies, so that it will receive spikes also from sources which live on other threads...
Definition correlation_detector.h:193
correlation_detector()
Definition correlation_detector.cpp:214
void calibrate_time(const TimeConverter &tc) override
Re-calculate time-based properties of the node.
Definition correlation_detector.cpp:354
void init_buffers_() override
Configure persistent internal data structures.
Definition correlation_detector.cpp:242
void update(Time const &, const long, const long) override
Advance the state of the node in time through the given interval.
Definition correlation_detector.cpp:260
void pre_run_hook() override
Re-calculate dependent parameters of the node.
Definition correlation_detector.cpp:249
virtual size_t handles_test_event(SpikeEvent &, size_t receptor_type)
Check if the node can handle a particular event and receptor type.
Definition node.cpp:271
virtual void handle(SpikeEvent &e)
Handle incoming spike events.
Definition node.cpp:265
const std::string recorder("recorder")
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
void register_correlation_detector(const std::string &name)
Correlation detector breaks with the persistence scheme as follows: the internal buffers for storing ...
Definition correlation_detector.cpp:42
Declarations for base class Node.
Definition correlation_detector.h:262
void get(Dictionary &) const
Store current values in dictionary.
Definition correlation_detector.cpp:110
Time Tstart_
start of recording
Definition correlation_detector.h:265
Parameters_()
Sets default parameter values.
Definition correlation_detector.cpp:51
Time get_default_delta_tau()
Definition correlation_detector.h:364
Time Tstop_
end of recording
Definition correlation_detector.h:266
Time tau_max_
maximum time difference of events to detect
Definition correlation_detector.h:264
Parameters_ & operator=(const Parameters_ &)
Definition correlation_detector.cpp:80
Time delta_tau_
width of correlation histogram bins
Definition correlation_detector.h:263
bool set(const Dictionary &, const correlation_detector &, Node *)
Set values from dictionary.
Definition correlation_detector.cpp:128
Spike structure to store in the deque of recently received events.
Definition correlation_detector.h:235
Spike_(long timestep, double weight)
Definition correlation_detector.h:239
bool operator>(const Spike_ &second) const
Greater operator needed for insertion sort.
Definition correlation_detector.h:249
double weight_
Definition correlation_detector.h:237
long timestep_
Definition correlation_detector.h:236
Definition correlation_detector.h:297
std::vector< double > histogram_correction_
used for Kahan summation algorithm
Definition correlation_detector.h:307
std::vector< long > count_histogram_
Unweighted histogram.
Definition correlation_detector.h:310
std::vector< SpikelistType > incoming_
incoming spikes, sorted
Definition correlation_detector.h:299
void set(const Dictionary &, const Parameters_ &, bool, Node *)
Definition correlation_detector.cpp:170
void reset(const Parameters_ &)
Definition correlation_detector.cpp:191
State_()
initialize default state
Definition correlation_detector.cpp:95
void get(Dictionary &) const
Definition correlation_detector.cpp:119
std::vector< double > histogram_
Weighted histogram.
Definition correlation_detector.h:304
std::vector< long > n_events_
spike counters
Definition correlation_detector.h:298