run_simulation_perlin_map.py 12 KB

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