Debian Codegraph @

examples/ei_networks/pointnet/build_network.py

raw
1 import os
2 import numpy as np
3
4 from bmtk.builder import NetworkBuilder
5 from bmtk.utils.io.spike_trains import PoissonSpikesGenerator
6 from bmtk.builder.auxi.node_params import positions_columinar, xiter_random
7
8 # Brunel network parameters -------------------------------------------------------------------------
9 N_LIF_exc = 10000 # number of excitatory LIF neurons 80%
10 N_LIF_inh = 2500 # number of inhibitory LIF neurons 20%
11 n_cells = N_LIF_exc + N_LIF_inh # total number of internal neurons
12 N_ext = 100 # number of neurons in the external network
13
14 JE = 0.2 # excitatory synaptic strength (mV)
15 g = 2.5 # ratio of inhibitory synpatic strength to excitatory synaptic strength (unitless)*************
16 JI = -g * JE # inhibitory synpatic strength (mv)
17 eps = 0.1 # percentage of all possible target neurons that are connected to any given source
18 CE = int(eps * N_LIF_exc) # number of internal connections with excitatory source neurons
19 C_ext = CE # number of connections to each internal cell from the input network
20 n_syn = int(C_ext / N_ext) # number of synapses per neuron in the input network
21 CI = int(eps * N_LIF_inh) # number of internal connections with inhibitory source neurons
22 C = CE + CI # number of internal connections
23 tau_e = 20. # membrane time constant of excitatory neurons (ms)
24 theta = 20. # neuron firing threshold (mV)
25 Vr = 10. # neuron rest potential (mV)
26 tau_rp = 2. # refractory period (ms)
27 D = 1.5 # transmission delay (ms)
28 v_ext_v_thr_ratio = 10. # ratio of v_ext to v_thr (unitless)***************
29 eps_ext = C_ext / float(n_cells) # external connection probability
30 v_thr = theta / (JE * CE * tau_e) # frequency needed for a neuron to reach threshold in absence of feedback
31 v_ext = v_ext_v_thr_ratio * v_thr # external firing frequency
32
33 # -----------------------------------------------------------------------------------------------
34 def generate_random_positions(N):
35 x = np.random.random(N) # x-axis location
36 y = np.random.random(N) # y-axis location
37 z = np.random.random(N) # z-axis location
38
39 positions = np.column_stack((x, y, z))
40 return positions
41
42
43 def random_connections(source, target, p=0.1):
44 sid = source['node_id'] # Get source id
45 tid = target['node_id'] # Get target id
46
47 # Avoid self-connections.
48 if sid == tid:
49 if sid % 1000 == 0:
50 print(sid)
51 return None
52
53 # print np.random.binomial(1, p)
54 return np.random.binomial(1, p) # nsyns
55
56
57 def build_lif_network():
58 ### Define all the cell models in a dictionary.
59 print('Building Internal Network')
60
61 LIF_models = {
62 'LIF_exc': {
63 'N': N_LIF_exc,
64 'ei': 'e',
65 'pop_name': 'LIF_exc',
66 'model_type': 'point_process',
67 'model_template': 'nest:iaf_psc_exp',
68 'dynamics_params': 'excitatory.json'
69 },
70 'LIF_inh': {
71 'N': N_LIF_inh,
72 'ei': 'i',
73 'pop_name': 'LIF_inh',
74 'model_type': 'point_process',
75 'model_template': 'nest:iaf_psc_exp',
76 'dynamics_params': 'inhibitory.json'
77 }
78 }
79 net = NetworkBuilder('internal')
80
81 for model in LIF_models:
82 params = LIF_models[model].copy()
83 positions = positions_columinar(N=LIF_models[model]['N'], center=[0, 10.0, 0], max_radius=50.0, height=200.0)
84 net.add_nodes(x=positions[:, 0], y=positions[:, 1], z=positions[:, 2],
85 **params)
86
87 net.add_edges(source={'ei': 'e'},
88 target={'ei': 'e'},
89 connection_rule=random_connections,
90 connection_params={'p': eps},
91 syn_weight=200,
92 delay=D,
93 dynamics_params='ExcToExc.json',
94 model_template='static_synapse')
95
96 net.add_edges(source={'ei': 'e'},
97 target={'ei': 'i'},
98 connection_rule=random_connections,
99 connection_params={'p': eps},
100 syn_weight=300,
101 delay=D,
102 dynamics_params='ExcToInh.json',
103 model_template='static_synapse')
104
105 net.add_edges(source={'ei': 'i'},
106 target={'ei': 'e'},
107 connection_rule=random_connections,
108 connection_params={'p': eps},
109 syn_weight=-550,
110 delay=D,
111 dynamics_params='InhToExc.json',
112 model_template='static_synapse')
113
114 net.add_edges(source={'ei': 'i'},
115 target={'ei': 'i'},
116 connection_rule=random_connections,
117 connection_params={'p': eps},
118 syn_weight=-300,
119 delay=D,
120 dynamics_params='InhToInh.json',
121 model_template='static_synapse')
122
123 net.build()
124 net.save(output_dir='network')
125 # net.save_edges(edges_file_name='edges.h5', edge_types_file_name='edge_types.csv', output_dir='network')
126 # net.save_nodes(nodes_file_name='nodes.h5', node_types_file_name='node_types.csv', output_dir='network')
127 return net
128
129
130 def build_source_network(target_net):
131 input_network_model = {
132 'external': {
133 'N': 1,
134 'ei': 'e',
135 'pop_name': 'input_network',
136 'model_type': 'virtual'
137 }
138 }
139
140 inputNetwork = NetworkBuilder("external")
141 inputNetwork.add_nodes(**input_network_model['external'])
142
143 inputNetwork.add_edges(target=target_net.nodes(pop_name='LIF_exc'),
144 connection_rule=random_connections,
145 connection_params={'p': 0.1},
146 syn_weight=400,
147 delay=D,
148 dynamics_params='ExcToExc.json',
149 model_template='static_synapse')
150
151 inputNetwork.add_edges(target=target_net.nodes(pop_name='LIF_inh'),
152 connection_rule=random_connections,
153 connection_params={'p': 0.1},
154 syn_weight=400,
155 delay=D,
156 dynamics_params='ExcToExc.json',
157 model_template='static_synapse')
158
159 inputNetwork.build()
160 net.save(output_dir='network')
161 #inputNetwork.save_nodes(nodes_file_name='one_input_node.h5', node_types_file_name='one_input_node_type.csv',
162 # output_dir='lif_network')
163 #inputNetwork.save_edges(edges_file_name='one_input_edges.h5', edge_types_file_name='one_input_edge_type.csv',
164 # output_dir='lif_network')
165 return inputNetwork
166
167 def injective_connections(sources, target):
168 if target.node_id % 1000 == 0:
169 print(target.node_id)
170
171 # Since we used the 'all_to_one' iterator "sources" is a list of all input nodes and function returns a list.
172 nsyns = np.zeros(len(sources))
173 nsyns[target.node_id] = 1
174 return nsyns
175
176
177 def build_injective_inputs(target_net):
178 print('Building External Network')
179
180 input_network_model = {
181 'external': {
182 'N': len(target_net.nodes()), # Need one virtual node for every LIF_network node
183 'ei': 'e',
184 'pop_name': 'input_network',
185 'model_type': 'virtual'
186 }
187 }
188
189 inputNetwork = NetworkBuilder("external")
190 inputNetwork.add_nodes(**input_network_model['external'])
191
192 inputNetwork.add_edges(target=target_net.nodes(pop_name='LIF_exc'),
193 connection_rule=injective_connections,
194 iterator='all_to_one', # will make building a little faster
195 syn_weight=200,
196 delay=D,
197 dynamics_params='ExcToExc.json',
198 model_template='static_synapse')
199
200 inputNetwork.add_edges(target=target_net.nodes(pop_name='LIF_inh'),
201 connection_rule=injective_connections,
202 iterator='all_to_one',
203 syn_weight=100,
204 delay=D,
205 dynamics_params='ExcToExc.json',
206 model_template='static_synapse')
207
208 inputNetwork.build()
209 inputNetwork.save(output_dir='network')
210
211
212 def build_spike_trains(N=N_LIF_exc + N_LIF_inh):
213 from bmtk.utils.reports.spike_trains import PoissonSpikeGenerator
214 from bmtk.utils.reports.spike_trains import sort_order
215
216 # Steadily increase the firing rate of each virtual cell from 1.0 Hz (node 0) to 50.0 Hz (node 12499)
217 firing_rates = 500*np.ones(N)
218
219 psg = PoissonSpikeGenerator(population='external')
220 for i in range(N):
221 # create a new spike-train for each node.
222 psg.add(node_ids=i, firing_rate=firing_rates[i], times=(0.0, 3.0))
223
224 psg.to_sonata('inputs/injective_500hz.h5', sort_order=sort_order.by_id)
225
226
227 if __name__ == '__main__':
228 lif_net = build_lif_network()
229
230 # Create a new source network that has 12500 virtual nodes
231 build_injective_inputs(lif_net)
232
233 # create a spikes file that has different firing rates for each virtual node
234 build_spike_trains()
235
236
237