run_simulation_perlin_map.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import numpy as np
  2. from brian2.units import *
  3. from pypet import Environment, cartesian_product
  4. from pypet.brian2.parameter import Brian2MonitorResult
  5. from scripts.spatial_maps.spatial_network_layout import create_grid_of_excitatory_neurons, \
  6. create_interneuron_sheet_entropy_max_orientation, get_excitatory_neurons_in_inhibitory_axonal_clouds
  7. from scripts.spatial_network.spatial_network_setup import get_synaptic_weights, \
  8. create_head_direction_input, ex_in_network
  9. from scripts.spatial_network.models import *
  10. from scripts.spatial_maps.perlin_map.uniform_perlin_map import UniformPerlinMap
  11. POLARIZED = 'ellipsoid'
  12. CIRCULAR = 'circular'
  13. NO_SYNAPSES = 'no conn'
  14. DATA_FOLDER = "../../../data/"
  15. LOG_FOLDER = "../../../logs/"
  16. TRAJ_NAME = "spatial_network_perlin_map"
  17. def get_perlin_map(scale, seed, sheet_size, N_E):
  18. number_of_excitatory_neurons_per_row = int(np.sqrt(N_E))
  19. # TODO: The +1 should not be there
  20. input_map = UniformPerlinMap(number_of_excitatory_neurons_per_row + 1, number_of_excitatory_neurons_per_row + 1,
  21. scale, sheet_size, sheet_size, seed)
  22. return input_map.get_tuning
  23. def get_input_head_directions(traj):
  24. directions = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
  25. return directions
  26. def spatial_network_with_entropy_maximisation(traj):
  27. sheet_size = traj.input_map.sheet_size
  28. N_E = traj.network.N_E
  29. N_I = traj.network.N_I
  30. perlin_map = get_perlin_map(traj.input_map.scale, traj.input_map.seed, sheet_size, N_E)
  31. ex_positions, ex_tunings = create_grid_of_excitatory_neurons(sheet_size, int(np.sqrt(N_E)), perlin_map)
  32. inhibitory_axon_long_axis = traj.morphology.long_axis
  33. inhibitory_axon_short_axis = traj.morphology.short_axis
  34. entropy_maximisation_trial_orientations = traj.interneuron.entropy_maximisation.trial_orientations if \
  35. inhibitory_axon_long_axis != inhibitory_axon_short_axis else 0
  36. inhibitory_axonal_clouds, ellipse_single_trial_entropy = create_interneuron_sheet_entropy_max_orientation(
  37. ex_positions, ex_tunings, N_I, inhibitory_axon_long_axis,
  38. inhibitory_axon_short_axis, sheet_size,
  39. sheet_size, trial_orientations=entropy_maximisation_trial_orientations)
  40. ie_connections = get_excitatory_neurons_in_inhibitory_axonal_clouds(ex_positions, inhibitory_axonal_clouds)
  41. inhibitory_synapse_strength = traj.synapse.inhibitory * nS
  42. excitatory_synapse_strength = traj.synapse.excitatory * mV
  43. if inhibitory_synapse_strength != 0.0 * nS and excitatory_synapse_strength != 0.0 * mV \
  44. and inhibitory_axon_long_axis == inhibitory_axon_short_axis:
  45. traj.f_add_derived_parameter("morphology.morph_label", CIRCULAR,
  46. comment="Interneuron morphology of this run is circular")
  47. elif inhibitory_synapse_strength != 0.0 * nS and excitatory_synapse_strength != 0.0 * mV:
  48. traj.f_add_derived_parameter("morphology.morph_label", POLARIZED,
  49. comment="Interneuron morphology of this run is ellipsoid")
  50. else:
  51. traj.f_add_derived_parameter("morphology.morph_label", NO_SYNAPSES,
  52. comment="There are no interneurons")
  53. ex_in_weights, in_ex_weights = get_synaptic_weights(N_E, N_I, ie_connections, excitatory_synapse_strength,
  54. inhibitory_synapse_strength)
  55. input_directions = get_input_head_directions(traj)
  56. for idx, direction in enumerate(input_directions):
  57. # We recreate the network here for every dir, which slows down the simulation quite considerably. Otherwise,
  58. # we get a problem with saving and restoring the spike times (0s spike for neuron 0)
  59. net = ex_in_network(N_E, N_I,
  60. hodgkin_huxley_eqs_with_synaptic_conductance,
  61. hodgkin_huxley_params,
  62. lif_interneuron_eqs,
  63. lif_interneuron_params,
  64. lif_interneuron_options,
  65. delta_synapse_model,
  66. delta_synapse_on_pre,
  67. delta_synapse_param,
  68. ex_in_weights,
  69. exponential_synapse,
  70. exponential_synapse_on_pre,
  71. exponential_synapse_params,
  72. in_ex_weights,
  73. random_seed=traj.input_map.seed)
  74. input_to_excitatory_population = create_head_direction_input(traj.input.baseline * nA, ex_tunings,
  75. traj.input.phase_dispersion,
  76. traj.input.amplitude * nA, direction)
  77. excitatory_neurons = net["excitatory_neurons"]
  78. excitatory_neurons.I = input_to_excitatory_population
  79. inhibitory_neurons = net["interneurons"]
  80. inhibitory_neurons.u_ext = traj.inh_input.baseline * mV
  81. inhibitory_neurons.tau = traj.interneuron.tau * ms
  82. net.run(traj.simulation.duration * ms)
  83. direction_id = 'dir{:d}'.format(idx)
  84. traj.f_add_result(Brian2MonitorResult, '{:s}.spikes.e'.format(direction_id), net["excitatory_spike_monitor"],
  85. comment='The spiketimes of the excitatory population')
  86. traj.f_add_result(Brian2MonitorResult, '{:s}.spikes.i'.format(direction_id), net["inhibitory_spike_monitor"],
  87. comment='The spiketimes of the inhibitory population')
  88. traj.f_add_result('ex_positions', np.array(ex_positions),
  89. comment='The positions of the excitatory neurons on the sheet')
  90. traj.f_add_result('ex_tunings', np.array(ex_tunings),
  91. comment='The input tunings of the excitatory neurons')
  92. ie_connections_save_array = np.zeros((N_I, N_E))
  93. for i_idx, ie_conn in enumerate(ie_connections):
  94. for e_idx in ie_conn:
  95. ie_connections_save_array[i_idx, e_idx] = 1
  96. traj.f_add_result('ie_adjacency', ie_connections_save_array,
  97. comment='Recurrent connection adjacency matrix')
  98. axon_cloud_save_list = [[p.x, p.y, p.phi] for p in inhibitory_axonal_clouds]
  99. axon_cloud_save_array = np.array(axon_cloud_save_list)
  100. traj.f_add_result('inhibitory_axonal_cloud_array', axon_cloud_save_array,
  101. comment='The inhibitory axonal clouds')
  102. return 1
  103. def main():
  104. # TODO: Set ncores to the desired number of processes to use by pypet
  105. env = Environment(trajectory=TRAJ_NAME,
  106. comment="Compare the head direction tuning for circular and polarized interneuron morphology, "
  107. "when tuning orientations to maximise entropy of connected excitatory tunings.",
  108. multiproc=True, filename=DATA_FOLDER, ncores=24, overwrite_file=True, log_folder=LOG_FOLDER)
  109. traj = env.trajectory
  110. traj.f_add_parameter_group("input_map")
  111. traj.f_add_parameter("input_map.scale", 200.0, comment="Scaling factor of the input map")
  112. traj.f_add_parameter("input_map.seed", 1, comment="Random seed for input map generation.")
  113. traj.f_add_parameter("input_map.sheet_size", 900, comment="Sheet size in um")
  114. traj.f_add_parameter_group("network")
  115. traj.f_add_parameter("network.N_E", 3600, comment="Number of excitatory neurons")
  116. traj.f_add_parameter("network.N_I", 400, comment="Number of inhibitory neurons")
  117. traj.f_add_parameter_group("interneuron")
  118. traj.f_add_parameter("interneuron.tau", 7., comment="Interneuron timescale in ms")
  119. traj.f_add_parameter("interneuron.entropy_maximisation.trial_orientations", 30,
  120. comment="Steps for entropy maximisation")
  121. traj.f_add_parameter_group("synapse")
  122. traj.f_add_parameter("synapse.inhibitory", 30.0, "Strength of conductance-based inhibitory synapse in nS.")
  123. traj.f_add_parameter("synapse.excitatory", 2.5, "Strength of conductance-based inhibitory synapse in mV.")
  124. traj.f_add_parameter_group("input")
  125. traj.f_add_parameter("input.phase_dispersion", 2.5, comment="Standard deviation of incoming head direction input.")
  126. traj.f_add_parameter("input.baseline", 0.05, comment="Head direction input baseline")
  127. traj.f_add_parameter("input.amplitude", 0.6, comment="Head direction input amplitude")
  128. traj.f_add_parameter("input.number_of_directions", 12, comment="Number of probed directions")
  129. traj.f_add_parameter_group("inh_input")
  130. traj.f_add_parameter("inh_input.baseline", -50., comment="Head direction input baseline")
  131. traj.f_add_parameter("inh_input.amplitude", 0., comment="Head direction input amplitude")
  132. traj.f_add_parameter_group("morphology")
  133. traj.f_add_parameter("morphology.long_axis", 100.0, comment="Long axis of axon ellipsoid")
  134. traj.f_add_parameter("morphology.short_axis", 25.0, comment="Short axis of axon ellipsoid")
  135. traj.f_add_parameter_group("simulation")
  136. traj.f_add_parameter("simulation.dt", 0.1, comment="Network simulation time step in ms")
  137. traj.f_add_parameter("simulation.duration", 1000, comment="Network simulation duration in ms")
  138. # To test if the simulation, analysis and plotting work as intended, use this setup to have a lower overall runtime
  139. # scale_range = [200.0]
  140. # seed_range = [1]
  141. # TODO: This is the parameter range used for the results shown in Fig 4. Uncomment for the full run.
  142. scale_range = np.linspace(1.0, 800.0, 24, endpoint=True).tolist()
  143. seed_range = range(10)
  144. ellipsoid_parameter_exploration = {
  145. "morphology.long_axis": [100.0],
  146. "morphology.short_axis": [25.0],
  147. "input_map.scale": scale_range,
  148. "input_map.seed": seed_range,
  149. "synapse.inhibitory": [30.],
  150. "synapse.excitatory": [2.5]
  151. }
  152. corresponding_circular_radius = float(np.sqrt(ellipsoid_parameter_exploration[
  153. "morphology.long_axis"][0] * ellipsoid_parameter_exploration[
  154. "morphology.short_axis"][0]))
  155. circle_parameter_exploration = {
  156. "morphology.long_axis": [corresponding_circular_radius],
  157. "morphology.short_axis": [corresponding_circular_radius],
  158. "input_map.scale": ellipsoid_parameter_exploration["input_map.scale"],
  159. "input_map.seed": ellipsoid_parameter_exploration["input_map.seed"],
  160. "synapse.inhibitory": ellipsoid_parameter_exploration["synapse.inhibitory"],
  161. "synapse.excitatory": ellipsoid_parameter_exploration["synapse.excitatory"]
  162. }
  163. no_conn_parameter_exploration = {
  164. "morphology.long_axis": [corresponding_circular_radius],
  165. "morphology.short_axis": [corresponding_circular_radius],
  166. "input_map.scale": ellipsoid_parameter_exploration["input_map.scale"],
  167. "input_map.seed": ellipsoid_parameter_exploration["input_map.seed"],
  168. "synapse.inhibitory": [0.],
  169. "synapse.excitatory": [0.]
  170. }
  171. expanded_dicts = [cartesian_product(dict) for dict in [ellipsoid_parameter_exploration,
  172. circle_parameter_exploration,
  173. no_conn_parameter_exploration]]
  174. final_dict = {}
  175. for key in expanded_dicts[0].keys():
  176. list_of_parameter_lists = [dict[key] for dict in expanded_dicts]
  177. final_dict[key] = sum(list_of_parameter_lists, [])
  178. traj.f_explore(final_dict)
  179. env.run(spatial_network_with_entropy_maximisation)
  180. env.disable_logging()
  181. if __name__ == "__main__":
  182. main()