Auryn simulator  v0.8.1-206-gb56e451
Plastic Spiking Neural Network Simulator
Functions
aubs.cpp File Reference
#include "auryn.h"
#include <iostream>
#include <fstream>
#include <vector>
#include "auryn/AurynVersion.h"
Include dependency graph for aubs.cpp:

Functions

AurynLong find_frame (std::ifstream *file, AurynTime target)
 
void read_header (std::ifstream *input, double &dt, AurynLong num_events, double &last_time, std::string filename)
 
int main (int ac, char *av[])
 

Detailed Description

Implements helper program to decode files written with BinaryStateMonitor

Function Documentation

◆ find_frame()

AurynLong find_frame ( std::ifstream *  file,
AurynTime  target 
)

Perform binary search on ifstream to extract frame number from a target time reference that should be given in discrete time.

40 {
41  // get number of elements
42  file->seekg (0, file->end);
43  AurynLong num_of_frames = file->tellg()/sizeof(StateValue_type);
44 
45  AurynLong lo = 1; // first frame is used for header
46  AurynLong hi = num_of_frames;
47 
48  while ( lo+1 < hi ) {
49  AurynLong pivot = lo + (hi-lo)/2;
50  file->seekg (pivot*sizeof(StateValue_type), file->beg);
51 
52  StateValue_type state_data;
53  file->read((char*)&state_data, sizeof(StateValue_type));
54 
55  if ( state_data.time < target ) lo = pivot;
56  else hi = pivot;
57  }
58 
59  return hi;
60 }
Auryn spike event for binary monitors.
Definition: auryn_definitions.h:201
unsigned long AurynLong
An unsigned long type used to count synapses or similar.
Definition: auryn_definitions.h:154

◆ main()

int main ( int  ac,
char *  av[] 
)
101 {
102  std::string input_filename;
103  std::ifstream * input;
104 
105  std::string output_file_name = "";
106  double from_time = 0.0;
107  double to_time = -1.0;
108  double seconds_to_extract_from_end = -1.0; // negative means disabled
109  // one more decimal than neede to show values are not rounded
110  bool debug_output = false;
111 
112  try {
113  po::options_description desc("Allowed options");
114  desc.add_options()
115  ("help,h", "produce help message")
116  ("version,v", "show version information")
117  ("debug,d", "show verbose debug output")
118  ("input,i", po::value<std::string>(), "input file")
119  ("output,o", po::value<std::string>(), "output file (output to stout if not given)")
120  ("from,f", po::value<double>(), "'from time' in seconds")
121  ("to,t", po::value<double>(), "'to time' in seconds")
122  ("last,l", po::value<double>(), "last x seconds (overrides from/to)")
123  ;
124 
125  po::variables_map vm;
126  po::store(po::parse_command_line(ac, av, desc), vm);
127  po::notify(vm);
128 
129  if (vm.count("help")) {
130  std::cout << desc << "\n";
131  return 1;
132  }
133 
134  if (vm.count("version")) {
135  AurynVersion build;
136  std::cout << "Auryn Binary Extract version "
137  << build.version << "."
138  << build.subversion << "."
139  << build.revision_number << "\n";
140  return EXIT_SUCCESS;
141  }
142 
143  if (vm.count("debug")) {
144  debug_output = true;
145  }
146 
147  if (vm.count("input")) {
148  input_filename = vm["input"].as<std::string>();
149  }
150 
151  if (vm.count("output")) {
152  output_file_name = vm["output"].as<std::string>();
153  }
154 
155  if (vm.count("from")) {
156  from_time = vm["from"].as<double>();
157  }
158 
159  if (vm.count("to")) {
160  to_time = vm["to"].as<double>();
161  }
162 
163  if (vm.count("last")) {
164  seconds_to_extract_from_end = vm["last"].as<double>();
165  }
166 
167  }
168  catch(std::exception& e) {
169  std::cerr << "error: " << e.what() << "\n";
170  return 1;
171  }
172  catch(...) {
173  std::cerr << "Exception of unknown type!\n";
174  }
175 
176  double last_time = 0.0;
177  double dt = 0.0;
178 
179  if ( input_filename.empty() ) {
180  std::cerr << "Missing input file." << std::endl;
181  exit(EXIT_FAILURE);
182  }
183 
184  input = new std::ifstream( input_filename.c_str(), std::ios::binary );
185  if (!(*input)) {
186  std::cerr << "Unable to open input file "
187  << input_filename
188  << std::endl;
189  exit(EXIT_FAILURE);
190  }
191 
192  double tmp_last_time = 0;
193  double tmp_dt = 0;
194  AurynLong tmp_num_events = 0;
195  read_header( input, tmp_dt, tmp_num_events, tmp_last_time, input_filename );
196 
197  if ( debug_output ) {
198  std::cerr << "# Last frame in file:" << tmp_num_events << std::endl;
199  }
200 
201  if ( dt == 0 ) {
202  dt = tmp_dt;
203  } else {
204  if ( dt != tmp_dt ) { // should not happen
205  std::cerr << "Not all input file headers match." << std::endl;
206  exit(EXIT_FAILURE);
207  }
208  }
209 
210  if ( tmp_last_time > last_time ) {
211  last_time = tmp_last_time;
212  }
213 
214 
215  if ( to_time < 0 ) {
216  to_time = last_time;
217  }
218 
219  if ( seconds_to_extract_from_end > 0 ) {
220  from_time = to_time-seconds_to_extract_from_end;
221  }
222 
223  if ( from_time < 0 ) from_time = 0.0 ;
224 
225  if ( from_time > to_time || from_time < 0 ) {
226  std::cerr << "Times must be positive and start "
227  "time needs to be < to time." << std::endl;
228  exit(EXIT_FAILURE);
229  }
230 
231  // translate second times into auryn time
232  AurynTime to_auryn_time = to_time/dt;
233 
234 
235  if ( debug_output ) {
236  std::cerr << "# Timestep: " << dt << std::endl;
237  std::cerr << "# Sizeof SpikeEvent struct: " << sizeof(StateValue_type) << std::endl;
238  std::cerr << "# Time of last event in files: " << last_time << std::endl;
239  std::cerr << "# From time: " << from_time << std::endl;
240  std::cerr << "# To time: " << to_time << std::endl;
241  }
242 
243 
244  // set stream to respetive start frame
245 
246  // compute start and end frames
247  AurynLong start_frame = find_frame(input, from_time/dt);
248 
249  // prepare input stream
250  input->seekg (start_frame*sizeof(StateValue_type), input->beg);
251  input->clear();
252  if ( debug_output ) {
253  std::cerr << "# Start frame stream: "
254  << start_frame << std::endl;
255  }
256 
257 
258  StateValue_type frame;
259  input->read((char*)&frame, sizeof(StateValue_type));
260 
261  AurynTime time_reference = from_time/dt;
262  int decimal_places = -std::log(dt)/std::log(10)+2;
263 
264  // open output filestream if needed
265  std::ofstream of;
266  bool write_to_stdout = true;
267  if( !output_file_name.empty() ) {
268  write_to_stdout = false;
269  of.open( output_file_name.c_str(), std::ofstream::out );
270  of << std::fixed << std::setprecision(decimal_places);
271  }
272  // sets output format to right number of decimal places
273  std::cout << std::fixed << std::setprecision(decimal_places);
274 
275  while ( true ) {
276  time_reference = frame.time;
277  if ( time_reference >= to_auryn_time || input->eof() ) break;
278 
279  if ( debug_output && false ) {
280  std::cout << "# time_reference " << time_reference << std::endl;
281  }
282 
283  // output from next_stream
284  while ( frame.time <= time_reference && !input->eof() ) {
285  if ( write_to_stdout )
286  std::cout << std::fixed << std::setprecision(decimal_places)
287  << frame.time*dt << " "
288  << std::scientific // << std::setprecision(8)
289  << frame.value << "\n";
290  else
291  of << std::fixed << std::setprecision(decimal_places)
292  << frame.time*dt << " "
293  << std::scientific // << std::setprecision(8)
294  << frame.value << "\n";
295  input->read((char*)&frame, sizeof(StateValue_type));
296  }
297  }
298 
299  if ( !write_to_stdout )
300  of.close();
301 
302  // close input stream
303  input->close();
304 
305  return EXIT_SUCCESS;
306 }
Auryn spike event for binary monitors.
Definition: auryn_definitions.h:201
Container class providing Auryn version number.
Definition: AurynVersion.h:37
AurynLong find_frame(std::ifstream *file, AurynTime target)
Definition: aubs.cpp:39
static int revision_number
Definition: AurynVersion.h:41
unsigned long AurynLong
An unsigned long type used to count synapses or similar.
Definition: auryn_definitions.h:154
static int subversion
Definition: AurynVersion.h:40
static int version
Definition: AurynVersion.h:39
NeuronID AurynTime
Defines Auryns discrete time unit of the System clock. Change to AurynLong if 120h of simtime are not...
Definition: auryn_definitions.h:155
void read_header(std::ifstream *input, double &dt, AurynLong num_events, double &last_time, std::string filename)
Definition: aubs.cpp:63
std::string string
Standard library string type which is imported into Auryn namespace.
Definition: auryn_definitions.h:156
Here is the call graph for this function:

