NEURON
report_configuration_parser.cpp
Go to the documentation of this file.
1 /*
2 # =============================================================================
3 # Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
4 #
5 # See top-level LICENSE file for details.
6 # =============================================================================
7 */
8 
9 #include <algorithm>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <fstream>
14 #include <iostream>
15 #include <limits>
16 #include <sstream>
17 #include <string>
18 #include <vector>
19 
25 
26 namespace coreneuron {
27 
28 
29 /*
30  * Split filter comma separated strings ("mech.var_name") into mech_name and var_name
31  */
32 void parse_filter_string(const std::string& filter, ReportConfiguration& config) {
33  std::vector<std::string> mechanisms;
34  std::stringstream ss(filter);
35  std::string mechanism;
36  // Multiple report variables are separated by `,`
37  while (getline(ss, mechanism, ',')) {
38  mechanisms.push_back(mechanism);
39 
40  // Split mechanism name and corresponding reporting variable
41  std::string mech_name;
42  std::string var_name;
43  std::istringstream iss(mechanism);
44  std::getline(iss, mech_name, '.');
45  std::getline(iss, var_name, '.');
46  if (var_name.empty()) {
47  var_name = "i";
48  }
49  config.mech_names.emplace_back(mech_name);
50  config.var_names.emplace_back(var_name);
51  if (mech_name == "i_membrane") {
52  nrn_use_fast_imem = true;
53  }
54  }
55 }
56 
58  report.type = report_type;
59  switch (report.target_type) {
61  report.section_type = All;
62  report.section_all_compartments = true;
63  break;
64  case TargetType::Cell:
65  report.section_type = Cell;
66  report.section_all_compartments = false;
67  break;
69  report.section_type = Soma;
70  report.section_all_compartments = false;
71  break;
73  report.section_type = Soma;
74  report.section_all_compartments = true;
75  break;
77  report.section_type = Axon;
78  report.section_all_compartments = false;
79  break;
81  report.section_type = Axon;
82  report.section_all_compartments = true;
83  break;
85  report.section_type = Dendrite;
86  report.section_all_compartments = false;
87  break;
89  report.section_type = Dendrite;
90  report.section_all_compartments = true;
91  break;
93  report.section_type = Apical;
94  report.section_all_compartments = false;
95  break;
97  report.section_type = Apical;
98  report.section_all_compartments = true;
99  break;
100  default:
101  std::cerr << "Report error: unsupported target type" << std::endl;
102  nrn_abort(1);
103  }
104 }
105 
106 std::vector<ReportConfiguration> create_report_configurations(const std::string& conf_file,
107  const std::string& output_dir,
108  SpikesInfo& spikes_info) {
109  std::string report_on;
110  int target;
111  std::ifstream report_conf(conf_file);
112 
113  int num_reports = 0;
114  report_conf >> num_reports;
115  std::vector<ReportConfiguration> reports(num_reports);
116  for (auto& report: reports) {
117  report.buffer_size = 4; // default size to 4 Mb
118 
119  report_conf >> report.name >> report.target_name >> report.type_str >> report_on >>
120  report.unit >> report.format >> target >> report.report_dt >> report.start >>
121  report.stop >> report.num_gids >> report.buffer_size;
122 
123  report.target_type = static_cast<TargetType>(target);
124  std::transform(report.type_str.begin(),
125  report.type_str.end(),
126  report.type_str.begin(),
127  [](unsigned char c) { return std::tolower(c); });
128  report.output_path = output_dir + "/" + report.name;
129  ReportType report_type;
130  if (report.type_str == "compartment") {
131  report_type = SectionReport;
132  if (report_on == "i_membrane") {
133  nrn_use_fast_imem = true;
134  report_type = IMembraneReport;
135  }
136  } else if (report.type_str == "synapse") {
137  report_type = SynapseReport;
138  } else if (report.type_str == "summation") {
139  report_type = SummationReport;
140  } else if (report.type_str == "lfp") {
141  nrn_use_fast_imem = true;
142  report_type = LFPReport;
143  } else {
144  std::cerr << "Report error: unsupported type " << report.type_str << std::endl;
145  nrn_abort(1);
146  }
147  register_target_type(report, report_type);
148  if (report.type == SynapseReport || report.type == SummationReport) {
149  parse_filter_string(report_on, report);
150  }
151  if (report.num_gids) {
152  report.target.resize(report.num_gids);
153  report_conf.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
154  report_conf.read(reinterpret_cast<char*>(report.target.data()),
155  report.num_gids * sizeof(int));
156  // extra new line: skip
157  report_conf.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
158  }
159  }
160  // read population information for spike report
161  int num_populations;
162  std::string spikes_population_name;
163  int spikes_population_offset;
164  if (report_conf.peek() == '\n') {
165  // skip newline and move forward to spike reports
166  report_conf.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
167  }
168  if (isdigit(report_conf.peek())) {
169  report_conf >> num_populations;
170  } else {
171  // support old format: one single line "All"
172  num_populations = 1;
173  }
174  for (int i = 0; i < num_populations; i++) {
175  if (!(report_conf >> spikes_population_name >> spikes_population_offset)) {
176  // support old format: one single line "All"
177  report_conf >> spikes_population_name;
178  spikes_population_offset = 0;
179  }
180  spikes_info.population_info.emplace_back(
181  std::make_pair(spikes_population_name, spikes_population_offset));
182  }
183  report_conf >> spikes_info.file_name;
184 
185  return reports;
186 }
187 } // namespace coreneuron
#define i
Definition: md1redef.h:19
static int c
Definition: hoc.cpp:169
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
void nrn_abort(int errcode)
Definition: utils.cpp:13
static const char * mechanism[]
Definition: capac.cpp:19
@ IMembraneReport
Definition: nrnreport.hpp:77
@ SummationReport
Definition: nrnreport.hpp:79
void register_target_type(ReportConfiguration &report, ReportType report_type)
void parse_filter_string(const std::string &filter, ReportConfiguration &config)
std::vector< ReportConfiguration > create_report_configurations(const std::string &filename, const std::string &output_dir, SpikesInfo &spikes_info)
bool nrn_use_fast_imem
Definition: fast_imem.cpp:19
data_handle< T > transform(data_handle< T > handle, Transform type)
Definition: node.cpp:32
std::vector< std::string > var_names
Definition: nrnreport.hpp:91
std::vector< std::string > mech_names
Definition: nrnreport.hpp:90
std::string file_name
Definition: nrnreport.hpp:43
std::vector< std::pair< std::string, int > > population_info
Definition: nrnreport.hpp:44