run_scaled_up_perlin.py 11 KB

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