NEST  2.6.0,not_revisioned_source_dir@0
recording_device.h
Go to the documentation of this file.
1 /*
2  * recording_device.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 RECORDING_DEVICE_H
24 #define RECORDING_DEVICE_H
25 
26 #include "nest.h"
27 #include "dictdatum.h"
28 #include "dictutils.h"
29 #include "lockptr.h"
30 #include "device.h"
31 
32 #include <vector>
33 #include <fstream>
34 
35 namespace nest {
36 
37  /*BeginDocumentation
38  Name: RecordingDevice - Common properties of all recording devices.
39  Description:
40 
41  Recording devices are used to measure properties of or signals emitted
42  by network nodes, e.g., using a voltmeter or a spike detector.
43 
44  Recording devices can collect data in memory, display it on the terminal
45  output or write it to file in any combination. The output format can be
46  controlled by device parameters as discussed below.
47 
48  Recording devices can be subdivided into two groups: collectors and samplers.
49  - Collectors collect events sent to them; the spike detector is the archetypical
50  example. Nodes are connected to collectors and the collector then collects all
51  spikes emitted by the nodes connected to it and records them.
52  - Samplers actively interrogate their targets at given time intervals (default: 1ms),
53  and record the data they obtain. This means that the sampler must be connected
54  to the node (not the node to the sampler), and that the node must support the
55  particular type of sampling; see device specific documentation for details.
56 
57  Recording devices share the start, stop, and origin parameters global to
58  devices. Start and stop have the following meaning for stimulating devices
59  (origin is just a global offset):
60  - Collectors collect all events with timestamps T that fulfill
61  start < T <= stop.
62  Note that events with timestamp T == start are NOT recorded.
63  - Sampling devices sample at times t = nh with
64  start < t <= stop
65  (t-start) mod interval == 0
66 
67  Remarks:
68  - Recording devices can only reliably record data generated during the previous
69  min_delay interval. This means that in order to ensure consistent results,
70  you should always set a stop time for a recording device that is at least
71  one min_delay before the end of the simulation time.
72  - By default, devices record to memory. If you want to record to file, it may
73  be a good idea to turn off recording to memory, to avoid that you computer's
74  memory fills up with gigabytes of data: << /to_file true /to_memory false >>.
75  - Events are not necessarily recorded in chronological order.
76  - The device will not open an existing file, since that would erase the existing
77  data in the file. If you want existing files to be overwritten automatically,
78  you must set /overwrite_files in the root node.
79 
80  Parameters:
81  The following parameters are shared with all devices:
82  /start - Actication time, relative to origin.
83  /stop - Inactivation time, relative to origin.
84  /origin - Reference time for start and stop.
85 
86  The following parameter is only relevant for sampling devices:
87  /interval - Sampling interval in ms (default: 1ms).
88 
89  The following parameters control where output is sent/data collected:
90  /record_to - An array containing any combination of /file, /memory, /screen,
91  indicating whether to write to file, record in memory or write
92  to the console window. An empty array turns all recording of
93  individual events off, only an event count is kept. You can also pass
94  strings (file), (memory), (screen), mainly for compatibility with
95  Python.
96 
97  The name of the output file is
98  data_path/data_prefix(label|model_name)-gid-vp.file_extension
99 
100  See /label and /file_extension for how to change the name. /data_prefix
101  is changed in the root node. If you change any part of the name,
102  an open file will be closed and a new file opened.
103 
104  To close the file, pass a /record_to array without /file, or pass
105  /to_file false. If you later turn recording to file on again, the
106  file will be overwritten, unless you have changed data_prefix, label,
107  or file_extension.
108 
109  /to_file - If true, turn on recording to file. Similar to /record_to [/file], but
110  does not affect settings for recording to memory and screen.
111  /to_screen - If true, turn on recording to screen. Similar to /record_to [/screen], but
112  does not affect settings for recording to memory and file.
113  /to_memory - If true, turn on recording to memory Similar to /record_to [/memory], but
114  does not affect settings for recording to file and screen.
115 
116  /filenames - Array containing the filenames where data is recorded to. This array has one
117  entry per local thread and is only available if /to_file is set to true, or
118  if /record_to contains /to_file.
119 
120  /label - String specifying an arbitrary label for the device. It is used instead of
121  model_name in the output file name.
122  /file_extension - String specifying the file name extension, without leading dot. The
123  default depends on the specific device.
124  /close_after_simulate - Close output stream before Simulate returns. If set to false, any output
125  streams will remain open when Simulate returns. (Default: false).
126  /flush_after_simulate - Flush output stream before Simulate returns. If set to false, any output
127  streams will be in an undefined state when Simulate returns. (Default: true).
128  /flush_records - Flush output stream whenever new data has been written to the stream. This
129  may impede performance (Default: false).
130  /close_on_reset - Close output file stream upon ResetNetwork. Upon the next call to Simulate,
131  the file is reopened, overwriting its contents. If set to false, the file
132  will remain open after ResetNetwork, so you can record continuously. NB:
133  the file is always closed upon ResetKernel. (Default: true).
134 
135  The following parameters control how output is formatted:
136  /withtime - boolean value which specifies whether the network time should be
137  recorded (default: true).
138  /withgid - boolean value which specifies whether the global id of the observed node(s)
139  should be recorded (default: false).
140  /withweight - boolean value which specifies whether the weight of the event should be
141  recorded (default: false).
142  /time_in_steps - boolean value which specifies whether to print time in steps, i.e., multiples
143  of the resolution, rather than in ms. If combined with /precise_times, each
144  time is printed as a pair of an integer step number and a double offset < 0.
145  /precise_times - boolean value which specifies whether offsets describing the precise timing
146  of a spike within a time step should be taken into account when computing
147  the spike time. This is only useful when recording from neurons that can
148  emit spikes off-grid (see module precise). Times are given in milliseconds.
149  If /time_in_steps is true, times are given as steps and negative offset.
150  /scientific - if set to true, doubles are written in scientific format, otherwise in
151  fixed format; affects file output only, not screen output (default: false)
152  /precision - number of digits to use in output of doubles to file (default: 3)
153  /binary - if set to true, data is written in binary mode to files instead of ASCII.
154  This setting affects file output only, not screen output (default: false)
155  /fbuffer_size - the size of the buffer to use for writing to files. The default size is
156  determined by the implementation of the C++ standard library. To obtain an
157  unbuffered file stream, use a buffer size of 0.
158 
159  Data recorded in memory is available through the following parameter:
160  /n_events - Number of events collected or sampled. n_events can be set to 0, but
161  no other value. Setting n_events to 0 will delete all spikes recorded
162  in memory. n_events will count events even when not recording to memory.
163  /events - Dictionary with elements /senders (sender GID, only if /withgid or /withpath
164  are true), /times (spike times in ms or steps, depending on /time_in_steps;
165  only if /withtime is true) and /offsets (only if /time_in_steps,
166  /precise_times and /withtime are true). All data stored in memory
167  is erased when /n_events is set to 0.
168 
169  SeeAlso: Device, StimulatingDevice
170  */
171 
172 
214  class RecordingDevice : public Device
215  {
216 
217  public:
222 
232  RecordingDevice(const Node&, Mode, const std::string&, bool, bool, bool=false);
233 
239  RecordingDevice(const Node&, const RecordingDevice&);
240  virtual ~RecordingDevice() {}
241 
243  void init_parameters(const RecordingDevice&);
244 
245  using Device::init_state;
246  void init_state(const RecordingDevice&);
247 
252  void init_buffers();
253 
258  void calibrate();
259 
263  void finalize();
264 
276  void record_event(const Event&, bool endrecord = true);
277 
283  template <typename ValueT>
284  void print_value(const ValueT&, bool endrecord = true);
285 
290  bool is_active(Time const & T) const;
291 
292  void get_status(DictionaryDatum &) const;
293 
307  void set_status(const DictionaryDatum &);
308 
318  template <typename DataT>
319  void set_status(const DictionaryDatum &, DataT& t);
320 
321  bool to_screen() const { return P_.to_screen_; }
322  bool to_file() const { return P_.to_file_; }
323  bool to_memory() const { return P_.to_memory_; }
324  bool to_accumulator() const { return P_.to_accumulator_; }
325 
326  inline
327  void set_precise(bool use_precise, long precision);
328 
329  private:
330 
340  void print_time_(std::ostream&, const Time&, double offset);
341 
345  void print_id_(std::ostream&, index);
346 
350  void print_weight_(std::ostream&, double);
351 
355  void store_data_(index, const Time&, double, double);
356 
361  void clear_data_();
362 
366  void flush_stream_();
367 
373  const std::string build_filename_() const;
374 
375  // ------------------------------------------------------------------
376 
377  struct Buffers_ {
378  std::ofstream fs_;
379  };
380 
381  // ------------------------------------------------------------------
382 
383  struct Parameters_ {
384  bool to_file_;
385  bool to_screen_;
386  bool to_memory_;
390  bool withgid_;
391  bool withtime_;
392  bool withweight_;
393 
394  long precision_;
395  bool scientific_;
396 
397  bool binary_;
400 
401  std::string label_;
402  std::string file_ext_;
403  std::string filename_;
408 
415  Parameters_(const std::string&, bool, bool, bool);
416 
417  void get(const RecordingDevice&, DictionaryDatum&) const;
418  void set(const RecordingDevice&, const Buffers_&, const DictionaryDatum&);
419  };
420 
421  // ------------------------------------------------------------------
422 
423  struct State_ {
424  size_t events_;
425  std::vector<long> event_senders_;
426  std::vector<double_t> event_times_ms_;
427  std::vector<long> event_times_steps_;
428  std::vector<double_t> event_times_offsets_;
429  std::vector<double_t> event_weights_;
430 
431  State_();
432 
433  void clear_events();
434  void get(DictionaryDatum&, const Parameters_&) const;
435  void set(const DictionaryDatum&);
436  };
437 
438  // ------------------------------------------------------------------
439 
440  const Node& node_;
441  const Mode mode_;
446 };
447 
448 
449 inline
450 bool RecordingDevice::is_active(Time const & T) const
451 {
452  const long_t stamp = T.get_steps();
453 
454  return get_t_min_() < stamp && stamp <= get_t_max_();
455 }
456 
457 inline
459 {
460  P_.get(*this, d);
461  S_.get(d, P_);
463 
465 }
466 
467 inline
468 void RecordingDevice::set_precise(bool use_precise, long precision)
469 {
470  P_.precise_times_ = use_precise;
472 }
473 
474 
475 
476 template <typename ValueT>
477 void RecordingDevice::print_value(const ValueT& value, bool endrecord)
478 {
479  if ( P_.to_screen_ )
480  {
481  std::cout << value << '\t';
482  if ( endrecord )
483  std::cout << '\n';
484  }
485 
486  if ( P_.to_file_ )
487  {
488  B_.fs_ << value << '\t';
489  if ( endrecord )
490  B_.fs_ << '\n';
491  }
492 }
493 
494 template <typename DataT>
496 {
497  // plain set_status does most of the work
498  set_status(d);
499 
500  // if n_events is 0, also clear event data
501  if ( S_.events_ == 0 )
502  data.clear();
503 }
504 
505 } // namespace
506 
507 #endif // RECORDING_DEVICE_H
size_t index
Unsigned long type for enumerations.
Definition: nest.h:109
bool to_accumulator_
true if data is to be accumulated; exclusive to all other to_*
Definition: recording_device.h:387
bool to_accumulator() const
Definition: recording_device.h:324
Buffers_ B_
Definition: recording_device.h:444
Definition: lockptrdatum.h:40
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
Definition: recording_device.h:221
std::string label_
a user-defined label for symbolic device names.
Definition: recording_device.h:401
void get(DictionaryDatum &, const Parameters_ &) const
Store current values in dictionary.
Definition: recording_device.cpp:214
bool to_screen_
true if recorder writes its output to stdout
Definition: recording_device.h:385
void init_state(const RecordingDevice &)
Definition: recording_device.cpp:325
void flush_stream_()
Flush output stream (not std::cout).
std::string filename_
the filename, if recording to a file (read-only)
Definition: recording_device.h:403
virtual void get_status(DictionaryDatum &) const
Definition: device.h:206
const Name offset("offset")
Miscellaneous parameters.
Definition: nest_names.h:211
long fbuffer_size_old_
the buffer size to use when writing to file (old)
Definition: recording_device.h:399
std::vector< double_t > event_times_ms_
List of event times in ms.
Definition: recording_device.h:426
Encapsulates information which is sent between Nodes.
Definition: event.h:73
const Node & node_
node to which device instance belongs
Definition: recording_device.h:440
void get(const RecordingDevice &, DictionaryDatum &) const
Store current values in dictionary.
Definition: recording_device.cpp:80
bool close_after_simulate_
if true, finalize() shall close the stream
Definition: recording_device.h:404
delay get_steps() const
Definition: nest_time.h:395
void print_time_(std::ostream &, const Time &, double offset)
Print the time-stamp according to the recorder's flags.
Definition: recording_device.cpp:555
Definition: recording_device.h:377
const Name element_type("element_type")
Node type.
Definition: nest_names.h:117
virtual void init_state(const Device &)
Reset dynamic state to that of model.
Definition: device.h:105
void clear_data_()
Clear data in internal structure, and call clear_data_hook().
void init_parameters(const RecordingDevice &)
Definition: recording_device.cpp:317
bool to_memory() const
Definition: recording_device.h:323
bool precise_times_
true if time is computed including offset
Definition: recording_device.h:389
virtual ~RecordingDevice()
Definition: recording_device.h:240
Buffers_ V_
Definition: recording_device.h:445
bool to_file_
true if recorder writes its output to a file
Definition: recording_device.h:384
void clear_events()
clear all data
Definition: recording_device.cpp:625
bool is_active(Time const &T) const
Indicate if recording device is active.
Definition: recording_device.h:450
const Name recorder("recorder")
Node type.
Definition: nest_names.h:245
void set(const DictionaryDatum &)
Get values from dictionary.
Definition: recording_device.cpp:279
bool withtime_
true if time of event is to be printed, default
Definition: recording_device.h:391
Mode
Device mode.
Definition: recording_device.h:221
Base class for all recording devices.
Definition: recording_device.h:214
std::vector< long > event_senders_
List of event sender ids.
Definition: recording_device.h:425
Definition: recording_device.h:221
Class implementing common interface and properties common for all devices.
Definition: device.h:94
std::vector< double_t > event_weights_
List of event weights.
Definition: recording_device.h:429
Definition: nest_time.h:130
void set_precise(bool use_precise, long precision)
Definition: recording_device.h:468
void print_id_(std::ostream &, index)
Print a node's global ID and/or address, according to the recorder's flags.
Definition: recording_device.cpp:549
std::ofstream fs_
the file to write the recorded data to
Definition: recording_device.h:378
bool to_file() const
Definition: recording_device.h:322
const std::string build_filename_() const
Build filename from parts.
Definition: recording_device.cpp:602
void record_event(const Event &, bool endrecord=true)
Record common information for one event.
Definition: recording_device.cpp:511
size_t events_
Event counter.
Definition: recording_device.h:424
bool withgid_
true if element GID is to be printed, default
Definition: recording_device.h:390
const Name precision("precision")
Recorder parameter.
Definition: nest_names.h:230
void print_value(const ValueT &, bool endrecord=true)
Print single item of type ValueT.
Definition: recording_device.h:477
void set_status(const DictionaryDatum &)
Set properties of recording device.
Definition: recording_device.cpp:483
std::vector< double_t > event_times_offsets_
List of event time offsets.
Definition: recording_device.h:428
virtual void init_parameters(const Device &)
Reset parameters to those of model; also resets state.
Definition: device.cpp:107
void calibrate()
Ensure streams are open for writing.
Definition: recording_device.cpp:344
Definition: recording_device.h:383
bool scientific_
use scientific format if true, else fixed
Definition: recording_device.h:395
long_t get_t_max_() const
Return upper limit in steps.
Definition: device.h:246
Parameters_ P_
Definition: recording_device.h:442
bool flush_records_
if true, flush stream after each output
Definition: recording_device.h:406
RecordingDevice(const Node &, Mode, const std::string &, bool, bool, bool=false)
Create recording device information.
Definition: recording_device.cpp:295
Definition: recording_device.h:423
long_t get_t_min_() const
Return lower limit in steps.
Definition: device.h:240
Definition: namedatum.h:90
std::vector< long > event_times_steps_
List of event times in steps.
Definition: recording_device.h:427
bool binary_
true if to write files in binary mode instead of ASCII
Definition: recording_device.h:397
Default types used by the NEST kernel.
bool withweight_
true if weight of event is to be printed
Definition: recording_device.h:392
void store_data_(index, const Time &, double, double)
Store data in internal structure.
Definition: recording_device.cpp:579
void init_buffers()
Close file stream.
Definition: recording_device.cpp:331
void set(const RecordingDevice &, const Buffers_ &, const DictionaryDatum &)
Set values from dicitonary.
Definition: recording_device.cpp:128
void finalize()
Flush output stream if requested.
Definition: recording_device.cpp:456
Base class for all NEST network objects.
Definition: node.h:96
Parameters_(const std::string &, bool, bool, bool)
Set default parameter values.
Definition: recording_device.cpp:44
bool time_in_steps_
true if time is printed in steps, not ms.
Definition: recording_device.h:388
bool flush_after_simulate_
if true, finalize() shall flush the stream
Definition: recording_device.h:405
bool close_on_reset_
if true, close stream in init_buffers()
Definition: recording_device.h:407
void get_status(DictionaryDatum &) const
Definition: recording_device.h:458
State_ S_
Definition: recording_device.h:443
long long_t
Integer number with at least 32 bit.
Definition: nest.h:96
long precision_
precision of doubles written to file
Definition: recording_device.h:394
std::string file_ext_
the file name extension to use, without .
Definition: recording_device.h:402
const Mode mode_
operating mode, depends on owning node
Definition: recording_device.h:441
bool to_screen() const
Definition: recording_device.h:321
Definition: recording_device.h:221
long fbuffer_size_
the buffer size to use when writing to file
Definition: recording_device.h:398
State_()
Sets default parameter values.
Definition: recording_device.cpp:68
bool to_memory_
true if data should be recorded in memory, default
Definition: recording_device.h:386
void print_weight_(std::ostream &, double)
Print the weight of an event.
Definition: recording_device.cpp:572