Browse Source

Orientation maps are generated, uniformized and ready to run.

moritz 3 years ago
parent
commit
de925e16d4

+ 56 - 36
scripts/spatial_maps/orientation_maps/orientation_map_test_plot_pypet.py

@@ -1,46 +1,66 @@
 import matplotlib.pyplot as plt
 import numpy as np
 
-from scripts.interneuron_placement import create_grid_of_excitatory_neurons, plot_neural_sheet
+from scripts.interneuron_placement import create_grid_of_excitatory_neurons, plot_neural_sheet, \
+    get_correct_position_mesh
 from scripts.spatial_maps.orientation_maps.orientation_map import OrientationMap
 from pypet import Trajectory
 from scripts.spatial_maps.orientation_maps.orientation_map_generator_pypet import DATA_FOLDER, TRAJ_NAME_ORIENTATION_MAPS
+from scripts.spatial_network.orientation_map.run_orientation_map import get_orientation_map, get_uniform_orientation_map
 
 if __name__ == "__main__":
 
-    traj = Trajectory(filename=DATA_FOLDER + TRAJ_NAME_ORIENTATION_MAPS + ".hdf5")
-
-    traj.f_load(index=-1, load_parameters=2, load_results=2)
-
-    corr_len = 800.0
-    seed = 1
-
-    map_by_params = lambda x, y: x == corr_len and y == seed
-
-    idx_iterator = traj.f_find_idx(['corr_len', 'seed'], map_by_params)
-
-    # TODO: Since it has only one entry, maybe iterator can be replaced
-    for idx in idx_iterator:
-        traj.v_idx = idx
-        map_angle_grid = traj.crun.map
-
-    N_E = 3600
-
-    sheet_x = 900
-    sheet_y = 900
-
-    number_of_excitatory_neurons_per_row = int(np.sqrt(N_E))
-
-    map = OrientationMap(number_of_excitatory_neurons_per_row, number_of_excitatory_neurons_per_row,
-                         corr_len, sheet_x, sheet_y, seed)
-    map.angle_grid = map_angle_grid
-
-    tuning_map = lambda x, y: map.tuning(x, y)
-
-    ex_positions, ex_tunings = create_grid_of_excitatory_neurons(sheet_x, sheet_y,
-                                                                 number_of_excitatory_neurons_per_row, tuning_map)
-
-    plot_neural_sheet(ex_positions, ex_tunings)
+    # traj = Trajectory(filename=DATA_FOLDER + TRAJ_NAME_ORIENTATION_MAPS + ".hdf5")
+    #
+    # traj.f_load(index=-1, load_parameters=2, load_results=2)
+    #
+    # corr_len = 800.0
+    # seed = 1
+    #
+    # map_by_params = lambda x, y: x == corr_len and y == seed
+    #
+    # idx_iterator = traj.f_find_idx(['corr_len', 'seed'], map_by_params)
+    #
+    # # TODO: Since it has only one entry, maybe iterator can be replaced
+    # for idx in idx_iterator:
+    #     traj.v_idx = idx
+    #     map_angle_grid = traj.crun.map
+    #
+    # N_E = 3600
+    #
+    # sheet_x = 900
+    # sheet_y = 900
+    #
+    # number_of_excitatory_neurons_per_row = int(np.sqrt(N_E))
+    #
+    # map = OrientationMap(number_of_excitatory_neurons_per_row, number_of_excitatory_neurons_per_row,
+    #                      corr_len, sheet_x, sheet_y, seed)
+    # map.angle_grid = map_angle_grid
+    #
+    # tuning_map = lambda x, y: map.tuning(x, y)
+
+    # corr_lens = [200.0, 400., 800.]
+    # seed = 1
+
+    corr_lens = np.linspace(1.0, 800.0, 12, endpoint=True).tolist()
+    seed_range = range(10)
+
+    size = 900
+    dim = 60
+
+    fig, axes = plt.subplots(len(corr_lens), len(seed_range))
+
+    for c_id, corr_len in enumerate(corr_lens):
+        for s_id, seed in enumerate(seed_range):
+            tuning_map = get_uniform_orientation_map(corr_len, seed, size, dim*dim)
+
+            ex_positions, ex_tunings = create_grid_of_excitatory_neurons(size, size, dim, tuning_map)
+
+            X, Y = get_correct_position_mesh(ex_positions)
+
+            n_ex = dim
+            head_dir_preference = np.array(ex_tunings).reshape((n_ex, n_ex))
+            c = axes[c_id, s_id].pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap="twilight")
+            fig.colorbar(c, ax=axes[c_id, s_id], label="Orientation")
+            axes[c_id, s_id].set_aspect('equal')
     plt.show()
