NEST  2.6.0,not_revisioned_source_dir@0
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 #include <vector>
28 #include <deque>
29 #include "nest.h"
30 #include "event.h"
31 #include "node.h"
33 
34 /* BeginDocumentation
35 
36  Name: correlation_detector - Device for evaluating cross correlation between two spike sources
37 
38  Description:
39  The correlation_detector device is a recording device. It is used to record
40  spikes from two pools of spike inputs and calculates the count_histogram of inter-spike
41  intervals (raw cross correlation) binned to bins of duration delta_tau.
42  The result can be obtained via GetStatus under the key /count_histogram.
43  In parallel it records a weighted histogram, where the connection weights
44  are used to weight every count. In order to minimize numerical errors the Kahan summation
45  algorithm is used when calculating the weighted histogram.
46  (http://en.wikipedia.org/wiki/Kahan_summation_algorithm)
47  Both are arrays of 2*tau_max/delta_tau+1 values containing the histogram counts in the
48  following way:
49 
50  Let t_{1,i} be the spike times of source 1, t_{2,j} the spike times of source 2.
51  histogram[n] then contains the sum of products of the weight w_{1,i}*w_{2,j},
52  count_histogram[n] contains 1 summed over all events with t_{2,j} - t_{1,i} in
53 
54  [ n*delta_tau - tau_max - delta_tau/2 , n*delta_tau - tau_max + delta_tau/2 ) .
55 
56  The bins are centered around the time difference they represent, but are left-closed
57  and right-open. This means that events with time difference -tau_max-delta_tau/2 are
58  counted in the leftmost bin, but event with difference tau_max+delta_tau/2 are not
59  counted at all.
60 
61  The correlation detector has two inputs, which are selected via the receptor_port of the
62  incoming connection: All incoming connections with receptor_port = 0 will be pooled as the
63  spike source 1, the ones with receptor_port = 1 will be used as spike source 2.
64 
65  Parameters:
66  Tstart double - Time when to start counting events. This time should be set to at least
67  start + tau_max in order to avoid edge effects of the correlation counts.
68  Tstop double - Time when to stop counting events. This time should be set to at most
69  Tsim - tau_max, where Tsim is the duration of simulation,
70  in order to avoid edge effects of the correlation counts.
71  delta_tau double - bin width in ms
72  tau_max double - one-sided histogram width in ms. Events with differences in
73  [-tau_max-delta_tau/2, tau_max+delta_tau/2) are counted.
74 
75  histogram double vector, read-only - raw, weighted cross correlation counts
76  histogram_correction double_vector, read-only - correction factors for kahan summation algorithm
77  count_histogram long vector, read-only - raw, cross correlation counts
78  n_events integer vector - number of events from source 0 and 1.
79  By setting n_events to [0 0], the histogram
80  is cleared.
81 
82  Remarks: This recorder does not record to file, screen or memory in the usual sense.
83 
84  Example:
85  /s1 /spike_generator Create def
86  /s2 /spike_generator Create def
87  s1 << /spike_times [ 1.0 1.5 2.7 4.0 5.1 ] >> SetStatus
88  s2 << /spike_times [ 0.9 1.8 2.1 2.3 3.5 3.8 4.9 ] >> SetStatus
89  /cd /correlation_detector Create def
90  cd << /delta_tau 0.5 /tau_max 2.5 >> SetStatus
91  s1 cd << /receptor_type 0 >> Connect
92  s2 cd << /receptor_type 1 >> Connect
93  10 Simulate
94  cd [/n_events] get == --> [# 5 7 #]
95  cd [/histogram] get == --> [. 0 3 3 1 4 3 2 6 1 2 2 .]
96  cd << /reset true >> SetStatus
97  cd [/histogram] get == --> [. 0 0 0 0 0 0 0 0 0 0 0 .]
98 
99  Receives: SpikeEvent
100 
101  Author: Moritz Helias
102  Jakob Jordan (implemented Kahan summation algorithm) 2013/02/18
103  FirstVersion: 2007/5/21
104  SeeAlso: spike_detector, Device, PseudoRecordingDevice
105  Availability: NEST
106 */
107 
108 
109 namespace nest
110 {
111 
112  class Network;
113 
133  class correlation_detector : public Node
134  {
135 
136  public:
137 
140 
145  bool has_proxies() const {return true;}
146 
151  using Node::handle;
153 
154  void handle(SpikeEvent &);
155 
157 
158  void get_status(DictionaryDatum &) const;
159  void set_status(const DictionaryDatum &);
160 
161  private:
162 
163  void init_state_(Node const&);
164  void init_buffers_();
165  void calibrate();
166 
167  void update(Time const &, const long_t, const long_t);
168 
169  // ------------------------------------------------------------
170 
175  struct Spike_
176  {
179 
181  : timestep_(timestep), weight_(weight)
182  {}
183 
187  inline bool operator>(const Spike_ &second) const
188  {
189  return timestep_ > second.timestep_;
190  }
191  };
192 
193  typedef std::deque <Spike_> SpikelistType;
194 
195  // ------------------------------------------------------------
196 
197  struct State_;
198 
199  struct Parameters_ {
200 
205 
206  Parameters_();
207  Parameters_(const Parameters_&);
208 
209  void get(DictionaryDatum&) const;
210 
216  bool set(const DictionaryDatum&, const correlation_detector&);
217  };
218 
219  // ------------------------------------------------------------
220 
230  struct State_ {
231  std::vector<long_t> n_events_;
232  std::vector<SpikelistType> incoming_;
233 
236  std::vector<double_t> histogram_;
237  std::vector<double_t> histogram_correction_;
238 
241  std::vector<long_t> count_histogram_;
242 
243  State_();
244 
245  void get(DictionaryDatum&) const;
246 
250  void set(const DictionaryDatum&, const Parameters_&, bool);
251 
252  void reset(const Parameters_&);
253  };
254 
255  // ------------------------------------------------------------
256 
260  };
261 
262  inline
264  {
265  if (receptor_type < 0 || receptor_type > 1)
266  throw UnknownReceptorType(receptor_type, get_name());
267 
268  return receptor_type;
269  }
270 
271  inline
273  {
274  device_.get_status(d);
275  P_.get(d);
276  S_.get(d);
277 
279  }
280 
281  inline
283  {
284  Parameters_ ptmp = P_;
285  const bool reset_required = ptmp.set(d, *this);
286  State_ stmp = S_;
287  stmp.set(d, P_, reset_required);
288 
289  device_.set_status(d);
290  P_ = ptmp;
291  S_ = stmp;
292  }
293 
294 
295 } // namespace
296 
297 #endif /* #ifndef CORRELATION_DETECTOR_H */
void init_buffers_()
Private function to initialize the buffers of a node.
Definition: correlation_detector.cpp:212
Correlation detector class.
Definition: correlation_detector.h:133
double_t weight
Weight of a connection.
Definition: nest.h:170
const Name receptor_type("receptor_type")
Connection parameters.
Definition: nest_names.h:240
Definition: lockptrdatum.h:40
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
Declarations for base class Node.
State_()
initialize default state
Definition: correlation_detector.cpp:63
Base class for all pseudo recording devices.
Definition: pseudo_recording_device.h:73
PseudoRecordingDevice device_
Definition: correlation_detector.h:257
bool has_proxies() const
This device has proxies, so that it will receive spikes also from sources which live on other threads...
Definition: correlation_detector.h:145
port handles_test_event(SpikeEvent &, rport)
Check if the node can handle a particular event and receptor type.
Definition: correlation_detector.h:263
const Name element_type("element_type")
Node type.
Definition: nest_names.h:117
void init_state_(Node const &)
Private function to initialize the state of a node to model defaults.
Definition: correlation_detector.cpp:203
long_t rport
Connection port number to distinguish incoming connections, also called receiver port.
Definition: nest.h:147
std::deque< Spike_ > SpikelistType
Definition: correlation_detector.h:193
Time Tstop_
end of recording
Definition: correlation_detector.h:204
void calibrate()
Re-calculate dependent parameters of the node.
Definition: correlation_detector.cpp:218
void get_status(DictionaryDatum &) const
Definition: correlation_detector.h:272
void set_status(const DictionaryDatum &)
Definition: correlation_detector.h:282
const Name recorder("recorder")
Node type.
Definition: nest_names.h:245
void handle(SpikeEvent &)
Definition: correlation_detector.cpp:231
Time Tstart_
start of recording
Definition: correlation_detector.h:203
correlation_detector()
Definition: correlation_detector.cpp:177
Parameters_ P_
Definition: correlation_detector.h:258
Definition: nest_time.h:130
Spike structure to store in the deque of recently received events.
Definition: correlation_detector.h:175
std::string get_name() const
Return class name.
Definition: node.cpp:83
Definition: correlation_detector.h:199
double_t weight_
Definition: correlation_detector.h:178
Exception to be thrown if the specified receptor type does not exist in the node. ...
Definition: exceptions.h:254
Spike_(long_t timestep, double_t weight)
Definition: correlation_detector.h:180
long_t timestep_
Definition: correlation_detector.h:177
Time delta_tau_
width of correlation histogram bins
Definition: correlation_detector.h:201
std::vector< long_t > n_events_
spike counters
Definition: correlation_detector.h:231
void reset(const Parameters_ &)
Definition: correlation_detector.cpp:148
std::vector< double_t > histogram_
Weighted histogram.
Definition: correlation_detector.h:236
std::vector< double_t > histogram_correction_
used for Kahan summation algorithm
Definition: correlation_detector.h:237
void update(Time const &, const long_t, const long_t)
Bring the node from state $t$ to $t+n*dt$.
Definition: correlation_detector.cpp:228
Definition: correlation_detector.h:230
long_t port
Connection port number to distinguis outgoing connections.
Definition: nest.h:155
virtual void handle(SpikeEvent &e)
Handle incoming spike events.
Definition: node.cpp:198
double double_t
Double precision floating point numbers.
Definition: nest.h:93
std::vector< SpikelistType > incoming_
incoming spikes, sorted
Definition: correlation_detector.h:232
virtual port handles_test_event(SpikeEvent &, rport receptor_type)
Check if the node can handle a particular event and receptor type.
Definition: node.cpp:203
Definition: namedatum.h:90
Default types used by the NEST kernel.
Event for spike information.
Definition: event.h:320
Base class for all NEST network objects.
Definition: node.h:96
bool set(const DictionaryDatum &, const correlation_detector &)
Set values from dicitonary.
Definition: correlation_detector.cpp:92
State_ S_
Definition: correlation_detector.h:259
bool operator>(const Spike_ &second) const
Greater operator needed for insertion sort.
Definition: correlation_detector.h:187
void set(const DictionaryDatum &, const Parameters_ &, bool)
Definition: correlation_detector.cpp:131
long long_t
Integer number with at least 32 bit.
Definition: nest.h:96
Time tau_max_
maximum time difference of events to detect
Definition: correlation_detector.h:202
std::vector< long_t > count_histogram_
Unweighted histogram.
Definition: correlation_detector.h:241
Parameters_()
Sets default parameter values.
Definition: correlation_detector.cpp:38