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 
57 void fill_vec_int(std::ifstream& ss, std::vector<int>& v, const int n) {
58  v.resize(n);
59  ss.read(reinterpret_cast<char*>(v.data()), n * sizeof(int));
60  // extra new line: skip
61  ss.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
62 }
63 
64 std::vector<ReportConfiguration> create_report_configurations(const std::string& conf_file,
65  const std::string& output_dir,
66  SpikesInfo& spikes_info) {
67  std::string token;
68  int target;
69  std::ifstream report_conf(conf_file);
70 
71  int num_reports = 0;
72  report_conf >> num_reports;
73  std::vector<ReportConfiguration> reports(num_reports);
74  for (auto& report: reports) {
75  report_conf >> report.name >> report.target_name;
76  report.output_path = output_dir + "/" + report.name;
77  // type
78  report_conf >> token;
79  report.type = report_type_from_string(token);
80  // report_on
81  report_conf >> token;
82  if (report.type == ReportType::Synapse || report.type == ReportType::Summation ||
83  report.type == ReportType::Compartment || report.type == ReportType::CompartmentSet) {
84  parse_filter_string(token, report);
85  }
86  report_conf >> report.unit >> report.format;
87  // sections
88  report_conf >> token;
89  report.sections = section_type_from_string(token);
90  // compartments
91  report_conf >> token;
92  report.compartments = compartments_from_string(token);
93 
94  // doubles
95  report_conf >> report.report_dt >> report.start >> report.stop >> report.num_gids >>
96  report.buffer_size;
97  // scaling
98  report_conf >> token;
99  report.scaling = scaling_from_string(token);
100 
101  if (report.type == ReportType::LFP) {
102  nrn_use_fast_imem = true;
103  }
104 
105  // checks
106  if (report.type == ReportType::Compartment || report.type == ReportType::CompartmentSet) {
107  nrn_assert(report.mech_ids.empty());
108  nrn_assert(report.mech_names.size() == 1);
109  nrn_assert(report.var_names.size() == 1);
110  }
111 
112  report_conf.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
113  // gids
114  if (report.num_gids) {
115  fill_vec_int(report_conf, report.target, report.num_gids);
116  if (report.type == ReportType::CompartmentSet) {
117  fill_vec_int(report_conf, report.point_section_ids, report.num_gids);
118  fill_vec_int(report_conf, report.point_compartment_ids, report.num_gids);
119  }
120  }
121  }
122  // read population information for spike report
123  int num_populations;
124  std::string spikes_population_name;
125  int spikes_population_offset;
126  if (report_conf.peek() == '\n') {
127  // skip newline and move forward to spike reports
128  report_conf.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
129  }
130  if (isdigit(report_conf.peek())) {
131  report_conf >> num_populations;
132  } else {
133  // support old format: one single line "All"
134  num_populations = 1;
135  }
136  for (int i = 0; i < num_populations; i++) {
137  if (!(report_conf >> spikes_population_name >> spikes_population_offset)) {
138  // support old format: one single line "All"
139  report_conf >> spikes_population_name;
140  spikes_population_offset = 0;
141  }
142  spikes_info.population_info.emplace_back(
143  std::make_pair(spikes_population_name, spikes_population_offset));
144  }
145  report_conf >> spikes_info.file_name;
146 
147  return reports;
148 }
149 } // namespace coreneuron
#define v
Definition: md1redef.h:11
#define i
Definition: md1redef.h:19
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
Compartments compartments_from_string(const std::string &str)
Definition: nrnreport.hpp:217
static const char * mechanism[]
Definition: capac.cpp:19
void fill_vec_int(std::ifstream &ss, std::vector< int > &v, const int n)
Scaling scaling_from_string(const std::string &str)
Definition: nrnreport.hpp:204
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
SectionType section_type_from_string(std::string_view str)
Definition: nrnreport.hpp:191
ReportType report_type_from_string(const std::string &s)
Definition: nrnreport.hpp:169
#define nrn_assert(x)
assert()-like macro, independent of NDEBUG status
Definition: nrn_assert.h:33
int const size_t const size_t n
Definition: nrngsl.h:10
std::vector< std::string > var_names
Definition: nrnreport.hpp:226
std::vector< std::string > mech_names
Definition: nrnreport.hpp:225
std::string file_name
Definition: nrnreport.hpp:85
std::vector< std::pair< std::string, int > > population_info
Definition: nrnreport.hpp:86