parameter_scan_gain_merged.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 scripts.prc_and_iterative_diagram.timed_inhibition import get_mean_period
  8. from scripts.models import hodgkin_huxley_eqs_with_synaptic_conductance, exponential_synapse, \
  9. exponential_synapse_on_pre, exponential_synapse_params, eqs_ih, hodgkin_huxley_params, \
  10. ih_params, delta_synapse_model, delta_synapse_on_pre
  11. def set_parameters_from_dict(neurongroup, dictionary_of_parameters):
  12. for param_key, param_value in dictionary_of_parameters.items():
  13. try:
  14. neurongroup.__setattr__(param_key, param_value)
  15. except AttributeError as err:
  16. warnings.warn("{:s} has no parameter {:s}".format(neurongroup.name, param_key))
  17. '''
  18. Model for the excitatory neurons
  19. '''
  20. # Hodgkin Huxley model from Brian2 documentation
  21. spike_threshold = 40*mV
  22. excitatory_neuron_options = {
  23. "threshold" : "v > spike_threshold",
  24. "refractory" : "v > spike_threshold",
  25. "method" : 'exponential_euler'
  26. }
  27. '''
  28. Model for the interneuron, currently only the inhibition timing is of interest thus the lif model
  29. '''
  30. lif_interneuron_eqs = """
  31. dv/dt =1.0/tau* (-v + u_ext) :volt (unless refractory)
  32. u_ext = u_ext_const : volt
  33. """
  34. lif_interneuron_params = {
  35. "tau": 7*ms,
  36. "v_threshold": -40*mV,
  37. "v_reset": -50*mV,
  38. "tau_refractory": 0.0*ms,
  39. "u_ext_const" : - 41 * mV
  40. }
  41. lif_interneuron_options = {
  42. "threshold": "v>v_threshold",
  43. "reset": "v=v_reset",
  44. "refractory": "tau_refractory",
  45. }
  46. '''
  47. Synapse Models
  48. '''
  49. delta_synapse_delay = 2.0 * ms
  50. '''
  51. Setup of the Model
  52. '''
  53. ## With voltage delta synapse
  54. ei_synapse_options = {
  55. 'model' : delta_synapse_model,
  56. 'on_pre' : delta_synapse_on_pre,
  57. }
  58. #
  59. # ie_synapse_options = {
  60. # 'model' : delta_synapse_model,
  61. # 'on_pre' : delta_synapse_on_pre,
  62. # 'delay' : delta_synapse_delay
  63. # }
  64. excitatory_neuron_eqs = hodgkin_huxley_eqs_with_synaptic_conductance + eqs_ih
  65. excitatory_neuron_params = hodgkin_huxley_params
  66. excitatory_neuron_params.update(ih_params)
  67. excitatory_neuron_params.update(excitatory_neuron_params)
  68. excitatory_neuron_params.update({"E_i": -120 * mV})
  69. # excitatory_neuron_params["stimulus"] = stimulus_array
  70. # excitatory_neuron_params["stimulus"] = stimulus_fun
  71. # ### With conductance based delta synapse
  72. # neuron_eqs = hodgkin_huxley_eqs_with_synaptic_conductance + eqs_ih
  73. #
  74. # synapse_model = exponential_synapse
  75. # synapse_on_pre = exponential_synapse_on_pre
  76. # synapse_params = exponential_synapse_params
  77. #
  78. # neuron_params = hodgkin_huxley_params
  79. # neuron_params.update(ih_params)
  80. # neuron_params.update({"E_i": -80 * mV})
  81. #
  82. # inhibition_off = 0.0 * nS
  83. # inhibition_on = 100 * nS
  84. network_params = copy.deepcopy(excitatory_neuron_params)
  85. network_params.update(lif_interneuron_params)
  86. network_params["spike_threshold"] = spike_threshold
  87. record_variables = ['v', 'I']
  88. integration_method = 'exponential_euler'
  89. initial_states = {
  90. "v": hodgkin_huxley_params["El"]
  91. }
  92. threshold_eqs = """
  93. spike_threshold: volt
  94. """
  95. '''
  96. Setup of the neuron groups, synapses and ultimately the network
  97. '''
  98. excitatory_neurons = br.NeuronGroup(N=2, \
  99. model=excitatory_neuron_eqs, \
  100. threshold=excitatory_neuron_options["threshold"], \
  101. refractory=excitatory_neuron_options["refractory"], \
  102. method=excitatory_neuron_options["method"])
  103. # set_parameters_from_dict(excitatory_neurons, excitatory_neuron_params) Why doesnt this work?
  104. set_parameters_from_dict(excitatory_neurons, initial_states)
  105. # excitatory_neurons.I_ext_const = 0.15*nA
  106. input_current = 0.15*nA
  107. excitatory_neurons.I = input_current
  108. inhibitory_neurons = br.NeuronGroup(N=1, \
  109. model=lif_interneuron_eqs, \
  110. threshold=lif_interneuron_options["threshold"], \
  111. refractory=lif_interneuron_options["refractory"], \
  112. reset=lif_interneuron_options["reset"])
  113. # set_parameters_from_dict(inhibitory_neurons, lif_interneuron_params) Same here
  114. e_to_i_synapses = br.Synapses(source=excitatory_neurons, \
  115. target=inhibitory_neurons, \
  116. model=ei_synapse_options["model"], \
  117. on_pre=ei_synapse_options["on_pre"])
  118. e_to_i_synapses.connect()
  119. i_to_e_synapses = br.Synapses(source=inhibitory_neurons, \
  120. target=excitatory_neurons, \
  121. model=exponential_synapse, \
  122. on_pre=exponential_synapse_on_pre, \
  123. delay=2.0*ms)
  124. i_to_e_synapses.connect()
  125. set_parameters_from_dict(i_to_e_synapses,exponential_synapse_params)
  126. exc_spike_recorder = br.SpikeMonitor(source=excitatory_neurons)
  127. inh_spike_recorder = br.SpikeMonitor(source=inhibitory_neurons)
  128. neuron_state_recorder = br.StateMonitor(excitatory_neurons, record_variables, record=[0,1])
  129. net = br.Network()
  130. net.add(excitatory_neurons)
  131. net.add(inhibitory_neurons)
  132. net.add(e_to_i_synapses)
  133. net.add(i_to_e_synapses)
  134. net.add(exc_spike_recorder)
  135. net.add(inh_spike_recorder)
  136. net.add(neuron_state_recorder)
  137. net.store()
  138. '''
  139. Run of the simulation
  140. '''
  141. def nearest(array, value):
  142. array = np.asarray(array)
  143. idx = (np.abs(array - value)).argmin()
  144. return array[idx]
  145. def plot_spiking():
  146. ex_spike_trains = exc_spike_recorder.spike_trains()
  147. in_spike_trains = inh_spike_recorder.spike_trains()
  148. fig = plt.figure()
  149. ax = fig.add_subplot(111)
  150. for key, times in ex_spike_trains.items():
  151. ax.plot(times / ms, key / 2.0 * np.ones(times.shape), 'b|')
  152. offset = 2
  153. for key, times in in_spike_trains.items():
  154. ax.plot(times / ms, (key + offset) / 2.0 * np.ones(times.shape), 'r|')
  155. ax.grid(axis='x')
  156. ax.set_ylim(-0.1, 1.1)
  157. ax.set_xlabel("Time(ms)");
  158. def run_sim(inh_strength, exc_strength, record_states=True, run_time=50 * ms):
  159. net.restore()
  160. e_to_i_synapses.synaptic_strength = exc_strength
  161. i_to_e_synapses.synaptic_strength = inh_strength
  162. net.run(duration=run_time, namespace=network_params)
  163. run_time = 600. * ms
  164. exc_strength = lif_interneuron_params["v_threshold"]-lif_interneuron_params["v_reset"] + 5*mV
  165. ex_drive_range = np.linspace(-0.05,0.05,10)*nA
  166. input_current = 0.15*nA
  167. inh_strength = 20.0 * nS
  168. n_ghbar = 10
  169. ghbar_range = linspace(0.0,50.0,n_ghbar)*nS
  170. n_syn_strength = 10
  171. synaptic_strength_range = linspace(0.0,160.0,n_syn_strength)*nS
  172. gain_array = np.ndarray((n_ghbar,n_syn_strength), dtype=float)
  173. syn_str_id = 0
  174. for synaptic_strength_val in synaptic_strength_range:
  175. inh_strength = synaptic_strength_val
  176. ghbar_id = 0
  177. for ghbar_val in ghbar_range:
  178. rate_diff = []
  179. for drive in ex_drive_range:
  180. net.restore()
  181. excitatory_neurons[0].I = input_current + drive
  182. network_params['ghbar'] = ghbar_val
  183. net.store()
  184. run_sim(inh_strength=inh_strength, exc_strength=exc_strength, record_states=True, run_time=run_time)
  185. ex_spike_trains = exc_spike_recorder.spike_trains()
  186. if len(ex_spike_trains[0]) <= 1:
  187. rate_1 = 0.0
  188. else:
  189. rate_1 = 1.0/get_mean_period(ex_spike_trains[0])
  190. if len(ex_spike_trains[1]) <= 1:
  191. rate_2 = 0.0
  192. else:
  193. rate_2 = 1.0 / get_mean_period(ex_spike_trains[1])
  194. rate_diff.append(rate_1-rate_2)
  195. if drive/nA == nearest(ex_drive_range/nA,0.0):
  196. print("rates: ",rate_1,rate_2)
  197. print(ex_drive_range)
  198. print(rate_diff)
  199. gain = np.polyfit(ex_drive_range/nA,rate_diff/Hz,1)[0]
  200. print(gain)
  201. gain_array[ghbar_id, syn_str_id] = gain
  202. ghbar_id = ghbar_id + 1
  203. syn_str_id = syn_str_id + 1
  204. '''
  205. Plotting
  206. '''
  207. print(gain_array)
  208. ### Visualization of the h current role
  209. x,y = np.meshgrid(ghbar_range / nS,synaptic_strength_range / nS)
  210. # levels = MaxNLocator(nbins=2).tick_values(0.0,1.0)
  211. # cmap = plt.get_cmap('PiYG')
  212. # norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
  213. fig, ax = plt.subplots(1, 1)
  214. im = ax.pcolormesh(x,y,gain_array, clim=(100.,800.))#, cmap=cmap, norm=norm)
  215. ax.set_title('bistability via iter. map')
  216. ax.set_xlabel('ghbar (nS)')
  217. ax.set_ylabel('syn_strength (nS)')
  218. fig.colorbar(im, ax=ax)
  219. fig.tight_layout()
  220. plt.show()