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  * @return The corresponding enum value for the input string.
140  *
141  * @note Aborts the program if the string does not match any entry in the mapping.
142  */
143 template <typename EnumT, std::size_t N>
144 EnumT from_string(std::string_view str,
145  const std::array<std::pair<EnumT, std::string_view>, N>& mapping,
146  const std::string_view enum_name) {
147  auto it = std::find_if(mapping.begin(), mapping.end(), [&str](const auto& pair) {
148  return equals_case_insensitive(str, pair.second);
149  });
150  if (it != mapping.end()) {
151  return it->first;
152  }
153 
154  std::cerr << "Unknown string for " << enum_name << ": " << str << std::endl;
155  nrn_abort(1);
156 }
157 
158 // ReportType
160 constexpr std::array<std::pair<ReportType, std::string_view>, 5> report_type_map{
161  {{ReportType::Compartment, "compartment"},
162  {ReportType::CompartmentSet, "compartment_set"},
163  {ReportType::Summation, "summation"},
164  {ReportType::Synapse, "synapse"},
165  {ReportType::LFP, "lfp"}}};
166 inline std::string to_string(ReportType t) {
167  return to_string(t, report_type_map, "ReportType");
168 }
169 inline ReportType report_type_from_string(const std::string& s) {
170  return from_string<ReportType>(s, report_type_map, "ReportType");
171 }
172 
173 // SectionType
175 
176 constexpr std::array<std::pair<SectionType, std::string_view>, 11> section_type_map{
177  {{SectionType::Cell, "Cell"},
178  {SectionType::Soma, "Soma"},
179  {SectionType::Axon, "Axon"},
180  {SectionType::Dendrite, "Dend"},
181  {SectionType::Apical, "Apic"},
182  {SectionType::Ais, "Ais"},
183  {SectionType::Node, "Node"},
184  {SectionType::Myelin, "Myelin"},
185  {SectionType::All, "All"},
186  {SectionType::Invalid, "Invalid"}}};
187 
188 inline std::string to_string(SectionType t) {
189  return to_string(t, section_type_map, "SectionType");
190 }
191 inline SectionType section_type_from_string(std::string_view str) {
192  return from_string<SectionType>(str, section_type_map, "SectionType");
193 }
194 
195 // Scaling
196 enum class Scaling { None, Area };
197 
198 constexpr std::array<std::pair<Scaling, std::string_view>, 2> scaling_map{
199  {{Scaling::None, "None"}, {Scaling::Area, "Area"}}};
200 
201 inline std::string to_string(Scaling s) {
202  return to_string(s, scaling_map, "Scaling");
203 }
204 inline Scaling scaling_from_string(const std::string& str) {
205  return from_string<Scaling>(str, scaling_map, "Scaling");
206 }
207 
208 enum class Compartments { All, Center, Invalid };
209 constexpr std::array<std::pair<Compartments, std::string_view>, 3> compartments_map{
210  {{Compartments::All, "All"},
211  {Compartments::Center, "Center"},
212  {Compartments::Invalid, "Invalid"}}};
213 
214 inline std::string to_string(Compartments s) {
215  return to_string(s, compartments_map, "Compartments");
216 }
217 inline Compartments compartments_from_string(const std::string& str) {
218  return from_string<Compartments>(str, compartments_map, "Compartments");
219 }
220 
222  std::string name; // name of the report
223  std::string output_path; // full path of the report
224  std::string target_name; // target of the report
225  std::vector<std::string> mech_names; // mechanism names
226  std::vector<std::string> var_names; // variable names
227  std::vector<int> mech_ids; // mechanisms
228  std::string unit; // unit of the report
229  std::string format; // format of the report (SONATA)
230  ReportType type; // type of the report
231  SectionType sections; // type of section report
232  Compartments compartments; // flag for section report (all values)
233  double report_dt; // reporting timestep
234  double start; // start time of report
235  double stop; // stop time of report
236  int num_gids; // total number of gids
237  int buffer_size; // hint on buffer size used for this report
238  std::vector<int> target; // list of gids for this report
239  std::vector<int> point_section_ids; // list of section_ids for this compartment set report
240  // (empty otherwise)
241  std::vector<int> point_compartment_ids; // list of compartment_ids for this compartment set
242  // report (empty otherwise)
244 };
245 
246 void setup_report_engine(double dt_report, double mindelay);
247 std::vector<ReportConfiguration> create_report_configurations(const std::string& filename,
248  const std::string& output_dir,
249  SpikesInfo& spikes_info);
250 void finalize_report();
251 void nrn_flush_reports(double t);
252 void set_report_buffer_size(int n);
253 
254 } // namespace coreneuron
255 
256 #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:160
EnumT from_string(std::string_view str, const std::array< std::pair< EnumT, std::string_view >, N > &mapping, const std::string_view enum_name)
Converts a string to its corresponding enum value, case-insensitively.
Definition: nrnreport.hpp:144
void nrn_abort(int errcode)
Definition: utils.cpp:13
Compartments compartments_from_string(const std::string &str)
Definition: nrnreport.hpp:217
constexpr std::array< std::pair< SectionType, std::string_view >, 11 > section_type_map
Definition: nrnreport.hpp:176
Scaling scaling_from_string(const std::string &str)
Definition: nrnreport.hpp:204
constexpr std::array< std::pair< Scaling, std::string_view >, 2 > scaling_map
Definition: nrnreport.hpp:198
void nrn_flush_reports(double t)
Definition: nrnreport.cpp:34
void set_report_buffer_size(int n)
Definition: nrnreport.cpp:59
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
SectionType section_type_from_string(std::string_view str)
Definition: nrnreport.hpp:191
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
ReportType report_type_from_string(const std::string &s)
Definition: nrnreport.hpp:169
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)
constexpr std::array< std::pair< Compartments, std::string_view >, 3 > compartments_map
Definition: nrnreport.hpp:209
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:226
std::vector< int > point_compartment_ids
Definition: nrnreport.hpp:241
std::vector< std::string > mech_names
Definition: nrnreport.hpp:225
std::vector< int > point_section_ids
Definition: nrnreport.hpp:239
std::vector< int > mech_ids
Definition: nrnreport.hpp:227
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