summaryrefslogtreecommitdiff
path: root/network.hh
blob: 14ad5c314ba21f3adfb807cf9bbbd37910bebf77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
  © Copyright 2008 Randal A. Koene <randalk@netmorph.org>
  
  With design assistance from J. van Pelt & A. van Ooyen, and support
  from the Netherlands Organization for Scientific Research (NWO)
  Program Computational Life Sciences grant CLS2003 (635.100.005) and
  from the EC Marie Curie Research and Training Network (RTN)
  NEURoVERS-it 019247.

  This file is part of NETMORPH.

  NETMORPH is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  NETMORPH is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with NETMORPH.  If not, see <http://www.gnu.org/licenses/>.
*/
// network.hh
// Randal A. Koene, 20041118
//
// Classes that define neuronal networks and related functions.

#ifndef __NETWORK_HH
#include "templates.hh"
#include "Network_Statistics.hh"
#include "Spatial_Segment_Subset.hh"
#include "Command_Line_Parameters.hh"
#include "Results.hh"
#include "Connection_Statistics.hh"
#include "dendritic_growth_model.hh"
#include "slice.hh"
#include "spatial.hh"
#include "event.hh"
#include "state_storable.hh"
#include "Txt_Object.hh"
#include "VRML_Object.hh"
#include "Catacomb_Object.hh"
#define __NETWORK_HH

// variables

extern bool sampling; // sampling flag to distinguish callers (should be set and reset by caller)
extern bool numneurons_is_default; // flag to indicate that numneurons was set by a fallback to default 

// classes

class network_statistics_data;

#ifndef __NETWORK_GENERATED_STATISTICS_HH
class network_statistics_base;
#endif
#ifndef __NEURITE_DIAMETER_MODEL_HH
class Neurite_Diameter_Model;
#endif

class region: public PLLHandle<region> {
  // A region object is an arbitrary collection of neurons. The network can
  // assign any number of regions, in accordance with groupings established,
  // for example during neuron placement.
protected:
  String name;
  PLLRoot<neuronptrlist> nlist;
public:
  region(String _name): name(_name) {}
  region(String _name, neuron & n): name(_name) { nlist.link_before(new neuronptrlist(&n)); }
  String Name() { return name; }
  PLLRoot<neuronptrlist> & Nlist() { return nlist; }
  region & append(neuron & n) { nlist.link_before(new neuronptrlist(&n)); return *this; }
  region & operator+=(neuron & n) { return append(n); }
  region & append(String _name, neuron & n);
  region & conditional_create(String _name, neuron & n);
  int find(neuron & n);
  spatial center();
  Catacomb_Group * net_Catacomb();
};

class regionslist: public PLLRoot<region> {
public:
  regionslist() {}
  regionslist(String _name, neuron & n) {
    link_before(new region(_name,n));
  }
  region & append(String _name, neuron & n) {
    if (head()) return head()->conditional_create(_name,n);
    link_before(new region(_name,n));
    return *head();
  }
  int find(neuron & n);
};

