run_entropy_maximisation_orientation_map.py 12 KB

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