◆ read_header()

void read_header ( std::ifstream *  input,
double &  dt,
AurynLong  num_events,
double &  last_time,
std::string  filename 
)
64 {
65  // get length of the file
66  StateValue_type state_data;
67  input->seekg (0, input->end);
68  num_events = input->tellg()/sizeof(StateValue_type)-1;
69 
70  // read first entry to infer dt
71  input->seekg (0, input->beg);
72  input->read((char*)&state_data, sizeof(StateValue_type));
73  dt = 1.0/state_data.time;
74 
75  // do some version checking
76  AurynVersion build;
77  AurynState tag = state_data.value;
78  if ( (int)tag/1000 != (int)(build.tag_binary_state_monitor)/1000 ) {
79  std::cerr << "Header not recognized. "
80  "Not a BinaryStateMonitor file?"
81  << std::endl;
82  exit(EXIT_FAILURE);
83  }
84 
85  if ( tag != build.tag_binary_state_monitor ) {
86  std::cerr << "# Warning: Either the Auryn version does not match "
87  "the version of this tool or this is not a BinaryStateMonitor "
88  "file." << std::endl;
89  // TODO tell user if it is a state file
90  }
91 
92  // read out last time
93  input->seekg (num_events*sizeof(StateValue_type), input->beg);
94  input->read((char*)&state_data, sizeof(StateValue_type));
95  last_time = (state_data.time+1)*dt;
96  // places last_time _behind_ the last time because we are the
97  // exclusive interval end
98 }
Auryn spike event for binary monitors.
Definition: auryn_definitions.h:201
Container class providing Auryn version number.
Definition: AurynVersion.h:37
static AurynState tag_binary_state_monitor
file signature for BinaryStateMonitor files
Definition: AurynVersion.h:43
AurynFloat AurynState
Type for Auryn state variables (default single precision since it needs to be compatible with auryn_v...
Definition: auryn_definitions.h:160
AurynTime time
Definition: auryn_definitions.h:203