NEURON
nrnreport.hpp
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 /**
10  * @file nrnreport.h
11  * @brief interface with libsonata for soma reports
12  */
13 
14 #ifndef _H_NRN_REPORT_
15 #define _H_NRN_REPORT_
16 
17 #include <algorithm>
18 #include <cstdint>
19 #include <iostream>
20 #include <set>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
26 
27 #define REPORT_MAX_NAME_LEN 256
28 #define REPORT_MAX_FILEPATH_LEN 4096
29 
30 namespace coreneuron {
31 
33  // Contains the values of the summation with index == segment_id
34  std::vector<double> summation_ = {};
35  // Map containing the pointers of the currents and its scaling factor for every segment_id
36  std::unordered_map<size_t, std::vector<std::pair<double*, double>>> currents_;
37  // Map containing the list of segment_ids per gid
38  std::unordered_map<int, std::vector<size_t>> gid_segments_;
39 };
40 
41 /// @brief Print to ostream the SummationReport
42 inline std::ostream& operator<<(std::ostream& os, const SummationReport& report) {
43  os << "SummationReport:\n";
44 
45  os << "summation_ (" << report.summation_.size() << "): [";
46  for (size_t i = 0; i < report.summation_.size(); ++i) {
47  os << report.summation_[i];
48  if (i + 1 < report.summation_.size())
49  os << ", ";
50  }
51  os << "]\n";
52 
53  os << "currents_ (" << report.currents_.size() << "):\n";
54  for (const auto& [seg_id, vec]: report.currents_) {
55  os << " " << seg_id << ": [";
56  for (size_t i = 0; i < vec.size(); ++i) {
57  const auto& [ptr, scale] = vec[i];
58  os << "(ptr=" << ptr << ", val=" << (ptr ? *ptr : 0.0) << ", scale=" << scale << ")";
59  if (i + 1 < vec.size())
60  os << ", ";
61  }
62  os << "]\n";
63  }
64 
65  os << "gid_segments_ (" << report.gid_segments_.size() << "):\n";
66  for (const auto& [gid, segs]: report.gid_segments_) {
67  os << " " << gid << ": [";
68  for (size_t i = 0; i < segs.size(); ++i) {
69  os << segs[i];
70  if (i + 1 < segs.size())
71  os << ", ";
72  }
73  os << "]\n";
74  }
75 
76  return os;
77 }
78 
80  // Map containing a SummationReport object per report
81  std::unordered_map<std::string, SummationReport> summation_reports_;
82 };
83 
84 struct SpikesInfo {
85  std::string file_name = "out";
86  std::vector<std::pair<std::string, int>> population_info;
87 };
88 
89 /**
90  * @brief Converts an enum value to its corresponding string representation.
91  *
92  * @tparam EnumT Enum type.
93  * @tparam N Size of the mapping array.
94  * @param e Enum value to convert.
95  * @param mapping A fixed-size array mapping enum values to string views.
96  * @param enum_name Name of the enum type, used for error reporting.
97  * @return The corresponding string representation of the enum value.
98  *
99  * @note Aborts the program if the enum value is not found in the mapping.
100  */
101 template <typename EnumT, std::size_t N>
102 std::string to_string(EnumT e,
103  const std::array<std::pair<EnumT, std::string_view>, N>& mapping,
104  const std::string_view enum_name) {
105  auto it = std::find_if(mapping.begin(), mapping.end(), [e](const auto& pair) {
106  return pair.first == e;
107  });
108  if (it != mapping.end()) {
109  return std::string(it->second);
110  }
111 
112  std::cerr << "Unknown value for " << enum_name << ": " << static_cast<int>(e) << std::endl;
113  nrn_abort(1);
114 }
115 
116 /**
117  * @brief Compares two strings for equality, ignoring case.
118  *
119  * @param a First string.
120  * @param b Second string.
121  * @return true if both strings are equal ignoring case, false otherwise.
122  */
123 inline bool equals_case_insensitive(std::string_view a, std::string_view b) {
124  if (a.size() != b.size())
125  return false;
126  return std::equal(a.begin(), a.end(), b.begin(), [](unsigned char c1, unsigned char c2) {
127  return std::tolower(c1) == std::tolower(c2);
128  });
129 }
130 
131 /**
132  * @brief Converts a string to its corresponding enum value, case-insensitively.
133  *
134  * @tparam EnumT Enum type.
135  * @tparam N Size of the mapping array.
136  * @param str Input string to convert.
137  * @param mapping A fixed-size array mapping enum values to string views.
138  * @param enum_name Name of the enum type, used for error reporting.
139  * @param file_path Optional path to the source file providing the string value.
140  * Used to give additional context when reporting errors.
141  * @return The corresponding enum value for the input string.
142  *
143  * @note Aborts the program if the string does not match any entry in the mapping.
144  */
145 template <typename EnumT, std::size_t N>
146 EnumT from_string(const std::string_view str,
147  const std::array<std::pair<EnumT, std::string_view>, N>& mapping,
148  const std::string_view enum_name,
149  const std::string_view file_path) {
150  auto it = std::find_if(mapping.begin(), mapping.end(), [&str](const auto& pair) {
151  return equals_case_insensitive(str, pair.second);
152  });
153  if (it != mapping.end()) {
154  return it->first;
155  }
156 
157  std::cerr << "Unknown string for " << enum_name << ": \"" << str << "\".\n";
158  std::cerr << "Valid options are: ";
159  for (std::size_t i = 0; i < mapping.size(); ++i) {
160  std::cerr << "\"" << mapping[i].second << "\"";
161  if (i + 1 != mapping.size())
162  std::cerr << ", ";
163  }
164  std::cerr << ".\n";
165  if (file_path.size()) {
166  std::cerr << "Probably neuron is not compatible with \"" << file_path << "\".\n";
167  }
168  nrn_abort(1);
169 }
170 
171 // ReportType
173 constexpr std::array<std::pair<ReportType, std::string_view>, 5> report_type_map{
174  {{ReportType::Compartment, "compartment"},
175  {ReportType::CompartmentSet, "compartment_set"},
176  {ReportType::Summation, "summation"},
177  {ReportType::Synapse, "synapse"},
178  {ReportType::LFP, "lfp"}}};
179 inline std::string to_string(ReportType t) {
180  return to_string(t, report_type_map, "ReportType");
181 }
182 inline ReportType report_type_from_string(const std::string_view str,
183  const std::string_view file_path = "") {
184  return from_string<ReportType>(str, report_type_map, "ReportType", file_path);
185 }
186 
187 // SectionType
189 
190 constexpr std::array<std::pair<SectionType, std::string_view>, 11> section_type_map{
191  {{SectionType::Cell, "Cell"},
192  {SectionType::Soma, "Soma"},
193  {SectionType::Axon, "Axon"},
194  {SectionType::Dendrite, "Dend"},
195  {SectionType::Apical, "Apic"},
196  {SectionType::Ais, "Ais"},
197  {SectionType::Node, "Node"},
198  {SectionType::Myelin, "Myelin"},
199  {SectionType::All, "All"},
200  {SectionType::Invalid, "Invalid"}}};
201 
202 inline std::string to_string(SectionType t) {
203  return to_string(t, section_type_map, "SectionType");
204 }
205 inline SectionType section_type_from_string(std::string_view str,
206  const std::string_view file_path = "") {
207  return from_string<SectionType>(str, section_type_map, "SectionType", file_path);
208 }
209 
210 // Scaling
211 enum class Scaling { None, Area };
212 
213 constexpr std::array<std::pair<Scaling, std::string_view>, 2> scaling_map{
214  {{Scaling::None, "None"}, {Scaling::Area, "Area"}}};
215 
216 inline std::string to_string(Scaling s) {
217  return to_string(s, scaling_map, "Scaling");
218 }
219 inline Scaling scaling_from_string(const std::string_view str,
220  const std::string_view file_path = "") {
221  return from_string<Scaling>(str, scaling_map, "Scaling", file_path);
222 }
223 
224 enum class Compartments { All, Center, Invalid };
225 constexpr std::array<std::pair<Compartments, std::string_view>, 3> compartments_map{
226  {{Compartments::All, "All"},
227  {Compartments::Center, "Center"},
228  {Compartments::Invalid, "Invalid"}}};
229 
230 inline std::string to_string(Compartments s) {
231  return to_string(s, compartments_map, "Compartments");
232 }
233 inline Compartments compartments_from_string(const std::string_view str,
234  const std::string_view file_path = "") {
235  return from_string<Compartments>(str, compartments_map, "Compartments", file_path);
236 }
237 
239  std::string name; // name of the report
240  std::string output_path; // full path of the report
241  std::string target_name; // target of the report
242  std::vector<std::string> mech_names; // mechanism names
243  std::vector<std::string> var_names; // variable names
244  std::vector<int> mech_ids; // mechanisms
245  std::string unit; // unit of the report
246  std::string format; // format of the report (SONATA)
247  ReportType type; // type of the report
248  SectionType sections; // type of section report
249  Compartments compartments; // flag for section report (all values)
250  double report_dt; // reporting timestep
251  double start; // start time of report
252  double stop; // stop time of report
253  int num_gids; // total number of gids
254  int buffer_size; // hint on buffer size used for this report
255  std::vector<int> target; // list of gids for this report
256  std::vector<int> point_section_ids; // list of section_ids for this compartment set report
257  // (empty otherwise)
258  std::vector<int> point_compartment_ids; // list of compartment_ids for this compartment set
259  // report (empty otherwise)
261 };
262 
263 void setup_report_engine(double dt_report, double mindelay);
264 std::vector<ReportConfiguration> create_report_configurations(const std::string& filename,
265  const std::string& output_dir,
266  SpikesInfo& spikes_info);
267 void finalize_report();
268 void nrn_flush_reports(double t);
269 void set_report_buffer_size(int n);
270 
271 } // namespace coreneuron
272 
273 #endif //_H_NRN_REPORT_
#define i
Definition: md1redef.h:19
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
constexpr std::array< std::pair< ReportType, std::string_view >, 5 > report_type_map
Definition: nrnreport.hpp:173
void nrn_abort(int errcode)
Definition: utils.cpp:13
Compartments compartments_from_string(const std::string_view str, const std::string_view file_path="")
Definition: nrnreport.hpp:233
Scaling scaling_from_string(const std::string_view str, const std::string_view file_path="")
Definition: nrnreport.hpp:219
constexpr std::array< std::pair< SectionType, std::string_view >, 11 > section_type_map
Definition: nrnreport.hpp:190
constexpr std::array< std::pair< Scaling, std::string_view >, 2 > scaling_map
Definition: nrnreport.hpp:213
void nrn_flush_reports(double t)
Definition: nrnreport.cpp:34
SectionType section_type_from_string(std::string_view str, const std::string_view file_path="")
Definition: nrnreport.hpp:205
void set_report_buffer_size(int n)
Definition: nrnreport.cpp:59
ReportType report_type_from_string(const std::string_view str, const std::string_view file_path="")
Definition: nrnreport.hpp:182
std::vector< ReportConfiguration > create_report_configurations(const std::string &filename, const std::string &output_dir, SpikesInfo &spikes_info)
void finalize_report()
Definition: nrnreport.cpp:66
void setup_report_engine(double dt_report, double mindelay)
in the current implementation, we call flush during every spike exchange interval.
Definition: nrnreport.cpp:48
std::string to_string(EnumT e, const std::array< std::pair< EnumT, std::string_view >, N > &mapping, const std::string_view enum_name)
Converts an enum value to its corresponding string representation.
Definition: nrnreport.hpp:102
bool equals_case_insensitive(std::string_view a, std::string_view b)
Compares two strings for equality, ignoring case.
Definition: nrnreport.hpp:123
std::ostream & operator<<(std::ostream &os, const corenrn_parameters &corenrn_param)
EnumT from_string(const std::string_view str, const std::array< std::pair< EnumT, std::string_view >, N > &mapping, const std::string_view enum_name, const std::string_view file_path)
Converts a string to its corresponding enum value, case-insensitively.
Definition: nrnreport.hpp:146
constexpr std::array< std::pair< Compartments, std::string_view >, 3 > compartments_map
Definition: nrnreport.hpp:225
int const size_t const size_t n
Definition: nrngsl.h:10
s
Definition: multisend.cpp:521
static int equal(const char *c1, const char *c2)
Definition: units.cpp:776
std::vector< std::string > var_names
Definition: nrnreport.hpp:243
std::vector< int > point_compartment_ids
Definition: nrnreport.hpp:258
std::vector< std::string > mech_names
Definition: nrnreport.hpp:242
std::vector< int > point_section_ids
Definition: nrnreport.hpp:256
std::vector< int > mech_ids
Definition: nrnreport.hpp:244
std::string file_name
Definition: nrnreport.hpp:85
std::vector< std::pair< std::string, int > > population_info
Definition: nrnreport.hpp:86
std::vector< double > summation_
Definition: nrnreport.hpp:34
std::unordered_map< size_t, std::vector< std::pair< double *, double > > > currents_
Definition: nrnreport.hpp:36
std::unordered_map< int, std::vector< size_t > > gid_segments_
Definition: nrnreport.hpp:38
std::unordered_map< std::string, SummationReport > summation_reports_
Definition: nrnreport.hpp:81