class network: public PLLRoot<neuron>, public Event_Queue {
protected:
  String netinfo;
  bool edges; // if false, functional edge effects are avoided by looping connections around, e.g. turning a rectangle into a torus
  bool candidate_synapses;
  bool synapses_during_development;
  spatial center;
  neuron * paramspreadmin, * paramspreadmax; // samples used to determine the spread
  Network_Statistics_Root * nsr;
  Spatial_Segment_Subset * sss; // used within develop_connection_structure()
  Neurite_Diameter_Model * ndm;
  regionslist regions;
  void add_typed_neuron(neuron_type nt, neuron * psmin, neuron * psmax);
  void remove_abstract_connections_without_synapses();
public:
  network(): Event_Queue(this), edges(true), candidate_synapses(true), synapses_during_development(true), sss(NULL), ndm(NULL) {}
  network(int numneurons, bool e = true); // (see nibr.cc)
  network(int numneurons, double principalprobability, bool e = true); // (see nibr.cc)
  network(int numneurons, neuron * psmin, neuron * psmax, double principalprobability, bool e = true); // (see nibr.cc)
  network(int numneurons, neuron * psmin, neuron * psmax, Network_Statistics_Root & netstats, bool _edges = true); // (see nibr.cc)
  virtual ~network() { delete sss; }
  regionslist & Regions() { return regions; }
  spatial & Center() { return center; }
  double Time() { return t; }
  bool Seek_Candidate_Synapses() { return candidate_synapses; }
  Shape_Hexagon_Result shape_hexagon(Command_Line_Parameters & clp);
  Shape_Rectangle_Result shape_rectangle(Command_Line_Parameters & clp);
  Shape_Circle_Result shape_circle(Command_Line_Parameters & clp);
#ifdef VECTOR3D
  Shape_Box_Result shape_box(Command_Line_Parameters & clp);
  Shape_Regions_Result shape_regions(Command_Line_Parameters & clp, neuron * psmin = NULL, neuron * psmax = NULL);
#endif
  Shape_Result shape_network(Command_Line_Parameters & clp, neuron * psmin = NULL, neuron * psmax = NULL);
  neuron * nearest(spatial & p);
  neuronset * inrange(neuron * n, double radius, neuron_type ntype = UNTYPED_NEURON); // (see nibr.cc)
  void uniform_random_connectivity(double range, int minconn, int maxconn, neuron_type sourcetype = UNTYPED_NEURON, neuron_type targettype = UNTYPED_NEURON, bool clearexisting = true); // (see nibr.cc)
  void develop_connection_structure(Connection_Statistics_Root & cstats, dendritic_growth_model & dgm, dendritic_growth_model & agm); // (see nibr.cc)
  Spatial_Segment_Subset * spatial_segment_subset() { return sss; }
  void spatial_extents(spatial & minpoint, spatial & maxpoint);
  void initialize_spatial_segment_subset(Connection_Statistics_Root & cstats, dendritic_growth_model & dgm, dendritic_growth_model & agm);
  int number_of_connections(); // (see nibr.cc)
  int number_of_synapses(); // (see nibr.cc)
#ifdef SAMPLES_INCLUDE_NETWORK_STATISTICS_BASE
  void Collect_Data(network_statistics_base & nsb);
#endif
  double mean_number_of_output_terminal_segments(network_statistics_data * nsd = NULL);
  double mean_number_of_input_terminal_segments(network_statistics_data * nsd = NULL);
  double mean_presynaptic_structure_length(network_statistics_data & termnsd, network_statistics_data & nsd, network_statistics_data * internsd = NULL);
  double mean_postsynaptic_structure_length(network_statistics_data & termnsd, network_statistics_data & nsd, network_statistics_data * internsd = NULL);
  void move(spatial & pos); // move all neurons in the network to center their soma on pos
  void fanin_rot(); // in spherical coordinates, rotate all points to theta=0
  void abstract_connections();
  Fig_Group * abstract_connections_Fig();
  Fig_Group * Fig_Abstract_Connections(String figname, double width = 0.0, bool overwrite = false);
  Fig_Group * net_Fig(); // (see nibr.cc)
  Fig_Text * time_Fig(Fig_Object & rfo);
  Fig_Group * progress_bar_Fig(); // (see nibr.cc)
  static Fig_Group * scale_bar_Fig(Fig_Object & rfo);
  static Fig_Group * scale_bar_Fig(long xoffset, long yoffset);
  static Fig_Group * axis_arrows_Fig(Fig_Object & rfo);
  Fig_Group * Fig_Output(String figname, double width = 0.0, bool overwrite = false); // (see nibr.cc)
  Fig_Group * Fig_Neurons(String figname, double width = 0.0);
  Txt_Group * net_Txt();
  bool Txt_Output(String txtname);
  bool Data_Output_Synapse_Distance(String dataname);
  bool Data_Output_Connection_Distance(String dataname);
  VRML_Group * net_VRML();
  bool VRML_Output(String ac3dname);
  Catacomb_Group * net_Catacomb();
  bool Catacomb_Output(String ccmname);
#ifdef VECTOR3D
  void net_Slice(Slice & slice);
  bool Slice_Output(String slicename, Slice & slice);
  Fig_Group * Slice_Outlines_Output(String figname, Slice & slice, double width = 0.0, bool overwrite = false);
#endif
  void set_figattr(int fattr); // (see nibr.cc)
  void visible_pre_and_target_post(neuron & n); // (see nibr.cc)
  virtual void parse_CLP(Command_Line_Parameters & clp);
  virtual String report_parameters();
};

class electrodearray: public network {
public:
  electrodearray() {}
  electrodearray(int numelectrodes) {
    netinfo = "initialized with "+String((long) numelectrodes)+" electrodes\n";
    for (int i=0; i<numelectrodes; i++) PLLRoot<neuron>::link_before(new electrode());
  }
  void set_center(const spatial & c) { center = c; }
};