-
-    traj.f_restore_default()

+ 53 - 2
scripts/spatial_network/orientation_map/run_orientation_map.py

@@ -66,12 +66,63 @@ def get_orientation_map(correlation_length, seed, sheet_size, N_E, data_folder=N
 
     number_of_excitatory_neurons_per_row = int(np.sqrt(N_E))
 
-    map = OrientationMap(number_of_excitatory_neurons_per_row + 1, number_of_excitatory_neurons_per_row + 1,
+    map = OrientationMap(number_of_excitatory_neurons_per_row, number_of_excitatory_neurons_per_row,
                          corr_len, sheet_size, sheet_size, seed)
     map.angle_grid = map_angle_grid
 
     return map.tuning
 
+def get_uniform_orientation_map(correlation_length, seed, sheet_size, N_E, data_folder=None):
+    if data_folder is None:
+        data_folder = DATA_FOLDER
+
+    traj = Trajectory(filename=data_folder + TRAJ_NAME_ORIENTATION_MAPS + ".hdf5")
+
+    traj.f_load(index=-1, load_parameters=2, load_results=2)
+
+    available_lengths = sorted(list(set(traj.f_get("corr_len").f_get_range())))
+    closest_length = available_lengths[np.argmin(np.abs(np.array(available_lengths)-correlation_length))]
+    if closest_length!=correlation_length:
+        print("Warning: desired correlation length {:.1f} not available. Taking {:.1f} instead".format(
+            correlation_length, closest_length))
+    corr_len = closest_length
+    seed = seed
+
+    map_by_params = lambda x, y: x == corr_len and y == seed
+
+    idx_iterator = traj.f_find_idx(['corr_len', 'seed'], map_by_params)
+
+    # TODO: Since it has only one entry, maybe iterator can be replaced
+    for idx in idx_iterator:
+        traj.v_idx = idx
+        map_angle_grid = traj.crun.map
+
+    number_of_excitatory_neurons_per_row = int(np.sqrt(N_E))
+
+    map = OrientationMap(number_of_excitatory_neurons_per_row, number_of_excitatory_neurons_per_row,
+                         corr_len, sheet_size, sheet_size, seed)
+
+    # Uniformize orientation map
+
+    nrow = number_of_excitatory_neurons_per_row
+    size = sheet_size
+    scale = corr_len  # TODO: Probably this needs to be a linear interpolation
+
+    n = map_angle_grid / np.pi
+    m = np.concatenate(n)
+    sorted_idx = np.argsort(m)
+    max_val = nrow * 2
+    idx = len(m) // max_val
+    for ii, val in enumerate(range(max_val)):
+        m[sorted_idx[ii * idx:(ii + 1) * idx]] = val
+    p_map = (m - nrow) / nrow
+    map.angle_grid = p_map.reshape(nrow, -1) * np.pi
+    # self.map *= np.pi
+
+    # map.angle_grid = map_angle_grid
+
+    return map.tuning
+
 
 def spatial_network_with_entropy_maximisation(traj):
     sheet_size = traj.map.sheet_size
@@ -79,7 +130,7 @@ def spatial_network_with_entropy_maximisation(traj):
     N_E = traj.network.N_E
     N_I = traj.network.N_I
 
-    orientation_map = get_orientation_map(traj.map.correlation_length, traj.map.seed, sheet_size, N_E)
+    orientation_map = get_uniform_orientation_map(traj.map.correlation_length, traj.map.seed, sheet_size, N_E)
     ex_positions, ex_tunings = create_grid_of_excitatory_neurons(sheet_size,
                                                                  sheet_size,
                                                                  int(np.sqrt(N_E)), orientation_map)