NEURON
nrnsection_mapping.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 #pragma once
10 
11 #include <numeric>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 #include <map>
16 #include <iostream>
17 #include <unordered_map>
18 
19 namespace coreneuron {
20 
21 /** type to store every section and associated segments */
22 using segvec_type = std::vector<int>;
23 using secseg_map_type = std::map<int, segvec_type>;
24 using secseg_it_type = secseg_map_type::iterator;
25 
26 /** @brief Section to segment mapping
27  *
28  * For a section list (of a particulat type), store mapping
29  * of section to segments
30  * a section is a arbitrary user classification to recognize some segments (ex: api, soma, dend,
31  * axon)
32  *
33  */
34 struct SecMapping {
35  /** name of section list */
36  std::string name;
37 
38  /** map of section and associated segments */
40 
41  SecMapping() = default;
42 
43  explicit SecMapping(std::string s)
44  : name(std::move(s)) {}
45 
46  /** @brief return total number of sections in section list */
47  size_t num_sections() const noexcept {
48  return secmap.size();
49  }
50 
51  /** @brief return number of segments in section list */
52  size_t num_segments() const {
53  return std::accumulate(secmap.begin(), secmap.end(), 0, [](int psum, const auto& item) {
54  return psum + item.second.size();
55  });
56  }
57 
58  /** @brief add section to associated segment */
59  void add_segment(int sec, int seg) {
60  secmap[sec].push_back(seg);
61  }
62 };
63 
64 /** @brief Compartment mapping information for a cell
65  *
66  * A cell can have multiple section list types like
67  * soma, axon, apic, dend etc. User will add these
68  * section lists using HOC interface.
69  */
70 struct CellMapping {
71  /** gid of a cell */
72  int gid;
73 
74  /** list of section lists (like soma, axon, apic) */
75  std::vector<SecMapping*> secmapvec;
76 
77  /** map containing segment ids an its respective lfp factors */
78  std::unordered_map<int, std::vector<double>> lfp_factors;
79 
80  CellMapping(int g)
81  : gid(g) {}
82 
83  /** @brief total number of sections in a cell */
84  int num_sections() const {
85  return std::accumulate(secmapvec.begin(),
86  secmapvec.end(),
87  0,
88  [](int psum, const auto& secmap) {
89  return psum + secmap->num_sections();
90  });
91  }
92 
93  /** @brief return number of segments in a cell */
94  int num_segments() const {
95  return std::accumulate(secmapvec.begin(),
96  secmapvec.end(),
97  0,
98  [](int psum, const auto& secmap) {
99  return psum + secmap->num_segments();
100  });
101  }
102 
103  /** @brief return the number of electrodes in the lfp_factors map **/
104  int num_electrodes() const {
105  int num_electrodes = 0;
106  if (!lfp_factors.empty()) {
107  num_electrodes = lfp_factors.begin()->second.size();
108  }
109  return num_electrodes;
110  }
111 
112  /** @brief number of section lists */
113  size_t size() const noexcept {
114  return secmapvec.size();
115  }
116 
117  /** @brief add new SecMapping */
119  secmapvec.push_back(s);
120  }
121 
122  /** @brief return section list mapping with given name */
123  SecMapping* get_seclist_mapping(const std::string& name) const {
124  for (auto& secmap: secmapvec) {
125  if (name == secmap->name) {
126  return secmap;
127  }
128  }
129 
130  std::cout << "Warning: Section mapping list " << name << " doesn't exist! \n";
131  return nullptr;
132  }
133 
134  /** @brief return segment count for specific section list with given name */
135  size_t get_seclist_segment_count(const std::string& name) const {
137  size_t count = 0;
138  if (s) {
139  count = s->num_segments();
140  }
141  return count;
142  }
143  /** @brief return segment count for specific section list with given name */
144  size_t get_seclist_section_count(const std::string& name) const {
146  size_t count = 0;
147  if (s) {
148  count = s->num_sections();
149  }
150  return count;
151  }
152 
153  /** @brief add the lfp electrode factors of a segment_id */
154  void add_segment_lfp_factor(const int segment_id, std::vector<double>& factors) {
155  lfp_factors.insert({segment_id, factors});
156  }
157 
159  for (size_t i = 0; i < secmapvec.size(); i++) {
160  delete secmapvec[i];
161  }
162  }
163 };
164 
165 /** @brief Compartment mapping information for NrnThread
166  *
167  * NrnThread could have more than one cell in cellgroup
168  * and we store this in vector.
169  */
171  /** list of cells mapping */
172  std::vector<CellMapping*> mappingvec;
173 
174  /** list of segment ids */
175  std::vector<int> segment_ids;
176 
177  std::vector<double> _lfp;
178 
179  /** @brief number of cells */
180  size_t size() const {
181  return mappingvec.size();
182  }
183 
184  /** @brief memory cleanup */
186  for (size_t i = 0; i < mappingvec.size(); i++) {
187  delete mappingvec[i];
188  }
189  }
190 
191  /** @brief get cell mapping information for given gid
192  * if exist otherwise return nullptr.
193  */
194  CellMapping* get_cell_mapping(int gid) const {
195  for (const auto& mapping: mappingvec) {
196  if (mapping->gid == gid) {
197  return mapping;
198  }
199  }
200  return nullptr;
201  }
202 
203  /** @brief add mapping information of new cell */
205  mappingvec.push_back(c);
206  }
207 
208  /** @brief add a new segment */
209  void add_segment_id(const int segment_id) {
210  segment_ids.push_back(segment_id);
211  }
212 
213  /** @brief Resize the lfp vector */
214  void prepare_lfp() {
215  size_t lfp_size = std::accumulate(mappingvec.begin(),
216  mappingvec.end(),
217  0,
218  [](size_t total, const auto& mapping) {
219  return total + mapping->num_electrodes();
220  });
221  _lfp.resize(lfp_size);
222  }
223 };
224 } // namespace coreneuron
#define sec
Definition: md1redef.h:20
#define i
Definition: md1redef.h:19
static int c
Definition: hoc.cpp:169
const char * name
Definition: init.cpp:16
void move(Item *q1, Item *q2, Item *q3)
Definition: list.cpp:200
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
secseg_map_type::iterator secseg_it_type
std::map< int, segvec_type > secseg_map_type
std::vector< int > segvec_type
type to store every section and associated segments
s
Definition: multisend.cpp:521
Compartment mapping information for a cell.
SecMapping * get_seclist_mapping(const std::string &name) const
return section list mapping with given name
size_t get_seclist_section_count(const std::string &name) const
return segment count for specific section list with given name
void add_segment_lfp_factor(const int segment_id, std::vector< double > &factors)
add the lfp electrode factors of a segment_id
void add_sec_map(SecMapping *s)
add new SecMapping
std::vector< SecMapping * > secmapvec
list of section lists (like soma, axon, apic)
int num_segments() const
return number of segments in a cell
std::unordered_map< int, std::vector< double > > lfp_factors
map containing segment ids an its respective lfp factors
int num_sections() const
total number of sections in a cell
size_t get_seclist_segment_count(const std::string &name) const
return segment count for specific section list with given name
size_t size() const noexcept
number of section lists
int num_electrodes() const
return the number of electrodes in the lfp_factors map
Compartment mapping information for NrnThread.
size_t size() const
number of cells
CellMapping * get_cell_mapping(int gid) const
get cell mapping information for given gid if exist otherwise return nullptr.
std::vector< CellMapping * > mappingvec
list of cells mapping
std::vector< int > segment_ids
list of segment ids
void add_cell_mapping(CellMapping *c)
add mapping information of new cell
void add_segment_id(const int segment_id)
add a new segment
void prepare_lfp()
Resize the lfp vector.
Section to segment mapping.
void add_segment(int sec, int seg)
add section to associated segment
size_t num_sections() const noexcept
return total number of sections in section list
std::string name
name of section list
secseg_map_type secmap
map of section and associated segments
size_t num_segments() const
return number of segments in section list