triple_network_hh_Ih_cond_syn.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import copy
  2. import warnings
  3. import brian2 as br
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from brian2.units import *
  7. from brian2 import implementation
  8. from brian2 import network_operation
  9. from brian2 import defaultclock
  10. from scripts.models import hodgkin_huxley_cond_synapse_weak_noise_eqs, exponential_synapse, \
  11. exponential_synapse_on_pre, exponential_synapse_params, eqs_ih, hodgkin_huxley_params, \
  12. ih_params, delta_synapse_model, delta_synapse_on_pre, noise_params
  13. def set_parameters_from_dict(neurongroup, dictionary_of_parameters):
  14. for param_key, param_value in dictionary_of_parameters.items():
  15. try:
  16. neurongroup.__setattr__(param_key, param_value)
  17. except AttributeError as err:
  18. warnings.warn("{:s} has no parameter {:s}".format(neurongroup.name, param_key))
  19. '''
  20. Model for the excitatory neurons
  21. '''
  22. # Hodgkin Huxley model from Brian2 documentation
  23. spike_threshold = 40*mV
  24. excitatory_neuron_options = {
  25. "threshold" : "v > spike_threshold",
  26. "refractory" : "v > spike_threshold",
  27. "method" : 'euler'
  28. }
  29. '''
  30. Model for the interneuron, currently only the inhibition timing is of interest thus the lif model
  31. '''
  32. lif_interneuron_eqs = """
  33. dv/dt =1.0/tau* (-v + u_ext) :volt (unless refractory)
  34. u_ext = u_ext_const : volt
  35. """
  36. lif_interneuron_params = {
  37. "tau": 7*ms,
  38. "v_threshold": -40*mV,
  39. "v_reset": -50*mV,
  40. "tau_refractory": 0.0*ms,
  41. "u_ext_const" : - 41 * mV
  42. }
  43. lif_interneuron_options = {
  44. "threshold": "v>v_threshold",
  45. "reset": "v=v_reset",
  46. "refractory": "tau_refractory",
  47. 'method' : 'euler'
  48. }
  49. '''
  50. Synapse Models
  51. '''
  52. delta_synapse_delay = 2.0 * ms
  53. '''
  54. Setup of the Model
  55. '''
  56. ## With voltage delta synapse
  57. ei_synapse_options = {
  58. 'model' : delta_synapse_model,
  59. 'on_pre' : delta_synapse_on_pre,
  60. }
  61. #
  62. # ie_synapse_options = {
  63. # 'model' : delta_synapse_model,
  64. # 'on_pre' : delta_synapse_on_pre,
  65. # 'delay' : delta_synapse_delay
  66. # }
  67. excitatory_neuron_eqs = hodgkin_huxley_cond_synapse_weak_noise_eqs + eqs_ih
  68. excitatory_neuron_params = hodgkin_huxley_params
  69. excitatory_neuron_params.update(ih_params)
  70. excitatory_neuron_params.update(excitatory_neuron_params)
  71. excitatory_neuron_params.update({"E_i": -120 * mV})
  72. excitatory_neuron_params.update(noise_params)
  73. # excitatory_neuron_params["stimulus"] = stimulus_array
  74. # excitatory_neuron_params["stimulus"] = stimulus_fun
  75. # ### With conductance based delta synapse
  76. # neuron_eqs = hodgkin_huxley_eqs_with_synaptic_conductance + eqs_ih
  77. #
  78. # synapse_model = exponential_synapse
  79. # synapse_on_pre = exponential_synapse_on_pre
  80. # synapse_params = exponential_synapse_params
  81. #
  82. # neuron_params = hodgkin_huxley_params
  83. # neuron_params.update(ih_params)
  84. # neuron_params.update({"E_i": -80 * mV})
  85. #
  86. # inhibition_off = 0.0 * nS
  87. # inhibition_on = 100 * nS
  88. network_params = copy.deepcopy(excitatory_neuron_params)
  89. network_params.update(lif_interneuron_params)
  90. network_params["spike_threshold"] = spike_threshold
  91. record_variables = ['v', 'ih', 'I']
  92. integration_method = 'exponential_euler'
  93. initial_states = {
  94. "v": hodgkin_huxley_params["El"]
  95. }
  96. threshold_eqs = """
  97. spike_threshold: volt
  98. """
  99. '''
  100. Setup of the neuron groups, synapses and ultimately the network
  101. '''
  102. excitatory_neurons = br.NeuronGroup(N=3, \
  103. model=excitatory_neuron_eqs, \
  104. threshold=excitatory_neuron_options["threshold"], \
  105. refractory=excitatory_neuron_options["refractory"], \
  106. method=excitatory_neuron_options["method"])
  107. # set_parameters_from_dict(excitatory_neurons, excitatory_neuron_params) Why doesnt this work?
  108. set_parameters_from_dict(excitatory_neurons, initial_states)
  109. # excitatory_neurons.I_ext_const = 0.15*nA
  110. input_current = 0.15*nA
  111. excitatory_neurons.I = input_current
  112. inhibitory_neurons = br.NeuronGroup(N=1, \
  113. model=lif_interneuron_eqs, \
  114. threshold=lif_interneuron_options["threshold"], \
  115. refractory=lif_interneuron_options["refractory"], \
  116. reset=lif_interneuron_options["reset"], \
  117. method = lif_interneuron_options['method'])
  118. # set_parameters_from_dict(inhibitory_neurons, lif_interneuron_params) Same here
  119. e_to_i_synapses = br.Synapses(source=excitatory_neurons, \
  120. target=inhibitory_neurons, \
  121. model=ei_synapse_options["model"], \
  122. on_pre=ei_synapse_options["on_pre"])
  123. e_to_i_synapses.connect()
  124. i_to_e_synapses = br.Synapses(source=inhibitory_neurons, \
  125. target=excitatory_neurons, \
  126. model=exponential_synapse, \
  127. on_pre=exponential_synapse_on_pre, \
  128. delay=2.0*ms, \
  129. method='euler')
  130. i_to_e_synapses.connect()
  131. set_parameters_from_dict(i_to_e_synapses,exponential_synapse_params)
  132. exc_spike_recorder = br.SpikeMonitor(source=excitatory_neurons)
  133. inh_spike_recorder = br.SpikeMonitor(source=inhibitory_neurons)
  134. neuron_state_recorder = br.StateMonitor(excitatory_neurons, record_variables, record=[0,1,2], dt=0.1*ms)
  135. '''
  136. External Stimulus
  137. '''
  138. #First Try
  139. # @implementation('cython', '''
  140. # cdef double stimulus_fun(double t):
  141. # return 6.25*1e-9 * sin(4. * 3.14 /(1000. 1e-3) * t)
  142. # ''')
  143. # @check_units(t=second, result=amp)
  144. # def stimulus_fun(t):
  145. # # return 4 * nA *t /ms
  146. # return 6.25* nA * sin(4. * 3.14 /(1000 * ms) * t)
  147. #Second Try
  148. # pulse = 0.25
  149. # stimulus_array = br.TimedArray([0.,0.,0.,0.,0.,0.,pulse,0.,0.,0.,0.,0.,0.,-pulse,0.,0.,0.,0.,0.,0.] * nA,dt=100.*ms)
  150. #Third Try
  151. pulse_length = 40*ms
  152. pulse_1_start = 200*ms
  153. pulse_2_start = 400*ms
  154. pulse_3_start = 600*ms
  155. pulse_1_and_2_start = 800*ms
  156. pulse_strength = 0.6 * nA
  157. @network_operation(dt=1*ms)
  158. def update_input(t):
  159. # Push 1
  160. if t > pulse_1_start and t < pulse_1_start + 2*ms:
  161. excitatory_neurons[0].I = pulse_strength
  162. elif t > pulse_1_start + pulse_length and t < pulse_1_start + pulse_length + 2*ms:
  163. excitatory_neurons[0].I = input_current
  164. # Push 2
  165. elif t > pulse_2_start and t < pulse_2_start + 2*ms:
  166. excitatory_neurons[1].I = pulse_strength
  167. elif t > pulse_2_start + pulse_length and t < pulse_2_start + pulse_length + 2*ms:
  168. excitatory_neurons[1].I = input_current
  169. # Push 3
  170. elif t > pulse_3_start and t < pulse_3_start + 2 * ms:
  171. excitatory_neurons[2].I = pulse_strength
  172. elif t > pulse_3_start + pulse_length and t < pulse_3_start + pulse_length + 2 * ms:
  173. excitatory_neurons[2].I = input_current
  174. # Push 1 and 2
  175. elif t > pulse_1_and_2_start and t < pulse_1_and_2_start + 2 * ms:
  176. excitatory_neurons[0].I = pulse_strength
  177. excitatory_neurons[1].I = pulse_strength
  178. elif t > pulse_1_and_2_start + pulse_length and t < pulse_1_and_2_start + pulse_length + 2 * ms:
  179. excitatory_neurons[0].I = input_current
  180. excitatory_neurons[1].I = input_current
  181. defaultclock.dt = 0.005*ms
  182. net = br.Network(update_input)
  183. net.add(excitatory_neurons)
  184. net.add(inhibitory_neurons)
  185. net.add(e_to_i_synapses)
  186. net.add(i_to_e_synapses)
  187. net.add(exc_spike_recorder)
  188. net.add(inh_spike_recorder)
  189. net.add(neuron_state_recorder)
  190. net.store()
  191. '''
  192. Run of the simulation
  193. '''
  194. def plot_spiking():
  195. ex_spike_trains = exc_spike_recorder.spike_trains()
  196. in_spike_trains = inh_spike_recorder.spike_trains()
  197. fig = plt.figure()
  198. ax = fig.add_subplot(111)
  199. for key, times in ex_spike_trains.items():
  200. ax.plot(times / ms, key / 2.0 * np.ones(times.shape), 'b|')
  201. offset = 2
  202. for key, times in in_spike_trains.items():
  203. ax.plot(times / ms, (key + offset) / 2.0 * np.ones(times.shape), 'r|')
  204. ax.grid(axis='x')
  205. ax.set_ylim(-0.1, 1.1)
  206. ax.set_xlabel("Time(ms)");
  207. def run_sim(inh_strength, exc_strength, record_states=True, run_time=50 * ms):
  208. net.restore()
  209. e_to_i_synapses.synaptic_strength = exc_strength
  210. i_to_e_synapses.synaptic_strength = inh_strength
  211. # excitatory_neurons.stimulus = get_sinoid_stimulus(0.25 * nA, 4 * np.pi / run_time)
  212. # network_params["omega"] = 4 * np.pi / run_time
  213. # excitatory_neurons.I_ext_diff = [0.25, -0.25] * nA
  214. # neuron_state_recorder.record = record_states
  215. net.run(duration=run_time, namespace=network_params)
  216. run_time = 1000. * ms
  217. inh_strength = 150 * nS
  218. exc_strength = lif_interneuron_params["v_threshold"]-lif_interneuron_params["v_reset"] + 5*mV
  219. run_sim(inh_strength=inh_strength, exc_strength=exc_strength, record_states=True, run_time=run_time)
  220. plot_spiking()
  221. print(neuron_state_recorder.v.shape)
  222. t_state = neuron_state_recorder.t
  223. v1_state = neuron_state_recorder[0].v
  224. v2_state = neuron_state_recorder[1].v
  225. v3_state = neuron_state_recorder[2].v
  226. I1_state = neuron_state_recorder[0].I
  227. I2_state = neuron_state_recorder[1].I
  228. I3_state = neuron_state_recorder[2].I
  229. fig, axs = plt.subplots(4, sharex=True)
  230. axs[0].plot(t_state / ms, v1_state / mV, 'C1')
  231. axs[1].plot(t_state / ms, v2_state / mV, 'C2')
  232. axs[2].plot(t_state / ms, v3_state / mV, 'C3')
  233. axs[3].plot(t_state / ms, I1_state / nA, 'C1')
  234. axs[3].plot(t_state / ms, I2_state / nA + 0.01, 'C2')
  235. axs[3].plot(t_state / ms, I3_state / nA + 0.02, 'C3')
  236. run_sim(inh_strength=0*nS, exc_strength=10.*mV, record_states=True, run_time=run_time)
  237. plot_spiking()
  238. plt.show()
  239. plt.show()