class connectivity_graph {
protected:
  network * net;
public:
  connectivity_graph(network & _n);
  ~connectivity_graph() { delete[] X; delete[] Y; delete[] R; delete[] n; }
  int N;
  long *X, *Y, *R;
  long graph_R;
  neuronptr * n; // [***NOTE] Or I could use the neuronset class.
  double maxstrength;
  int find(neuron * m);
};

#ifdef VECTOR3D

class shaped_volume: public CLP_Modifiable {
protected:
  String * label;
public:
  shaped_volume(String * l = NULL): label(l) {}
  virtual ~shaped_volume() {}
  virtual String str() = 0;
  virtual double displaywidth() = 0;
  virtual spatial random_location() = 0;
};

class disc_shaped_volume: public shaped_volume {
protected:
  spatial center;
  double radius;
  double thickness;
public:
  disc_shaped_volume(String * l = NULL): shaped_volume(l), radius(1.0), thickness(1.0) {}
  virtual String str() { return String("disc"); };
  virtual void parse_CLP(Command_Line_Parameters & clp);
  virtual String report_parameters();
  virtual double displaywidth() { return 2.0*radius; }
  virtual spatial random_location();
};

class box_shaped_volume: public shaped_volume {
protected:
  spatial center;
  double height;
  double width;
  double depth;
public:
  box_shaped_volume(String * l = NULL): shaped_volume(l), height(1.0), width(1.0), depth(1.0) {}
  virtual String str() { return String("box"); };
  virtual void parse_CLP(Command_Line_Parameters & clp);
  virtual String report_parameters();
  virtual double displaywidth() {
    if (height>width) {
      if (height>depth) return height;
      return depth;
    } else {
      if (width>depth) return width;
      return depth;
    }
  }
  virtual spatial random_location();
};

class sphere_shaped_volume: public shaped_volume {
protected:
  spatial center;
  double radius;
public:
  sphere_shaped_volume(String * l = NULL): shaped_volume(l), radius(1.0) {}
  virtual String str() { return String("sphere"); };
  virtual void parse_CLP(Command_Line_Parameters & clp);
  virtual String report_parameters();
  virtual double displaywidth() { return 2.0*radius; }
  virtual spatial random_location();
};

class region_parameters: public PLLHandle<region_parameters> {
protected:
  String label;
  shaped_volume * sv;
  neuronptr * memberneurons;
  int nummemberneurons; // only those from the general pool
  int generalandspecificneurons; // including those specifically defined for the region
  double minneuronseparation;
  int specificneurons[UNTYPED_NEURON];
public:
  region_parameters(const char * l, Command_Line_Parameters & clp);
  ~region_parameters() { delete sv; delete[] memberneurons; }
  String Label() { return label; }
  shaped_volume & shape() { return *sv; }
  virtual String report_parameters();
  neuron * add_neurons(PLLRoot<neuron> * all, neuron * n, neuron * psmin = NULL, neuron * psmax = NULL);
  neuronptr * neurons() { return memberneurons; }
  int N() { return generalandspecificneurons; }
  int general_pool_N() { return nummemberneurons; }
  int specific_N() { return generalandspecificneurons-nummemberneurons; }
  void set_general_pool(unsigned int num) {
    generalandspecificneurons -= nummemberneurons;
    nummemberneurons = num;
    generalandspecificneurons += nummemberneurons;
  }
  double average_distance_to_nearest_neighbor();
};

class region_parameters_root: public PLLRoot<region_parameters>, public CLP_Modifiable {
protected:
  int numregions;
public:
  region_parameters_root(): numregions(0) {}
  virtual ~region_parameters_root() {}
  virtual void parse_CLP(Command_Line_Parameters & clp);
  virtual String report_parameters();
  int total_N() {
    int num = 0;
    PLL_LOOP_FORWARD(region_parameters,head(),1) num += e->N();
    return num;
  }
  int total_general_pool_N() {
    int num = 0;
    PLL_LOOP_FORWARD(region_parameters,head(),1) num += e->general_pool_N();
    return num;
  }
  int total_specific_N() {
    int num = 0;
    PLL_LOOP_FORWARD(region_parameters,head(),1) num += e->specific_N();
    return num;
  }
};

#endif

#endif